Beispiel #1
0
 def do_destroy(self, args):
     '''
         Deletes an instance based on the class name and id.
     '''
     args = shlex.split(args)
     if len(args) == 0:
         print("** class name missing **")
         return
     elif len(args) == 1:
         print("** instance id missing **")
         return
     class_name = args[0]
     class_id = args[1]
     storage = FileStorage()
     storage.reload()
     obj_dict = storage.all()
     try:
         eval(class_name)
     except NameError:
         print("** class doesn't exist **")
         return
     key = class_name + "." + class_id
     try:
         del obj_dict[key]
     except KeyError:
         print("** no instance found **")
     storage.save()
Beispiel #2
0
 def test_reload(self):
     """
     Test reload method
     """
     my_storage = FileStorage()
     my_storage.save()
     objects = my_storage.all()
     my_storage.reload()
     self.assertEqual(my_storage.all(), objects)
Beispiel #3
0
 def test_save_method_serializes___objects_to_json_file(self):
     """tests that save() converts the __objects dict to json format
     and saves it to __file_path (file.json)"""
     f = FileStorage()
     model = BaseModel()
     f.new(model)
     f.save()
     result = os.path.isfile('file.json')
     self.assertEqual(result, True)
Beispiel #4
0
 def test_delete_method(self):
     """Test delete method"""
     fs = FileStorage()
     new_state = State()
     fs.new(new_state)
     fs.save()
     self.assertIn(new_state, fs.all(State).values())
     fs.delete(new_state)
     self.assertNotIn(new_state, fs.all(State).values())
Beispiel #5
0
 def test_reload(self):
     '''test reload'''
     store = FileStorage()
     inst = BaseModel()
     inst_id = inst.id
     store.new(inst)
     store.save()
     store.reload()
     inst_dict = store.all()
     self.assertEqual(type(inst_dict), dict)
 def test_save(self):
     """ test if file.json is saved or created"""
     store = FileStorage()
     self.assertIsInstance(store, FileStorage)
     b1 = BaseModel()
     store.new(b1)
     store.name = "Gary"
     store.my_number = 89
     store.save()
     self.assertTrue(os.path.exists('file.json'))
    def test_save(self):
        """Tests the save method of File Storage class"""

        Storage = FileStorage()
        Storage.reset()
        b1 = BaseModel()
        Storage.new(b1)
        self.assertFalse(path.exists("file.json"))
        Storage.save()
        self.assertTrue(path.exists("file.json"))
Beispiel #8
0
 def test_delete(self):
     """tests if all works in File Storage"""
     storage = FileStorage()
     state_instance = State(name="California")
     self.assertIsNotNone(state_instance)
     storage.new(state_instance)
     storage.save()
     storage.delete(state_instance)
     obj_state = storage.all(State)
     self.assertEqual(len(obj_state), 0)
Beispiel #9
0
 def test_03a_working_reload(self):
     """Checks reload functionality if file_path doesn't exist"""
     fs = FileStorage()
     b = BaseModel()
     key = "BaseModel" + '.' + b.id
     fs.new(b)
     fs.save()
     fs.reset()
     fs.reload()
     self.assertTrue(fs.all()[key])
    def test_reload(self):
        """ test relaod """

        b = BaseModel()
        f = FileStorage()
        f.new(b)
        f.save()
        d = f.all()
        f.reload()
        self.assertEqual(d, f.all())
 def test_file_exist(self):
     """ Checking if JSON file exist
         and is reloading
     """
     user1 = User()
     fileStorage = FileStorage()
     k = "{}.{}".format(type(user1).__name__, user1.id)
     fileStorage.new(user1)
     fileStorage.save()
     self.assertTrue(path.exists('file.json'))
Beispiel #12
0
 def test_savebyreadingfile(self):
     """Tests the save method by reading file"""
     Storage = FileStorage()
     Storage.reset()
     b1 = BaseModel()
     Storage.new(b1)
     Storage.save()
     with open("file.json", "r", encoding='utf-8') as r:
         content = r.read()
         self.assertTrue("BaseModel.{}".format(b1.id) in content)
Beispiel #13
0
    def test_get(self):
        """Tests the get() method, retrieves an object by its name
        """
        storage = FileStorage()
        storage.reload()
        jur1 = Jurisdiction(**{"name": "Cauca", "victims": 3})
        storage.save()

        jur1_clone = storage.get(Jurisdiction, "Cauca")
        self.assertTrue(jur1_clone is jur1)
 def test_file_storage_new_method(self):
     """Test new method"""
     my_model = BaseModel()
     my_storage = FileStorage()
     my_storage.new(my_model)
     my_storage.save()
     desired_key = my_model.__class__.__name__ + '.' + my_model.id
     for key in my_storage._FileStorage__objects:
         if key == desired_key:
             self.assertEqual(desired_key, key)
 def test_for_reload(self):
     """test for reload function"""
     f1 = FileStorage()
     f2 = BaseModel()
     key = "BaseModel" + "." + f2.id
     f1.new(f2)
     f1.save()
     f1._FileStorage__objects = {}
     f1.reload()
     self.assertTrue(f1.all()[key])
Beispiel #16
0
 def test_reset(self):
     """ Tests if the reset method empties the database
     """
     storage = FileStorage()
     storage.reload()
     jur1 = Jurisdiction(**{"name": "Cauca", "victims": 3})
     storage.save()
     storage.reset()
     data = storage.all()
     self.assertTrue(data == {})
Beispiel #17
0
 def test_FileStorage_save_file_key(self):
     """Test the save() method to check contents of file.json """
     bm1 = BaseModel()
     class_name = bm1.__class__.__name__
     key = class_name + '.' + str(bm1.id)
     storage = FileStorage()
     storage.new(bm1)
     storage.save()
     with open('file.json') as file:
         self.assertIn(key, file.read())
 def test_base_model(self):
     """testing the BaseModel"""
     dog = FileStorage()
     self.assertIs(type(dog.id), str)
     self.assertIs(type(dog.created_at), datetime)
     self.assertIs(type(dog.updated_at), datetime)
     self.assertNotEqual(dog.created_at, dog.updated_at)
     self.assertFalse(dog.updated_at == datetime.utcnow())
     old_updated = dog.updated_at
     dog.save()
Beispiel #19
0
 def test_FileStorage_save_file_exists(self):
     """Test the save() method if file.json exists"""
     bm1 = BaseModel()
     class_name = bm1.__class__.__name__
     key = class_name + '.' + str(bm1.id)
     storage = FileStorage()
     storage.new(bm1)
     storage.save()
     with open('file.json') as file:
         self.assertTrue(isinstance(file.read(), str))
Beispiel #20
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)
Beispiel #21
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_save_method(self):
     """ Test for save method """
     _dict = self.storage.to_dict()
     new_key = _dict['__class__'] + "." + _dict['id']
     new = FileStorage()
     new.save()
     with open("file.json", 'r') as fd:
         file_r = json.load(fd)
     new = file_r[new_key]
     for key in new:
         self.assertEqual(_dict[key], new[key])
 def test_change_length(self):
     """
     Create an object and save it and checks the new length for dictionary
     """
     new = FileStorage()
     len1 = len(new.all())
     second = BaseModel()
     new.save()
     new.reload()
     len2 = len(new.all())
     self.assertEqual(len1, len2 - 1)
 def test_for_save(self):
     """test for save function"""
     s = FileStorage()
     s._FileStorage__objects = {}
     s.new(BaseModel())
     self.assertFalse(os.path.exists("file.json"))
     s.save()
     self.assertTrue(os.path.exists("file.json"))
     with open("file.json", 'r') as f:
         self.assertEqual(type(f.read()), str)
     self.assertNotEqual(os.stat("file.json").st_size, 0)
Beispiel #25
0
class TestFileStorage(unittest.TestCase):
    """This is Unittest for the method FileStorage"""
    def setUp(self):
        """This is the setup method for Unittesting"""
        self.otter = FileStorage()
        self.otter.file_path = "test_file.json"
        self.otter.reload()
        self.objects = self.otter.objects
        self.UrchinA = self.objects["Amenity.\
19b4933a-55d0-4287-99a0-773c1dcab528"]
        self.UrchinB = self.objects["BaseModel.\
7152df91-ec26-4595-bf75-fd84f02a3bd9"]
        self.UrchinC = self.objects["City.\
f76cbf19-eb40-4901-88ca-569684490fd1"]
        self.UrchinP = self.objects["Place.\
52cf288d-f402-4564-a91f-06c71004c6b7"]
        self.UrchinR = self.objects["Review.\
ac984526-613a-4ec8-933d-ab32a5f924b9"]
        self.UrchinS = self.objects["State.\
125b2cf3-66d9-4185-b442-e8a49cb7801d"]
        self.UrchinU = self.objects["User.\
bf654f25-d81f-4d02-a3c0-1777c5bfeda5"]

    def test_all(self):
        """This is the Unittest for the method all"""
        test_dict = self.otter.all()
        self.assertIsInstance(test_dict, dict)
        for key in test_dict:
            self.assertEqual(test_dict[key], self.otter.objects[key])

    def test_new(self):
        """This is a Unittest for the method new"""
        new_obj = copy.deepcopy(self.UrchinB)
        new_obj.id = "7152df91-ec26-4595-bf75-fd84f02atest"
        self.otter.new(new_obj)
        otter_dict = self.otter.objects
        test_obj = otter_dict["BaseModel.7152df91-ec26-4595-bf75-fd84f02atest"]
        self.assertIsInstance(test_obj, BaseModel)
        self.assertEqual(test_obj.id, "7152df91-ec26-4595-bf75-fd84f02atest")

    def test_save(self):
        """This is a Unittest for the the method save"""
        if os.path.isfile("test_save.json"):
            os.remove("test_save.json")
        self.otter.reload()
        self.otter.file_path = "test_save.json"
        self.otter.save()
        with open(self.otter.file_path, "r") as FILE:
            test_save_dict = json.loads(FILE.read())
        with open("test_file.json", "r") as FILE:
            test_file_dict = json.loads(FILE.read())
        self.assertDictEqual(test_save_dict, test_file_dict)
        os.remove("test_save.json")
        self.otter.file_path = "test_file.json"
Beispiel #26
0
 def test_all_no_specification(self):
     ''' tests all when no class is passed '''
     return True
     fs = FileStorage()
     new_state1 = State()
     fs.new(new_state1)
     fs.save()
     new_user1 = User()
     fs.new(new_user1)
     fs.save()
     self.assertEqual(8, len(fs.all()))
Beispiel #27
0
 def test_FileStorage_reload_successful(self):
     """Test the reload() method to see if object reloads successfully"""
     FileStorage._FileStorage__objects = {}
     bm1 = BaseModel()
     class_name = bm1.__class__.__name__
     key = class_name + '.' + str(bm1.id)
     storage = FileStorage()
     storage.new(bm1)
     storage.save()
     storage.reload()
     self.assertIn(key, FileStorage._FileStorage__objects)
Beispiel #28
0
class TestFileStorage(unittest.TestCase):
    '''
    Test cases for file_storage class
    '''

    def setUp(self):
        '''
        simple set up
        '''
        self.my_model = BaseModel()
        self.storage = FileStorage()

    def tearDown(self):
        '''
        tear down method
        '''
        if os.path.exists("file.json"):
            os.remove("file.json")
        else:
            pass

    def test_new(self):
        '''
        tests new method in file storage
        '''
        self.storage.new(self.my_model)
        new_dict = self.storage.all()
        key = self.my_model.__class__.__name__ + '.' + self.my_model.id
        self.assertIsInstance(new_dict[key], BaseModel)

    def test_all(self):
        '''
        tests if all returns a dict
        '''
        self.assertIsInstance(self.storage.all(), dict)

    def test_save(self):
        '''
        tests the save method of file storage class
        '''
        self.storage.save()
        self.assertTrue(os.path.exists("file.json"))

    def test_json_file_content_type(self):
        '''
        tests if the content of the json file is type dict
        '''
        self.storage.save()
        self.storage.new(self.my_model)

        with open("file.json", encoding='utf-8') as fd:
            data = json.load(fd)

        self.assertIsInstance(data, dict)
Beispiel #29
0
 def test_reload_2(self):
     '''Tests reload normal'''
     Storage = FileStorage()
     b1 = BaseModel()
     Storage.new(b1)
     Storage.save()
     Storage.reset()
     Storage.reload()
     self.assertTrue(Storage.all()["BaseModel.{}".format(b1.id)])
     self.assertTrue(Storage._FileStorage__objects["BaseModel.{}".format(
         b1.id)])
Beispiel #30
0
 def test_empty_reload(self):
     """ Empty reload function """
     my_obj = FileStorage()
     new_obj = BaseModel()
     my_obj.new(new_obj)
     my_obj.save()
     my_dict1 = my_obj.all()
     os.remove("test.json")
     my_obj.reload()
     my_dict2 = my_obj.all()
     self.assertTrue(my_dict2 == my_dict1)
 def test_save_FileStorage(self):
     '''Test if is saving the changes'''
     dic1 = self.my_model.to_dict()
     key1 = dic1['__class__'] + "." + dic1['id']
     s = FileStorage()
     s.save()
     with open("file.json", mode='r') as f:
         str1 = json.load(f)
     new = str1[key1]
     for key in new:
         self.assertEqual(dic1[key], new[key])
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:
            self.assertTrue(False)