-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
74 lines (61 loc) · 2.38 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import asyncio
import unittest
from datetime import datetime, timezone
import uploader
# Switch to IsolatedAsyncioTestCase in python >= 3.8
def async_test(coro):
def wrapper(*args, **kwargs):
loop = asyncio.new_event_loop()
try:
res = loop.run_until_complete(coro(*args, **kwargs))
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
return res
return wrapper
class MergeEquencesTest(unittest.TestCase):
@async_test
async def test_merge_sequences(self):
async def gen1():
yield (datetime(2020, 1, 1, tzinfo=timezone.utc), 0)
yield (datetime(2020, 1, 4, tzinfo=timezone.utc), 0)
yield (datetime(2020, 1, 6, tzinfo=timezone.utc), 0)
async def gen2():
yield (datetime(2020, 1, 2, tzinfo=timezone.utc), 1)
yield (datetime(2020, 1, 3, tzinfo=timezone.utc), 1)
yield (datetime(2020, 1, 5, tzinfo=timezone.utc), 1)
seq = uploader.merge_sequences([gen1(), gen2()])
self.assertEqual(await seq.__anext__(), (datetime(2020, 1, 1, tzinfo=timezone.utc), 0))
self.assertEqual(await seq.__anext__(), (datetime(2020, 1, 2, tzinfo=timezone.utc), 1))
self.assertEqual(await seq.__anext__(), (datetime(2020, 1, 3, tzinfo=timezone.utc), 1))
self.assertEqual(await seq.__anext__(), (datetime(2020, 1, 4, tzinfo=timezone.utc), 0))
self.assertEqual(await seq.__anext__(), (datetime(2020, 1, 5, tzinfo=timezone.utc), 1))
self.assertEqual(await seq.__anext__(), (datetime(2020, 1, 6, tzinfo=timezone.utc), 0))
with self.assertRaises(StopAsyncIteration):
await seq.__anext__()
class DictGetTest(unittest.TestCase):
def test_dict_get_shallow(self):
test_dict = {
'a': 10
}
self.assertEqual(uploader.dict_get(test_dict, 'a'), 10)
def test_dict_get_deep(self):
test_dict = {
'a': {
'b': {
'c': 10
}
}
}
self.assertEqual(uploader.dict_get(test_dict, 'a.b.c'), 10)
def test_dict_get_none(self):
test_dict = {
'a': {
'b': {
'c': 10
}
}
}
self.assertIsNone(uploader.dict_get(test_dict, 'a.b.d'))
if __name__ == '__main__':
unittest.main()