-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test.py
107 lines (89 loc) · 2.89 KB
/
run_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import unittest
class PenutTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def test_timecost(self):
import time
from penut import TimeCost
with TimeCost('Sleep Time'):
time.sleep(1)
ts = TimeCost('Custom Format', verbose_fmt=lambda msg, tts: f'Hello {msg}, you cost {tts:.2f}s!!')
with ts:
time.sleep(1)
def test_io_test(self):
import os
import penut.io as pio
# JSON IO
def json_test(d):
pio.dump(d, 'a.json', is_ascii=True)
pio.dump(d, 'b.json', is_ascii=False)
assert pio.load('a.json') == pio.load('b.json')
# Pickle IO
def pkl_test(d):
pio.dump(d, 'a.pkl')
assert d == pio.load('a.pkl')
# Numpy IO
def npy_test(d):
import numpy as np
d = np.array(d)
pio.dump(d, 'a.npy')
assert (d == pio.load('a.npy')).all()
# CSV IO
def csv_test(d):
pio.dump(d, 'a.csv')
pio.load('a.csv')
# Cross Compare
def cross_compare(d):
pio.dump(d, 'a.json')
pio.dump(d, 'a.pkl')
assert pio.load('a.json') == pio.load('a.pkl')
# Array Testing
d = [1, 2, 3]
json_test(d)
print('JSON Array IO Pass')
pkl_test(d)
print('Pickle Array IO Pass')
csv_test(d)
print('CSV Array IO Pass')
npy_test(d)
print('Numpy Array IO Pass')
cross_compare(d)
print('Array IO Cross Comparison Pass')
# Dictionary Testing
d = {'Name': 'Testing', 'Arr': [1, 2, 3], '測試': '項目'}
json_test(d)
print('JSON Dict IO Pass')
pkl_test(d)
print('Pickle Dict IO Pass')
csv_test(d)
print('CSV Dict IO Pass')
cross_compare(d)
print('Dict IO Cross Comparison Pass')
## Remove Files
os.remove('a.csv')
os.remove('a.pkl')
os.remove('a.npy')
os.remove('a.json')
os.remove('b.json')
def test_walk_dir(self):
from penut import walk_dir
for fp in walk_dir('./'):
self.assertIs(type(fp), str)
for fp, fn in walk_dir('./', True):
self.assertIs(type(fp), str)
self.assertIs(type(fn), str)
for fp, fn, dn in walk_dir('./', True, True):
self.assertIs(type(fp), str)
self.assertIs(type(fn), str)
self.assertIs(type(dn), str)
def test_td2s(self):
from penut import td2s
import datetime as dt
a = dt.datetime(2021, 1, 6, 0, 0, 0)
b = dt.datetime(2021, 1, 6, 1, 2, 3)
d = b - a
print(td2s(d))
fmt = lambda h, m, s: f'{h} 小時 {m} 分 {s} 秒'
print(td2s(d, fmt=fmt))
if __name__ == "__main__":
unittest.main()