Exemplo n.º 1
0
 def test_all(self):
     """Testing for all method
     """
     b = FileStorage()
     b.reset_obj()
     self.assertFalse(b.all())
     b.new(BaseModel())
     self.assertTrue(b.all())
Exemplo n.º 2
0
    def test_save(self):
        """ test save """

        b = BaseModel()
        f = FileStorage()
        f.new(b)
        f.save()
        self.assertTrue(os.path.exists("file.json"))
Exemplo n.º 3
0
    def test_all(self):
        '''Testing all method'''
        storage = FileStorage()
        obj_1 = BaseModel()
        storage.new(obj_1)

        self.assertIs(type(storage.all()), dict)
        self.assertTrue(len(storage.all()) != 0)
Exemplo n.º 4
0
 def test_new_method(self):
     "tests the new method"
     a = FileStorage()
     b = BaseModel()
     c = BaseModel.__class__.__name__
     d = b.id
     e = a.all()
     a.new(b)
Exemplo n.º 5
0
 def test_reload_method_updates___objects_dict(self):
     """tests that reload() updates __objects with data from json file"""
     f = FileStorage()
     model = BaseModel()
     f.new(model)
     f.save()
     f.reload()
     self.assertNotEqual(f._FileStorage__objects, {})
Exemplo n.º 6
0
 def test_save(self):
     """Testing save method
     """
     b = FileStorage()
     b.reset_obj()
     b.new(BaseModel())
     b.save()
     self.assertTrue(path.isfile("file.json"))
Exemplo n.º 7
0
 def testAll(self):
     '''
     test all() should return dict
     '''
     test_store = FileStorage()
     new_base = BaseModel()
     test_store.new(new_base)
     self.assertEqual(type(test_store.all()), dict)
Exemplo n.º 8
0
    def test_new(self):
        '''Testing new method'''
        storage = FileStorage()
        obj_1 = BaseModel()
        storage.new(obj_1)

        objects = storage.all()

        self.assertEqual(objects['BaseModel' + "." + obj_1.id], obj_1)
Exemplo n.º 9
0
 def test_new_two_args(self):
     """Test new function with two arguments"""
     fs = FileStorage()
     bm = BaseModel()
     pl = Place()
     with self.assertRaises(TypeError):
         fs.new(bm, 3)
     with self.assertRaises(TypeError):
         fs.new(bm, pl)
Exemplo n.º 10
0
 def test_new(self):
     """
     Test new(obj) method
     """
     dict_before = self.new_fs_inst._FileStorage__objects
     new_bm_inst = BaseModel()
     FileStorage.new(self, new_bm_inst)
     dict_after = self.new_fs_inst._FileStorage__objects
     self.assertTrue(len(dict_before) + 1, len(dict_after))
Exemplo n.º 11
0
 def test_delete_in_fileStorage(self):
     """Test for method delete when using FileStorage
     """
     storage = FileStorage()
     obj = BaseModel()
     storage.new(obj)
     self.assertTrue(obj in storage._FileStorage__objects.values())
     obj.delete()
     self.assertTrue(obj not in storage._FileStorage__objects.values())
Exemplo n.º 12
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)
Exemplo n.º 13
0
 def test_new(self):
     file1 = FileStorage()
     inst_dic = file1.all()
     tidus = User()
     tidus.id = 999999
     tidus.name = "Tidus"
     file1.new(tidus)
     key = tidu.__class__.__name__ + "." + str(tidus.id)
     self.assertIsNotNone(inst_dic[key])
Exemplo n.º 14
0
 def test_filestorage_get(self):
     '''
         Tests Get method for storage
     '''
     files = FileStorage()
     new = State()
     files.new(new)
     firstid = list(storage.all("State").values())[0].id
     self.assertEqual(type(storage.get("State", firstid)), State)
Exemplo n.º 15
0
 def test_new(self):
     """test when new is created"""
     storage = FileStorage()
     obj = storage.all()
     user = User()
     user.name = "Kevin"
     storage.new(user)
     key = user.__class__.__name__ + "." + str(user.id)
     self.assertIsNotNone(obj[key])
Exemplo n.º 16
0
 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"))
Exemplo n.º 17
0
 def test_reload_update_and_create(self):
     '''Tests if updated and created are the same'''
     f1 = FileStorage()
     b1 = BaseModel()
     f1.new(b1)
     all_objs = f1.all()
     for key, value in all_objs.items():
         if key == "BaseModel.{}".format(b1.id):
             self.assertFalse(value.created_at == value.updated_at)
Exemplo n.º 18
0
 def test_new(self):
     """ testing """
     storage = FileStorage()
     obj = storage.all()
     user = User()
     user.name = "holberton"
     storage.new(user)
     key = user.__class__.__name__ + "." + str(user.id)
     self.assertIsNotNone(obj[key])
Exemplo n.º 19
0
 def test_new(self):
     '''Tests new method'''
     s = FileStorage()
     dic = s.all()
     maine = State()
     maine.name = "Maine"
     s.new(maine)
     key = maine.__class__.__name__ + "." + str(maine.id)
     self.assertIsNotNone(dic[key])
Exemplo n.º 20
0
 def test_reload(self):
     """ Tests reload() method """
     bm1 = BaseModel()
     fs1 = FileStorage()
     fs1.new(bm1)
     fs1.save()
     dict1 = fs1.reload()
     # check reload() output
     self.assertTrue(dict1 is fs1.reload())
Exemplo n.º 21
0
 def test_FileStorage_reload_file_exists(self):
     """Test the reload() method to check if file.json exists"""
     bm1 = BaseModel()
     class_name = bm1.__class__.__name__
     key = class_name + '.' + str(bm1.id)
     storage = FileStorage()
     storage.new(bm1)
     storage.save()
     self.assertTrue(os.path.exists('file.json'))
Exemplo n.º 22
0
 def test_delete_method(self):
     ''' tests 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())
Exemplo n.º 23
0
 def test_file_storage_new_value(self):
     """Test to see that new sets value as an object in __objects"""
     test = FileStorage()
     temp = BaseModel()
     test.new(temp)
     test_dict = test.all()
     test_entry = temp.__class__.__name__ + "." + temp.id
     test_obj = test_dict[test_entry]
     self.assertIsInstance(test_obj, type(temp))
Exemplo n.º 24
0
 def test_save_1(self):
     """testing if save saves new instance properly
     """
     my_model = BaseModel()
     my_storage = FileStorage()
     my_storage.new(my_model)
     my_storage.save()
     file_existence = os.path.exists(self.my_path)
     self.assertEqual(True, file_existence)
Exemplo n.º 25
0
 def test_get(self):
     ''' test get '''
     fs = FileStorage()
     new = State()
     new.name = "Wyoming"
     fs.new(new)
     my_id = new.id
     fs.save()
     self.assertTrue(new, self.storage.get(State, my_id))
Exemplo n.º 26
0
 def test_reload(self):
     """ testing reload """
     test = FileStorage()
     test1 = BaseModel()
     test.new(test1)
     test.save()
     test2 = test.all()
     test.reload()
     self.assertEqual(test2, test.all())
Exemplo n.º 27
0
class TestClass(unittest.TestCase):
    """Test cases"""

    def setUp(self):
        self.storage = FileStorage()
        self.model = BaseModel()
        return super().setUp()

    def tearDown(self):
        del(self.storage)
        del(self.model)
        if os.path.exists("file.json"):
            os.remove("file.json")
        return super().tearDown()

    def test_is_instance(self):
        """isInstance"""

        self.assertIsInstance(self.storage, FileStorage)

    def test_find_object_success(self):

        self.storage.new(self.model)
        self.assertIs(
            self.storage.find('BaseModel', self.model.id), self.model
            )

    def test_find_object_not_found(self):

        self.storage.new(self.model)
        self.assertRaisesRegex(
            Exception,
            'no instance found',
            self.storage.find,
            'BaseModel',
            'does-not-exist')

    def test_reset(self):
        """reset"""
        pass

    def test_new_method(self):
        """new"""
        pass

    def test_save_method(self):
        """save method"""
        pass

    def test_reload_function(self):
        """reload function"""
        pass

    def test_function_all(self):
        """all functions"""
        pass
Exemplo n.º 28
0
 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'))
Exemplo n.º 29
0
 def test_new(self):
     """ test method new """
     store = FileStorage()
     store_dict = store.all()
     gary = User()
     gary.id = 123
     gary.name = "Link"
     store.new(gary)
     key = "{}.{}".format(gary.__class__.__name__, gary.id)
     self.assertIsNotNone(store_dict[key])
Exemplo n.º 30
0
 def testNew(self):
     ''' Test that the new function adds a key:value pair to __objects '''
     s = FileStorage()
     FileStorage._FileStorage__objects = {}
     b1 = BaseModel()
     s.new(b1)
     temp = s.all()
     self.assertEqual(len(s.all()), 1)
     FileStorage._FileStorage__objects = {}
     s.reload()
Exemplo n.º 31
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:
            self.assertTrue(False)