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_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)
def test_all(self): """tests if all works in DB Storage""" storage = DBStorage() obj = storage.all() self.assertIsNotNone(obj) self.assertEqual(type(obj), dict) self.assertIs(obj, storage._DBStorage__objects)
def do_all(self, args): ''' Prints all string representation of all instances based or not on the class name. ''' obj_list = [] if os.environ.get('HBNB_TYPE_STORAGE') == 'db': storage = DBStorage() else: storage = FileStorage() if args: objects = storage.all(eval(args)().__class__) if not args: 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_name_attribute(self): expected = "wifi" amenity_name = session.query(Amenity).filter( Amenity.id == self.amenityA.id).one() amenity_name.name = "wifi" DBStorage.save(amenity_name) actual = DBStorage.__session.query( Amenity.name).filter(Amenity.id == self.amenityA.id).one() self.assertEqual(actual, expected)
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 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()
def test_new(self): """test when new is created""" storage = DBStorage() obj = storage.all() user = User() user.id = 123455 user.name = "Kevin" storage.new(user) key = user.__class__.__name__ + "." + str(user.id) self.assertIsNotNone(obj[key])
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
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()
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 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_new(self): """test when new is created""" storage = DBStorage() obj = storage.all() user = User() user.id = 123455 user.name = "Kevin" user.email = "x" user.password = "******" storage.new(user) key = user.__class__.__name__ + "." + str(user.id) if os.environ['HBNB_TYPE_STORAGE'] != 'db': self.assertIsNotNone(obj[key])
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)
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 __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 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()
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)
def setUpClass(cls): """set up for test""" cls.user = User() cls.user.first_name = "Kev" cls.user.last_name = "Yo" cls.user.email = "*****@*****.**" cls.storage = DBStorage()
def setUpClass(cls): """DBStorage testing setup. Instantiate new DBStorage. Fill DBStorage test session with instances of all classes. """ if type(models.storage) == DBStorage: cls.storage = DBStorage() Base.metadata.create_all(cls.storage._DBStorage__engine) Session = sessionmaker(bind=cls.storage._DBStorage__engine) cls.storage._DBStorage__session = Session() cls.state = State(name="California") cls.storage._DBStorage__session.add(cls.state) cls.city = City(name="San_Jose", state_id=cls.state.id) cls.storage._DBStorage__session.add(cls.city) cls.user = User(email="*****@*****.**", password="******") cls.storage._DBStorage__session.add(cls.user) cls.place = Place(city_id=cls.city.id, user_id=cls.user.id, name="School") cls.storage._DBStorage__session.add(cls.place) cls.amenity = Amenity(name="Wifi") cls.storage._DBStorage__session.add(cls.amenity) cls.review = Review(place_id=cls.place.id, user_id=cls.user.id, text="stellar") cls.storage._DBStorage__session.add(cls.review) cls.storage._DBStorage__session.commit()
def setUpClass(cls): ''' Initializing classes ''' cls.dbstorage = DBStorage() cls.output = StringIO() sys.stdout = cls.output
def setUpClass(cls): """setup for funsies.""" cls.user = User() cls.user.first_name = "Nate" cls.user.last_name = "KittyCat" cls.user.email = "*****@*****.**" cls.storage = DBStorage()
def setUpClass(cls): """DBStorage testing setup. Instantiate new DBStorage. Fill DBStorage test session with instances of all classes. """ if type(models.storage) == DBStorage: cls.storage = DBStorage() Base.metadata.create_all(cls.storage._DBStorage__engine) Session = sessionmaker(bind=cls.storage._DBStorage__engine) cls.storage._DBStorage__session = Session() cls.state = State(name="California") # cls.storage._DBStorage__session.add(cls.state) cls.city = City(name="San_Francisco", state_id=cls.state.id) # cls.storage._DBStorage__session.add(cls.city) cls.user = User(email="*****@*****.**", password="******") # cls.storage._DBStorage__session.add(cls.user) cls.place = Place(city_id=cls.city.id, user_id=cls.user.id, name="Lovely_place", number_rooms=3, number_bathrooms=1, max_guest=6, price_by_night=120, latitude=37.773972, longitude=-122.431297) # cls.storage._DBStorage__session.add(cls.place) cls.amenity = Amenity(name="Wifi") # cls.storage._DBStorage__session.add(cls.amenity) cls.review = Review(place_id=cls.place.id, user_id=cls.user.id, text="stellar")
def setUpClass(cls): """Place testing setup. Temporarily renames any existing file.json. Resets FileStorage objects dictionary. Creates FileStorage, DBStorage and Place instances for testing. """ try: os.rename("file.json", "tmp") except IOError: pass FileStorage._FileStorage__objects = {} cls.state = State(name="California") cls.city = City(name="San Francisco", state_id=cls.state.id) cls.user = User(email="*****@*****.**", password="******") cls.place = Place(city_id=cls.city.id, user_id=cls.user.id, name="Betty") cls.review = Review(text="stellar", place_id=cls.place.id, user_id=cls.user.id) cls.amenity = Amenity(name="water", place=cls.place.id) cls.filestorage = FileStorage() if type(models.storage) == DBStorage: cls.dbstorage = DBStorage() Base.metadata.create_all(cls.dbstorage._DBStorage__engine) Session = sessionmaker(bind=cls.dbstorage._DBStorage__engine) cls.dbstorage._DBStorage__session = Session()
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)
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 if all works in DB Storage""" storage = DBStorage() """obj_1 = storage.all() self.assertIsNotNone(obj_1) self.assertEqual(type(obj_1), dict)""" """self.assertIs(obj_1, storage._FileStorage__objects)""" """obj_2 = storage.all(User) self.assertIsNotNone(obj_2) self.assertEqual(type(obj_2), dict)""" """self.assertIs(obj_2, storage._FileStorage__objects)"""
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()
def setUpClass(cls): """create a session""" storage._DBStorage__session.close() cls.test_dbstorage = DBStorage() test_args = { 'updated_at': datetime.datetime(2017, 8, 16, 21, 51, 33, 669555), 'id': "0234", 'created_at': datetime.datetime(2017, 8, 16, 21, 53, 26, 563266), 'name': 'protoss[<0;73;15M' } cls.model = Amenity(**test_args) cls.store.reload() cls.test_len = 0