class TestStockTransactionSync(TestCase): def setUp(self): self.endpoint = MockEndpoint("http://test-api.com/", "dummy", "dummy") self.stock_api_object = MockILSStockDataSynchronization(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), "data") initial_bootstrap(TEST_DOMAIN) self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = ILSGatewayConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = "******" config.username = "******" config.url = "http://test-api.com/" config.save() l1 = Location(name="Test location 1", external_id="3445", location_type="FACILITY", domain=TEST_DOMAIN) l2 = Location(name="Test location 2", external_id="4407", location_type="FACILITY", domain=TEST_DOMAIN) l1.save() l2.save() SupplyPointCase.create_from_location(TEST_DOMAIN, l1) SupplyPointCase.create_from_location(TEST_DOMAIN, l2) with open(os.path.join(self.datapath, "sample_products.json")) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json)) StockTransaction.objects.all().delete() def test_stock_data_migration(self): stock_data_task(self.stock_api_object) self.assertEqual(SupplyPointStatus.objects.all().count(), 5) self.assertEqual(SupplyPointStatus.objects.all().values_list("location_id", flat=True).distinct().count(), 2) self.assertEqual(StockTransaction.objects.filter(report__domain=TEST_DOMAIN).count(), 16) self.assertEqual(DeliveryGroupReport.objects.all().count(), 4) self.assertEqual(DeliveryGroupReport.objects.all().values_list("location_id", flat=True).distinct().count(), 2) self.assertEqual(StockState.objects.all().count(), 6) self.assertEqual(StockReport.objects.all().count(), 2) def test_stock_data_migration2(self): StockDataCheckpoint.objects.create( domain=TEST_DOMAIN, api="", limit=1000, offset=0, date=datetime.datetime(2015, 7, 1) ) stock_data_task(self.stock_api_object) self.assertEqual(SupplyPointStatus.objects.all().count(), 5) self.assertEqual(SupplyPointStatus.objects.all().values_list("location_id", flat=True).distinct().count(), 2) self.assertEqual(StockTransaction.objects.filter(report__domain=TEST_DOMAIN).count(), 15) self.assertEqual(DeliveryGroupReport.objects.all().count(), 4) self.assertEqual(DeliveryGroupReport.objects.all().values_list("location_id", flat=True).distinct().count(), 2) self.assertEqual(StockState.objects.all().count(), 6) self.assertEqual(StockReport.objects.all().count(), 2) def tearDown(self): Domain.get_by_name(TEST_DOMAIN).delete()
class ProductSyncTest(TestCase): def setUp(self): self.endpoint = MockEndpoint("http://test-api.com/", "dummy", "dummy") self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), "data") initial_bootstrap(TEST_DOMAIN) for product in Prod.by_domain(TEST_DOMAIN): product.delete() def test_create_product(self): with open(os.path.join(self.datapath, "sample_products.json")) as f: product = Product(json.loads(f.read())[0]) self.assertEqual(0, len(Prod.by_domain(TEST_DOMAIN))) ilsgateway_product = self.api_object.product_sync(product) self.assertEqual(product.sms_code, ilsgateway_product.code.lower()) self.assertEqual(product.name, ilsgateway_product.name) self.assertEqual(product.description, ilsgateway_product.description) self.assertEqual(product.units, str(ilsgateway_product.unit)) def test_products_migration(self): checkpoint = MigrationCheckpoint( domain=TEST_DOMAIN, start_date=datetime.utcnow(), date=datetime.utcnow(), api="product", limit=100, offset=0 ) product_api = ApiSyncObject("product", self.endpoint.get_products, self.api_object.product_sync) synchronization(product_api, checkpoint, None, 100, 0) self.assertEqual("product", checkpoint.api) self.assertEqual(100, checkpoint.limit) self.assertEqual(0, checkpoint.offset) self.assertEqual(6, len(list(Prod.by_domain(TEST_DOMAIN))))
class ProductSyncTest(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) for product in Prod.by_domain(TEST_DOMAIN): product.delete() def test_create_product(self): with open(os.path.join(self.datapath, 'sample_products.json')) as f: product = Product(json.loads(f.read())[0]) self.assertEqual(0, len(Prod.by_domain(TEST_DOMAIN))) ilsgateway_product = self.api_object.product_sync(product) self.assertEqual(product.sms_code, ilsgateway_product.code.lower()) self.assertEqual(product.name, ilsgateway_product.name) self.assertEqual(product.description, ilsgateway_product.description) self.assertEqual(product.units, str(ilsgateway_product.unit)) def test_products_migration(self): checkpoint = MigrationCheckpoint( domain=TEST_DOMAIN, start_date=datetime.utcnow(), date=datetime.utcnow(), api='product', limit=100, offset=0 ) product_api = ApiSyncObject( 'product', self.endpoint.get_products, self.api_object.product_sync ) synchronization(product_api, checkpoint, None, 100, 0) self.assertEqual('product', checkpoint.api) self.assertEqual(100, checkpoint.limit) self.assertEqual(0, checkpoint.offset) self.assertEqual(6, len(list(Prod.by_domain(TEST_DOMAIN))))
class TestStockTransactionSync(TestCase): def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.stock_api_object = MockILSStockDataSynchronization( TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = ILSGatewayConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = '******' config.username = '******' config.url = 'http://test-api.com/' config.save() l1 = Location(name='Test location 1', external_id='3445', location_type='FACILITY', domain=TEST_DOMAIN) l2 = Location(name='Test location 2', external_id='4407', location_type='FACILITY', domain=TEST_DOMAIN) l1.save() l2.save() SupplyPointCase.create_from_location(TEST_DOMAIN, l1) SupplyPointCase.create_from_location(TEST_DOMAIN, l2) with open(os.path.join(self.datapath, 'sample_products.json')) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json)) StockTransaction.objects.all().delete() def test_stock_data_migration(self): stock_data_task(self.stock_api_object) self.assertEqual(SupplyPointStatus.objects.all().count(), 5) self.assertEqual( StockTransaction.objects.filter( report__domain=TEST_DOMAIN).count(), 16) self.assertEqual(DeliveryGroupReport.objects.all().count(), 4) self.assertEqual(StockState.objects.all().count(), 6) self.assertEqual(StockReport.objects.all().count(), 2) def test_stock_data_migration2(self): StockDataCheckpoint.objects.create(domain=TEST_DOMAIN, api='', limit=1000, offset=0, date=datetime.datetime(2015, 7, 1)) stock_data_task(self.stock_api_object) self.assertEqual(SupplyPointStatus.objects.all().count(), 5) self.assertEqual( StockTransaction.objects.filter( report__domain=TEST_DOMAIN).count(), 15) self.assertEqual(DeliveryGroupReport.objects.all().count(), 4) self.assertEqual(StockState.objects.all().count(), 6) self.assertEqual(StockReport.objects.all().count(), 2) def tearDown(self): Domain.get_by_name(TEST_DOMAIN).delete()