def test_store_relationship(self): 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_list_with_items(self): store = StoreModel('Test') store.save_to_db() ItemModel('Test Item', 19.99, 1).save_to_db() response = self.client.get('/stores') self.assertEqual(response.status_code, 200) self.assertDictEqual(json.loads(response.data), {'stores': [store.json()]})
def test_item_relationship(self): store = StoreModel('Test Store') item = ItemModel('Test', 19.99, 1) store.save_to_db() item.save_to_db() self.assertEqual(store.items.count(), 1) self.assertEqual(store.items.first().name, item.name)
def test_store_list(self): store1 = StoreModel('Test1') store2 = StoreModel('Test2') store1.save_to_db() store2.save_to_db() response = self.client.get('/stores') self.assertEqual(response.status_code, 200) self.assertDictEqual(json.loads(response.data), {'stores': [store1.json(), store2.json()]})
def test_store_json_with_item(self): store = StoreModel('Test Store') item = ItemModel('Test', 19.99, 1) store.save_to_db() item.save_to_db() expected = { 'name': 'Test Store', 'items': [{ 'name': 'Test', 'price': 19.99 }] } self.assertEqual(store.json(), expected)
def test_store_found_with_items(self): item = ItemModel('Test Item', 19.99,1 ) store = StoreModel('Test') store.save_to_db() item.save_to_db() response = self.client.get('store/Test') self.assertEqual(response.status_code, 200) expected = { 'name': 'Test', 'items': [ {'name': 'Test Item', 'price': 19.99} ] } self.assertDictEqual(json.loads(response.data), expected)
def test_crud(self): store = StoreModel('Test') self.assertIsNone( store.find_by_name('Test'), "Found a store with name '{}', but expected not to.".format( store.name)) store.save_to_db() self.assertIsNotNone( store.find_by_name('Test'), "Did not find a store with name '{}', but expected to.".format( store.name)) store.delete_from_db() self.assertIsNone( store.find_by_name('Test'), "Found a store with name '{}', but expected not to.".format( store.name))
def on_post(self, req, resp, name): if StoreModel.validate_name(name): msg = "Invalid Request" raise falcon.HTTPBadRequest("Bad Request", msg) elif StoreModel.find_by_name(name): message = f"A store with name {name} already exists." resp.body = json.dumps({"message": message}, ensure_ascii=False) resp.status = falcon.HTTP_400 # Bad Request else: data = req.context.get("json") store = StoreModel(name) try: store.save_to_db() except Exception as e: msg = "An error occured inserting the store" raise falcon.HTTPInternalServerError("Internal Server Error", msg) else: resp.body = json.dumps({"message": "Store added successfully"}, ensure_ascii=False) resp.status = falcon.HTTP_201
def test_find_store(self): store = StoreModel('Test') store.save_to_db() response = self.client.get('/store/Test') self.assertEqual(response.status_code, 200) self.assertDictEqual(json.loads(response.data), store.json())