Пример #1
0
def delete(id):
    mapper = ItemMapper()
    deleted = mapper.delete(id)
    if not deleted:
        raise NotFound(description='リクエストデータはすでに削除されたか、または存在しません。')

    return ApiResponse(204, '削除しました')
Пример #2
0
def index():
    option = ItemSearchOption()
    if request.args is not None:
        option.category_id = request.args.get('category_id', type=int)
        option.q = request.args.get('q', type=str)

    mapper = ItemMapper()
    items = mapper.find(option)

    return ApiResponse(200, data={'items': items})
Пример #3
0
def _get_items(categories):
    category_ids = [c['id'] for c in categories]
    option = ItemSearchOption(category_ids=category_ids)
    mapper = ItemMapper()
    rows = mapper.find_by_category_ids(option)
    items = []
    # 登録済みカテゴリにひもづく商品がなければ、無効なカテゴリとする
    for c in categories:
        registered_items = [r for r in rows if c['id'] == r['category_id']]
        if len(registered_items) > 0:
            c['disabled'] = False
            items += registered_items
        else:
            c['disabled'] = True
    return items
Пример #4
0
def edit(id):
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest()

    item = Item(**request.json)
    if not item.is_valid():
        raise BadRequest(
            description='保存エラー。エラー内容を確認してください。',
            response=item.validation_errors
        )

    mapper = ItemMapper()
    saved = mapper.save(item)
    if not saved:
        raise Conflict()

    return ApiResponse(200, message='更新しました')
Пример #5
0
 def setUp(self):
     self.mapper = ItemMapper()
     self.db = PgAdapter()
Пример #6
0
class TestItemMapper(unittest.TestCase):
    def setUp(self):
        self.mapper = ItemMapper()
        self.db = PgAdapter()

    def tearDown(self):
        self.db = PgAdapter()
        query = """
            TRUNCATE TABLE items
            RESTART IDENTITY;
        """
        self.db.execute(query)
        self.db.commit()

    def test_add_ok(self):
        data = Item(None, 1, 'test', 500)
        result = self.mapper.save(data)
        self.assertTrue(result)

    def test_edit_ok(self):
        self.__init_data()
        item = Item(1, 1, 'Edit', 500)
        result = self.mapper.save(item)
        self.assertTrue(result)

    def test_delete_ok(self):
        self.__init_data()
        result = self.mapper.delete(1)
        self.assertTrue(result)

    def test_find_all_items(self):
        self.__init_data()
        data = ItemSearchOption(1)
        result = self.mapper.find(data)
        self.assertEqual(len(result), 3)

    def test_find_when_empty_row(self):
        data = ItemSearchOption(
            category_id=1,
            q='No data')
        result = self.mapper.find(data)
        self.assertEqual(len(result), 0)

    def test_find_when_keyword_search(self):
        self.__init_data()
        data = ItemSearchOption(
            category_id=None,
            q='パスタ'
        )
        result = self.mapper.find(data)
        self.assertEqual(len(result), 2)

    def test_find_when_coplex_search(self):
        self.__init_data()
        data = ItemSearchOption(
            category_id=3,
            q='パスタ'
        )
        result = self.mapper.find(data)
        self.assertEqual(len(result), 1)

    def test_find_by_category_ids(self):
        self.__init_data()
        data = ItemSearchOption(
            category_ids=[1, 2]
        )
        result = self.mapper.find_by_category_ids(data)
        self.assertNotEqual(0, len(result))

    def test_find_by_category_ids_when_empty_row(self):
        self.__init_data()
        data = ItemSearchOption(
            category_ids=[4]
        )
        result = self.mapper.find_by_category_ids(data)
        self.assertEqual(0, len(result))

    def test_find_by_category_ids_when_no_ids(self):
        self.__init_data()
        data = ItemSearchOption(category_ids=[])
        result = self.mapper.find_by_category_ids(data)
        self.assertEqual(0, len(result))

    def test_save_ng_when_invalid_value(self):
        with self.assertRaises(ValueError):
            self.mapper.save(None)
            self.mapper.save(1)
            self.mapper.save('test')

    def test_delete_ng_when_invalid_value(self):
        with self.assertRaises(ValueError):
            self.mapper.delete(None)
            self.mapper.delete('1')
            self.mapper.delete(0)
            self.mapper.delete(-1)

    def test_invalid_find_option(self):
        with self.assertRaises(ValueError):
            self.mapper.find(None)
            self.mapper.find('')
            self.mapper.find(1)
            self.mapper.find(True)
            self.mapper.find_by_category_ids(None)
            self.mapper.find_by_category_ids('')
            self.mapper.find_by_category_ids(1)
            self.mapper.find_by_category_ids(True)

            option = ItemSearchOption()
            option.category_ids = None
            self.mapper.find_by_category_ids(option)

    def __init_data(self):
        self.db.execute_proc('create_test_data_items')
        self.db.commit()