Skip to content

Implement get and count methods; add tests #3672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/console.cpython-38.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Amenity.93cad938-ec23-4861-8fd7-104ca47cb306": {"id": "93cad938-ec23-4861-8fd7-104ca47cb306", "created_at": "2024-08-06T20:47:36.308460", "updated_at": "2024-08-06T20:47:36.308460", "__class__": "Amenity"}, "BaseModel.e0593949-6aa4-46ff-be99-3d0102d4987e": {"id": "e0593949-6aa4-46ff-be99-3d0102d4987e", "created_at": "2024-08-06T20:47:36.308472", "updated_at": "2024-08-06T20:47:36.308472", "__class__": "BaseModel"}, "City.87fec006-4888-442c-8156-95289b317d27": {"id": "87fec006-4888-442c-8156-95289b317d27", "created_at": "2024-08-06T20:47:36.308483", "updated_at": "2024-08-06T20:47:36.308483", "__class__": "City"}, "Place.fe5f3d64-881f-4315-a92a-bfe27c236d4a": {"id": "fe5f3d64-881f-4315-a92a-bfe27c236d4a", "created_at": "2024-08-06T20:47:36.308493", "updated_at": "2024-08-06T20:47:36.308493", "__class__": "Place"}, "Review.683566f0-e28b-4641-b693-b77add8c5fff": {"id": "683566f0-e28b-4641-b693-b77add8c5fff", "created_at": "2024-08-06T20:47:36.308501", "updated_at": "2024-08-06T20:47:36.308501", "__class__": "Review"}, "State.674aa172-9119-447e-a428-330cf45b7c8d": {"id": "674aa172-9119-447e-a428-330cf45b7c8d", "created_at": "2024-08-06T20:47:36.308510", "updated_at": "2024-08-06T20:47:36.308510", "__class__": "State"}, "User.b025ddf9-c08a-4f92-9a6c-741b43148d74": {"id": "b025ddf9-c08a-4f92-9a6c-741b43148d74", "created_at": "2024-08-06T20:47:36.308520", "updated_at": "2024-08-06T20:47:36.308520", "__class__": "User"}}
Binary file added models/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/amenity.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/base_model.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/city.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/place.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/review.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/state.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/user.cpython-38.pyc
Binary file not shown.
Binary file added models/engine/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 11 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ def reload(self):
def close(self):
"""call remove() method on the private session attribute"""
self.__session.remove()

def get(self, cls, id):
"""Retrieve an object"""
if cls and id:
key = "{}.{}".format(cls.__name__, id)
return self.all().get(key)
return None

def count(self, cls=None):
"""Count the number of objects in storage"""
return len(self.all(cls))
13 changes: 12 additions & 1 deletion models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def reload(self):
jo = json.load(f)
for key in jo:
self.__objects[key] = classes[jo[key]["__class__"]](**jo[key])
except:
except BaseException:
pass

def delete(self, obj=None):
Expand All @@ -68,3 +68,14 @@ def delete(self, obj=None):
def close(self):
"""call reload() method for deserializing the JSON file to objects"""
self.reload()

def get(self, cls, id):
"""Retrieve an object"""
if cls and id:
key = "{}.{}".format(cls.__name__, id)
return self.all().get(key)
return None

def count(self, cls=None):
"""count the number of objects in storage"""
return len(self.all(cls))
Binary file added tests/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_console.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 4 additions & 4 deletions tests/test_models/test_base_model.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ def test_datetime_attributes(self):
"""Test that two BaseModel instances have different datetime objects
and that upon creation have identical updated_at and created_at
value."""
tic = datetime.now()
tic = datetime.utcnow()
inst1 = BaseModel()
toc = datetime.now()
toc = datetime.utcnow()
self.assertTrue(tic <= inst1.created_at <= toc)
time.sleep(1e-4)
tic = datetime.now()
tic = datetime.utcnow()
inst2 = BaseModel()
toc = datetime.now()
toc = datetime.utcnow()
self.assertTrue(tic <= inst2.created_at <= toc)
self.assertEqual(inst1.created_at, inst1.updated_at)
self.assertEqual(inst2.created_at, inst2.updated_at)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
29 changes: 29 additions & 0 deletions tests/test_models/test_engine/test_db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
DBStorage = db_storage.DBStorage
classes = {"Amenity": Amenity, "City": City, "Place": Place,
"Review": Review, "State": State, "User": User}
print(f"Running tests with storage type: {models.storage_t}")


class TestDBStorageDocs(unittest.TestCase):
Expand Down Expand Up @@ -86,3 +87,31 @@ def test_new(self):
@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_save(self):
"""Test that save properly saves objects to file.json"""

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_get(self):
"""tests get method"""
storage = DBStorage()
state = State(name="Michigan")
storage.new(state)
storage.save()
obj = storage.get(State, state.id)
self.assertIsNotNone(obj)
self.assertEqual(obj.id, state.id)
self.assertEqual(obj.name, "Michigan")
storage.delete(state)
storage.save()

@unittest.skipIf(models.storage_t != 'db', "not testing db storage")
def test_count(self):
"""tests count method"""
storage = DBStorage()
initial_count = storage.count()
state = State(name="Michigan")
storage.new(state)
storage.save()
count = storage.count()
self.assertEqual(count, initial_count + 1)
storage.delete(state)
storage.save()
self.assertEqual(count, initial_count)
29 changes: 29 additions & 0 deletions tests/test_models/test_engine/test_file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,32 @@ def test_save(self):
with open("file.json", "r") as f:
js = f.read()
self.assertEqual(json.loads(string), json.loads(js))

@unittest.skipIf(models.storage_t == 'db', "not testing file storage")
def test_get(self):
"""tests the get method"""
storage = FileStorage()
state = State(name="Michigan")
storage.new(state)
storage.save()
obj = storage.get(State, state.id)
self.assertIsNotNone(obj)
self.assertEqual(obj.id, state.id)
self.assertEqual(obj.name, "Michigan")
storage.delete(state)
storage.save()

@unittest.skipIf(models.storage_t == 'db', "not testing file storage")
def test_count(self):
"""tests the count method"""
storage = FileStorage()
initial_count = storage.count()
state = State(name="Michigan")
storage.new(state)
storage.save()
count = storage.count()
self.assertEqual(count, initial_count + 1)
storage.delete(state)
storage.save()
count = storage.count()
self.assertEqual(count, initial_count)