-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_base_model.py
executable file
·109 lines (87 loc) · 3.69 KB
/
test_base_model.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
""" testing files """
import unittest
import inspect
import pep8
from models.base_model import BaseModel
from datetime import datetime
class BaseModel_testing(unittest.TestCase):
""" check BaseModel """
def testpep8(self):
""" testing codestyle """
pepstylecode = pep8.StyleGuide(quiet=True)
rest = pepstylecode.check_files(['models/base_model.py',
'models/__init__.py',
'models/engine/file_storage.py'])
self.assertEqual(rest.total_errors, 0,
"Found code style errors (and warnings).")
class test_for_base_model(unittest.TestCase):
""" Class test for BaseModel """
my_model = BaseModel()
def TearDown(self):
""" delete json file """
del self.test
def SetUp(self):
""" Create instance """
self.test = BaseModel()
def test_attr_none(self):
"""None attribute."""
object_test = BaseModel(None)
self.assertTrue(hasattr(object_test, "id"))
self.assertTrue(hasattr(object_test, "created_at"))
self.assertTrue(hasattr(object_test, "updated_at"))
def test_kwargs_constructor_2(self):
""" check id with data """
dictonary = {'score': 100}
object_test = BaseModel(**dictonary)
self.assertTrue(hasattr(object_test, 'id'))
self.assertTrue(hasattr(object_test, 'created_at'))
self.assertTrue(hasattr(object_test, 'updated_at'))
self.assertTrue(hasattr(object_test, 'score'))
def test_str(self):
""" Test string """
dictonary = {'id': 'cc9909cf-a909-9b90-9999-999fd99ca9a9',
'created_at': '2025-06-28T14:00:00.000001',
'__class__': 'BaseModel',
'updated_at': '2030-06-28T14:00:00.000001',
'score': 100
}
object_test = BaseModel(**dictonary)
out = "[{}] ({}) {}\n".format(type(object_test).__name__, object_test.id, object_test.__dict__)
def test_to_dict(self):
""" check dict """
object_test = BaseModel(score=300)
n_dict = object_test.to_dict()
self.assertEqual(n_dict['id'], object_test.id)
self.assertEqual(n_dict['score'], 300)
self.assertEqual(n_dict['__class__'], 'BaseModel')
self.assertEqual(n_dict['created_at'], object_test.created_at.isoformat())
self.assertEqual(n_dict['updated_at'], object_test.updated_at.isoformat())
self.assertEqual(type(n_dict['created_at']), str)
self.assertEqual(type(n_dict['created_at']), str)
def test_datetime(self):
""" check datatime """
bas1 = BaseModel()
self.assertFalse(datetime.now() == bas1.created_at)
def test_BaseModel(self):
""" check attributes values in a BaseModel """
self.my_model.name = "Holbie"
self.my_model.my_number = 100
self.my_model.save()
my_model_json = self.my_model.to_dict()
self.assertEqual(self.my_model.name, my_model_json['name'])
self.assertEqual(self.my_model.my_number, my_model_json['my_number'])
self.assertEqual('BaseModel', my_model_json['__class__'])
self.assertEqual(self.my_model.id, my_model_json['id'])
def test_savefirst(self):
"""check numbers"""
with self.assertRaises(AttributeError):
BaseModel.save([455, 323232, 2323, 2323, 23332])
def test_savesecond(self):
""" check string """
with self.assertRaises(AttributeError):
BaseModel.save("THIS IS A TEST")
def test_inst(self):
"""check class """
ml = BaseModel()
self.assertTrue(ml, BaseModel)