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_store(self): store = StoreModel('test') self.assertEqual( store.name, 'test', "The name of the store after creation does not equal the constructor argument." )
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 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'}
def test_create_store(self): store = StoreModel('test') self.assertListEqual( store.items.all(), [], "The store's items length was not 0 even though no items were added." )