def test_crud(self): with self.app_context(): store = StoreModel("test_store") self.assertIsNone(StoreModel.find_by_name("test_store")) store.save_to_db() self.assertIsNotNone(StoreModel.find_by_name("test_store")) store.delete_from_db() self.assertIsNone(StoreModel.find_by_name("test_store"))
def test_store_item_relationship(self): with self.app_context(): store = StoreModel("test_store") item = ItemModel("test_item", 12.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_item_store_relationship(self): with self.app_context(): store = StoreModel("test_store") item = ItemModel("test_item", 12.99, 1) store.save_to_db() item.save_to_db() # check if the item indeed belongs to the particular store self.assertEqual(item.store.name, "test_store")
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 test_store_json_with_item(self): with self.app_context(): store = StoreModel("test_store") item = ItemModel("test_item", 12.99, 1) store.save_to_db() item.save_to_db() expected = { "id": 1, "name": "test_store", "items": [{ "name": "test_item", "price": 12.99 }] } self.assertDictEqual(expected, store.json())