Esempio n. 1
0
 def test_filestorage_count(self):
     '''
         Testing Count method
     '''
     filestor = FileStorage()
     old_count = filestor.count("State")
     new = State(name="Alabama")
     filestor.new(new)
     filestor.save()
     new_count = filestor.count("State")
     self.assertEqual(old_count + 1, new_count)
 def test_count(self):
     '''
     Tests that the count method returns total number of objects in
     file storage
     '''
     fs = FileStorage()
     count = fs.count("State")
     new_state1 = State(name="Florida")
     new_state2 = State(name="Illinois")
     new_state3 = State(name="Washington")
     fs.new(new_state1)
     fs.new(new_state2)
     fs.new(new_state3)
     fs.save()
     after_count = fs.count("State")
     self.assertEqual(count, after_count - 3)
Esempio n. 3
0
 def test_count(self):
     '''
        Test count to confirm number of objects are returned
     '''
     fs2 = FileStorage()
     for i in range(4):
         new_state = State()
         new_state.save()
     self.assertEqual(fs2.count(), 9)
     num_state = self.storage.count()
     self.assertEqual(self.storage.count(State), 4)
     self.assertGreaterEqual(storage.count(), storage.count('State'))
     self.assertEqual(storage.count(), storage.count('BaseModel'))
     self.assertEqual(self.storage.count(), 9)
     self.assertEqual(self.storage.count('no class'), 0)
class testFileStorage(unittest.TestCase):
    '''
        Testing the FileStorage class
    '''
    def setUp(self):
        '''
            Initializing classes
        '''
        self.storage = FileStorage()
        self.my_model = BaseModel()

    def tearDown(self):
        '''
            Cleaning up.
        '''
        pass
        try:
            os.remove("file.json")
        except FileNotFoundError:
            pass

    def test_all_return_type(self):
        '''
            Tests the data type of the return value of the all method.
        '''
        storage_all = self.storage.all()
        self.assertIsInstance(storage_all, dict)

    def test_new_method(self):
        '''
            Tests that the new method sets the right key and value pair
            in the FileStorage.__object attribute
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        self.assertTrue(key in self.storage._FileStorage__objects)

    def test_objects_value_type(self):
        '''
            Tests that the type of value contained in the FileStorage.__object
            is of type obj.__class__.__name__
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        val = self.storage._FileStorage__objects[key]
        self.assertIsInstance(self.my_model, type(val))

    def test_save_file_exists(self):
        '''
            Tests that a file gets created with the name file.json
        '''
        self.storage.save()
        self.assertTrue(os.path.isfile("file.json"))

    def test_save_file_read(self):
        '''
            Testing the contents of the files inside the file.json
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = json.load(fd)

        self.assertTrue(type(content) is dict)

    def test_the_type_file_content(self):
        '''
            testing the type of the contents inside the file.
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = fd.read()

        self.assertIsInstance(content, str)

    def test_reaload_without_file(self):
        '''
            Tests that nothing happens when file.json does not exists
            and reload is called
        '''

        try:
            self.storage.reload()
            self.assertTrue(True)
        except:
            self.assertTrue(False)

    def test_parameter_validity(self):
        '''
            Tests whether or not the parameter passed is an instance of the
            class
        '''
        console = HBNBCommand()
        capturedOutput = io.StringIO()
        sys.stdout = capturedOutput
        my_id = console.onecmd("create State name='California'")
        sys.stdout = sys.__stdout__
        with open("file.json", encoding="UTF-8") as fd:
            json_dict = json.load(fd)
        for key, value in json_dict.items():
            my_key = 'State.' + str(my_id)
            if key == my_key:
                self.assertTrue(value['name'] == 'California')

    def test_parameter_lack_of_validity(self):
        '''
            Tests whether or not the parameter passes is an instance
            of the class
        '''
        console = HBNBCommand()
        capturedOutput = io.StringIO()
        sys.stdout = capturedOutput
        my_id = console.onecmd("create State address=98")
        sys.stdout = sys.__stdout__
        self.assertIsNone(my_id)

    def test_deletion(self):
        '''
            Tests for an object being deleted with the delete method
        '''
        fs = FileStorage()
        new_state = State()
        new_state.name = "Polynesia"
        fs.new(new_state)
        my_id = new_state.id
        fs.save()
        fs.delete(new_state)
        with open("file.json", encoding="UTF-8") as fd:
            json_dict = json.load(fd)
        for key, value in json_dict.items():
            self.assertTrue(value['id'] != my_id)

    def test_get_file_storage(self):
        '''
            Tests the get method of file storage
        '''
        state = State(name="Cali")
        state_id = state.id
        self.storage.new(state)
        state_obj = self.storage.get("State", state_id)
        self.assertEqual(state_obj, state)

    def test_count_db_storage_works(self):
        '''
        Tests if the count method in file storage is working
        '''
        all_dict = self.storage.all()
        all_count = len(all_dict)
        count = self.storage.count()
        self.assertEqual(all_count, count)

    def test_count_file_storage_no_class(self):
        '''
        Tests the count method in file storage when no class is passed
        '''
        first_count = self.storage.count()
        state = State(name="Colorado")
        state.save()
        second_count = self.storage.count()
        self.assertTrue(first_count + 1, second_count)

    def test_count_file_storage_class(self):
        '''
        Tests the count method in file storage when passing a class
        '''
        first_count = self.storage.count("State")
        state = State(name="Colorado")
        state.save()
        second_count = self.storage.count("State")
        self.assertTrue(first_count + 1, second_count)
Esempio n. 5
0
class testFileStorage(unittest.TestCase):
    '''
        Testing the FileStorage class
    '''
    def setUp(self):
        '''
            Initializing classes
        '''
        self.storage = FileStorage()
        self.my_model = BaseModel()

    def tearDown(self):
        '''
            Cleaning up.
        '''
        self.file_storage_reloader()

    @staticmethod
    def file_storage_reloader():
        '''
            Deletes the file that represent the storage
        '''
        try:
            os.remove("file.json")
        except FileNotFoundError:
            pass
        storage.reload()

    def test_all_return_type(self):
        '''
            Tests the data type of the return value of the all method.
        '''
        storage_all = self.storage.all()
        self.assertIsInstance(storage_all, dict)

    def test_new_method(self):
        '''
            Tests that the new method sets the right key and value pair
            in the FileStorage.__object attribute
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        self.assertTrue(key in self.storage._FileStorage__objects)

    def test_objects_value_type(self):
        '''
            Tests that the type of value contained in the FileStorage.__object
            is of type obj.__class__.__name__
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        val = self.storage._FileStorage__objects[key]
        self.assertIsInstance(self.my_model, type(val))

    def test_save_file_exists(self):
        '''
            Tests that a file gets created with the name file.json
        '''
        self.storage.save()
        self.assertTrue(os.path.isfile("file.json"))

    def test_save_file_read(self):
        '''
            Testing the contents of the files inside the file.json
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = json.load(fd)

        self.assertTrue(type(content) is dict)

    def test_the_type_file_content(self):
        '''
            testing the type of the contents inside the file.
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = fd.read()

        self.assertIsInstance(content, str)

    def test_reload_without_file(self):
        '''
            Tests that nothing happens when file.json does not exists
            and reload is called
        '''
        try:
            self.storage.reload()
            self.assertTrue(True)
        except:
            self.assertTrue(False)

    def test_delete(self):
        '''
            Test delete method
        '''
        fs = FileStorage()
        new_state = State()
        fs.new(new_state)
        state_id = new_state.id
        fs.save()
        fs.delete(new_state)
        with open("file.json", encoding="UTF-8") as fd:
            state_dict = json.load(fd)
        for k, v in state_dict.items():
            self.assertFalse(state_id == k.split('.')[1])

    def test_model_storage(self):
        '''
            Test State model in Filestorage
        '''
        self.assertTrue(isinstance(storage, FileStorage))

    def test_get(self):
        '''
            Testing get method
        '''
        # new state for the test case
        state = State(name="California")
        self.storage.new(state)
        state_id = state.id

        # state is instance of State
        self.assertIsInstance(state, State)

        # state can be retrieved using storage.get()
        state = storage.get("State", state_id)
        self.assertEqual(state.id, state_id)

        # fake_state is None
        # when the given id doesn't exist
        fake_state = storage.get("State", "fake_id")
        self.assertIsNone(fake_state)

        # clean up
        self.storage.delete(state)

    def test_count(self):
        '''
            Testing cout method
        '''
        count = storage.count()

        # count is int
        self.assertIsInstance(count, int)
        # count == 6
        self.assertEqual(count, 6)

        # count = 7
        # after the new record has been created
        state = State({"name": "California"})
        self.storage.new(state)
        count = self.storage.count()
        self.assertEqual(count, 7)

        # while counting only states
        count = self.storage.count("State")

        # count is int
        self.assertIsInstance(count, int)
        # count == 1
        self.assertEqual(count, 1)
Esempio n. 6
0
class testFileStorage(unittest.TestCase):
    '''
        Testing the FileStorage class
    '''

    def setUp(self):
        '''
            Initializing classes
        '''
        self.storage = FileStorage()
        self.my_model = BaseModel()
        try:
            os.remove("file.json")
        except FileNotFoundError:
            pass

    def tearDown(self):
        '''
            Cleaning up.
        '''
        try:
            os.remove("file.json")
        except FileNotFoundError:
            pass

    def test_all_return_type(self):
        '''
            Tests the data type of the return value of the all method.
        '''
        storage_all = self.storage.all()
        self.assertIsInstance(storage_all, dict)

    def test_new_method(self):
        '''
            Tests that the new method sets the right key and value pair
            in the FileStorage.__object attribute
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        self.assertTrue(key in self.storage._FileStorage__objects)

    def test_objects_value_type(self):
        '''
            Tests that the type of value contained in the FileStorage.__object
            is of type obj.__class__.__name__
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        val = self.storage._FileStorage__objects[key]
        self.assertIsInstance(self.my_model, type(val))

    def test_save_file_exists(self):
        '''
            Tests that a file gets created with the name file.json
        '''
        self.storage.save()
        self.assertTrue(os.path.isfile("file.json"))

    def test_save_file_read(self):
        '''
            Testing the contents of the files inside the file.json
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = json.load(fd)

        self.assertTrue(isinstance(content, dict))

    def test_the_type_file_content(self):
        '''
            testing the type of the contents inside the file.
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = fd.read()

        self.assertIsInstance(content, str)

    def test_reaload_without_file(self):
        '''
            Tests that nothing happens when file.json does not exists
            and reload is called
        '''

        try:
            self.storage.reload()
            self.assertTrue(True)
        except BaseException:
            self.assertTrue(False)

    def test_delete(self):
        '''
            Test delete method
        '''
        fs = FileStorage()
        new_state = State()
        fs.new(new_state)
        state_id = new_state.id
        fs.save()
        fs.delete(new_state)
        with open("file.json", encoding="UTF-8") as fd:
            state_dict = json.load(fd)
        for k, v in state_dict.items():
            self.assertFalse(state_id == k.split('.')[1])

    def test_model_storage(self):
        '''
            Test State model in Filestorage
        '''
        self.assertTrue(isinstance(storage, FileStorage))

    def test_get(self):
        '''
            Test for get method in filestorage
        '''
        fs = FileStorage()
        new_state = State()
        fs.new(new_state)
        new_id = new_state.id
        fs.save()
        return_state = fs.get(State, new_id)
        self.assertTrue(new_id == return_state.id)
        self.assertTrue(new_state.id == return_state.id)
        self.assertTrue(isinstance(return_state, State))
        get_none = fs.get(State, 'no id')
        self.assertTrue(get_none is None)
        get_none2 = fs.get('no state', new_id)
        self.assertTrue(get_none2 is None)

    def test_raise(self):
        '''
           Test for Exception errors
        '''
        self.assertRaises(TypeError, storage.get, None)

    def test_count(self):
        '''
           Test count to confirm number of objects are returned
        '''
        fs2 = FileStorage()
        for i in range(4):
            new_state = State()
            new_state.save()
        self.assertEqual(fs2.count(), 9)
        num_state = self.storage.count()
        self.assertEqual(self.storage.count(State), 4)
        self.assertGreaterEqual(storage.count(), storage.count('State'))
        self.assertEqual(storage.count(), storage.count('BaseModel'))
        self.assertEqual(self.storage.count(), 9)
        self.assertEqual(self.storage.count('no class'), 0)
class Test_FileStorage(unittest.TestCase):
    """
    Test the file storage class
    """
    def setUp(self):
        self.store = FileStorage()

        test_args = {
            'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
            'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
            'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)
        }
        self.model = BaseModel(**test_args)

        self.test_len = len(self.store.all())

#    @classmethod
#    def tearDownClass(cls):
#        import os
#        if os.path.isfile("test_file.json"):
#            os.remove('test_file.json')

    def test_all(self):
        self.assertEqual(len(self.store.all()), self.test_len)

    def test_all_arg(self):
        """test all(State)"""
        new_obj = State()
        new_obj.save()
        everything = self.store.all()
        nb_states = 0
        for e in everything.values():
            if e.__class__.__name__ == "State":
                nb_states += 1
        self.assertEqual(len(self.store.all("State")), nb_states)


# should test with a bad class name

    def test_new(self):
        # note: we cannot assume order of test is order written
        test_len = len(self.store.all())
        # self.assertEqual(len(self.store.all()), self.test_len)
        new_obj = State()
        new_obj.save()
        self.assertEqual(len(self.store.all()), test_len + 1)
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)

    def test_save(self):
        self.test_len = len(self.store.all())
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 1)
        b = User()
        self.assertNotEqual(len(self.store.all()), self.test_len + 2)
        b.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)

    def test_reload(self):
        self.model.save()
        a = BaseModel()
        a.save()
        self.store.reload()
        for value in self.store.all().values():
            self.assertIsInstance(value.created_at, datetime)

    def test_state(self):
        """test State creation with an argument"""
        a = State(name="Kamchatka", id="Kamchatka666")
        a.save()
        self.assertIn("Kamchatka666", self.store.all("State").keys())

    def test_count(self):
        """test count all"""
        test_len = len(self.store.all())
        a = Amenity(name="test_amenity")
        a.save()
        self.assertEqual(test_len + 1, self.store.count())

    def test_count_arg(self):
        """test count with an argument"""
        test_len = len(self.store.all("Amenity"))
        a = Amenity(name="test_amenity_2")
        a.save()
        self.assertEqual(test_len + 1, self.store.count("Amenity"))

    def test_count_bad_arg(self):
        """test count with dummy class name"""
        self.assertEqual(-1, self.store.count("Dummy"))

    def test_get(self):
        """test get with valid cls and id"""
        a = Amenity(name="test_amenity3", id="test_3")
        a.save()
        result = self.store.get("Amenity", "test_3")
        self.assertEqual(a.name, result.name)
        self.assertEqual(a.created_at, result.created_at)

    def test_get_bad_cls(self):
        """test get with invalid cls"""
        result = self.store.get("Dummy", "test")
        self.assertIsNone(result)

    def test_get_bad_id(self):
        """test get with invalid id"""
        result = self.store.get("State", "very_bad_id")
        self.assertIsNone(result)
class testFileStorage(unittest.TestCase):
    '''
        Testing the FileStorage class
    '''

    def setUp(self):
        '''
            Initializing classes
        '''
        self.storage = FileStorage()
        self.my_model = BaseModel()

    def tearDown(self):
        '''
            Cleaning up.
        '''

        try:
            os.remove("file.json")
        except FileNotFoundError:
            pass

    def test_all_return_type(self):
        '''
            Tests the data type of the return value of the all method.
        '''
        storage_all = self.storage.all()
        self.assertIsInstance(storage_all, dict)

    def test_new_method(self):
        '''
            Tests that the new method sets the right key and value pair
            in the FileStorage.__object attribute
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        self.assertTrue(key in self.storage._FileStorage__objects)

    def test_objects_value_type(self):
        '''
            Tests that the type of value contained in the FileStorage.__object
            is of type obj.__class__.__name__
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        val = self.storage._FileStorage__objects[key]
        self.assertIsInstance(self.my_model, type(val))

    def test_save_file_exists(self):
        '''
            Tests that a file gets created with the name file.json
        '''
        self.storage.save()
        self.assertTrue(os.path.isfile("file.json"))

    def test_save_file_read(self):
        '''
            Testing the contents of the files inside the file.json
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = json.load(fd)

        self.assertTrue(type(content) is dict)

    def test_the_type_file_content(self):
        '''
            testing the type of the contents inside the file.
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = fd.read()

        self.assertIsInstance(content, str)

    def test_reaload_without_file(self):
        '''
            Tests that nothing happens when file.json does not exists
            and reload is called
        '''

        try:
            self.storage.reload()
            self.assertTrue(True)
        except Exception:
            self.assertTrue(False)

    def test_delete(self):
        '''
            Test delete method
        '''
        fs = FileStorage()
        new_state = State()
        fs.new(new_state)
        state_id = new_state.id
        fs.save()
        fs.delete(new_state)
        with open("file.json", encoding="UTF-8") as fd:
            state_dict = json.load(fd)
        for k, v in state_dict.items():
            self.assertFalse(state_id == k.split('.')[1])

    def test_model_storage(self):
        '''
            Test State model in Filestorage
        '''
        self.assertTrue(isinstance(storage, FileStorage))

    def test_count(self):
        '''
        Test count method in db_storage
        '''
        self.assertEqual(self.storage.count(), len(self.storage.all()))

    def test_count_state(self):
        '''
        Test count method in db_storage using states
        '''
        self.assertEqual(self.storage.count("State"),
                         len(self.storage.all("State")))

    def test_get(self):
        '''
        Test get method in db_storage
        '''
        new_state = State(name="NewYork")
        self.storage.new(new_state)
        self.storage.save()
        self.assertEqual(new_state, self.storage.get("State",
                                                     new_state.id))
class Test_FileStorage(unittest.TestCase):
    """
    Test the file storage class
    """
    def setUp(self):
        self.store = FileStorage()

        test_args = {
            'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
            'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
            'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)
        }
        self.model = BaseModel(test_args)

        self.test_len = len(self.store.all())


#    @classmethod
#    def tearDownClass(cls):
#        import os
#        if os.path.isfile("test_file.json"):
#            os.remove('test_file.json')

    def test_all(self):
        self.assertEqual(len(self.store.all()), self.test_len)

    def test_get(self):
        a = Amenity(name='internet')
        output = self.store.get('Amenity', a.id)
        assertTrue(a.id in output)

    def test_count(self):
        output = self.store.count('Amenity')
        assertEqual(output, len(self.store.all('Amenity')))

    def test_new(self):
        # note: we cannot assume order of test is order written
        test_len = len(self.store.all())
        # self.assertEqual(len(self.store.all()), self.test_len)
        new_obj = State()
        new_obj.save()
        self.assertEqual(len(self.store.all()), test_len + 1)
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)

    def test_save(self):
        self.test_len = len(self.store.all())
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 1)
        b = User()
        self.assertNotEqual(len(self.store.all()), self.test_len + 2)
        b.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)

    def test_reload(self):
        self.model.save()
        a = BaseModel()
        a.save()
        self.store.reload()
        for value in self.store.all().values():
            self.assertIsInstance(value.created_at, datetime)

    def test_state(self):
        """test State creation with an argument"""
        pass
Esempio n. 10
0
#!/usr/bin/python3
"""init file"""
from models.engine.file_storage import FileStorage
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from models.review import Review


types = {'BaseModel': BaseModel, 'User': User, 'State': State,
         'City': City, 'Amenity': Amenity, 'Place': Place,
         'Review': Review}
storage = FileStorage()
storage.reload()
count = storage.count()
Esempio n. 11
0
class TestFileStorage(unittest.TestCase):
    '''
        Testing the FileStorage class
    '''

    def setUp(self):
        '''
            Initializing classes
        '''
        self.storage = FileStorage()
        self.my_model = BaseModel()

    def tearDown(self):
        '''
            Cleaning up.
        '''

        try:
            os.remove("file.json")
        except FileNotFoundError:
            pass

    def test_all_return_type(self):
        '''
            Tests the data type of the return value of the all method.
        '''
        storage_all = self.storage.all()
        self.assertIsInstance(storage_all, dict)

    def test_new_method(self):
        '''
            Tests that the new method sets the right key and value pair
            in the FileStorage.__object attribute
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        self.assertTrue(key in self.storage._FileStorage__objects)

    def test_objects_value_type(self):
        '''
            Tests that the type of value contained in the FileStorage.__object
            is of type obj.__class__.__name__
        '''
        self.storage.new(self.my_model)
        key = str(self.my_model.__class__.__name__ + "." + self.my_model.id)
        val = self.storage._FileStorage__objects[key]
        self.assertIsInstance(self.my_model, type(val))

    def test_save_file_exists(self):
        '''
            Tests that a file gets created with the name file.json
        '''
        self.storage.save()
        self.assertTrue(os.path.isfile("file.json"))

    def test_save_file_read(self):
        '''
            Testing the contents of the files inside the file.json
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = json.load(fd)

        self.assertTrue(type(content) is dict)

    def test_the_type_file_content(self):
        '''
            testing the type of the contents inside the file.
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding="UTF8") as fd:
            content = fd.read()

        self.assertIsInstance(content, str)

    def test_reaload_without_file(self):
        '''
            Tests that nothing happens when file.json does not exists
            and reload is called
        '''

        try:
            self.storage.reload()
            self.assertTrue(True)
        except Exception:
            self.assertTrue(False)

    def test_delete(self):
        '''
            Test delete method
        '''
        fs = FileStorage()
        new_state = State()
        fs.new(new_state)
        state_id = new_state.id
        fs.save()
        fs.delete(new_state)
        with open("file.json", encoding="UTF-8") as fd:
            state_dict = json.load(fd)
        for k, v in state_dict.items():
            self.assertFalse(state_id == k.split('.')[1])

    def test_model_storage(self):
        '''
            Test State model in Filestorage
        '''
        self.assertTrue(isinstance(storage, FileStorage))

    def test_get_method(self):
        """Tests FileStorage's get method that retrieves one object"""
        state_dict = {'name': 'Nevada', 'id': 'thisismyid'}
        state = State(**state_dict)
        state.save()
        new_state = self.storage.get('State', 'thisismyid')
        self.assertEqual(state.id, new_state.id)

    def test_count_method_without_filter(self):
        """Tests FileStorage's count method which retrieves a count of objects
           with an optional class filter
        """
        self.assertEqual(len(self.storage.all()), self.storage.count())

    def test_count_method_with_filter(self):
        """Tests FileStorage's count method which retrieves a count of objects
           with an optional class filter
        """
        objs_dict = self.storage.all()
        count = 0
        for obj in objs_dict.values():
            if type(obj) is User:
                count += 1
        self.assertEqual(count, self.storage.count('User'))
class Test_FileStorage(unittest.TestCase):
    """
    Test the file storage class
    """

    def setUp(self):
        self.store = FileStorage()

        test_args = {'updated_at': datetime(2017, 2, 12, 00, 31, 53, 331997),
                     'id': 'f519fb40-1f5c-458b-945c-2ee8eaaf4900',
                     'created_at': datetime(2017, 2, 12, 00, 31, 53, 331900)}
        self.model = BaseModel(**test_args)

        self.test_len = len(self.store.all())

#    @classmethod
#    def tearDownClass(cls):
#        import os
#        if os.path.isfile("test_file.json"):
#            os.remove('test_file.json')

    def test_all(self):
        self.assertEqual(len(self.store.all()), self.test_len)

    def test_all_arg(self):
        """test all(State)"""
        new_obj = State()
        new_obj.save()
        everything = self.store.all()
        nb_states = 0
        for e in everything.values():
            if e.__class__.__name__ == "State":
                nb_states += 1
        self.assertEqual(len(self.store.all("State")), nb_states)

# should test with a bad class name

    def test_new(self):
        # note: we cannot assume order of test is order written
        test_len = len(self.store.all())
        # self.assertEqual(len(self.store.all()), self.test_len)
        new_obj = State()
        new_obj.save()
        self.assertEqual(len(self.store.all()), test_len + 1)
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)

    def test_save(self):
        self.test_len = len(self.store.all())
        a = BaseModel()
        a.save()
        self.assertEqual(len(self.store.all()), self.test_len + 1)
        b = User()
        self.assertNotEqual(len(self.store.all()), self.test_len + 2)
        b.save()
        self.assertEqual(len(self.store.all()), self.test_len + 2)

    def test_reload(self):
        self.model.save()
        a = BaseModel()
        a.save()
        self.store.reload()
        for value in self.store.all().values():
            self.assertIsInstance(value.created_at, datetime)

    def test_state(self):
        """test State creation with an argument"""
        a = State(name="Kamchatka", id="Kamchatka666")
        a.save()
        self.assertIn("Kamchatka666", self.store.all("State").keys())

    def test_count(self):
        """test count all"""
        test_len = len(self.store.all())
        a = Amenity(name="test_amenity")
        a.save()
        self.assertEqual(test_len + 1, self.store.count())

    def test_count_arg(self):
        """test count with an argument"""
        test_len = len(self.store.all("Amenity"))
        a = Amenity(name="test_amenity_2")
        a.save()
        self.assertEqual(test_len + 1, self.store.count("Amenity"))

    def test_count_bad_arg(self):
        """test count with dummy class name"""
        self.assertEqual(-1, self.store.count("Dummy"))

    def test_get(self):
        """test get with valid cls and id"""
        a = Amenity(name="test_amenity3", id="test_3")
        a.save()
        result = self.store.get("Amenity", "test_3")
        self.assertEqual(a.name, result.name)
        self.assertEqual(a.created_at, result.created_at)

    def test_get_bad_cls(self):
        """test get with invalid cls"""
        result = self.store.get("Dummy", "test")
        self.assertIsNone(result)

    def test_get_bad_id(self):
        """test get with invalid id"""
        result = self.store.get("State", "very_bad_id")
        self.assertIsNone(result)