コード例 #1
0
ファイル: test_item.py プロジェクト: lciamp/flask-test
    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')
コード例 #2
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
 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()]})
コード例 #3
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
    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()]})
コード例 #4
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
    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)
コード例 #5
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
    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)
コード例 #6
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
    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)
コード例 #7
0
    def on_delete(self, req, resp, name):

        store = StoreModel.find_by_name(name)
        if store:
            store.delete_from_db()
        resp.body = json.dumps({"message": f"Store {name} deleted"},
                               ensure_ascii=False)
        resp.status = falcon.HTTP_200
コード例 #8
0
    def on_get(self, req, resp, name):

        store = StoreModel.find_by_name(name)
        if store:
            resp.body = json.dumps(store.json(), ensure_ascii=False)
            resp.status = falcon.HTTP_200
        else:
            resp.body = json.dumps({"message": "Store not found"},
                                   ensure_ascii=False)
            resp.status = falcon.HTTP_404
コード例 #9
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
    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))
コード例 #10
0
ファイル: test_item.py プロジェクト: lciamp/flask-test
    def test_crud(self):
        StoreModel('Test Store').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'),
            "Did not find an item with name '{}', but expected to.".format(
                item.name))

        item.delete_from_db()

        self.assertIsNone(
            ItemModel.find_by_name('Test'),
            "Found an item with name '{}', but expected not to.".format(
                item.name))
コード例 #11
0
    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
コード例 #12
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
 def test_create_store(self):
     response = self.client.post('/store/Test')
     self.assertEqual(response.status_code, 201)
     self.assertDictEqual(json.loads(response.data), StoreModel('Test').json())
コード例 #13
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
 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())
コード例 #14
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
 def test_create_duplicate_store(self):
     StoreModel('Test').save_to_db()
     response = self.client.post('/store/Test')
     self.assertEqual(response.status_code, 400)
     expected = {'message': "A store with name 'Test' already exists."}
     self.assertDictEqual(expected, json.loads(response.data))
コード例 #15
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
    def test_create_store_items_empty(self):
        store = StoreModel('Test Store')

        self.assertListEqual(
            store.items.all(), [],
            "Store items length was not zero even though no items were added.")
コード例 #16
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
 def test_create_store(self):
     store = StoreModel('Test Name')
     self.assertIsNotNone(store)
     self.assertEqual(store.name, 'Test Name')
コード例 #17
0
ファイル: test_store.py プロジェクト: lciamp/flask-test
 def test_delete_store(self):
     StoreModel('Test').save_to_db()
     response = self.client.delete('/store/Test')
     self.assertEqual(response.status_code, 200)
     expected = {'message': 'Store deleted'}
     self.assertDictEqual(json.loads(response.data), expected)