def index(): request_query = request.args if request_query is None: raise BadRequest() sales_date = format_date(request_query.get('sales_date', type=str)) start_time = None if request_query.get('time_from', type=str): start_time = format_time(request_query.get('time_from', type=str)) end_time = None if request_query.get('time_to', type=str): end_time = format_time(request_query.get('time_to', type=str)) option = DailySalesSearchOption(sales_date=sales_date, start_time=start_time, end_time=end_time) mapper = SalesMapper() daily_sales = mapper.find_daily_sales(option) if len(daily_sales) > 0: sales_ids = [s['id'] for s in daily_sales] item_mapper = SalesItemMapper() sales_items = item_mapper.find_daily_sales_items(sales_ids) for s in daily_sales: items = [] for item in sales_items: if item['sales_id'] == s['id']: items.append(item) s['items'] = items return ApiResponse(200, data={'daily_sales': daily_sales})
def add(): request_data = request.get_json() if request_data is None: raise BadRequest() if request_data['items'] is None or len(request_data['items']) < 1: raise BadRequest(description='売上明細データがセットされていません') items = [] for i, item in enumerate(request_data['items'], 1): items.append(SalesItem(item_no=i, **item)) del request.json['items'] now_date = datetime.now().date() now_time = datetime.now().time() sales = Sales(None, sales_date=now_date, sales_time=now_time, items=items, **request.json) if not sales.is_valid(): raise BadRequest(description='保存エラー。エラー内容を確認してください。', response=sales.validation_errors) mapper = SalesMapper() saved = mapper.add(sales) if not saved: raise Conflict() return ApiResponse(201, message='保存しました')
def __get_monthly_sales(year, month): # 月末日取得 _, last = monthrange(year, month) option = MonthlySalesSearchOption( sales_date_from=datetime(year, month, 1).date(), sales_date_to=datetime(year, month, last).date() ) mapper = SalesMapper() return mapper.find_monthly_sales(year, month, option)
def cancel(): request_data = request.get_json() if request_data is None: raise BadRequest(description='売上IDをセットしてください') # todo: 売上データ存在チェック # if sales_data is None: # raise NotFound(description='取消対象の売上データが見つかりませんでした') mapper = SalesMapper() canceled = mapper.cancel(request_data['params']['sales_id']) if not canceled: raise Conflict() return ApiResponse(201, message='売上キャンセルしました')
def setUp(self): self.mapper = SalesMapper() self.today = datetime.today() self.now_time = datetime.now().time()
class TestMonthlySales(TestCase): def setUp(self): self.mapper = SalesMapper() self.today = datetime.today() self.now_time = datetime.now().time() def tearDown(self): db = PgAdapter() query = """ TRUNCATE TABLE sales RESTART IDENTITY; """ db.execute(query) db.commit() # def test_find_monthly_sales(self): # data = [[self.today, self.now_time, 100, 0, 0, 0, 0, 100] for i in range(0, 3)] # self.__init_data(data) # year = self.today.year # month = self.today.month # result = self.mapper.find_monthly_sales(year, month) # self.assertNotEqual(len(result), 0) # self.assertEqual(result[1].proceeds, 300) # def test_find_monthly_sales_when_include_discount_price(self): # data = ( # [self.today, self.now_time, 500, 100, 0, 0, 0, 500], # [self.today, self.now_time, 800, 0, 0, 0, 0, 1000], # [self.today, self.now_time, 450, 50, 0, 0, 0, 500]) # expected = 0 # for d in data: # expected += (d[2] - d[3]) # self.__init_data(data) # year = self.today.year # month = self.today.month # result = self.mapper.find_monthly_sales(year, month) # self.assertNotEqual(len(result), 0) # self.assertEqual(result[0].proceeds, expected) # def test_find_monthly_sales_when_include_discount_rate(self): # data = ( # [self.today, self.now_time, 500, 0, 10, 0, 0, 500], # [self.today, self.now_time, 800, 0, 15, 0, 0, 1000], # [self.today, self.now_time, 450, 0, 0, 0, 0, 500]) # expected = 0 # for d in data: # expected += (d[2] * (2 - d[4] / 100)) # self.__init_data(data) # year = self.today.year # month = self.today.month # result = self.mapper.find_monthly_sales(year, month) # self.assertNotEqual(len(result), 0) # self.assertEqual(result[0].proceeds, expected) def test_find_monthly_sales_when_invalid_arguments(self): year = self.today.year month = self.today.month with self.assertRaises(ValueError): self.mapper.find_monthly_sales(None, month) self.mapper.find_monthly_sales(year, None) self.mapper.find_monthly_sales('2000', month) self.mapper.find_monthly_sales(year, '999') self.mapper.find_monthly_sales(year, 0) self.mapper.find_monthly_sales(year, 13) def __init_data(self, data): query = """ INSERT INTO sales ( sales_date, sales_time, total_price, discount_price, discount_rate, inclusive_tax, exclusive_tax, deposit ) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s ); """ db = PgAdapter() db.bulk_insert(query, data) db.commit()
class TestSalesMapper(unittest.TestCase): def setUp(self): self.mapper = SalesMapper() self.db = PgAdapter() def tearDown(self): self.db.execute_proc('cleanup_sales') self.db.commit() def test_add_ok(self): items = self.__get_sales_items() today = datetime.now().date() now_time = datetime.now().time() sales = Sales(None, today, now_time, 100, 0, 0, 0, 0, 100, items) result = self.mapper.add(sales) self.assertTrue(result) def test_cancel_ok(self): items = self.__get_sales_items() today = datetime.now().date() now_time = datetime.now().time() sales = Sales(None, today, now_time, 100, 0, 0, 0, 0, 100, items) self.mapper.add(sales) result = self.mapper.cancel(1) self.assertTrue(result) def test_add_ng_when_ivalid_argment(self): with self.assertRaises(ValueError): self.mapper.add(None) self.mapper.add('a') self.mapper.add(1) self.mapper.add(True) def test_cancel_ng_when_invalid_argment(self): with self.assertRaises(ValueError): self.mapper.cancel(None) self.mapper.cancel('a') self.mapper.cancel(1) self.mapper.cancel(True) self.mapper.cancel(0) def __get_sales_items(self): items = [] for i in range(1, 11): item = SalesItem(None, None, i, 'test{0}'.format(i), 100, 1, 100) items.append(item) return items
def setUp(self): self.mapper = SalesMapper() self.db = PgAdapter()
def setUp(self): self.mapper = SalesMapper() self.db = PgAdapter() self.today = datetime.now()
class TestDailySales(TestCase): def setUp(self): self.mapper = SalesMapper() self.db = PgAdapter() self.today = datetime.now() def tearDown(self): self.db.execute_proc('cleanup_sales') self.db.commit() def test_find_daily_sales(self): self.__init_data() option = DailySalesSearchOption(sales_date=self.today.date(), start_time=self.__get_time('09:00:00'), end_time=self.__get_time('21:00:00')) result = self.mapper.find_daily_sales(option) self.assertNotEqual(0, len(result)) def test_find_daily_sales_when_set_start_time(self): self.__init_data() option = DailySalesSearchOption(sales_date=self.today.date(), start_time=self.__get_time('12:00:00'), end_time=None) result = self.mapper.find_daily_sales(option) self.assertEquals(1, len(result)) def test_find_daily_sales_when_set_end_time(self): self.__init_data() option = DailySalesSearchOption(sales_date=self.today.date(), start_time=None, end_time=self.__get_time('10:00:00')) result = self.mapper.find_daily_sales(option) self.assertEquals(2, len(result)) def test_find_daily_sales_when_empty_row(self): option = DailySalesSearchOption(sales_date=self.today.date(), start_time=self.__get_time('09:00:00'), end_time=self.__get_time('21:00:00')) result = self.mapper.find_daily_sales(option) self.assertEqual(0, len(result)) def test_find_daily_sales_when_no_row(self): self.__init_data() option = DailySalesSearchOption(sales_date=self.today.date(), start_time=self.__get_time('13:00:00'), end_time=self.__get_time('21:00:00')) result = self.mapper.find_daily_sales(option) self.assertEqual(0, len(result)) def test_find_daily_sales_when_invalid_args(self): with self.assertRaises(ValueError): self.mapper.find_daily_sales(None) self.mapper.find_daily_sales('Test') self.mapper.find_daily_sales(2) self.mapper.find_daily_sales(True) def __init_data(self): self.db.execute_proc('create_test_data_daily_sales') self.db.commit() def __get_time(self, value): return datetime.strptime(value, '%H:%M:%S').time()