예제 #1
0
 def test_store_list_with_items(self):
     with self.app() as client:
         with self.app_context():
             StoreModel("test_store").save_to_db()
             StoreModel("test_store2").save_to_db()
             ItemModel("test_item", 19.99, 1).save_to_db()
             ItemModel("test_item2", 9.99, 2).save_to_db()
             resp = client.get("/stores")
             self.assertDictEqual(
                 {
                     "stores":
                     [{
                         "id": 1,
                         "name": "test_store",
                         "items": [{
                             "name": "test_item",
                             "price": 19.99
                         }]
                     }, {
                         "id": 2,
                         "name": "test_store2",
                         "items": [{
                             "name": "test_item2",
                             "price": 9.99
                         }]
                     }]
                 }, json.loads(resp.data))
예제 #2
0
 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")
예제 #3
0
    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")
예제 #4
0
 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"))
예제 #5
0
 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())
예제 #6
0
 def test_delete_item(self):
     with self.app() as client:
         with self.app_context():
             StoreModel("test_store").save_to_db()
             ItemModel("test_item", 9.99, 1).save_to_db()
             resp = client.delete("/item/test_item")
             self.assertDictEqual({"message": "Item deleted"},
                                  json.loads(resp.data))
예제 #7
0
 def test_create_store(self):
     with self.app() as client:
         with self.app_context():
             resp = client.post("/store/test_store")
             self.assertEqual(resp.status_code, 201)
             self.assertIsNotNone(StoreModel.find_by_name("test_store"))
             self.assertDictEqual(
                 {
                     "id": 1,
                     "name": "test_store",
                     "items": []
                 }, json.loads(resp.data))
예제 #8
0
 def test_get_item(self):
     with self.app() as client:
         with self.app_context():
             StoreModel("test_store")
             ItemModel("test_item", 9.99, 1).save_to_db()
             resp = client.get("/item/test_item",
                               headers={"Authorization": self.access_token})
             self.assertEqual(resp.status_code, 200)
             self.assertDictEqual({
                 "name": "test_item",
                 "price": 9.99
             }, json.loads(resp.data))
예제 #9
0
 def test_store_found_with_items(self):
     with self.app() as client:
         with self.app_context():
             StoreModel("test_store").save_to_db()
             ItemModel("test_item", 19.99, 1).save_to_db()
             resp = client.get("/store/test_store")
             self.assertEqual(resp.status_code, 200)
             self.assertDictEqual(
                 {
                     "id": 1,
                     "name": "test_store",
                     "items": [{
                         "name": "test_item",
                         "price": 19.99
                     }]
                 }, json.loads(resp.data))
예제 #10
0
    def test_crud(self):
        with self.app_context():
            StoreModel("test").save_to_db()
            item = ItemModel('test', 19.99, 1)

            self.assertIsNone(
                ItemModel.find_by_name('test'),
                "Found an item with name {}, but expected not to.".format(
                    item.name))

            item.save_to_db()

            self.assertIsNotNone(ItemModel.find_by_name('test'))

            item.delete_from_db()

            self.assertIsNone(ItemModel.find_by_name('test'))
예제 #11
0
    def test_find_store(self):
        with self.app() as client:
            with self.app_context():
                resp = client.get("/store/test_store")
                self.assertEqual(resp.status_code, 404)
                self.assertDictEqual({"message": "Store not found"},
                                     json.loads(resp.data))

                client.post("/store/test_store")

                resp = client.get("/store/test_store")
                self.assertEqual(resp.status_code, 200)
                self.assertDictEqual(
                    {
                        "id": 1,
                        "name": "test_store",
                        "items": []
                    }, json.loads(resp.data))
                self.assertIsNotNone(StoreModel.find_by_name("test_store"))
예제 #12
0
    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
예제 #13
0
 def get(self, name):
     store = StoreModel.find_by_name(name)
     if store:
         return store.json(), 200
     return {'message': 'Store not found'}, 404
예제 #14
0
 def test_store_json(self):
     store = StoreModel("test_store")
     expected = {"id": None, "name": "test_store", "items": []}
     self.assertDictEqual(expected, store.json())
예제 #15
0
 def test_create_store_items_empty(self):
     store = StoreModel("test_store")
     self.assertEqual(
         store.items.all(), [],
         "The store's items list was not empty even though no items were added"
     )
예제 #16
0
    def delete(self, name):
        store = StoreModel.find_by_name(name)
        if store:
            store.delete_from_db()

        return {'message': 'Store deleted'}, 200
예제 #17
0
 def test_create_store(self):
     store = StoreModel("test_store")
     self.assertEqual(
         store.name, "test_store",
         "The name of the store after creation does not equal the constructor argument."
     )