Skip to content

Commit 23a83f5

Browse files
fixed use of time strings to allow both seconds and second etc (#112)
* fixed use of time strings to allow both and etc * fixed use of time strings to allow both 'seconds' and 'second' etc * revised unit tests to use parameterized approach * changed utils tests to pytest
1 parent be47e15 commit 23a83f5

File tree

3 files changed

+53
-63
lines changed

3 files changed

+53
-63
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ Run `make clean dist` from the main project directory.
7777

7878
# Testing
7979

80+
## Creating tests
81+
Preferred style is to use pytest rather than unittest but some unittest based code is used in compatibility mode.
82+
83+
Any new tests should be written as pytest compatible test classes.
84+
8085
## Running unit / integration tests
8186

8287
If using an environment with multiple Python versions, make sure to use virtual env or

dbldatagen/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ def parse_time_interval(spec):
196196
hours = time_value
197197
elif time_type in ["minutes", "minute"]:
198198
minutes = time_value
199-
elif time_type in ["seconds", "seconds"]:
199+
elif time_type in ["seconds", "second"]:
200200
seconds = time_value
201-
elif time_type in ["microseconds", "microseconds"]:
201+
elif time_type in ["microseconds", "microsecond"]:
202202
microseconds = time_value
203203
elif time_type in ["milliseconds", "millisecond"]:
204204
milliseconds = time_value

tests/test_utils.py

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,81 @@
11
import logging
2-
import unittest
2+
import sys
33
from datetime import timedelta
4+
import pytest
45

56
from dbldatagen import ensure, mkBoundsList, coalesce_values, deprecated, SparkSingleton, \
67
parse_time_interval, DataGenError
78

89
spark = SparkSingleton.getLocalInstance("unit tests")
910

1011

11-
class TestUtils(unittest.TestCase):
12+
class TestUtils:
1213
x = 1
1314

14-
def setUp(self):
15-
print("setting up")
16-
FORMAT = '%(asctime)-15s %(message)s'
17-
logging.basicConfig(format=FORMAT)
18-
19-
@classmethod
20-
def setUpClass(cls):
21-
pass
15+
@pytest.fixture(autouse=True)
16+
def setupLogger(self):
17+
self.logger = logging.getLogger("TestUtils")
2218

2319
@deprecated("testing deprecated")
2420
def testDeprecatedMethod(self):
2521
pass
2622

27-
@unittest.expectedFailure
2823
def test_ensure(self):
29-
ensure(1 == 2, "Expected error")
24+
with pytest.raises(Exception):
25+
ensure(1 == 2, "Expected error")
3026

31-
def testMkBoundsList1(self):
27+
def test_mkBoundsList1(self):
3228
""" Test utils mkBoundsList"""
3329
test = mkBoundsList(None, 1)
3430

35-
self.assertEqual(len(test), 2)
31+
assert len(test) == 2
3632

3733
test2 = mkBoundsList(None, [1, 1])
3834

39-
self.assertEqual(len(test2), 2)
35+
assert len(test2) == 2
4036

41-
def testCoalesce(self):
37+
@pytest.mark.parametrize("test_input,expected",
38+
[
39+
([None, 1], 1),
40+
([2, 1], 2),
41+
([3, None, 1], 3),
42+
([None, None, None], None),
43+
])
44+
def test_coalesce(self, test_input, expected):
4245
""" Test utils coalesce function"""
43-
result = coalesce_values(None, 1)
44-
45-
self.assertEqual(result, 1)
46-
47-
result2 = coalesce_values(3, None, 1)
48-
49-
self.assertEqual(result2, 3)
50-
51-
result3 = coalesce_values(None, None, None)
52-
53-
self.assertIsNone(result3)
54-
55-
def testParseTimeInterval1(self):
56-
interval = parse_time_interval("1 hours")
57-
self.assertEqual(timedelta(hours=1), interval)
46+
result = coalesce_values(*test_input)
47+
assert result == expected
48+
49+
@pytest.mark.parametrize("test_input,expected",
50+
[
51+
("1 hours, minutes = 2", timedelta(hours=1, minutes=2)),
52+
("4 days, 1 hours, 2 minutes", timedelta(days=4, hours=1, minutes=2)),
53+
("days=4, hours=1, minutes=2", timedelta(days=4, hours=1, minutes=2)),
54+
("1 hours, 2 seconds", timedelta(hours=1, seconds=2)),
55+
("1 hours, 2 minutes", timedelta(hours=1, minutes=2)),
56+
("1 hours", timedelta(hours=1)),
57+
("1 hour", timedelta(hours=1)),
58+
("1 hour, 1 second", timedelta(hours=1, seconds=1)),
59+
("1 hour, 10 milliseconds", timedelta(hours=1, milliseconds=10)),
60+
("1 hour, 10 microseconds", timedelta(hours=1, microseconds=10)),
61+
("1 year, 4 weeks", timedelta(weeks=56))
62+
])
63+
def testParseTimeInterval2b(self, test_input, expected):
64+
interval = parse_time_interval(test_input)
65+
assert expected == interval
5866

59-
def testParseTimeInterval2(self):
60-
interval = parse_time_interval("1 hours, 2 seconds")
61-
self.assertEqual(timedelta(hours=1, seconds=2), interval)
62-
63-
def testParseTimeInterval3(self):
64-
interval = parse_time_interval("1 hours, 2 minutes")
65-
self.assertEqual(timedelta(hours=1, minutes=2), interval)
66-
67-
def testParseTimeInterval4(self):
68-
interval = parse_time_interval("4 days, 1 hours, 2 minutes")
69-
self.assertEqual(timedelta(days=4, hours=1, minutes=2), interval)
70-
71-
def testParseTimeInterval1a(self):
72-
interval = parse_time_interval("hours=1")
73-
self.assertEqual(timedelta(hours=1), interval)
74-
75-
def testParseTimeInterval2a(self):
76-
interval = parse_time_interval("hours=1, seconds = 2")
77-
self.assertEqual(timedelta(hours=1, seconds=2), interval)
67+
def testDatagenExceptionObject(self):
68+
testException = DataGenError("testing")
7869

79-
def testParseTimeInterval3a(self):
80-
interval = parse_time_interval("1 hours, minutes = 2")
81-
self.assertEqual(timedelta(hours=1, minutes=2), interval)
70+
assert testException is not None
8271

83-
def testParseTimeInterval4a(self):
84-
interval = parse_time_interval("days=4, hours=1, minutes=2")
85-
self.assertEqual(timedelta(days=4, hours=1, minutes=2), interval)
72+
assert type(repr(testException)) is str
73+
self.logger.info(repr(testException))
8674

87-
def testDatagenExceptionObject(self):
88-
testException = DataGenError("testing")
75+
assert type(str(testException)) is str
76+
self.logger.info(str(testException))
8977

90-
self.assertIsNotNone(testException)
9178

92-
print("error has repr", repr(testException))
93-
print("error has str", str(testException))
9479

9580
# run the tests
9681
# if __name__ == '__main__':

0 commit comments

Comments
 (0)