Ejemplo n.º 1
0
    def test_crud(self):
        with self.app_context():
            store = StoreModel('test')

            self.assertIsNone(
                StoreModel.find_by_name('test'),
                "Found an store with name 'test' before save_to_db")

            store.save_to_db()

            self.assertIsNotNone(
                StoreModel.find_by_name('test'),
                "Did not find an store with name 'test' after save_to_db")

            store.delete_from_db()

            self.assertIsNone(
                StoreModel.find_by_name('test'),
                "Found an store with name 'test' after delete_from_db")
    def test_create_store(self):
        with self.app() as c:
            with self.app_context():
                r = c.post('/store/test')

                self.assertEqual(r.status_code, 201)
                self.assertIsNotNone(StoreModel.find_by_name('test'))
                self.assertDictEqual(d1={
                    'name': 'test',
                    'items': []
                },
                                     d2=json.loads(r.data))
    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'}