def test_create(self): current_count = Dummy.query.count() create_data = { 'email': faker.email(), 'name': faker.name(), 'title': faker.sentence(), 'is_awesome': True, } res = self.simulate_post('/dummies/', body=json.dumps(create_data), query_string='auth_key=superpassword') self.assertEqual(res.status, falcon.HTTP_201) item = res.json self.assertEqual(5, len(item)) self.assertIsInstance(item['uuid'], str) self.assertEqual(len(str(uuid.uuid4())), len(item['uuid'])) self.assertIsInstance(item['email'], str) self.assertIn('@', item['email']) self.assertIsInstance(item['title'], str) self.assertIsInstance(item['is_awesome'], bool) self.assertTrue(item['is_awesome']) self.assertTrue(Dummy.query.count(), current_count)
def test_fail_method_not_allowed(self): self.viewset.allowed_actions = ('list', ) item = Dummy.query.first() update_data = {'email': faker.email()} res = self.simulate_patch('/dummies/{}/'.format(item.uuid), body=json.dumps(update_data), query_string='auth_key=superpassword') self.assertEqual(res.status, falcon.HTTP_405)
def test_update(self): item = Dummy.query.first() update_data = {'email': faker.email()} res = self.simulate_patch('/dummies/{}/'.format(item.uuid), body=json.dumps(update_data), query_string='auth_key=superpassword') self.assertEqual(res.status, falcon.HTTP_204) item = Dummy.query.filter_by(uuid=item.uuid).first() self.assertEqual(item.email, update_data['email'])
def setUp(self): global ITEMS super().setUp() self.api = falcon.API() self.viewset = DummyViewSet() self.api.add_route('/dummies/', self.viewset) self.api.add_route('/dummies/{uuid}/', self.viewset) ITEMS = [] serializer = self.viewset.serializer # add 3 test objects for i in range(3): data = { 'name': faker.name(), 'email': faker.email(), 'title': faker.sentence(nb_words=5), 'is_awesome': i % 2 == 0, } obj = serializer.save(data=data) ITEMS.append(obj)
def setUp(self): super().setUp() db.init('sqlite://', echo=False) Dummy.metadata.create_all(db.engine) self.api = falcon.API() self.viewset = DummyViewSet() self.api.add_route('/dummies/', self.viewset) self.api.add_route('/dummies/{uuid}/', self.viewset) serializer = self.viewset.serializer # add 3 test objects for i in range(3): data = { 'name': faker.name(), 'email': faker.email(), 'title': faker.sentence(nb_words=5), 'is_awesome': i % 2 == 0, } obj = serializer.save(data=data) db.session.add(obj) db.session.commit()