-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_review.py
executable file
·204 lines (165 loc) · 6.09 KB
/
test_review.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python3
"""
Unittests for models/review.py.
Unittest classes:
TestReviewInstantiation
TestReviewSave
TestReviewToDict
"""
import os
import models
import unittest
from datetime import datetime
from time import sleep
from models.review import Review
class TestReviewInstantiation(unittest.TestCase):
""" Testing the Review class instantiation """
def test_no_args_instantiates(self):
self.assertEqual(Review, type(Review()))
def test_new_instance_stored_in_objects(self):
self.assertIn(Review(), models.storage.all().values())
def test_id_is_public_str(self):
self.assertEqual(str, type(Review().id))
def test_created_at_is_public_datetime(self):
self.assertEqual(datetime, type(Review().created_at))
def test_updated_at_is_public_datetime(self):
self.assertEqual(datetime, type(Review().updated_at))
def test_place_id_is_public_class_attr(self):
review = Review()
self.assertEqual(str, type(Review.place_id))
self.assertIn("place_id", dir(review))
self.assertNotIn("place_id", review.__dict__)
def test_user_id_is_public_class_attr(self):
review = Review()
self.assertEqual(str, type(Review.user_id))
self.assertIn("user_id", dir(review))
self.assertNotIn("user_id", review.__dict__)
def test_text_is_public_class_attribute(self):
review = Review()
self.assertEqual(str, type(Review.text))
self.assertIn("text", dir(review))
self.assertNotIn("text", review.__dict__)
def test_two_reviews_unique_ids(self):
review = Review()
review1 = Review()
self.assertNotEqual(review.id, review1.id)
def test_two_reviews_different_created_at(self):
review = Review()
sleep(0.05)
review1 = Review()
self.assertLess(review.created_at, review1.created_at)
def test_two_reviews_different_updated_at(self):
review = Review()
sleep(0.05)
review2 = Review()
self.assertLess(review.updated_at, review1.updated_at)
def test_str_representation(self):
dt = datetime.today()
dt_repr = repr(dt)
review = Review()
review.id = "123456"
review.created_at = review.updated_at = dt
rvstr = review.__str__()
self.assertIn("[Review] (123456)", rvstr)
self.assertIn("'id': '123456'", rvstr)
self.assertIn("'created_at': " + dt_repr, rvstr)
self.assertIn("'updated_at': " + dt_repr, rvstr)
def test_args_unused(self):
review = Review(None)
self.assertNotIn(None, review.__dict__.values())
def test_instantiation_with_kwargs(self):
dt = datetime.today()
dt_iso = dt.isoformat()
review = Review(id="345", created_at=dt_iso, updated_at=dt_iso)
self.assertEqual(review.id, "345")
self.assertEqual(review.created_at, dt)
self.assertEqual(review.updated_at, dt)
def test_instantiation_with_None_kwargs(self):
with self.assertRaises(TypeError):
Review(id=None, created_at=None, updated_at=None)
class TestReviewSave(unittest.TestCase):
""" Testing the Review class save """
@classmethod
def setUp(self):
try:
os.rename("file.json", "tmp")
except IOError:
pass
def tearDown(self):
try:
os.remove("file.json")
except IOError:
pass
try:
os.rename("tmp", "file.json")
except IOError:
pass
def test_one_save(self):
review = Review()
sleep(0.05)
first_updated_at = review.updated_at
review.save()
self.assertLess(first_updated_at, review.updated_at)
def test_two_saves(self):
review = Review()
sleep(0.05)
first_updated_at = review.updated_at
review.save()
second_updated_at = review.updated_at
self.assertLess(first_updated_at, second_updated_at)
sleep(0.05)
review.save()
self.assertLess(second_updated_at, review.updated_at)
def test_save_with_arg(self):
review = Review()
with self.assertRaises(TypeError):
review.save(None)
def test_save_updates_file(self):
review = Review()
review.save()
rvid = "Review." + rv.id
with open("file.json", "r") as f:
self.assertIn(rvid, f.read())
class TestReviewToDict(unittest.TestCase):
""" Testing the Review class to_dict """
def test_to_dict_type(self):
self.assertTrue(dict, type(Review().to_dict()))
def test_to_dict_contains_correct_keys(self):
review = Review()
self.assertIn("id", review.to_dict())
self.assertIn("created_at", review.to_dict())
self.assertIn("updated_at", review.to_dict())
self.assertIn("__class__", review.to_dict())
def test_to_dict_contains_added_attributes(self):
review = Review()
review.middle_name = "Holberton"
review.my_number = 98
self.assertEqual("Holberton", review.middle_name)
self.assertIn("my_number", review.to_dict())
def test_to_dict_datetime_attributes_are_strs(self):
review = Review()
review_dict = review.to_dict()
self.assertEqual(str, type(review_dict["id"]))
self.assertEqual(str, type(review_dict["created_at"]))
self.assertEqual(str, type(review_dict["updated_at"]))
def test_to_dict_output(self):
dt = datetime.today()
review = Review()
review.id = "123456"
review.created_at = review.updated_at = dt
tdict = {
'id': '123456',
'__class__': 'Review',
'created_at': dt.isoformat(),
'updated_at': dt.isoformat(),
}
self.assertDictEqual(review.to_dict(), tdict)
def test_contrast_to_dict_dunder_dict(self):
review = Review()
self.assertNotEqual(review.to_dict(), review.__dict__)
def test_to_dict_with_arg(self):
review = Review()
with self.assertRaises(TypeError):
review.to_dict(None)
if __name__ == "__main__":
unittest.main()