-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_state.py
executable file
·109 lines (90 loc) · 3.25 KB
/
test_state.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
108
109
#!/usr/bin/python3
"""
Unittest State class
"""
import unittest
import pep8
import sys
from datetime import datetime
from models import state
from models.state import State
import os
class TestPep8B(unittest.TestCase):
""" Test pep8 style validation """
def test_pep8(self):
""" test base and test_base for pep8 conformance """
style = pep8.StyleGuide(quiet=True)
file1 = 'models/state.py'
file2 = 'tests/test_models/test_state.py'
result = style.check_files([file1, file2])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warning).")
class TestDocsB(unittest.TestCase):
""" check for documentation """
def test_module_doc(self):
""" check for module documentation """
self.assertTrue(len(state.__doc__) > 0)
def test_class_doc(self):
""" check for documentation """
self.assertTrue(len(State.__doc__) > 0)
def test_method_docs(self):
""" check for method documentation """
for func in dir(State):
self.assertTrue(len(func.__doc__) > 0)
class StateclassTests(unittest.TestCase):
""" Test Case for state module """
def setUp(self):
""" Create instance global """
self.ins0 = State()
self.ins1 = State()
def tearDown(self):
""" Clean All test case """
pass
def test_instance(self):
""" Test Case to check instance """
self.assertIsInstance(self.ins0, State)
self.assertIsInstance(self.ins1, State)
def test_permissions(self):
"""test read-write-execute permissions"""
read = os.access('models/state.py', os.R_OK)
self.assertTrue(read)
write = os.access('models/state.py', os.W_OK)
self.assertTrue(write)
exe = os.access('models/state.py', os.X_OK)
self.assertTrue(exe)
def test_id(self):
"""
Test id of instances
instance id will be not equal
type, will be str
"""
self.assertNotEqual(self.ins0.id, self.ins1.id)
self.assertEqual(type(self.ins0.id), str)
self.assertEqual(type(self.ins1.id), str)
def test_datetime_save(self):
""" Test datetime to compare format """
cre = self.ins0.created_at
self.ins0.save()
up = self.ins0.updated_at
self.assertEqual(type(cre), datetime)
self.assertEqual(type(up), datetime)
self.assertNotEqual(cre, up) # time create and update are diff
def test_to_dict(self):
""" The dict return is the same """
dateform = '%Y-%m-%dT%H:%M:%S.%f'
dic = self.ins0.to_dict()
self.assertEqual(type(dic['created_at']), str)
self.assertEqual(type(dic['updated_at']), str)
self.assertEqual(dic['created_at'],
self.ins0.created_at.strftime(dateform))
self.assertEqual(dic['updated_at'],
self.ins0.updated_at.strftime(dateform))
def test_name_str(self):
""" Test type of date. Will be str"""
namestr = self.ins0.name
self.assertEqual(type(namestr), str)
if __name__ == '__main__':
unittest.main()
# Run Test
# python3 -m unittest discover tests
# python3 -m unittest tests/test_models/test_state.py