def test_cities_create(self): ''' Test city create function ''' state1 = classes['State'](name="California") state1.save() data = json.dumps({"name": "San Francisco"}) response = self.test_app.post( '/api/v1/states/{}/cities'.format(state1.id), data=data, content_type='application/json') check = json.loads(response.data.decode(defenc())) city1 = storage.get("City", check['id']) self.assertTrue(city1) self.assertEqual(response.status_code, 201) city1.delete() state1.delete()
def test_amenities_list_all(self): ''' Testing the list all amenities function ''' amenity1 = classes['Amenity'](name="Shower") amenity1.save() amenity2 = classes['Amenity'](name="Pet Friendly") amenity2.save() amenity3 = classes['Amenity'](name="No Smoking") amenity3.save() count = storage.count("Amenity") response = self.test_app.get('/api/v1/amenities') check = json.loads(response.data.decode(defenc())) self.assertEqual(len(check), count) amenity1.delete() amenity2.delete() amenity3.delete()
def test_states_list_all(self): ''' Testing list all states function ''' state1 = classes['State'](name="California") state1.save() state2 = classes['State'](name="Louisiana") state2.save() state3 = classes['State'](name="New York") state3.save() count = storage.count("State") response = self.test_app.get('/api/v1/states') check = json.loads(response.data.decode(defenc())) self.assertEqual(len(check), count) state1.delete() state2.delete() state3.delete()
def test_cities_ignore_keys(self): ''' Test to see if id, updated_at, and created_at keys are skipped ''' state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() data = '{"created_at": "nop",\ "updated_at": "nop", "id": "nop", "state_id": "nop"}' response = self.test_app.put( 'api/v1/cities/{}'.format(city1.id), data=data, content_type='application/json') check = json.loads(response.data.decode(defenc())) self.assertNotEqual(city1.id, "nop") self.assertNotEqual(city1.updated_at, "nop") self.assertNotEqual(city1.created_at, "nop") self.assertNotEqual(city1.state_id, "nop") city1.delete() state1.delete()
def test_places_delete(self): ''' Testing place delete by ID function ''' user1 = classes['User'](name="Billy", email="Nope", password="******") user1.save() state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() place1 = classes['Place'](name="Blue Shoe", city_id=city1.id, user_id=user1.id) place1.save() response = self.test_app.delete('/api/v1/places/{}'.format(place1.id)) check = json.loads(response.data.decode(defenc())) self.assertEqual(storage.get("Place", place1.id), None) city1.delete() user1.delete() state1.delete()
def test_places_fetch_by_id(self): ''' Test function to fetch a place by it's id ''' user1 = classes['User'](name="Billy", email="Nope", password="******") user1.save() state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() place1 = classes['Place'](name="Blue Shoe", city_id=city1.id, user_id=user1.id) place1.save() response = self.test_app.get('/api/v1/places/{}'.format(place1.id)) check = json.loads(response.data.decode(defenc())) self.assertEqual(place1.id, check['id']) place1.delete() city1.delete() user1.delete() state1.delete()
def test_places_create(self): ''' Test place create function ''' user1 = classes['User'](name="Billy", email="Nope", password="******") user1.save() state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() data = json.dumps({"name": "Buddy", "user_id": user1.id}) response = self.test_app.post('/api/v1/cities/{}/places'.format( city1.id), data=data, content_type='application/json') check = json.loads(response.data.decode(defenc())) place1 = storage.get("Place", check['id']) self.assertTrue(place1) self.assertEqual(response.status_code, 201) place1.delete() city1.delete() user1.delete() state1.delete()
def test_reviews_delete_by_review_id(self): ''' Test delete by review id function ''' state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() user1 = classes['User'](email="*****@*****.**", password="******") user1.save() place1 = classes['Place'](name="Blue Shoe", city_id=city1.id, user_id=user1.id) place1.save() review1 = classes['Review'](text="Nice place", place_id=place1.id, user_id=user1.id) review1.save() response = self.test_app.delete( '/api/v1/reviews/{}'.format(review1.id)) check = json.loads(response.data.decode(defenc())) self.assertEqual(storage.get("Review", review1.id), None) city1.delete() state1.delete() user1.delete() place1.delete()
def test_cities_all_by_state_id(self): ''' Testing function that lists all cities of a state ''' state1 = classes['State'](name="California") state1.save() state2 = classes['State'](name="New York") state2.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() city2 = classes['City'](name="Los Angeles", state_id=state1.id) city2.save() city3 = classes['City'](name="New York", state_id=state2.id) city3.save() count = len(state1.cities) response = self.test_app.get( '/api/v1/states/{}/cities'.format(state1.id)) check = json.loads(response.data.decode(defenc())) self.assertEqual(len(check), count) city1.delete() city2.delete() city3.delete() state1.delete() state2.delete()
def test_places_all_by_city(self): ''' Testing function that lists all places in a city ''' user1 = classes['User'](name="Billy", email="Nope", password="******") user1.save() state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() city2 = classes['City'](name="Los Angeles", state_id=state1.id) city2.save() place1 = classes['Place'](name="Blue Shoe", city_id=city1.id, user_id=user1.id) place1.save() place2 = classes['Place'](name="Little Ranch", city_id=city1.id, user_id=user1.id) place2.save() place3 = classes['Place'](name="Home Sweet Home", city_id=city2.id, user_id=user1.id) place3.save() count = len(city1.places) response = self.test_app.get('/api/v1/cities/{}/places'.format( city1.id)) check = json.loads(response.data.decode(defenc())) self.assertEqual(len(check), count) place1.delete() place2.delete() place3.delete() city1.delete() city2.delete() state1.delete() user1.delete()
def test_places_update_invalid_JSON(self): ''' Test place update with invalid JSON ''' user1 = classes['User'](name="Billy", email="Nope", password="******") user1.save() state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() place1 = classes['Place'](name="Blue Shoe", city_id=city1.id, user_id=user1.id) place1.save() data = '{}' response = self.test_app.put('api/v1/places/{}'.format(place1.id), data=data, content_type='application/json') check = json.loads(response.data.decode(defenc())) self.assertEqual(response.status_code, 400) place1.delete() city1.delete() user1.delete() state1.delete()
def test_places_update(self): ''' Tests place update function ''' user1 = classes['User'](name="Billy", email="Nope", password="******") user1.save() state1 = classes['State'](name="California") state1.save() city1 = classes['City'](name="San Francisco", state_id=state1.id) city1.save() place1 = classes['Place'](name="Blue Shoe", city_id=city1.id, user_id=user1.id) place1.save() data = json.dumps({"name": "Long Horn"}) response = self.test_app.put('api/v1/places/{}'.format(place1.id), data=data, content_type='application/json') check = json.loads(response.data.decode(defenc())) self.assertEqual(check['name'], 'Long Horn') place1.delete() city1.delete() user1.delete() state1.delete()