-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_city.py
executable file
·131 lines (114 loc) · 5.01 KB
/
test_city.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
#!/usr/bin/python3
"""
Contains the TestCityDocs classes
"""
from datetime import datetime
import inspect
from models import city
from models.base_model import BaseModel
import os
import pep8
import unittest
from sqlalchemy.orm.attributes import InstrumentedAttribute
City = city.City
class TestCityDocs(unittest.TestCase):
"""Tests to check the documentation and style of City class"""
@classmethod
def setUpClass(cls):
"""Set up for the doc tests"""
cls.city_f = inspect.getmembers(City, inspect.isfunction)
def test_pep8_conformance_city(self):
"""Test that models/city.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['models/city.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
def test_pep8_conformance_test_city(self):
"""Test that tests/test_models/test_city.py conforms to PEP8."""
pep8s = pep8.StyleGuide(quiet=True)
result = pep8s.check_files(['tests/test_models/test_city.py'])
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")
def test_city_module_docstring(self):
"""Test for the city.py module docstring"""
self.assertIsNot(city.__doc__, None,
"city.py needs a docstring")
self.assertTrue(len(city.__doc__) >= 1,
"city.py needs a docstring")
def test_city_class_docstring(self):
"""Test for the City class docstring"""
self.assertIsNot(City.__doc__, None,
"City class needs a docstring")
self.assertTrue(len(City.__doc__) >= 1,
"City class needs a docstring")
def test_city_func_docstrings(self):
"""Test for the presence of docstrings in City methods"""
for func in self.city_f:
self.assertIsNot(func[1].__doc__, None,
"{:s} method needs a docstring".format(func[0]))
self.assertTrue(len(func[1].__doc__) >= 1,
"{:s} method needs a docstring".format(func[0]))
class TestCity(unittest.TestCase):
"""Test the City class"""
def test_is_subclass(self):
"""Test that City is a subclass of BaseModel"""
city = City()
self.assertIsInstance(city, BaseModel)
self.assertTrue(hasattr(city, "id"))
self.assertTrue(hasattr(city, "created_at"))
self.assertTrue(hasattr(city, "updated_at"))
@unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') == 'db',
"Testing DBStorage")
def test_name_attr(self):
"""Test that City has attribute name, and it's an empty string"""
city = City()
self.assertTrue(hasattr(city, "name"))
self.assertEqual(city.name, "")
@unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') != 'db',
"Testing FileStorage")
def test_name_attr_db(self):
"""Test for DBStorage name attribute"""
city = City()
self.assertTrue(hasattr(City, "name"))
self.assertIsInstance(City.name, InstrumentedAttribute)
@unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') == 'db',
"Testing DBStorage")
def test_state_id_attr(self):
"""Test that City has attribute state_id, and it's an empty string"""
city = City()
self.assertTrue(hasattr(city, "state_id"))
self.assertEqual(city.state_id, "")
@unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') != 'db',
"Testing FileStorage")
def test_state_id_attr_db(self):
"""Test for DBStorage state_id attribute"""
city = City()
self.assertTrue(hasattr(City, "state_id"))
self.assertIsInstance(City.state_id, InstrumentedAttribute)
def test_to_dict_creates_dict(self):
"""test to_dict method creates a dictionary with proper attrs"""
c = City()
new_d = c.to_dict()
self.assertEqual(type(new_d), dict)
for attr in c.__dict__:
with self.subTest(attr=attr):
if attr == '_sa_instance_state':
continue
self.assertTrue(attr in new_d)
if os.getenv('HBNB_TYPE_STORAGE') != 'db':
self.assertTrue("__class__" in new_d)
def test_to_dict_values(self):
"""test that values in dict returned from to_dict are correct"""
t_format = "%Y-%m-%dT%H:%M:%S.%f"
c = City()
new_d = c.to_dict()
self.assertEqual(new_d["__class__"], "City")
self.assertEqual(type(new_d["created_at"]), str)
self.assertEqual(type(new_d["updated_at"]), str)
self.assertEqual(new_d["created_at"], c.created_at.strftime(t_format))
self.assertEqual(new_d["updated_at"], c.updated_at.strftime(t_format))
def test_str(self):
"""test that the str method has the correct output"""
city = City()
string = "[City] ({}) {}".format(city.id, city.__dict__)
self.assertEqual(string, str(city))