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

    return ApiResponse(204, '削除しました')
Esempio n. 2
0
def _get_categories():
    option = CategorySearchOption()
    mapper = CategoryMapper()
    rows = mapper.find(option)
    # 登録件数が10件未満の場合は、空データで埋め合わせ
    unregist_count = CategoryMapper.MAX_ADDABLE_ROW - len(rows)
    for i in range(0, unregist_count):
        rows.append({'id': None, 'name': ''})
    return rows
Esempio n. 3
0
def index():
    option = CategorySearchOption()
    if request.args is not None:
        option = CategorySearchOption(
            q=request.args.get('q', type=str)
        )

    mapper = CategoryMapper()
    categories = mapper.find(option)

    return ApiResponse(200, data={'categories': categories})
Esempio n. 4
0
def edit(id):
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest()

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

    mapper = CategoryMapper()
    saved = mapper.save(category)
    if not saved:
        raise Conflict()

    return ApiResponse(200, message='更新しました')
Esempio n. 5
0
def add():
    request_data = request.get_json()
    if request_data is None:
        raise BadRequest()

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

    mapper = CategoryMapper()
    if mapper.is_upper_limit():
        raise BadRequest(description='商品カテゴリの登録数は10件までです')

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

    return ApiResponse(201, message='保存しました')
Esempio n. 6
0
 def setUp(self):
     self.mapper = CategoryMapper()
     self.db = PgAdapter()
Esempio n. 7
0
class TestCategoryMapper(unittest.TestCase):
    def setUp(self):
        self.mapper = CategoryMapper()
        self.db = PgAdapter()

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

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

    def test_edit_ok(self):
        self.__init_data()
        data = Category(1, 'Morning set')
        result = self.mapper.save(data)
        self.assertTrue(result)

    def test_find_all_categories(self):
        self.__init_data()
        data = CategorySearchOption()
        result = self.mapper.find(data)
        self.assertEqual(len(result), 10)

    def test_find_keyword_search(self):
        self.__init_data()
        data = CategorySearchOption(q='セット')
        result = self.mapper.find(data)
        self.assertEqual(len(result), 2)

    def test_find_empty_rows(self):
        self.__init_data()
        data = CategorySearchOption(q='hoge')
        result = self.mapper.find(data)
        self.assertEqual(len(result), 0)

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

    def test_is_upper_limit(self):
        self.__init_data()
        result = self.mapper.is_upper_limit()
        self.assertTrue(result)

    def test_less_than_limit(self):
        self.__init_data()
        self.mapper.delete(1)
        result = self.mapper.is_upper_limit()
        self.assertLess(result, 10)

    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 __init_data(self):
        self.db.execute_proc('create_test_data_categories')
        self.db.commit()