class BaseApiTestCase(TestCase): # constants CONTENT_TYPE = 'application/json' def setUp(self): self.db = PgAdapter() self.endpoint = '' self.teardown_query = '' def tearDown(self): if self.teardown_query: self.db.execute(self.teardown_query) self.db.commit() self.db = None @classmethod def setUpClass(cls): app = startup_app() cls.client = app.test_client() @classmethod def tearDownClass(cls): cls.client = None def init_data(self): raise NotImplementedError()
def tearDown(self): self.db = PgAdapter() query = """ TRUNCATE TABLE items RESTART IDENTITY; """ self.db.execute(query) self.db.commit()
class BaseMapperTestCase(TestCase): def setUp(self): self.db = PgAdapter() self.mapper = None self.teardown_query = '' def tearDown(self): if self.teardown_query: self.db.execute(self.teardown_query) self.db.commit() self.db = None def init_data(self): raise NotImplementedError()
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 __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()
def tearDown(self): query = """ TRUNCATE TABLE categories RESTART IDENTITY; """ db = PgAdapter() db.execute(query) db.commit()
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()
def setUp(self): self.db = PgAdapter() self.mapper = None self.teardown_query = ''
def __init_data(self): db = PgAdapter() db.execute_proc('create_test_data_categories') db.commit()
def setUp(self): self.mapper = CategoryMapper() self.db = PgAdapter()
def setUp(self): self.db = PgAdapter() self.endpoint = '' self.teardown_query = ''
def setUp(self): self.mapper = ItemMapper() self.db = PgAdapter()
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()
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()
def setUp(self): self.mapper = SalesMapper() self.db = PgAdapter() self.today = datetime.now()
def tearDown(self): db = PgAdapter() db.execute_proc('cleanup_sales') db.commit()
def setUpClass(cls): cls.db = PgAdapter()
def setUp(self): self.mapper = SalesMapper() self.db = PgAdapter()