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_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_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_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_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 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'}