forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_amenity.py
executable file
·117 lines (99 loc) · 3.74 KB
/
test_amenity.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
#!/usr/bin/python3
"""
Unit Test for Amenity Class
"""
import unittest
from datetime import datetime
import models
import json
import os
Amenity = models.amenity.Amenity
BaseModel = models.base_model.BaseModel
storage_type = os.environ.get('HBNB_TYPE_STORAGE')
class TestAmenityDocs(unittest.TestCase):
"""Class for testing BaseModel docs"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('..... Testing Documentation .....')
print('........ Amenity Class ........')
print('.................................\n\n')
def test_doc_file(self):
"""... documentation for the file"""
expected = '\nAmenity Class from Models Module\n'
actual = models.amenity.__doc__
self.assertEqual(expected, actual)
def test_doc_class(self):
"""... documentation for the class"""
expected = 'Amenity class handles all application amenities'
actual = Amenity.__doc__
self.assertEqual(expected, actual)
class TestAmenityInstances(unittest.TestCase):
"""testing for class instances"""
@classmethod
def setUpClass(cls):
print('\n\n.................................')
print('....... Testing Functions .......')
print('......... Amenity Class .........')
print('.................................\n\n')
def setUp(self):
"""initializes new amenity for testing"""
self.amenity = Amenity()
def test_instantiation(self):
"""... checks if Amenity is properly instantiated"""
self.assertIsInstance(self.amenity, Amenity)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_to_string(self):
"""... checks if BaseModel is properly casted to string"""
my_str = str(self.amenity)
my_list = ['Amenity', 'id', 'created_at']
actual = 0
for sub_str in my_list:
if sub_str in my_str:
actual += 1
self.assertTrue(3 == actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_instantiation_no_updated(self):
"""... should not have updated attribute"""
my_str = str(self.amenity)
actual = 0
if 'updated_at' in my_str:
actual += 1
self.assertTrue(0 == actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_updated_at(self):
"""... save function should add updated_at attribute"""
self.amenity.save()
actual = type(self.amenity.updated_at)
expected = type(datetime.now())
self.assertEqual(expected, actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_to_json(self):
"""... to_json should return serializable dict object"""
self.amenity_json = self.amenity.to_json()
actual = 1
try:
serialized = json.dumps(self.amenity_json)
except:
actual = 0
self.assertTrue(1 == actual)
@unittest.skipIf(storage_type == 'db', 'skip if environ is db')
def test_json_class(self):
"""... to_json should include class key with value Amenity"""
self.amenity_json = self.amenity.to_json()
actual = None
if self.amenity_json['__class__']:
actual = self.amenity_json['__class__']
expected = 'Amenity'
self.assertEqual(expected, actual)
def test_amenity_attribute(self):
"""... add amenity attribute"""
self.amenity.name = "greatWifi"
if hasattr(self.amenity, 'name'):
actual = self.amenity.name
else:
actual = ''
expected = "greatWifi"
self.assertEqual(expected, actual)
if __name__ == '__main__':
unittest.main