def test_csv_file_upload_products(self): self.business.user = self.user self.business.save() factory = RequestFactory() base_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(base_path, 'product.csv') file = open(path, 'rb') request = factory.post( reverse('handle_csv', args=['products']), {'file': file}, **self.auth_headers) view = HandleCSV.as_view() response = view(request, param='products') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertIn('message', response.data) self.assertIn('noOfProductsAdded', response.data) self.assertGreater(response.data['noOfProductsAdded'], 0) # duplicated products file = open(path, 'rb') request_two = factory.post( reverse('handle_csv', args=['products']), {'file': file}, **self.auth_headers) view_two = HandleCSV.as_view() response_two = view_two(request_two, param='products') self.assertEqual(response_two.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('duplicatedProducts', response_two.data) self.assertGreater(len(response_two.data['duplicatedProducts']), 0)
def test_customers_csv_file_upload(self): factory = RequestFactory() base_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(base_path, 'customers.csv') file = open(path, 'rb') request = factory.post(reverse('handle_csv', args=['customers']), {'file': file}, **self.auth_headers) view = HandleCSV.as_view() response = view(request, param='customers') self.assertEqual(response.status_code, 201) self.assertEqual(response.data["success"], SUCCESS_RESPONSES["csv_upload_success"]) self.assertEqual(response.data["noOfCustomersAdded"], 4)
def test_invalid_batch_boolean(self): factory = RequestFactory() base_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join( base_path, 'batch_info_csv_samples/invalid_boolean.csv' ) file = open(path, 'rb') request = factory.post( reverse('handle_csv', args=['batch_info']), {'file': file}, **self.auth_headers) view = HandleCSV.as_view() response = view(request, param='batch_info') self.assertEqual(response.status_code, 400) self.assertEqual(response.data[0], PRODUCTS_ERROR_RESPONSES["batch_bool_error"] )
def test_successful_batch_upload(self): factory = RequestFactory() base_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join( base_path, 'batch_info_csv_samples/valid_batch_info.csv' ) file = open(path, 'rb') request = factory.post( reverse('handle_csv', args=['batch_info']), {'file': file}, **self.auth_headers) view = HandleCSV.as_view() response = view(request, param='batch_info') self.assertEqual(response.status_code, 201) self.assertEqual( response.data['success'], PRODUCTS_SUCCESS_RESPONSES["batch_upload_success"] )
def test_invalid_csv_file_upload_products(self): factory = RequestFactory() base_path = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(base_path, 'invalid_product.csv') file = open(path, 'rb') request = factory.post( reverse('handle_csv', args=['products']), {'file': file}, **self.auth_headers) view = HandleCSV.as_view() response = view(request, param='products') self.assertEqual(response.status_code, 400) self.assertIn('rows', response.data) self.assertIn('columns', response.data) self.assertEqual(response.data.get('rows')[0].get('1')['name'], ERROR_RESPONSES['required_field'].format('name')) self.assertEqual(response.data.get('columns')[0], ERROR_RESPONSES['not_allowed_field'] .format('not allowed column'))
def setUp(self): super(SuppliersTestCase, self).setUp() self.factory = RequestFactory() self.base_path = os.path.dirname(os.path.realpath(__file__)) login_admin = self.client.execute( loginUser_mutation.format(**self.login_master_admin)) master_admin_rest_token = login_admin.data['loginUser']['restToken'] self.admin_auth_headers = { 'HTTP_AUTHORIZATION': ' Token ' + str(master_admin_rest_token) } call_command('loaddata', 'healthid/fixtures/tests') self.view = HandleCSV.as_view() self.client = APIClient() self.url = reverse('export_csv', kwargs={'param': 'products'}) self.supplier_1 = SuppliersFactory() self.supplier_data = { "id": self.supplier_1.id, "name": self.supplier_1.name }