def test_delete(self): """Test for deleting an instance of the database using the :http:method:`delete` method. """ # Creating the person who's gonna be deleted response = self.app.post('/api/person', data=dumps({'name': u'Lincoln', 'age': 23})) self.assertEqual(response.status_code, 201) self.assertIn('id', loads(response.data)) # Making sure it has been created deep = {'computers': []} person = self.session.query(self.Person).filter_by(id=1).first() inst = _to_dict(person, deep) response = self.app.get('/api/person/1') self.assertEqual(loads(response.data), inst) # Deleting it response = self.app.delete('/api/person/1') self.assertEqual(response.status_code, 204) # Making sure it has been deleted people = self.session.query(self.Person).filter_by(id=1) self.assertEquals(people.count(), 0)
def test_post(self): """Test for creating a new instance of the database model using the :http:method:`post` method. """ # Invalid JSON in request data should respond with error. response = self.app.post('/api/person', data='Invalid JSON string') self.assertEqual(response.status_code, 400) self.assertEqual( loads(response.data)['message'], 'Unable to decode data') # Now, let's test the validation stuff # response = self.app.post('/api/person', data=dumps({'name': u'Test', # 'age': 'oi'})) # assert loads(response.data)['message'] == 'Validation error' # assert loads(response.data)['error_list'].keys() == ['age'] response = self.app.post('/api/person', data=dumps({ 'name': u'Lincoln', 'age': 23 })) self.assertEqual(response.status_code, 201) self.assertIn('id', loads(response.data)) response = self.app.get('/api/person/1') self.assertEqual(response.status_code, 200) deep = {'computers': []} person = self.session.query(self.Person).filter_by(id=1).first() inst = _to_dict(person, deep) self.assertEqual(loads(response.data), inst)
def test_delete(self): """Test for deleting an instance of the database using the :http:method:`delete` method. """ # Creating the person who's gonna be deleted response = self.app.post('/api/person', data=dumps({ 'name': u'Lincoln', 'age': 23 })) self.assertEqual(response.status_code, 201) self.assertIn('id', loads(response.data)) # Making sure it has been created deep = {'computers': []} person = self.session.query(self.Person).filter_by(id=1).first() inst = _to_dict(person, deep) response = self.app.get('/api/person/1') self.assertEqual(loads(response.data), inst) # Deleting it response = self.app.delete('/api/person/1') self.assertEqual(response.status_code, 204) # Making sure it has been deleted people = self.session.query(self.Person).filter_by(id=1) self.assertEquals(people.count(), 0)
def test_post(self): """Test for creating a new instance of the database model using the :http:method:`post` method. """ # Invalid JSON in request data should respond with error. response = self.app.post('/api/person', data='Invalid JSON string') self.assertEqual(response.status_code, 400) self.assertEqual(loads(response.data)['message'], 'Unable to decode data') # Now, let's test the validation stuff # response = self.app.post('/api/person', data=dumps({'name': u'Test', # 'age': 'oi'})) # assert loads(response.data)['message'] == 'Validation error' # assert loads(response.data)['error_list'].keys() == ['age'] response = self.app.post('/api/person', data=dumps({'name': 'Lincoln', 'age': 23})) self.assertEqual(response.status_code, 201) self.assertIn('id', loads(response.data)) response = self.app.get('/api/person/1') self.assertEqual(response.status_code, 200) deep = {'computers': []} inst = _to_dict(_get_by(self.db.session, self.Person, id=1), deep) self.assertEqual(loads(response.data), inst)
def test_post(self): """Test for creating a new instance of the database model using the :http:method:`post` method. """ # Invalid JSON in request data should respond with error. response = self.app.post("/api/person", data="Invalid JSON string") self.assertEqual(response.status_code, 400) self.assertEqual(loads(response.data)["message"], "Unable to decode data") # Now, let's test the validation stuff # response = self.app.post('/api/person', data=dumps({'name': u'Test', # 'age': 'oi'})) # assert loads(response.data)['message'] == 'Validation error' # assert loads(response.data)['error_list'].keys() == ['age'] response = self.app.post("/api/person", data=dumps({"name": u"Lincoln", "age": 23})) self.assertEqual(response.status_code, 201) self.assertIn("id", loads(response.data)) response = self.app.get("/api/person/1") self.assertEqual(response.status_code, 200) deep = {"computers": []} person = self.session.query(self.Person).filter_by(id=1).first() inst = _to_dict(person, deep) self.assertEqual(loads(response.data), inst)
def test_datetime_serialization(self): """Tests that datetime objects in the database are correctly serialized in the :meth:`flask_restless.model.Entity.to_dict` method. """ computer = self.Computer(buy_date=datetime.now()) self.session.commit() d = _to_dict(computer) self.assertIn("buy_date", d) self.assertEqual(d["buy_date"], computer.buy_date.isoformat())
def test_date_serialization(self): """Tests that date objects in the database are correctly serialized in the :meth:`flask_restless.model.Entity.to_dict` method. """ person = self.Person(birth_date=date(1986, 9, 15)) self.session.commit() d = _to_dict(person) self.assertIn("birth_date", d) self.assertEqual(d["birth_date"], person.birth_date.isoformat())
def test_date_serialization(self): """Tests that date objects in the database are correctly serialized in the :meth:`flask_restless.model.Entity.to_dict` method. """ person = self.Person(birth_date=date(1986, 9, 15)) self.session.commit() d = _to_dict(person) self.assertIn('birth_date', d) self.assertEqual(d['birth_date'], person.birth_date.isoformat())
def test_datetime_serialization(self): """Tests that datetime objects in the database are correctly serialized in the :meth:`flask_restless.model.Entity.to_dict` method. """ computer = self.Computer(buy_date=datetime.now()) self.session.commit() d = _to_dict(computer) self.assertIn('buy_date', d) self.assertEqual(d['buy_date'], computer.buy_date.isoformat())
def test_to_dict_dynamic_relation(self): """Tests that a dynamically queried relation is resolved when getting the dictionary representation of an instance of a model. """ person = self.LazyPerson(name="Lincoln") self.session.add(person) computer = self.LazyComputer(name="lixeiro") self.session.add(computer) person.computers.append(computer) self.session.commit() person_dict = _to_dict(person, deep={"computers": []}) computer_dict = _to_dict(computer, deep={"owner": None}) self.assertEqual(sorted(person_dict), ["computers", "id", "name"]) self.assertFalse(isinstance(computer_dict["owner"], list)) self.assertEqual(sorted(computer_dict), ["id", "name", "owner", "ownerid"]) expected_person = _to_dict(person) expected_computer = _to_dict(computer) self.assertEqual(person_dict["computers"], [expected_computer]) self.assertEqual(computer_dict["owner"], expected_person)
def test_to_dict_dynamic_relation(self): """Tests that a dynamically queried relation is resolved when getting the dictionary representation of an instance of a model. """ person = self.LazyPerson(name='Lincoln') self.session.add(person) computer = self.LazyComputer(name='lixeiro') self.session.add(computer) person.computers.append(computer) self.session.commit() person_dict = _to_dict(person, deep={'computers': []}) computer_dict = _to_dict(computer, deep={'owner': None}) self.assertEqual(sorted(person_dict), ['computers', 'id', 'name']) self.assertEqual(sorted(computer_dict), ['id', 'name', 'owner', 'ownerid']) expected_person = _to_dict(person) expected_computer = _to_dict(computer) self.assertEqual(person_dict['computers'], [expected_computer]) self.assertEqual(computer_dict['owner'], expected_person)
def test_to_dict_dynamic_relation(self): """Tests that a dynamically queried relation is resolved when getting the dictionary representation of an instance of a model. """ person = self.LazyPerson(name='Lincoln') self.session.add(person) computer = self.LazyComputer(name='lixeiro') self.session.add(computer) person.computers.append(computer) self.session.commit() person_dict = _to_dict(person, deep={'computers': []}) computer_dict = _to_dict(computer, deep={'owner': None}) self.assertEqual(sorted(person_dict), ['computers', 'id', 'name']) self.assertEqual(sorted(computer_dict), ['id', 'name', 'owner', 'ownerid']) expected_person = _to_dict(person) expected_computer = _to_dict(computer) self.assertEqual(person_dict['computers'], [expected_computer]) self.assertEqual(computer_dict['owner'], [expected_person])
def test_to_dict(self): """Test for serializing attributes of an instance of the model by the :meth:`flask_restless.model.Entity.to_dict` method. """ me = self.Person(name=u'Lincoln', age=24, birth_date=date(1986, 9, 15)) self.session.commit() me_dict = _to_dict(me) expectedfields = sorted(['birth_date', 'age', 'id', 'name', 'other']) self.assertEqual(sorted(me_dict), expectedfields) self.assertEqual(me_dict['name'], u'Lincoln') self.assertEqual(me_dict['age'], 24) self.assertEqual(me_dict['birth_date'], me.birth_date.isoformat())
def test_to_dict(self): """Test for serializing attributes of an instance of the model by the :meth:`flask_restless.model.Entity.to_dict` method. """ me = self.Person(name=u"Lincoln", age=24, birth_date=date(1986, 9, 15)) self.session.commit() me_dict = _to_dict(me) expectedfields = sorted(["birth_date", "age", "id", "name", "other"]) self.assertEqual(sorted(me_dict), expectedfields) self.assertEqual(me_dict["name"], u"Lincoln") self.assertEqual(me_dict["age"], 24) self.assertEqual(me_dict["birth_date"], me.birth_date.isoformat())
def serialize(self, exclude_columns=None, exclude_relations=None, include_columns=None, include_relations=None): relations = frozenset(_get_relations(self)) # do not follow relations that will not be included in the response if include_columns is not None: cols = frozenset(include_columns) rels = frozenset(include_relations) relations &= (cols | rels) elif exclude_columns is not None: relations -= frozenset(exclude_columns) deep = dict((r, {}) for r in relations) # FIXME: необходимо реализавать фильтрацию по атрибутам и связям # result = _to_dict(instance=self, deep=deep, exclude=exclude_columns, include=include_columns, exclude_relations=exclude_relations, include_relations=include_relations) result = _to_dict(instance=self, deep=deep) return result
def test_to_dict_deep(self): """Tests that fields corresponding to related model instances are correctly serialized by the :meth:`flask_restless.model.Entity.to_dict` method. """ now = datetime.now() someone = self.Person(name=u"John", age=25) computer = self.Computer(name=u"lixeiro", vendor=u"Lemote", buy_date=now) someone.computers.append(computer) self.session.commit() deep = {"computers": []} computers = _to_dict(someone, deep)["computers"] self.assertEqual(len(computers), 1) self.assertEqual(computers[0]["name"], u"lixeiro") self.assertEqual(computers[0]["vendor"], u"Lemote") self.assertEqual(computers[0]["buy_date"], now.isoformat()) self.assertEqual(computers[0]["owner_id"], someone.id)
def test_to_dict_deep(self): """Tests that fields corresponding to related model instances are correctly serialized by the :meth:`flask_restless.model.Entity.to_dict` method. """ now = datetime.now() someone = self.Person(name=u'John', age=25) computer = self.Computer(name=u'lixeiro', vendor=u'Lemote', buy_date=now) someone.computers.append(computer) self.session.commit() deep = {'computers': []} computers = _to_dict(someone, deep)['computers'] self.assertEqual(len(computers), 1) self.assertEqual(computers[0]['name'], u'lixeiro') self.assertEqual(computers[0]['vendor'], u'Lemote') self.assertEqual(computers[0]['buy_date'], now.isoformat()) self.assertEqual(computers[0]['owner_id'], someone.id)
def test_delete(self): """Test for deleting an instance of the database using the :http:method:`delete` method. """ # Creating the person who's gonna be deleted response = self.app.post('/api/person', data=dumps({'name': 'Lincoln', 'age': 23})) self.assertEqual(response.status_code, 201) self.assertIn('id', loads(response.data)) # Making sure it has been created deep = {'computers': []} inst = _to_dict(_get_by(self.db.session, self.Person, id=1), deep) response = self.app.get('/api/person/1') self.assertEqual(loads(response.data), inst) # Deleting it response = self.app.delete('/api/person/1') self.assertEqual(response.status_code, 204) # Making sure it has been deleted self.assertIsNone(_get_by(self.db.session, self.Person, id=1))