diff --git a/AUTHORS b/AUTHORS index 64b26acdc14..99cf0faf523 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,4 +3,5 @@ Jennifer Huang <133@holbertonschool.com> Alexa Orrico <210@holbertonschool.com> -Joann Vuong <130@holbertonschool.com> +Joann Vuong <130@holbertonschool.com> +Lawrence Maina diff --git a/__pycache__/console.cpython-310.pyc b/__pycache__/console.cpython-310.pyc new file mode 100644 index 00000000000..4ff8dd33568 Binary files /dev/null and b/__pycache__/console.cpython-310.pyc differ diff --git a/console.py b/console.py index 4798f9ac76b..e133363e235 100755 --- a/console.py +++ b/console.py @@ -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 @@ -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() @@ -160,5 +160,6 @@ def do_update(self, arg): else: print("** class doesn't exist **") + if __name__ == '__main__': HBNBCommand().cmdloop() diff --git a/file.json b/file.json new file mode 100644 index 00000000000..e42af3cec81 --- /dev/null +++ b/file.json @@ -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"}} \ No newline at end of file diff --git a/models/__pycache__/__init__.cpython-310.pyc b/models/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000000..9d86f92b02a Binary files /dev/null and b/models/__pycache__/__init__.cpython-310.pyc differ diff --git a/models/__pycache__/amenity.cpython-310.pyc b/models/__pycache__/amenity.cpython-310.pyc new file mode 100644 index 00000000000..e8d76d98dc0 Binary files /dev/null and b/models/__pycache__/amenity.cpython-310.pyc differ diff --git a/models/__pycache__/base_model.cpython-310.pyc b/models/__pycache__/base_model.cpython-310.pyc new file mode 100644 index 00000000000..2aeb4c5dff1 Binary files /dev/null and b/models/__pycache__/base_model.cpython-310.pyc differ diff --git a/models/__pycache__/city.cpython-310.pyc b/models/__pycache__/city.cpython-310.pyc new file mode 100644 index 00000000000..4749cb015c0 Binary files /dev/null and b/models/__pycache__/city.cpython-310.pyc differ diff --git a/models/__pycache__/place.cpython-310.pyc b/models/__pycache__/place.cpython-310.pyc new file mode 100644 index 00000000000..ea5732e3bfa Binary files /dev/null and b/models/__pycache__/place.cpython-310.pyc differ diff --git a/models/__pycache__/review.cpython-310.pyc b/models/__pycache__/review.cpython-310.pyc new file mode 100644 index 00000000000..e6634fceb05 Binary files /dev/null and b/models/__pycache__/review.cpython-310.pyc differ diff --git a/models/__pycache__/state.cpython-310.pyc b/models/__pycache__/state.cpython-310.pyc new file mode 100644 index 00000000000..919066939c9 Binary files /dev/null and b/models/__pycache__/state.cpython-310.pyc differ diff --git a/models/__pycache__/user.cpython-310.pyc b/models/__pycache__/user.cpython-310.pyc new file mode 100644 index 00000000000..b66e3e31295 Binary files /dev/null and b/models/__pycache__/user.cpython-310.pyc differ diff --git a/models/engine/__pycache__/__init__.cpython-310.pyc b/models/engine/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000000..b95b0181e4a Binary files /dev/null and b/models/engine/__pycache__/__init__.cpython-310.pyc differ diff --git a/models/engine/__pycache__/db_storage.cpython-310.pyc b/models/engine/__pycache__/db_storage.cpython-310.pyc new file mode 100644 index 00000000000..b2f70de02f7 Binary files /dev/null and b/models/engine/__pycache__/db_storage.cpython-310.pyc differ diff --git a/models/engine/__pycache__/file_storage.cpython-310.pyc b/models/engine/__pycache__/file_storage.cpython-310.pyc new file mode 100644 index 00000000000..ad0b5e5247c Binary files /dev/null and b/models/engine/__pycache__/file_storage.cpython-310.pyc differ diff --git a/models/engine/db_storage.py b/models/engine/db_storage.py index b8e7d291e6f..375000c0378 100755 --- a/models/engine/db_storage.py +++ b/models/engine/db_storage.py @@ -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 diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index c8cb8c1764d..b63a1ebc363 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -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): @@ -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 diff --git a/tests/__pycache__/test_console.cpython-310.pyc b/tests/__pycache__/test_console.cpython-310.pyc new file mode 100644 index 00000000000..321bf012cde Binary files /dev/null and b/tests/__pycache__/test_console.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/__init__.cpython-310.pyc b/tests/test_models/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000000..2742c1ab781 Binary files /dev/null and b/tests/test_models/__pycache__/__init__.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/test_amenity.cpython-310.pyc b/tests/test_models/__pycache__/test_amenity.cpython-310.pyc new file mode 100644 index 00000000000..a3f7dc1836b Binary files /dev/null and b/tests/test_models/__pycache__/test_amenity.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/test_base_model.cpython-310.pyc b/tests/test_models/__pycache__/test_base_model.cpython-310.pyc new file mode 100644 index 00000000000..f374bd0df56 Binary files /dev/null and b/tests/test_models/__pycache__/test_base_model.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/test_city.cpython-310.pyc b/tests/test_models/__pycache__/test_city.cpython-310.pyc new file mode 100644 index 00000000000..2b6a7063d07 Binary files /dev/null and b/tests/test_models/__pycache__/test_city.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/test_place.cpython-310.pyc b/tests/test_models/__pycache__/test_place.cpython-310.pyc new file mode 100644 index 00000000000..010ab8a17b1 Binary files /dev/null and b/tests/test_models/__pycache__/test_place.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/test_review.cpython-310.pyc b/tests/test_models/__pycache__/test_review.cpython-310.pyc new file mode 100644 index 00000000000..250bbfb4884 Binary files /dev/null and b/tests/test_models/__pycache__/test_review.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/test_state.cpython-310.pyc b/tests/test_models/__pycache__/test_state.cpython-310.pyc new file mode 100644 index 00000000000..2360681cbf1 Binary files /dev/null and b/tests/test_models/__pycache__/test_state.cpython-310.pyc differ diff --git a/tests/test_models/__pycache__/test_user.cpython-310.pyc b/tests/test_models/__pycache__/test_user.cpython-310.pyc new file mode 100644 index 00000000000..f0061367036 Binary files /dev/null and b/tests/test_models/__pycache__/test_user.cpython-310.pyc differ diff --git a/tests/test_models/test_base_model.py b/tests/test_models/test_base_model.py index 231dab48ccd..b893b4415ea 100644 --- a/tests/test_models/test_base_model.py +++ b/tests/test_models/test_base_model.py @@ -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) diff --git a/tests/test_models/test_engine/__pycache__/__init__.cpython-310.pyc b/tests/test_models/test_engine/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000000..f996d204f45 Binary files /dev/null and b/tests/test_models/test_engine/__pycache__/__init__.cpython-310.pyc differ diff --git a/tests/test_models/test_engine/__pycache__/test_db_storage.cpython-310.pyc b/tests/test_models/test_engine/__pycache__/test_db_storage.cpython-310.pyc new file mode 100644 index 00000000000..9183228a382 Binary files /dev/null and b/tests/test_models/test_engine/__pycache__/test_db_storage.cpython-310.pyc differ diff --git a/tests/test_models/test_engine/__pycache__/test_file_storage.cpython-310.pyc b/tests/test_models/test_engine/__pycache__/test_file_storage.cpython-310.pyc new file mode 100644 index 00000000000..9beecefb705 Binary files /dev/null and b/tests/test_models/test_engine/__pycache__/test_file_storage.cpython-310.pyc differ diff --git a/tests/test_models/test_engine/test_db_storage.py b/tests/test_models/test_engine/test_db_storage.py index 766e625b5af..7570554f1a1 100755 --- a/tests/test_models/test_engine/test_db_storage.py +++ b/tests/test_models/test_engine/test_db_storage.py @@ -68,7 +68,7 @@ def test_dbs_func_docstrings(self): "{:s} method needs a docstring".format(func[0])) -class TestFileStorage(unittest.TestCase): +class TestDBStorage(unittest.TestCase): """Test the FileStorage class""" @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_all_returns_dict(self): @@ -78,11 +78,99 @@ def test_all_returns_dict(self): @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_all_no_class(self): """Test that all returns all rows when no class is passed""" + state_data = {"name": "Austria"} + new_state = State(**state_data) + models.storage.new(new_state) + + session = models.storage._DBStorage__session + + all_states = session.query(State).all() + + self.assertEqual(len(all_states), 1) @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_new(self): """test that new adds an object to the database""" + state_data = {"name": "Mombasa"} + new_state = State(**state_data) + + models.storage.new(new_state) + + session = models.storage._DBStorage__session + + found_state = session.query(State).filter_by(id=new_state.id).first() + self.assertIsNotNone(found_state) + self.assertEqual(found_state.name, "Mombasa") + self.assertEqual(found_state.id, new_state.id) @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_save(self): """Test that save properly saves objects to file.json""" + state_data = {"name": "New_York"} + new_state = State(**state_data) + + models.storage.new(new_state) + + models.storage.save() + + session = models.storage._DBStorage__session + + found_state = session.query(State).filter_by(id=new_state.id).first() + self.assertIsNotNone(found_state) + self.assertEqual(found_state.name, "New_York") + self.assertEqual(found_state.id, new_state.id) + + @unittest.skipIf(models.storage_t != 'db', "not testing db storage") + def test_get(self): + """ + Method to retrieve an instance + """ + storage = models.storage + + storage.reload() + + state_data = {"name": "Baltimore"} + new_state = State(**state_data) + + storage.new(new_state) + + storage.save() + + retrieved_state = storage.get(State, new_state.id) + + self.assertIsNotNone(retrieved_state) + self.assertEqual(retrieved_state.name, "Baltimore") + + fake_state_id = storage.get(State, 'fake_id') + + self.assertIsNone(fake_state_id) + + @unittest.skipIf(models.storage_t != 'db', "not testing db storage") + def test_count(self): + """ + Method to count all instances + """ + storage = models.storage + + storage.reload() + + state_data = {"name": "Boston"} + new_state = State(**state_data) + storage.new(new_state) + + city_data = {"name": "Virginia", "state_id": new_state.id} + + city = City(**city_data) + storage.new(city) + storage.save() + + count = storage.count(State) + + self.assertIsNotNone(count) + self.assertTrue(isinstance(count, int)) + self.assertEqual(count, len(storage.all(State))) + + all_instances = storage.count() + + self.assertIsNotNone(all_instances) + self.assertEqual(all_instances, len(storage.all())) diff --git a/tests/test_models/test_engine/test_file_storage.py b/tests/test_models/test_engine/test_file_storage.py index 1474a34fec0..4649b814346 100755 --- a/tests/test_models/test_engine/test_file_storage.py +++ b/tests/test_models/test_engine/test_file_storage.py @@ -113,3 +113,57 @@ 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 db storage") + def test_get(self): + """ + The method to retrieve all instances + """ + storage = FileStorage() + + storage.reload() + + state_data = {"name": "Baltimore"} + state = State(**state_data) + + storage.new(state) + storage.save() + + retrieved_state = storage.get(State, state.id) + + self.assertIsNotNone(retrieved_state) + self.assertEqual(retrieved_state.name, "Baltimore") + + fake_state_id = storage.get(State, 'fake_id') + + self.assertIsNone(fake_state_id) + + @unittest.skipIf(models.storage_t != 'db', "not testing db storage") + def test_count(self): + """ + This method counts all instances + """ + storage = FileStorage() + + storage.reload() + + state_data = {"name": "Boston"} + state = State(**state_data) + storage.new(state) + + city_data = {"name": "Virginia", "state_id": state.id} + + city = City(**city_data) + storage.new(city) + storage.save() + + count = storage.count(State) + + self.assertIsNotNone(count) + self.assertTrue(isinstance(count, int)) + self.assertEqual(count, len(storage.all(State))) + + all_instances = storage.count() + + self.assertIsNotNone(all_instances) + self.assertEqual(all_instances, len(storage.all()))