def do_all(self, args): ''' Prints all string representation of all instances based or not on the class name. ''' obj_list = [] if os.getenv('HBNB_TYPE_STORAGE') == "db": storage = DBStorage() else: storage = FileStorage() storage.reload() objects = storage.all() try: if len(args) != 0: eval(args) except NameError: print("** class doesn't exist **") return for key, val in objects.items(): if len(args) != 0: if type(val) is eval(args): obj_list.append(val) else: obj_list.append(val) print(obj_list)
def test_reload(self): """Test for reload()""" obj = DBStorage() self.assertTrue(obj._DBStorage__engine is not None) self.assertTrue(obj._DBStorage__session is None) obj.reload() self.assertTrue(obj._DBStorage__session is not None)
class TestDataBase(unittest.TestCase): """this will test the console""" def test001(self): """ Test pass """ pass @classmethod def setUp(self): """Set up MySQL""" self.db = MySQLdb.connect(host="localhost", port=3306, user='******', passwd='hbnb_test_pwd', db='hbnb_test_db', charset='utf8') self.cur = self.db.cursor() self.storage = DBStorage() self.storage.reload() @classmethod def tearDown(self): """Tear down MySQL""" self.cur.close() self.db.close() @unittest.skipIf(type_storage != 'db', 'db') def test_add(self): """Test add method""" self.cur.execute(""" INSERT INTO states (id, created_at, updated_at, name) VALUES (1, '2017-11-10 00:53:19', '2017-11-10 00:53:19', "California") """) self.cur.execute('SELECT * FROM states') rows = self.cur.fetchall() self.assertEqual(len(rows), 1)
class TestDBStorage(unittest.TestCase): '''this will test the FileStorage''' @classmethod def setUp(self): """set up MySQLdb""" self.db = MySQLdb.connect(host="localhost", port=3306, user='******', passwd='hbnb_test_pwd', db='hbnb_test_db') self.cursor = self.db.cursor() self.storage = DBStorage() self.storage.reload() @classmethod def tearDown(self): """teardown""" self.cursor.close() self.db.close() @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') != 'db', 'Skip if >>NOT<< in db mode') def test_pep8_DBStorage(self): """Tests pep8 style""" style = pep8.StyleGuide(quiet=True) p = style.check_files(['models/engine/db_storage.py']) self.assertEqual(p.total_errors, 0, "fix pep8")
def test_all(self): """tests if all works in File Storage""" storage = DBStorage() storage.reload() dict_len = len(storage.all()) s = State(name="test_all_state") s.save() storage.save() self.assertIs(len(storage.all()), dict_len + 1)
def test_all(self): """tests all in File Storage""" storage = DBStorage() storage.reload() len_of_dict = len(storage.all()) state = State(name="test_all_state") state.save() storage.save() self.assertsIs(len(storage.all()), len_of_dict + 1)
class TestDBStorage(unittest.TestCase): """this will test the DBStorage""" @classmethod def setUpClass(cls): """setup for the test""" cls.storage = DBStorage() cls.__session = Session() self.stored = DBStorage() self.__session = Session() Session = sessionmaker(bind=self.__engine) self.__session = Session() self.stored.reload() self.state1 = State1() self.state1.name = "California" self.state2 = State2() self.state2.name = "Arizona" def tearDown(self): """tear down method""" pass # del self.stored def testAttributes(self): """Tests if required functions exits""" self.assertTrue(hasattr()) def test_pep8_DBStorage(self): """Tests for pep8 styling""" style = pep8.StyleGuide(quiet=True) p = style.check_files(['models/engine/db_storage.py']) self.assertEqual(p.total_errors, 0, "Fails PEP8 compliance") def test_all(self): """Tests for all in DBStorage""" self.objs = self.storage.all() self.assertIsNotNone(self.objs) self.assertEqual(type(self.objs), dict) def test_new(self): """Tests for new objects in DBStorage""" pass def test_save(self): """Tests for saving objects in DBStorage""" pass def test_delete(self): """Tests for deleting objects in DBStorage""" pass def test_reload(self): """Tests for reloading objects in DBStorage""" pass
class TestDBStorage(unittest.TestCase): '''this will test the DBStorage''' @classmethod def setUpClass(self): """set up for test""" self.User = getenv("HBNB_MYSQL_USER") self.Passwd = getenv("HBNB_MYSQL_PWD") self.Db = getenv("HBNB_MYSQL_DB") self.Host = getenv("HBNB_MYSQL_HOST") self.db = MySQLdb.connect(host=self.Host, user=self.User, passwd=self.Passwd, db=self.Db, charset="utf8") self.query = self.db.cursor() self.storage = DBStorage() self.storage.reload() @classmethod def teardown(self): """at the end of the test this will tear it down""" self.query.close() self.db.close() @unittest.skipIf(getenv("HBNB_TYPE_STORAGE") != 'db', 'NO DB') def test_pep8_DBStorage(self): """Test Pep8""" style = pep8.StyleGuide(quiet=True) p = style.check_files(['models/engine/db_storage.py']) self.assertEqual(p.total_errors, 0, "fix pep8") @unittest.skipIf(getenv("HBNB_TYPE_STORAGE") != 'db', 'NO DB') def test_read_tables(self): """existing tables""" self.query.execute("SHOW TABLES") salida = self.query.fetchall() self.assertEqual(len(salida), 7) @unittest.skipIf(getenv("HBNB_TYPE_STORAGE") != 'db', 'NO DB') def test_no_element_user(self): """no elem in users""" self.query.execute("SELECT * FROM users") salida = self.query.fetchall() self.assertEqual(len(salida), 0) @unittest.skipIf(getenv("HBNB_TYPE_STORAGE") != 'db', 'NO DB') def test_no_element_cities(self): """no elem in cities""" self.query.execute("SELECT * FROM cities") salida = self.query.fetchall() self.assertEqual(len(salida), 0)
class testDBstorage(unittest.TestCase): """ Class to test the db storage method """ def __init__(self, *args, **kwargs): """ Set up test environment """ super().__init__(*args, **kwargs) self.s = DBStorage() self.name = 'DBStorage' self.values = DBStorage self.s.__dict__['_DBStorage__engine'] = create_engine( "mysql+mysqldb://{}:{}@{}:3306/{}".format('hbnb_test', 'hbnb_test_pwd', 'localhost', 'hbnb_test_db'), pool_pre_ping=True) self.s.reload() self.q = self.s.__dict__['_DBStorage__session'] def setUp(self): """ its a setup """ try: Base.metadata.create_all(self.s) except: pass def tearDown(self): """ Remove db at end of tests """ try: Base.metadata.drop_all(self.s) except: pass def test_obj_list_empty(self): """ __objects is initially empty """ self.assertEqual(len(self.s.all()), 0) @unittest.skip("not working") def test_new(self): """ New object is correctly added to __objects """ new = User() new.save() for obj in self.s.all().values(): temp = obj self.assertTrue(temp is obj) def test_all(self): """ __objects is properly returned """ new = BaseModel() temp = self.s.all() self.assertIsInstance(temp, dict)
class TestDBStorage(unittest.TestCase): """ Class to test the file storage method """ @classmethod def setUpClass(self): """setUpClass module""" storage._DBStorage__session.close() self.store = DBStorage() self.store.reload() @classmethod def tearDownClass(self): """tearDownClass module""" self.store._DBStorage__session.close() storage.reload() def test_all(self): """print alls objects""" self.assertEqual(len(self.store.all()), 0) new_obj = State() new_obj.name = 'California' self.store.new(new_obj) self.assertEqual(len(self.store.all()), 1) # Make all('classname') work without console def test_new(self): """New objects""" new_obj = State() new_obj.name = "Texas" new_obj.save() self.assertTrue(len(self.store.all()), 1) def test_save(self): """save objects""" self.store.reload() new_obj = State() new_obj.name = 'Washington' self.store.new(new_obj) self.store.save() self.assertEqual(len(self.store.all()), 4) def test_delete(self): new_obj = State() new_obj.name = "Michigan" self.store.new(new_obj) self.store.save() self.store.delete(new_obj) self.assertFalse(new_obj in self.store.all()) def test_reload(self): """reload datas objects""" new_obj = City() self.store.new(new_obj) self.store.reload() test_len = len(self.store.all()) self.assertEqual(test_len, 3) self.store.reload() for value in self.store.all().values(): self.assertIsInstance(value.created_at, datetime)
class TestDBStorage(unittest.TestCase): """ Tests the db storage """ @classmethod def setUp(self): """ Set up database connection """ self.db = MySQLdb.connect(host="localhost", port=3306, user='******', passwd='hbnb_test_pwd', db='hbnb_test_db', charset='utf8') self.cursor = self.db.cursor() self.storage = DBStorage() self.storage.reload() @classmethod def tearDown(self): """ Close connections """ self.cursor.close() self.db.close()
class TestDBStorage(unittest.TestCase): '''this will test the DBStorage''' def setUp(self): """set up for test""" self.storage = DBStorage() self.storage.reload() def test_pep8_DBStorage(self): """Tests pep8 style""" style = pep8.StyleGuide(quiet=True) p = style.check_files(['models/engine/db_storage.py']) self.assertEqual(p.total_errors, 0, "fix pep8") def test_all(self): """test if all works in DBStorage""" self.objs = self.storage.all() self.assertIsNotNone(self.objs) self.assertEqual(type(self.objs), dict) def test_new(self): """Test that a new object is added to the DB""" state1 = State() setattr(state1, 'name', 'California') state2 = State() setattr(state2, 'name', 'Arizona') state1.save() state2.save() session = self.storage._DBStorage__session self.assertIsNotNone(session) results = session.query(State).all() self.assertIs(type(results), list) """TODO Assess results """ def tearDown(self): del self.storage
class TestDBStorage(unittest.TestCase): '''this will test the FileStorage''' @classmethod def setUp(self): """Set up MySQLdb""" self.db = MySQLdb.connect(host="localhost", port=3306, user='******', passwd='hbnb_test_pwd', db='hbnb_test_db', charset='utf8') self.cur = self.db.cursor() self.storage = DBStorage() self.storage.reload() @classmethod def tearDown(self): """Tear down MySQLdb""" self.cur.close() self.db.close() @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') != 'db', 'db') def test_pep8_DBStorage(self): """Tests pep8 style""" style = pep8.StyleGuide(quiet=True) p = style.check_files(['models/engine/db_storage.py']) self.assertEqual(p.total_errors, 0, "fix pep8") @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') != 'db', 'db') def test_add(self): """Test add method""" self.cur.execute(""" INSERT INTO states (id, created_at, updated_at, name) VALUES (1, '2017-11-10 00:53:19', '2017-11-10 00:53:19', "California") """) self.cur.execute('SELECT * FROM states') rows = self.cur.fetchall() self.assertEqual(len(rows), 1)
class test_dbStorage(unittest.TestCase): """ Class to test the file storage method """ def setUp(self): """ Set up test environment """ os.environ['HBNB_MYSQL_USER'] = "******" os.environ['HBNB_MYSQL_PWD'] = "hbnb_test_pwd" os.environ['HBNB_MYSQL_HOST'] = "localhost" os.environ['HBNB_MYSQL_DB'] = "hbnb_test_db" os.environ['HBNB_TYPE_STORAGE'] = "db" self.storage = DBStorage() self.storage.reload() storage = self.storage def tearDown(self): os.environ['HBNB_TYPE_STORAGE'] = "apple" def test_storage(self): """ docstring""" self.assertIsInstance(self.storage, DBStorage) @unittest.skipIf(os.getenv('HBNB_TYPE_STORAGE') != "db", "FileStorage") def test_city(self): """ Testing cities in the database """ bark = State(**{'name': 'Michigan'}) bark.save() meow = City(**{'state_id': bark.id}) meow.save() storage.save() self.assertIn("City." + meow.id, storage.all()) def test_all(self): """ __objects is properly returned """ new = BaseModel() temp = storage.all() self.assertIsInstance(temp, dict)
class TestDBStorage(unittest.TestCase): """Tests the DBStorage class""" def test_pep8_FileStorage(self): """Tests pep8 style""" style = pep8.StyleGuide(quiet=True) p = style.check_files(['models/engine/db_storage.py']) self.assertEqual(p.total_errors, 0, "fix pep8") @classmethod def setUpClass(cls): """set up for test""" """ environ['HBNB_ENV'] = 'test' environ['HBNB_MYSQL_USER'] = '******' environ['HBNB_MYSQL_PWD'] = 'hbnb_test_pwd' environ['HBNB_MYSQL_HOST'] = 'localhost' environ['HBNB_MYSQL_DB'] = 'hbnb_test_db' environ['HBNB_TYPE_STORAGE'] = 'db' """ pass @unittest.skipIf('HBNB_TYPE_STORAGE' not in environ or environ['HBNB_TYPE_STORAGE'] != 'db', 'These tests\ should only be used when storage type is db') def setUp(self): """Setup the class""" self.user = User() self.user.first_name = "Kev" self.user.last_name = "Yo" self.user.email = "*****@*****.**" self.user.password = "******" self.storage = DBStorage() self.storage.reload() def tearDownClass(): """ del environ['HBNB_ENV'] del environ['HBNB_MYSQL_USER'] del environ['HBNB_MYSQL_PWD'] del environ['HBNB_MYSQL_HOST'] del environ['HBNB_MYSQL_DB'] del environ['HBNB_TYPE_STORAGE'] """ pass @unittest.skipIf('HBNB_TYPE_STORAGE' not in environ or environ['HBNB_TYPE_STORAGE'] != 'db', 'These tests\ should only be used when storage type is db') def test_all(self): """ Tests db_storage all method to query objects in the database """ original_len = self.storage.all(User) self.storage.new(self.user) self.storage.save() new_len = self.storage.all(User) self.assertTrue(original_len != new_len) @unittest.skipIf('HBNB_TYPE_STORAGE' not in environ or environ['HBNB_TYPE_STORAGE'] != 'db', 'These tests\ should only be used when storage type is db') def test_new(self): """ Tests db_storage new method to add a new object""" original_len = self.storage.all(User) self.storage.new(self.user) self.storage.save() new_len = self.storage.all(User) self.assertTrue(original_len != new_len) @unittest.skipIf('HBNB_TYPE_STORAGE' not in environ or environ['HBNB_TYPE_STORAGE'] != 'db', 'These tests\ should only be used when storage type is db') def test_save(self): """ Tests db_storage save method to save the added object """ original_len = self.storage.all(User) self.storage.new(self.user) self.storage.save() new_len = self.storage.all(User) self.assertTrue(original_len != new_len) @unittest.skipIf('HBNB_TYPE_STORAGE' not in environ or environ['HBNB_TYPE_STORAGE'] != 'db', 'These tests\ should only be used when storage type is db') def test_delete(self): """ Tests db_storage delete method to delete an object form the db """ original_len = self.storage.all(User) self.storage.new(self.user) self.storage.save() self.storage.delete(self.user) new_len = self.storage.all(User) self.assertTrue(original_len == new_len) @unittest.skipIf('HBNB_TYPE_STORAGE' not in environ or environ['HBNB_TYPE_STORAGE'] != 'db', 'These tests\ should only be used when storage type is db') def test_reload(self): """Tests db_storage delete method""" pass """def test_reload(self):
class testDBStorage(unittest.TestCase): ''' unittest class for testing DBStorage module ''' def setUp(self): ''' Sets up the environment for testing DBStorage ''' os.environ['HBNB_TYPE_STORAGE'] = 'db' os.environ['HBNB_MYSQL_USER'] = '******' os.environ['HBNB_MYSQL_PWD'] = 'hbnb_test_pwd' os.environ['HBNB_MYSQL_HOST'] = 'localhost' os.environ['HBNB_MYSQL_DB'] = 'hbnb_test_db' self.storage = DBStorage() self.my_model = models.BaseModel() self.storage.reload() def test_DBStorage_type_storage_environ(self): ''' Test if environment is updating ''' self.assertEqual(os.getenv('HBNB_TYPE_STORAGE'), 'db') def test_DBStorage_all_method(self): ''' Test all method ''' new_dict = self.storage.all() self.assertTrue(type(new_dict) == type(dict())) def test_DBStorage_new_method(self): ''' Test new method ''' new_state = models.State() self.storage.new(new_state) self.assertTrue(new_state in self.storage._DBStorage__session) def test_DBStorage_reload_method(self): ''' Test reload method ''' test = self.storage._DBStorage__engine.execute("show databases;") test = [x for x in test] new = False for x in test: if os.getenv('HBNB_MYSQL_DB') in x[0]: new = True self.assertTrue(new,) def test_DBStorage_delete_method(self): ''' Test delete method ''' def test_DBStorage_delete_parent_deletes_children(self): ''' Test if deleting a parent deletes the children as well ''' def test_DBStorage_save_method(self): ''' Test save method ''' def test_DBStorage_in_correct_DB(self): ''' Test if going to correct database ''' def test_DBStorage_drop_all(self): '''
''' Package initializer ''' from models.engine.file_storage import FileStorage from models.base_model import BaseModel from models.user import User from models.place import Place from models.state import State from models.city import City from models.amenity import Amenity from models.review import Review import os classes = { "User": User, "BaseModel": BaseModel, "Place": Place, "State": State, "City": City, "Amenity": Amenity, "Review": Review } if os.getenv('HBNB_TYPE_STORAGE') == 'db': from models.engine.db_storage import DBStorage storage = DBStorage() else: storage = FileStorage() storage.reload()
def test_reload_dbstorage(self): """ tests reload """ storage = DBStorage() storage.reload()