def test_store_json(self): store = StoreModel('test') expected = {'name': 'test', 'items': []} self.assertEqual( store.json(), expected, "The JSON export of the store is incorrect. Received {}, expected {}." .format(store.json(), expected))
def test_store_relationship(self): with self.app_context(): store = StoreModel('test_store') item = ItemModel('test', 19.99, 1) store.save_to_db() item.save_to_db() self.assertEqual(item.store.name, 'test_store')
def test_store_relationship(self): with self.app_context(): store = StoreModel('test') item = ItemModel('test_item', 19.99, 1) store.save_to_db() item.save_to_db() self.assertEqual(store.items.count(), 1) self.assertEqual(store.items.first().name, 'test_item')
def test_create_duplicate_item(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() c.post('/item/test', data={'price': 17.99, 'store_id': 1}) r = c.post('/item/test', data={'price': 17.99, 'store_id': 1}) self.assertEqual(r.status_code, 400)
def test_put_update_item(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() c.put('/item/test', data={'price': 17.99, 'store_id': 1}) r = c.put('/item/test', data={'price': 18.99, 'store_id': 1}) self.assertEqual(r.status_code, 200) self.assertEqual(ItemModel.find_by_name('test').price, 18.99)
def test_delete_store(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() r = c.delete('/store/test') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'message': 'Store deleted'}, d2=json.loads(r.data))
def test_delete_item(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 17.99, 1).save_to_db() r = c.delete('/item/test') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'message': 'Item deleted'}, d2=json.loads(r.data))
def test_create_store(self): store = StoreModel('test') self.assertEqual( store.name, 'test', "The name of the store after creation does not equal the constructor argument." ) self.assertListEqual( store.items.all(), [], "The store's items length was not 0 even though no items were added." )
def test_store_list(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() r = c.get('/stores') self.assertDictEqual( d1={'stores': [{ 'name': 'test', 'items': [] }]}, d2=json.loads(r.data))
def test_create_store(self): with self.app() as c: with self.app_context(): r = c.post('/store/test') self.assertEqual(r.status_code, 201) self.assertIsNotNone(StoreModel.find_by_name('test')) self.assertDictEqual(d1={ 'name': 'test', 'items': [] }, d2=json.loads(r.data))
def test_store_found(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() r = c.get('/store/test') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={ 'name': 'test', 'items': [] }, d2=json.loads(r.data))
def test_crud(self): with self.app_context(): store = StoreModel('test') store.save_to_db() item = ItemModel('test', 19.99, 1) self.assertIsNone( ItemModel.find_by_name('test'), "Found an item with name 'test' before save_to_db") item.save_to_db() self.assertIsNotNone( ItemModel.find_by_name('test'), "Did not find an item with name 'test' after save_to_db") item.delete_from_db() self.assertIsNone( ItemModel.find_by_name('test'), "Found an item with name 'test' after delete_from_db")
def test_put_item(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() r = c.put('/item/test', data={'price': 17.99, 'store_id': 1}) self.assertEqual(r.status_code, 200) self.assertEqual(ItemModel.find_by_name('test').price, 17.99) self.assertDictEqual(d1={ 'name': 'test', 'price': 17.99 }, d2=json.loads(r.data))
def test_item_list(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 17.99, 1).save_to_db() r = c.get('/items') self.assertDictEqual( d1={'items': [{ 'name': 'test', 'price': 17.99 }]}, d2=json.loads(r.data))
def test_item_found(self): with self.app() as c: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 17.99, 1).save_to_db() r = c.get('/item/test', headers={'Authorization': self.auth_header}) self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={ 'name': 'test', 'price': 17.99 }, d2=json.loads(r.data))
def post(self, name): if StoreModel.find_by_name(name): return { 'message': "A store with name '{}' already exists.".format(name) }, 400 store = StoreModel(name) try: store.save_to_db() except: return {"message": "An error occurred creating the store."}, 500 return store.json(), 201
def get(self, name): store = StoreModel.find_by_name(name) if store: return store.json() return {'message': 'Store not found'}, 404
def delete(self, name): store = StoreModel.find_by_name(name) if store: store.delete_from_db() return {'message': 'Store deleted'}