From 3d845c4128ad638e1fd4f2e4a177b766775257a3 Mon Sep 17 00:00:00 2001 From: alexaorrico Date: Thu, 5 Oct 2017 06:18:21 +0000 Subject: [PATCH 1/3] test save method --- .../test_engine/test_file_storage.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py b/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py index c92b1d9..55edd0e 100755 --- a/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py +++ b/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py @@ -13,6 +13,7 @@ from models.review import Review from models.state import State from models.user import User +import json import pep8 import unittest FileStorage = file_storage.FileStorage @@ -87,3 +88,20 @@ def test_new(self): test_dict[instance_key] = instance self.assertEqual(test_dict, storage._FileStorage__objects) storage._FileStorage__objects = {} + + def test_save(self): + """Test that save properly saves objects to file.json""" + storage = FileStorage() + new_dict = {} + for key, value in classes.items(): + instance = value() + instance_key = instance.__class__.__name__ + "." + instance.id + new_dict[instance_key] = instance + storage._FileStorage__objects = new_dict + storage.save() + for key, value in new_dict.items(): + new_dict[key] = value.to_dict() + string = json.dumps(new_dict) + with open("file.json", "r") as f: + js = f.read() + self.assertEqual(string, js) From 7e9f80e9fdad5fb75e2946190342513e5c47e49c Mon Sep 17 00:00:00 2001 From: alexaorrico Date: Thu, 5 Oct 2017 06:28:46 +0000 Subject: [PATCH 2/3] fix comparing strings to comparing dictionaries --- .../tests/test_models/test_engine/test_file_storage.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py b/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py index 55edd0e..809ddf5 100755 --- a/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py +++ b/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py @@ -14,6 +14,7 @@ from models.state import State from models.user import User import json +import os import pep8 import unittest FileStorage = file_storage.FileStorage @@ -91,6 +92,7 @@ def test_new(self): def test_save(self): """Test that save properly saves objects to file.json""" + os.remove("file.json") storage = FileStorage() new_dict = {} for key, value in classes.items(): @@ -104,4 +106,4 @@ def test_save(self): string = json.dumps(new_dict) with open("file.json", "r") as f: js = f.read() - self.assertEqual(string, js) + self.assertEqual(json.loads(string), json.loads(js)) From aeed92c5a3cbd07b400361e9cfa64d6de0e714b3 Mon Sep 17 00:00:00 2001 From: alexaorrico Date: Fri, 6 Oct 2017 17:43:00 +0000 Subject: [PATCH 3/3] use class, not instance attr. Save original attr and reset at end of test --- .../test_models/test_engine/test_file_storage.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py b/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py index 809ddf5..0c422fa 100755 --- a/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py +++ b/AirBnB_clone/tests/test_models/test_engine/test_file_storage.py @@ -79,7 +79,8 @@ def test_all_returns_dict(self): def test_new(self): """test that new adds an object to the FileStorage.__objects attr""" storage = FileStorage() - storage._FileStorage__objects = {} + save = FileStorage._FileStorage__objects + FileStorage._FileStorage__objects = {} test_dict = {} for key, value in classes.items(): with self.subTest(key=key, value=value): @@ -87,8 +88,8 @@ def test_new(self): instance_key = instance.__class__.__name__ + "." + instance.id storage.new(instance) test_dict[instance_key] = instance - self.assertEqual(test_dict, storage._FileStorage__objects) - storage._FileStorage__objects = {} + self.assertEqual(test_dict, FileStorage._FileStorage__objects) + FileStorage._FileStorage__objects = save def test_save(self): """Test that save properly saves objects to file.json""" @@ -99,8 +100,10 @@ def test_save(self): instance = value() instance_key = instance.__class__.__name__ + "." + instance.id new_dict[instance_key] = instance - storage._FileStorage__objects = new_dict + save = FileStorage._FileStorage__objects + FileStorage._FileStorage__objects = new_dict storage.save() + FileStorage._FileStorage__objects = save for key, value in new_dict.items(): new_dict[key] = value.to_dict() string = json.dumps(new_dict)