Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge unittest? #245

Merged
merged 8 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions cf_units/tests/integration/test__num2date_to_nearest_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"""Test function :func:`cf_units._num2date_to_nearest_second`."""

import datetime
import unittest

import cftime
import numpy as np
import pytest

import cf_units
from cf_units import _num2date_to_nearest_second


class Test(unittest.TestCase):
class Test:
def setup_units(self, calendar):
self.useconds = cf_units.Unit("seconds since 1970-01-01", calendar)
self.uminutes = cf_units.Unit("minutes since 1970-01-01", calendar)
Expand All @@ -27,24 +27,24 @@ def check_dates(self, nums, units, expected, only_cftime=True):
res = _num2date_to_nearest_second(
num, unit, only_use_cftime_datetimes=only_cftime
)
self.assertEqual(exp, res)
self.assertIsInstance(res, type(exp))
assert exp == res
assert isinstance(res, type(exp))

def check_timedelta(self, nums, units, expected):
for num, unit, exp in zip(nums, units, expected):
epoch = cftime.num2date(0, unit.cftime_unit, unit.calendar)
res = _num2date_to_nearest_second(num, unit)
delta = res - epoch
seconds = np.round(delta.seconds + (delta.microseconds / 1000000))
self.assertEqual((delta.days, seconds), exp)
assert (delta.days, seconds) == exp

def test_scalar(self):
unit = cf_units.Unit("seconds since 1970-01-01", "gregorian")
num = 5.0
exp = datetime.datetime(1970, 1, 1, 0, 0, 5)
res = _num2date_to_nearest_second(num, unit)
self.assertEqual(exp, res)
self.assertIsInstance(res, cftime.datetime)
assert exp == res
assert isinstance(res, cftime.datetime)

def test_sequence(self):
unit = cf_units.Unit("seconds since 1970-01-01", "gregorian")
Expand All @@ -64,7 +64,7 @@ def test_multidim_sequence(self):
nums = [[20.0, 40.0, 60.0], [80, 100.0, 120.0]]
exp_shape = (2, 3)
res = _num2date_to_nearest_second(nums, unit)
self.assertEqual(exp_shape, res.shape)
assert exp_shape == res.shape

def test_masked_ndarray(self):
unit = cf_units.Unit("seconds since 1970-01-01", "gregorian")
Expand Down Expand Up @@ -254,8 +254,8 @@ def test_fractional_second_360_day(self):

def test_pydatetime_wrong_calendar(self):
unit = cf_units.Unit("days since 1970-01-01", calendar="360_day")
with self.assertRaisesRegex(
ValueError, "illegal calendar or reference date"
with pytest.raises(
ValueError, match="illegal calendar or reference date"
):
_num2date_to_nearest_second(
1,
Expand Down Expand Up @@ -342,4 +342,4 @@ def test_fractional_second_365_day(self):


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
pp-mo marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 8 additions & 8 deletions cf_units/tests/integration/test_date2num.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"""Test function :func:`cf_units.date2num`."""

import datetime
import unittest

import numpy as np
import pytest

from cf_units import date2num


class Test(unittest.TestCase):
def setUp(self):
class Test:
def setup_method(self):
self.unit = "seconds since 1970-01-01"
self.calendar = "gregorian"

Expand All @@ -24,7 +24,7 @@ def test_single(self):
res = date2num(date, self.unit, self.calendar)
# num2date won't return an exact value representing the date,
# even if one exists
self.assertAlmostEqual(exp, res, places=4)
pp-mo marked this conversation as resolved.
Show resolved Hide resolved
assert round(abs(exp - res), 4) == 0

def test_sequence(self):
dates = [
Expand Down Expand Up @@ -53,23 +53,23 @@ def test_multidim_sequence(self):
]
exp_shape = (2, 3)
res = date2num(dates, self.unit, self.calendar)
self.assertEqual(exp_shape, res.shape)
assert exp_shape == res.shape

def test_discard_mircosecond(self):
date = datetime.datetime(1970, 1, 1, 0, 0, 5, 750000)
exp = 5.0
res = date2num(date, self.unit, self.calendar)

self.assertAlmostEqual(exp, res, places=4)
assert round(abs(exp - res), 4) == 0

def test_long_time_interval(self):
# This test should fail with an error that we need to catch properly.
unit = "years since 1970-01-01"
date = datetime.datetime(1970, 1, 1, 0, 0, 5)
exp_emsg = 'interval of "months", "years" .* got "years".'
with self.assertRaisesRegex(ValueError, exp_emsg):
with pytest.raises(ValueError, match=exp_emsg):
date2num(date, unit, self.calendar)


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
11 changes: 6 additions & 5 deletions cf_units/tests/integration/test_num2date.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
# licensing details.
"""Test function :func:`cf_units.num2date`."""

import unittest

import pytest

from cf_units import num2date


class Test(unittest.TestCase):
class Test:
def test_num2date_wrong_calendar(self):
with self.assertRaisesRegex(
ValueError, "illegal calendar or reference date"
with pytest.raises(
ValueError, match="illegal calendar or reference date"
):
num2date(
1,
Expand All @@ -25,4 +26,4 @@ def test_num2date_wrong_calendar(self):


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
15 changes: 8 additions & 7 deletions cf_units/tests/integration/test_num2pydate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
"""Test function :func:`cf_units.num2pydate`."""

import datetime
import unittest

import pytest

from cf_units import num2pydate


class Test(unittest.TestCase):
class Test:
def test_num2pydate_simple(self):
result = num2pydate(1, "days since 1970-01-01", calendar="standard")
expected = datetime.datetime(1970, 1, 2)
self.assertEqual(result, expected)
self.assertIsInstance(result, datetime.datetime)
assert result == expected
assert isinstance(result, datetime.datetime)

def test_num2pydate_wrong_calendar(self):
with self.assertRaisesRegex(
ValueError, "illegal calendar or reference date"
with pytest.raises(
ValueError, match="illegal calendar or reference date"
):
num2pydate(1, "days since 1970-01-01", calendar="360_day")


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
9 changes: 5 additions & 4 deletions cf_units/tests/test_coding_standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import os
import subprocess
import unittest
from datetime import datetime
from fnmatch import fnmatch
from glob import glob

import pytest

import cf_units

LICENSE_TEMPLATE = """# Copyright cf-units contributors
Expand All @@ -35,7 +36,7 @@
]


class TestLicenseHeaders(unittest.TestCase):
class TestLicenseHeaders:
@staticmethod
def whatchanged_parse(whatchanged_output):
"""
Expand Down Expand Up @@ -105,7 +106,7 @@ def test_license_headers(self):
last_change_by_fname = self.last_change_by_fname()
except ValueError:
# Caught the case where this is not a git repo.
return self.skipTest(
return pytest.skip(
"cf_units installation did not look like a " "git repo."
)

Expand Down Expand Up @@ -133,4 +134,4 @@ def test_license_headers(self):


if __name__ == "__main__":
unittest.main()
pytest.main([__file__])
Loading