def test_delete_amenity_in_place(self): """test remove an amenity from a place""" amenity_args = {"name": "bear", "id": "BA"} amenity = Amenity(**amenity_args) self.place.amenities_id.append(amenity.id) storage.new(self.place) amenity.save() rv = self.app.delete('{}/places/{}/amenities/{}/'.format( self.path, self.place.id, "BA"), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNotNone(storage.get("Amenity", amenity.id)) storage.delete(amenity)
def save(self): """ Updates update_at """ from models import storage self.updated_at = datetime.today() storage.new(self) storage.save()
def test_db_storage_get(self): """Tests the get method in db_storage""" my_state = State(name="Michigan") storage.new(my_state) first_id = list(storage.all("State").values())[0].id self.assertEqual(type(storage.get("State", first_id)), State)
def update_file(self): '''Updates the storage if the dictionary changes''' storage.new(self) self.__check = self.__dict__
def test_input_2(self): """ test multiple arguments """ obj = BaseModel() with self.assertRaises(TypeError): storage.new(obj, obj)
def test_new(self): """ tests new method of file_storage """ myobj = BaseModel() # store myobj into __objects variable by <class name>.id using new() __objects = storage.new(myobj)
def test_10(self): """Checks if correct error Rises.""" with self.assertRaises(AttributeError): storage.new(None)
def test_08(self): """Checks correct implementation of save() method""" obj_dict = storage.all() bm = BaseModel() us = User() st = State() pl = Place() cy = City() am = Amenity() rv = Review() storage.new(bm) storage.new(us) storage.new(st) storage.new(pl) storage.new(cy) storage.new(am) storage.new(rv) storage.save() with open("instance.json", "r") as sf: save_text = sf.read() self.assertIn("BaseModel." + bm.id, save_text) self.assertIn("User." + us.id, save_text) self.assertIn("State." + st.id, save_text) self.assertIn("Place." + pl.id, save_text) self.assertIn("City." + cy.id, save_text) self.assertIn("Amenity." + am.id, save_text) self.assertIn("Review." + rv.id, save_text)
def save(self): """ Currently just updates the updated at time """ from models import storage self.updated_at = datetime.now() storage.new(self) storage.save()
def test_new_no_args(self): """ Test new() against no args """ self.resetStorage() with self.assertRaises(TypeError): storage.new()
def test_new_many_args(self): """ Test new() against too many args """ self.resetStorage() with self.assertRaises(TypeError): storage.new(98, 99)
def test_new(self): """ test if new works """ my_obj = BaseModel() storage.new(my_obj) storager = storage.all() self.assertIn(my_obj, storager.values())
def setUp(self): self.temp_file = '/temp_store.json' self.temp_objs = [BaseModel(), BaseModel(), BaseModel()] for obj in self.temp_objs: storage.new(obj) storage.save()
def test_get_db_storage(self): """This test the get method in db_storage""" new_state = State(name="NewYork") storage.new(new_state) first_state_id = list(storage.all("State").values())[0].id self.assertEqual(type(storage.get("State", first_state_id)), State)
def test_new(self): """ test if 'new' adds an object in __objects """ u = User() storage.new(u) self.assertTrue(FileStorage._FileStorage__objects[type(u).__name__ + "." + u.id])
#!/usr/bin/python3 """ Populates the database with necessary data """ from flask import Flask, render_template, jsonify from models.report import Report from models.news import News from models.state import State import models from models import storage first_state = storage.session.query(State).filter_by(name="Antioquia").first() new_report = Report(infections=12, state_id=first_state.id) print(new_report.to_dict()) print(first_state.to_dict()) storage.new(new_report) storage.save() print(first_state.reports)
def test_new_empty(self): ''' test for new method (error)''' with self.assertRaises(TypeError): storage.new()
def test_09(self): """Checks correct implementation of reload() method""" obj_dict = storage.all() storage.reload() bm = BaseModel() us = User() st = State() pl = Place() cy = City() am = Amenity() rv = Review() storage.new(bm) storage.new(us) storage.new(st) storage.new(pl) storage.new(cy) storage.new(am) storage.new(rv) storage.save() storage.reload() self.assertIn("BaseModel." + bm.id, obj_dict.keys()) self.assertIn("User." + us.id, obj_dict.keys()) self.assertIn("State." + st.id, obj_dict.keys()) self.assertIn("Place." + pl.id, obj_dict.keys()) self.assertIn("City." + cy.id, obj_dict.keys()) self.assertIn("Amenity." + am.id, obj_dict.keys()) self.assertIn("Review." + rv.id, obj_dict.keys())
def test_new_classes(self): ''' test for new method (valid) ''' obj = BaseModel(id='123') obj_key = 'BaseModel' + '.' + obj.id obj1 = User(id='01') obj1_key = 'User' + '.' + obj1.id obj2 = City(id='02') obj2_key = 'City' + '.' + obj2.id obj3 = Amenity(id='03') obj3_key = 'Amenity' + '.' + obj3.id obj4 = Place(id='04') obj4_key = 'Place' + '.' + obj4.id obj5 = Review(id='05') obj5_key = 'Review' + '.' + obj5.id obj6 = State(id='06') obj6_key = 'State' + '.' + obj6.id self.assertEqual(storage.all(), {}) obj.id = 123 storage.new(obj) storage.new(obj1) storage.new(obj2) storage.new(obj3) storage.new(obj4) storage.new(obj5) storage.new(obj6) self.assertEqual(obj, storage.all()[obj_key]) self.assertEqual(obj1, storage.all()[obj1_key]) self.assertEqual(obj2, storage.all()[obj2_key]) self.assertEqual(obj3, storage.all()[obj3_key]) self.assertEqual(obj4, storage.all()[obj4_key]) self.assertEqual(obj5, storage.all()[obj5_key]) self.assertEqual(obj6, storage.all()[obj6_key])
def test_14(self): """Checks if correct error Rises.""" with self.assertRaises(TypeError): storage.new(BaseModel(), 1)
def test_new(self): """tests new method adds new obj to dictionary""" m = BaseModel() storage.new(m) obj = storage.all() self.assertEqual(obj["{}.{}".format("BaseModel", m.id)], m)
def save(self): """Updates updated_at with current time when instance is changed""" from models import storage self.updated_at = datetime.now() storage.new(self) storage.save()
def test_input_1(self): """ Test no arguments """ with self.assertRaises(TypeError): storage.new()