Skip to content

Commit 95281ef

Browse files
committed
fix unittests to accomodate DBStorage
1 parent 01fce9b commit 95281ef

9 files changed

+219
-46
lines changed

tests/test_models/test_amenity.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from datetime import datetime
77
import inspect
8+
import models
89
from models import amenity
910
from models.base_model import BaseModel
1011
import pep8
@@ -70,16 +71,22 @@ def test_name_attr(self):
7071
"""Test that Amenity has attribute name, and it's as an empty string"""
7172
amenity = Amenity()
7273
self.assertTrue(hasattr(amenity, "name"))
73-
self.assertEqual(amenity.name, "")
74+
if models.storage_t == 'db':
75+
self.assertEqual(amenity.name, None)
76+
else:
77+
self.assertEqual(amenity.name, "")
7478

7579
def test_to_dict_creates_dict(self):
7680
"""test to_dict method creates a dictionary with proper attrs"""
7781
am = Amenity()
82+
print(am.__dict__)
7883
new_d = am.to_dict()
7984
self.assertEqual(type(new_d), dict)
85+
self.assertFalse("_sa_instance_state" in new_d)
8086
for attr in am.__dict__:
81-
self.assertTrue(attr in new_d)
82-
self.assertTrue("__class__" in new_d)
87+
if attr is not "_sa_instance_state":
88+
self.assertTrue(attr in new_d)
89+
self.assertTrue("__class__" in new_d)
8390

8491
def test_to_dict_values(self):
8592
"""test that values in dict returned from to_dict are correct"""

tests/test_models/test_base_model.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def test_func_docstrings(self):
5858

5959
class TestBaseModel(unittest.TestCase):
6060
"""Test the BaseModel class"""
61-
@mock.patch('models.storage')
62-
def test_instantiation(self, mock_storage):
61+
def test_instantiation(self):
6362
"""Test that object is correctly created"""
6463
inst = BaseModel()
6564
self.assertIs(type(inst), BaseModel)
@@ -76,7 +75,6 @@ def test_instantiation(self, mock_storage):
7675
with self.subTest(attr=attr, typ=typ):
7776
self.assertIn(attr, inst.__dict__)
7877
self.assertIs(type(inst.__dict__[attr]), typ)
79-
self.assertTrue(mock_storage.new.called)
8078
self.assertEqual(inst.name, "Holberton")
8179
self.assertEqual(inst.number, 89)
8280

@@ -158,4 +156,5 @@ def test_save(self, mock_storage):
158156
new_updated_at = inst.updated_at
159157
self.assertNotEqual(old_updated_at, new_updated_at)
160158
self.assertEqual(old_created_at, new_created_at)
159+
self.assertTrue(mock_storage.new.called)
161160
self.assertTrue(mock_storage.save.called)

tests/test_models/test_city.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from datetime import datetime
77
import inspect
8+
import models
89
from models import city
910
from models.base_model import BaseModel
1011
import pep8
@@ -70,22 +71,30 @@ def test_name_attr(self):
7071
"""Test that City has attribute name, and it's an empty string"""
7172
city = City()
7273
self.assertTrue(hasattr(city, "name"))
73-
self.assertEqual(city.name, "")
74+
if models.storage_t == 'db':
75+
self.assertEqual(city.name, None)
76+
else:
77+
self.assertEqual(city.name, "")
7478

7579
def test_state_id_attr(self):
7680
"""Test that City has attribute state_id, and it's an empty string"""
7781
city = City()
7882
self.assertTrue(hasattr(city, "state_id"))
79-
self.assertEqual(city.state_id, "")
83+
if models.storage_t == 'db':
84+
self.assertEqual(city.state_id, None)
85+
else:
86+
self.assertEqual(city.state_id, "")
8087

8188
def test_to_dict_creates_dict(self):
8289
"""test to_dict method creates a dictionary with proper attrs"""
8390
c = City()
8491
new_d = c.to_dict()
8592
self.assertEqual(type(new_d), dict)
93+
self.assertFalse("_sa_instance_state" in new_d)
8694
for attr in c.__dict__:
87-
self.assertTrue(attr in new_d)
88-
self.assertTrue("__class__" in new_d)
95+
if attr is not "_sa_instance_state":
96+
self.assertTrue(attr in new_d)
97+
self.assertTrue("__class__" in new_d)
8998

9099
def test_to_dict_values(self):
91100
"""test that values in dict returned from to_dict are correct"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/python3
2+
"""
3+
Contains the TestDBStorageDocs and TestDBStorage classes
4+
"""
5+
6+
from datetime import datetime
7+
import inspect
8+
import models
9+
from models.engine import db_storage
10+
from models.amenity import Amenity
11+
from models.base_model import BaseModel
12+
from models.city import City
13+
from models.place import Place
14+
from models.review import Review
15+
from models.state import State
16+
from models.user import User
17+
import json
18+
import os
19+
import pep8
20+
import unittest
21+
DBStorage = db_storage.DBStorage
22+
classes = {"Amenity": Amenity, "City": City, "Place": Place,
23+
"Review": Review, "State": State, "User": User}
24+
25+
26+
class TestDBStorageDocs(unittest.TestCase):
27+
"""Tests to check the documentation and style of DBStorage class"""
28+
@classmethod
29+
def setUpClass(cls):
30+
"""Set up for the doc tests"""
31+
cls.dbs_f = inspect.getmembers(DBStorage, inspect.isfunction)
32+
33+
def test_pep8_conformance_db_storage(self):
34+
"""Test that models/engine/db_storage.py conforms to PEP8."""
35+
pep8s = pep8.StyleGuide(quiet=True)
36+
result = pep8s.check_files(['models/engine/db_storage.py'])
37+
self.assertEqual(result.total_errors, 0,
38+
"Found code style errors (and warnings).")
39+
40+
def test_pep8_conformance_test_db_storage(self):
41+
"""Test tests/test_models/test_db_storage.py conforms to PEP8."""
42+
pep8s = pep8.StyleGuide(quiet=True)
43+
result = pep8s.check_files(['tests/test_models/test_engine/\
44+
test_db_storage.py'])
45+
self.assertEqual(result.total_errors, 0,
46+
"Found code style errors (and warnings).")
47+
48+
def test_db_storage_module_docstring(self):
49+
"""Test for the db_storage.py module docstring"""
50+
self.assertIsNot(db_storage.__doc__, None,
51+
"db_storage.py needs a docstring")
52+
self.assertTrue(len(db_storage.__doc__) >= 1,
53+
"db_storage.py needs a docstring")
54+
55+
def test_db_storage_class_docstring(self):
56+
"""Test for the DBStorage class docstring"""
57+
self.assertIsNot(DBStorage.__doc__, None,
58+
"DBStorage class needs a docstring")
59+
self.assertTrue(len(DBStorage.__doc__) >= 1,
60+
"DBStorage class needs a docstring")
61+
62+
def test_dbs_func_docstrings(self):
63+
"""Test for the presence of docstrings in DBStorage methods"""
64+
for func in self.dbs_f:
65+
self.assertIsNot(func[1].__doc__, None,
66+
"{:s} method needs a docstring".format(func[0]))
67+
self.assertTrue(len(func[1].__doc__) >= 1,
68+
"{:s} method needs a docstring".format(func[0]))
69+
70+
71+
class TestFileStorage(unittest.TestCase):
72+
"""Test the FileStorage class"""
73+
@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
74+
def test_all_returns_dict(self):
75+
"""Test that all returns a dictionaty"""
76+
self.assertIs(type(models.storage.all()), dict)
77+
78+
@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
79+
def test_all_no_class(self):
80+
"""Test that all returns all rows when no class is passed"""
81+
82+
@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
83+
def test_new(self):
84+
"""test that new adds an object to the database"""
85+
86+
@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
87+
def test_save(self):
88+
"""Test that save properly saves objects to file.json"""

tests/test_models/test_engine/test_file_storage.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from datetime import datetime
77
import inspect
8+
import models
89
from models.engine import file_storage
910
from models.amenity import Amenity
1011
from models.base_model import BaseModel
@@ -54,9 +55,9 @@ def test_file_storage_module_docstring(self):
5455
def test_file_storage_class_docstring(self):
5556
"""Test for the FileStorage class docstring"""
5657
self.assertIsNot(FileStorage.__doc__, None,
57-
"State class needs a docstring")
58+
"FileStorage class needs a docstring")
5859
self.assertTrue(len(FileStorage.__doc__) >= 1,
59-
"State class needs a docstring")
60+
"FileStorage class needs a docstring")
6061

6162
def test_fs_func_docstrings(self):
6263
"""Test for the presence of docstrings in FileStorage methods"""
@@ -69,13 +70,15 @@ def test_fs_func_docstrings(self):
6970

7071
class TestFileStorage(unittest.TestCase):
7172
"""Test the FileStorage class"""
73+
@unittest.skipIf(models.storage_t == 'db', "not testing file storage")
7274
def test_all_returns_dict(self):
7375
"""Test that all returns the FileStorage.__objects attr"""
7476
storage = FileStorage()
7577
new_dict = storage.all()
7678
self.assertEqual(type(new_dict), dict)
7779
self.assertIs(new_dict, storage._FileStorage__objects)
7880

81+
@unittest.skipIf(models.storage_t == 'db', "not testing file storage")
7982
def test_new(self):
8083
"""test that new adds an object to the FileStorage.__objects attr"""
8184
storage = FileStorage()
@@ -91,9 +94,9 @@ def test_new(self):
9194
self.assertEqual(test_dict, storage._FileStorage__objects)
9295
FileStorage._FileStorage__objects = save
9396

97+
@unittest.skipIf(models.storage_t == 'db', "not testing file storage")
9498
def test_save(self):
9599
"""Test that save properly saves objects to file.json"""
96-
os.remove("file.json")
97100
storage = FileStorage()
98101
new_dict = {}
99102
for key, value in classes.items():

tests/test_models/test_place.py

+53-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from datetime import datetime
77
import inspect
8+
import models
89
from models import place
910
from models.base_model import BaseModel
1011
import pep8
@@ -70,68 +71,99 @@ def test_city_id_attr(self):
7071
"""Test Place has attr city_id, and it's an empty string"""
7172
place = Place()
7273
self.assertTrue(hasattr(place, "city_id"))
73-
self.assertEqual(place.city_id, "")
74+
if models.storage_t == 'db':
75+
self.assertEqual(place.city_id, None)
76+
else:
77+
self.assertEqual(place.city_id, "")
7478

7579
def test_user_id_attr(self):
7680
"""Test Place has attr user_id, and it's an empty string"""
7781
place = Place()
7882
self.assertTrue(hasattr(place, "user_id"))
79-
self.assertEqual(place.user_id, "")
83+
if models.storage_t == 'db':
84+
self.assertEqual(place.user_id, None)
85+
else:
86+
self.assertEqual(place.user_id, "")
8087

8188
def test_name_attr(self):
8289
"""Test Place has attr name, and it's an empty string"""
8390
place = Place()
8491
self.assertTrue(hasattr(place, "name"))
85-
self.assertEqual(place.name, "")
92+
if models.storage_t == 'db':
93+
self.assertEqual(place.name, None)
94+
else:
95+
self.assertEqual(place.name, "")
8696

8797
def test_description_attr(self):
8898
"""Test Place has attr description, and it's an empty string"""
8999
place = Place()
90100
self.assertTrue(hasattr(place, "description"))
91-
self.assertEqual(place.description, "")
101+
if models.storage_t == 'db':
102+
self.assertEqual(place.description, None)
103+
else:
104+
self.assertEqual(place.description, "")
92105

93106
def test_number_rooms_attr(self):
94107
"""Test Place has attr number_rooms, and it's an int == 0"""
95108
place = Place()
96109
self.assertTrue(hasattr(place, "number_rooms"))
97-
self.assertEqual(type(place.number_rooms), int)
98-
self.assertEqual(place.number_rooms, 0)
110+
if models.storage_t == 'db':
111+
self.assertEqual(place.number_rooms, None)
112+
else:
113+
self.assertEqual(type(place.number_rooms), int)
114+
self.assertEqual(place.number_rooms, 0)
99115

100116
def test_number_bathrooms_attr(self):
101117
"""Test Place has attr number_bathrooms, and it's an int == 0"""
102118
place = Place()
103119
self.assertTrue(hasattr(place, "number_bathrooms"))
104-
self.assertEqual(type(place.number_bathrooms), int)
105-
self.assertEqual(place.number_bathrooms, 0)
120+
if models.storage_t == 'db':
121+
self.assertEqual(place.number_bathrooms, None)
122+
else:
123+
self.assertEqual(type(place.number_bathrooms), int)
124+
self.assertEqual(place.number_bathrooms, 0)
106125

107126
def test_max_guest_attr(self):
108127
"""Test Place has attr max_guest, and it's an int == 0"""
109128
place = Place()
110129
self.assertTrue(hasattr(place, "max_guest"))
111-
self.assertEqual(type(place.max_guest), int)
112-
self.assertEqual(place.max_guest, 0)
130+
if models.storage_t == 'db':
131+
self.assertEqual(place.max_guest, None)
132+
else:
133+
self.assertEqual(type(place.max_guest), int)
134+
self.assertEqual(place.max_guest, 0)
113135

114136
def test_price_by_night_attr(self):
115137
"""Test Place has attr price_by_night, and it's an int == 0"""
116138
place = Place()
117139
self.assertTrue(hasattr(place, "price_by_night"))
118-
self.assertEqual(type(place.price_by_night), int)
119-
self.assertEqual(place.price_by_night, 0)
140+
if models.storage_t == 'db':
141+
self.assertEqual(place.price_by_night, None)
142+
else:
143+
self.assertEqual(type(place.price_by_night), int)
144+
self.assertEqual(place.price_by_night, 0)
120145

121146
def test_latitude_attr(self):
122147
"""Test Place has attr latitude, and it's a float == 0.0"""
123148
place = Place()
124149
self.assertTrue(hasattr(place, "latitude"))
125-
self.assertEqual(type(place.latitude), float)
126-
self.assertEqual(place.latitude, 0.0)
150+
if models.storage_t == 'db':
151+
self.assertEqual(place.latitude, None)
152+
else:
153+
self.assertEqual(type(place.latitude), float)
154+
self.assertEqual(place.latitude, 0.0)
127155

128-
def test_latitude_attr(self):
156+
def test_longitude_attr(self):
129157
"""Test Place has attr longitude, and it's a float == 0.0"""
130158
place = Place()
131159
self.assertTrue(hasattr(place, "longitude"))
132-
self.assertEqual(type(place.longitude), float)
133-
self.assertEqual(place.longitude, 0.0)
160+
if models.storage_t == 'db':
161+
self.assertEqual(place.longitude, None)
162+
else:
163+
self.assertEqual(type(place.longitude), float)
164+
self.assertEqual(place.longitude, 0.0)
134165

166+
@unittest.skipIf(models.storage_t == 'db', "not testing File Storage")
135167
def test_amenity_ids_attr(self):
136168
"""Test Place has attr amenity_ids, and it's an empty list"""
137169
place = Place()
@@ -144,9 +176,11 @@ def test_to_dict_creates_dict(self):
144176
p = Place()
145177
new_d = p.to_dict()
146178
self.assertEqual(type(new_d), dict)
179+
self.assertFalse("_sa_instance_state" in new_d)
147180
for attr in p.__dict__:
148-
self.assertTrue(attr in new_d)
149-
self.assertTrue("__class__" in new_d)
181+
if attr is not "_sa_instance_state":
182+
self.assertTrue(attr in new_d)
183+
self.assertTrue("__class__" in new_d)
150184

151185
def test_to_dict_values(self):
152186
"""test that values in dict returned from to_dict are correct"""

0 commit comments

Comments
 (0)