def test_action_inserting_products_from_the_csv_file_in_db_should_raise_a_id_error( self, get_mock, import_csv_mock, save_mock, product_mock): # Arrange id = str(uuid4()) get_mock.return_value = [product_mock] save_mock.side_effect = SQLAlchemyError import_csv_mock.return_value = [{ 'name': 'name', 'cost_values': 12.54, 'unit_box': 20, 'weight_unit': 25.23, 'validity': "2021-10-10", 'sku': '342343', 'description': 'descricao', 'category_line_id': '342343', 'supplier_id': id }] with pytest.raises(IdNotExistError) as exc: # Action insert_data(['import_csv'], 'sku', validate_by_supplier, [], [], []) # Assertions self.assertEqual(400, exc.value.code) self.assertEqual('Bad Request', exc.value.name) self.assertEqual('The ID(s) inserted does not exist in the database', exc.value.description)
def test_action_inserting_products_from_the_csv_file_in_db_should_raise_a_invalid_value_error( self, get_mock, import_csv_mock, save_mock, product_mock): # Arrange id = str(uuid4()) get_mock.return_value = [product_mock] save_mock.side_effect = SQLAlchemyError import_csv_mock.return_value = [{ 'name': 'name', 'cost_values': 12.54, 'unit_box': 20, 'weight_unit': 25.23, 'validity': "2021-10-10", 'sku': '342343', 'description': 'descricao', 'category_line_id': '342343', 'supplier_id': id }] with pytest.raises(InvalidValueError) as exc: # Action insert_data(['import_csv'], 'sku', validate, [], [], []) # Assertions self.assertEqual(400, exc.value.code) self.assertEqual('Bad Request', exc.value.name) self.assertEqual( 'Check if the ID field(s) are entered correctly. If so, the following field(s) must be ' 'unique: sku', exc.value.description)
def import_route_by_supplier(): try: file = request.files['data_file'] except: raise NoFileError() stream = io.StringIO(file.stream.read().decode("utf-8")) insert_data(stream, 'sku', validate_by_supplier, [], [], []) return jsonify([product.serialize() for product in get_products()]), 200
def test_action_inserting_products_from_the_csv_file_in_db_should_raise_a_data_already_exists_error( self, get_mock, import_csv_mock, save_mock, product_mock): # Arrange product_mock.side_effect = DataAlreadyExistsError with pytest.raises(DataAlreadyExistsError) as exc: # Action insert_data(['import_csv'], 'sku', validate, [], [], []) # Assertions self.assertEqual(400, exc.value.code) self.assertEqual('Bad Request', exc.value.name) self.assertEqual('All the data in the file has already been added', exc.value.description)
def test_action_inserting_products_by_supplier_from_the_csv_file_in_db_should_return_a_list_of_saved_products( self, get_mock, import_csv_mock, save_mock, product_mock): # Arrange id = str(uuid4()) get_mock.return_value = [product_mock] import_csv_mock.return_value = [{ 'name': 'name', 'cost_values': 12.54, 'unit_box': 20, 'weight_unit': 25.23, 'validity': "2021-10-10", 'sku': '342343', 'description': 'descricao', 'category_line_id': id, 'supplier_id': id }] # Action response = insert_data(['import_csv'], 'sku', validate_by_supplier, [], [], []) # Assertions self.assertTrue(get_mock.called) self.assertTrue(import_csv_mock.called) self.assertTrue(save_mock.called) self.assertTrue(product_mock.called) self.assertEqual([product_mock], get_mock.return_value) self.assertEqual([save_mock()], response) self.assertEqual([{ 'name': 'name', 'cost_values': 12.54, 'unit_box': 20, 'weight_unit': 25.23, 'validity': "2021-10-10", 'sku': '342343', 'description': 'descricao', 'category_line_id': id, 'supplier_id': id }], import_csv_mock.return_value)