Skip to content
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

test cases for storage #3689

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
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

Jennifer Huang <[email protected]>
Alexa Orrico <[email protected]>
Joann Vuong <[email protected]>
Joann Vuong <[email protected]>
Lawrence Maina <[email protected]>
Binary file added __pycache__/console.cpython-310.pyc
Binary file not shown.
9 changes: 5 additions & 4 deletions console.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def _key_value_parser(self, args):
else:
try:
value = int(value)
except:
except Exception:
try:
value = float(value)
except:
except Exception:
continue
new_dict[key] = value
return new_dict
Expand Down Expand Up @@ -140,12 +140,12 @@ def do_update(self, arg):
if args[2] in integers:
try:
args[3] = int(args[3])
except:
except Exception:
args[3] = 0
elif args[2] in floats:
try:
args[3] = float(args[3])
except:
except Exception:
args[3] = 0.0
setattr(models.storage.all()[k], args[2], args[3])
models.storage.all()[k].save()
Expand All @@ -160,5 +160,6 @@ def do_update(self, arg):
else:
print("** class doesn't exist **")


if __name__ == '__main__':
HBNBCommand().cmdloop()
1 change: 1 addition & 0 deletions file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Amenity.3120279c-a568-4e8d-9e4e-30a0e067c6b2": {"id": "3120279c-a568-4e8d-9e4e-30a0e067c6b2", "created_at": "2024-08-08T03:53:45.625624", "updated_at": "2024-08-08T03:53:45.625624", "__class__": "Amenity"}, "BaseModel.620b24f4-5166-463a-823c-d5a114ea9f84": {"id": "620b24f4-5166-463a-823c-d5a114ea9f84", "created_at": "2024-08-08T03:53:45.625657", "updated_at": "2024-08-08T03:53:45.625657", "__class__": "BaseModel"}, "City.56a01d61-a465-42dc-9896-c6e7134f481d": {"id": "56a01d61-a465-42dc-9896-c6e7134f481d", "created_at": "2024-08-08T03:53:45.625673", "updated_at": "2024-08-08T03:53:45.625673", "__class__": "City"}, "Place.323565c8-22df-47cb-b1e7-accd224c3e2b": {"id": "323565c8-22df-47cb-b1e7-accd224c3e2b", "created_at": "2024-08-08T03:53:45.625686", "updated_at": "2024-08-08T03:53:45.625686", "__class__": "Place"}, "Review.c0e3621f-11cc-4e72-a1c6-433cdbcf2af8": {"id": "c0e3621f-11cc-4e72-a1c6-433cdbcf2af8", "created_at": "2024-08-08T03:53:45.625774", "updated_at": "2024-08-08T03:53:45.625774", "__class__": "Review"}, "State.99087fdf-b51e-4c38-a517-a423ab825b36": {"id": "99087fdf-b51e-4c38-a517-a423ab825b36", "created_at": "2024-08-08T03:53:45.625792", "updated_at": "2024-08-08T03:53:45.625792", "__class__": "State"}, "User.9f379e9f-ac8d-49de-a6d6-9346ed6b9cc8": {"id": "9f379e9f-ac8d-49de-a6d6-9346ed6b9cc8", "created_at": "2024-08-08T03:53:45.625804", "updated_at": "2024-08-08T03:53:45.625804", "__class__": "User"}, "State.090c7317-b672-4bdd-ac88-99cc3a2d84c8": {"name": "Boston", "created_at": "2024-08-10T09:18:48.295131", "updated_at": "2024-08-10T09:18:48.295140", "id": "090c7317-b672-4bdd-ac88-99cc3a2d84c8", "__class__": "State"}, "City.bb703af4-1cec-46a2-ae47-959fae596699": {"name": "Virginia", "state_id": "090c7317-b672-4bdd-ac88-99cc3a2d84c8", "created_at": "2024-08-10T09:18:48.295246", "updated_at": "2024-08-10T09:18:48.295250", "id": "bb703af4-1cec-46a2-ae47-959fae596699", "__class__": "City"}, "State.55a18cc6-e44a-48de-8608-90d22f0d9a70": {"name": "Boston", "created_at": "2024-08-10T09:43:49.929346", "updated_at": "2024-08-10T09:43:49.929354", "id": "55a18cc6-e44a-48de-8608-90d22f0d9a70", "__class__": "State"}, "City.18e91472-56ab-43bd-85d7-b9868c5ce6d4": {"name": "Virginia", "state_id": "55a18cc6-e44a-48de-8608-90d22f0d9a70", "created_at": "2024-08-10T09:43:49.929452", "updated_at": "2024-08-10T09:43:49.929455", "id": "18e91472-56ab-43bd-85d7-b9868c5ce6d4", "__class__": "City"}, "State.ee6875b7-3be5-4c55-b252-8e7f8db0ea58": {"name": "Boston", "created_at": "2024-08-10T09:51:53.055273", "updated_at": "2024-08-10T09:51:53.055280", "id": "ee6875b7-3be5-4c55-b252-8e7f8db0ea58", "__class__": "State"}, "City.5c0950cc-4ff8-4208-9bf9-4133955271b4": {"name": "Virginia", "state_id": "ee6875b7-3be5-4c55-b252-8e7f8db0ea58", "created_at": "2024-08-10T09:51:53.055380", "updated_at": "2024-08-10T09:51:53.055383", "id": "5c0950cc-4ff8-4208-9bf9-4133955271b4", "__class__": "City"}, "State.41b3e24a-50c5-4d39-a17a-67e25d6a0a77": {"name": "Boston", "created_at": "2024-08-10T12:24:01.255455", "updated_at": "2024-08-10T12:24:01.255462", "id": "41b3e24a-50c5-4d39-a17a-67e25d6a0a77", "__class__": "State"}, "City.72cf8747-9f70-45ad-bcb7-b714b4f2a21f": {"name": "Virginia", "state_id": "41b3e24a-50c5-4d39-a17a-67e25d6a0a77", "created_at": "2024-08-10T12:24:01.255554", "updated_at": "2024-08-10T12:24:01.255558", "id": "72cf8747-9f70-45ad-bcb7-b714b4f2a21f", "__class__": "City"}, "State.b62809dc-a5bd-46ad-953f-8c04e6dc64e5": {"name": "Boston", "created_at": "2024-08-10T12:28:24.988935", "updated_at": "2024-08-10T12:28:24.988941", "id": "b62809dc-a5bd-46ad-953f-8c04e6dc64e5", "__class__": "State"}, "City.d58035b3-b772-4930-b889-ab9864405e75": {"name": "Virginia", "state_id": "b62809dc-a5bd-46ad-953f-8c04e6dc64e5", "created_at": "2024-08-10T12:28:24.989044", "updated_at": "2024-08-10T12:28:24.989047", "id": "d58035b3-b772-4930-b889-ab9864405e75", "__class__": "City"}, "State.3deab3a0-92af-4b9f-b1f1-83cc8ba93627": {"name": "Baltimore", "created_at": "2024-08-10T12:28:25.005441", "updated_at": "2024-08-10T12:28:25.005453", "id": "3deab3a0-92af-4b9f-b1f1-83cc8ba93627", "__class__": "State"}, "State.e7bf3083-abdc-4da4-a0a8-081afcd6b8db": {"name": "Boston", "created_at": "2024-08-10T12:34:09.885281", "updated_at": "2024-08-10T12:34:09.885289", "id": "e7bf3083-abdc-4da4-a0a8-081afcd6b8db", "__class__": "State"}, "City.143445cf-e547-4baf-9fc9-50e856b86f89": {"name": "Virginia", "state_id": "e7bf3083-abdc-4da4-a0a8-081afcd6b8db", "created_at": "2024-08-10T12:34:09.885416", "updated_at": "2024-08-10T12:34:09.885419", "id": "143445cf-e547-4baf-9fc9-50e856b86f89", "__class__": "City"}, "State.2354c35f-6722-441f-8ee2-5971de928534": {"name": "Baltimore", "created_at": "2024-08-10T12:34:09.890290", "updated_at": "2024-08-10T12:34:09.890300", "id": "2354c35f-6722-441f-8ee2-5971de928534", "__class__": "State"}, "State.2e45a0cc-f154-44b9-ba54-20423d928a63": {"name": "Boston", "created_at": "2024-08-10T12:37:48.427205", "updated_at": "2024-08-10T12:37:48.427211", "id": "2e45a0cc-f154-44b9-ba54-20423d928a63", "__class__": "State"}, "City.e46062c7-93fa-4a77-afdb-b16359d52822": {"name": "Virginia", "state_id": "2e45a0cc-f154-44b9-ba54-20423d928a63", "created_at": "2024-08-10T12:37:48.427322", "updated_at": "2024-08-10T12:37:48.427327", "id": "e46062c7-93fa-4a77-afdb-b16359d52822", "__class__": "City"}, "State.6b383ec1-cecc-4279-8cd2-4c6409046252": {"name": "Baltimore", "created_at": "2024-08-10T12:37:48.432387", "updated_at": "2024-08-10T12:37:48.432392", "id": "6b383ec1-cecc-4279-8cd2-4c6409046252", "__class__": "State"}, "State.ccc8d742-92ba-49f2-94ab-cba823f860ea": {"name": "Boston", "created_at": "2024-08-10T12:38:33.786540", "updated_at": "2024-08-10T12:38:33.786547", "id": "ccc8d742-92ba-49f2-94ab-cba823f860ea", "__class__": "State"}, "City.4497deaf-9b7a-450c-ae30-df89f7c573b6": {"name": "Virginia", "state_id": "ccc8d742-92ba-49f2-94ab-cba823f860ea", "created_at": "2024-08-10T12:38:33.786664", "updated_at": "2024-08-10T12:38:33.786668", "id": "4497deaf-9b7a-450c-ae30-df89f7c573b6", "__class__": "City"}, "State.629d2cff-7302-47aa-ae6c-2daa9a902c5f": {"name": "Baltimore", "created_at": "2024-08-10T12:38:33.802325", "updated_at": "2024-08-10T12:38:33.802331", "id": "629d2cff-7302-47aa-ae6c-2daa9a902c5f", "__class__": "State"}, "State.9b45a466-71c7-4e01-884b-bf5c31093ce3": {"name": "Boston", "created_at": "2024-08-10T13:04:44.494836", "updated_at": "2024-08-10T13:04:44.494843", "id": "9b45a466-71c7-4e01-884b-bf5c31093ce3", "__class__": "State"}, "City.fdb9f77a-6d0e-43d0-a00e-2f66a12a5c6f": {"name": "Virginia", "state_id": "9b45a466-71c7-4e01-884b-bf5c31093ce3", "created_at": "2024-08-10T13:04:44.494952", "updated_at": "2024-08-10T13:04:44.494956", "id": "fdb9f77a-6d0e-43d0-a00e-2f66a12a5c6f", "__class__": "City"}, "State.fb0e5ca8-b5e4-4755-b9f5-1f8354197b29": {"name": "Baltimore", "created_at": "2024-08-10T13:04:44.510893", "updated_at": "2024-08-10T13:04:44.510901", "id": "fb0e5ca8-b5e4-4755-b9f5-1f8354197b29", "__class__": "State"}, "State.87615ece-5f89-41b0-8e1e-9d53c81ef9d9": {"name": "Boston", "created_at": "2024-08-10T13:10:24.166531", "updated_at": "2024-08-10T13:10:24.166539", "id": "87615ece-5f89-41b0-8e1e-9d53c81ef9d9", "__class__": "State"}, "City.9c993245-8810-4b31-aa55-eb60f1e0c589": {"name": "Virginia", "state_id": "87615ece-5f89-41b0-8e1e-9d53c81ef9d9", "created_at": "2024-08-10T13:10:24.166648", "updated_at": "2024-08-10T13:10:24.166652", "id": "9c993245-8810-4b31-aa55-eb60f1e0c589", "__class__": "City"}, "State.15cb65f7-2f95-4f1e-8fff-87b9340dab87": {"name": "Baltimore", "created_at": "2024-08-10T13:10:24.197115", "updated_at": "2024-08-10T13:10:24.197122", "id": "15cb65f7-2f95-4f1e-8fff-87b9340dab87", "__class__": "State"}, "State.f061069e-a33e-4e99-b39e-98c1c1fba7be": {"name": "Boston", "created_at": "2024-08-10T13:12:59.247329", "updated_at": "2024-08-10T13:12:59.247336", "id": "f061069e-a33e-4e99-b39e-98c1c1fba7be", "__class__": "State"}, "City.f9481c16-8a25-4744-bbb4-8470ed70a772": {"name": "Virginia", "state_id": "f061069e-a33e-4e99-b39e-98c1c1fba7be", "created_at": "2024-08-10T13:12:59.247424", "updated_at": "2024-08-10T13:12:59.247428", "id": "f9481c16-8a25-4744-bbb4-8470ed70a772", "__class__": "City"}, "State.8ba3000b-22c9-49bb-af2d-b557ff2f0ebc": {"name": "Baltimore", "created_at": "2024-08-10T13:12:59.253469", "updated_at": "2024-08-10T13:12:59.253475", "id": "8ba3000b-22c9-49bb-af2d-b557ff2f0ebc", "__class__": "State"}, "State.24cfc997-adc3-4267-bdcb-045f3736bec1": {"name": "Boston", "created_at": "2024-08-10T13:13:52.420927", "updated_at": "2024-08-10T13:13:52.420934", "id": "24cfc997-adc3-4267-bdcb-045f3736bec1", "__class__": "State"}, "City.1f8b5c17-a7c5-4b90-a444-cc8a1e68b707": {"name": "Virginia", "state_id": "24cfc997-adc3-4267-bdcb-045f3736bec1", "created_at": "2024-08-10T13:13:52.421065", "updated_at": "2024-08-10T13:13:52.421069", "id": "1f8b5c17-a7c5-4b90-a444-cc8a1e68b707", "__class__": "City"}, "State.44648c0c-787a-41f4-a0a4-60e0f76906bb": {"name": "Baltimore", "created_at": "2024-08-10T13:13:52.436476", "updated_at": "2024-08-10T13:13:52.436482", "id": "44648c0c-787a-41f4-a0a4-60e0f76906bb", "__class__": "State"}, "State.8adcc68b-8199-4f48-a914-e420ecdefe41": {"name": "Boston", "created_at": "2024-08-10T13:22:08.663861", "updated_at": "2024-08-10T13:22:08.663868", "id": "8adcc68b-8199-4f48-a914-e420ecdefe41", "__class__": "State"}, "City.f3da25ea-34c3-4916-85fa-d2cc67084123": {"name": "Virginia", "state_id": "8adcc68b-8199-4f48-a914-e420ecdefe41", "created_at": "2024-08-10T13:22:08.664180", "updated_at": "2024-08-10T13:22:08.664187", "id": "f3da25ea-34c3-4916-85fa-d2cc67084123", "__class__": "City"}, "State.de226d5a-d959-4db3-971d-335910dc2496": {"name": "Baltimore", "created_at": "2024-08-10T13:22:08.717163", "updated_at": "2024-08-10T13:22:08.717173", "id": "de226d5a-d959-4db3-971d-335910dc2496", "__class__": "State"}, "State.7fb20780-9fe0-4f94-aa1f-a13cfa95464c": {"name": "Boston", "created_at": "2024-08-10T13:23:06.875200", "updated_at": "2024-08-10T13:23:06.875207", "id": "7fb20780-9fe0-4f94-aa1f-a13cfa95464c", "__class__": "State"}, "City.55b0e124-47f4-4592-b00d-79860fcde2a3": {"name": "Virginia", "state_id": "7fb20780-9fe0-4f94-aa1f-a13cfa95464c", "created_at": "2024-08-10T13:23:06.875312", "updated_at": "2024-08-10T13:23:06.875316", "id": "55b0e124-47f4-4592-b00d-79860fcde2a3", "__class__": "City"}, "State.8497c801-8381-4990-b986-de70d5a6ce20": {"name": "Baltimore", "created_at": "2024-08-10T13:23:06.907400", "updated_at": "2024-08-10T13:23:06.907407", "id": "8497c801-8381-4990-b986-de70d5a6ce20", "__class__": "State"}, "State.b6167c45-3cbc-49f2-b061-16e63a415968": {"name": "Boston", "created_at": "2024-08-10T13:49:12.567916", "updated_at": "2024-08-10T13:49:12.567938", "id": "b6167c45-3cbc-49f2-b061-16e63a415968", "__class__": "State"}, "City.eba717c7-cefd-4808-aaed-f76dda60bf48": {"name": "Virginia", "state_id": "b6167c45-3cbc-49f2-b061-16e63a415968", "created_at": "2024-08-10T13:49:12.568080", "updated_at": "2024-08-10T13:49:12.568084", "id": "eba717c7-cefd-4808-aaed-f76dda60bf48", "__class__": "City"}, "State.e2f68155-9d51-4965-96c8-7502f6cd8bfe": {"name": "Baltimore", "created_at": "2024-08-10T13:49:12.579717", "updated_at": "2024-08-10T13:49:12.579724", "id": "e2f68155-9d51-4965-96c8-7502f6cd8bfe", "__class__": "State"}, "State.2b9956e4-d7fb-450f-9597-fd7f5e9de5c3": {"name": "Boston", "created_at": "2024-08-10T13:59:29.139937", "updated_at": "2024-08-10T13:59:29.139944", "id": "2b9956e4-d7fb-450f-9597-fd7f5e9de5c3", "__class__": "State"}, "City.bd89459d-812d-4e54-9172-94f98ce11730": {"name": "Virginia", "state_id": "2b9956e4-d7fb-450f-9597-fd7f5e9de5c3", "created_at": "2024-08-10T13:59:29.140033", "updated_at": "2024-08-10T13:59:29.140036", "id": "bd89459d-812d-4e54-9172-94f98ce11730", "__class__": "City"}, "State.2ebf5a55-7902-4593-8020-426c60a05140": {"name": "Baltimore", "created_at": "2024-08-10T13:59:29.163687", "updated_at": "2024-08-10T13:59:29.163694", "id": "2ebf5a55-7902-4593-8020-426c60a05140", "__class__": "State"}}
Binary file added models/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/amenity.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/base_model.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/city.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/place.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/review.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/state.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/user.cpython-310.pyc
Binary file not shown.
Binary file added models/engine/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,27 @@ def reload(self):
def close(self):
"""call remove() method on the private session attribute"""
self.__session.remove()

def get(self, cls, id):
"""
Retrieve objects from db storage
"""
if cls and id:
if cls in classes.values() and isinstance(id, str):
all_objects = self.all(cls)
for key, value in all_objects.items():
if key.split('.')[1] == id:
return value

def count(self, cls=None):
"""
method to count the number of instances
"""
if not cls:
all_instances = self.all()
return len(all_instances)
if cls in classes.values():
cls_instances = self.all(cls)
return len(cls_instances)
if cls not in classes.values():
return
25 changes: 24 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 Exception:
pass

def delete(self, obj=None):
Expand All @@ -68,3 +68,26 @@ 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):
"""
method retrieves an instance of a class
"""
if cls and id:
if cls in classes.values():
all_objects = self.all()
for value in all_objects.values():
if value.id == id and value.__class__ == cls:
return value
return
return

def count(self, cls=None):
"""
"""
if not cls:
return len(self.all())
if cls in classes.values():
return len(self.all(cls))
if cls not in classes.values():
return
Binary file added tests/__pycache__/test_console.cpython-310.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.
16 changes: 7 additions & 9 deletions tests/test_models/test_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,15 @@ 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()
inst1 = BaseModel()
toc = datetime.now()
self.assertTrue(tic <= inst1.created_at <= toc)
time.sleep(1e-4)
tic = datetime.now()
inst2 = BaseModel()
toc = datetime.now()
self.assertTrue(tic <= inst2.created_at <= toc)
self.assertEqual(inst1.created_at, inst1.updated_at)
self.assertEqual(inst2.created_at, inst2.updated_at)

# Allow a small margin of error (e.g., 1 millisecond)
self.assertAlmostEqual(inst1.created_at.timestamp(),
inst1.updated_at.timestamp(), delta=0.001)
self.assertAlmostEqual(inst2.created_at.timestamp(),
inst2.updated_at.timestamp(), delta=0.001)

self.assertNotEqual(inst1.created_at, inst2.created_at)
self.assertNotEqual(inst1.updated_at, inst2.updated_at)

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading