def transform_json(): request_json = request.get_json(silent=True) transformed_data = transform(request_json["input_json"], request_json["schema"]) # validate(transformed_data, request_json['schema']) return jsonify(transformed_data)
def test_missing_field_in_source_data(purchase_order_data, purchase_order_schema): for item in purchase_order_data["items"]: del item["custom_fields"]["cost-center"] transformed_data = transform(purchase_order_data, purchase_order_schema) for item in transformed_data["items"]: assert item["custom_fields"]["cost-center"] == ""
def test_field_removal(purchase_order_data, purchase_order_schema): pos = purchase_order_schema # Remove vendor field del pos["properties"]["vendor"] transformed_data = transform(purchase_order_data, purchase_order_schema) assert purchase_order_data != transformed_data assert "vendor" not in transformed_data
def test_root_field_to_root_field_mapping(purchase_order_data, purchase_order_schema): pos = purchase_order_schema # Map comment to currency pos["properties"]["comment"]["source"] = "currency" transformed_data = transform(purchase_order_data, purchase_order_schema) assert transformed_data["comment"] == purchase_order_data["currency"]
def test_root_field_to_nested_field_mapping(purchase_order_data, purchase_order_schema): pos = purchase_order_schema # Map comment to vendor name pos["properties"]["comment"]["source"] = "vendor.name" transformed_data = transform(purchase_order_data, purchase_order_schema) assert transformed_data["comment"] == purchase_order_data["vendor"]["name"]
def test_alpha_2_country_with_valid_country(vendor_data, vendor_schema): vendor_schema["properties"]["country"] = { "type": "function", "source": ["alpha_2_country", ["country"], []], } transformed_data = transform(vendor_data, vendor_schema) assert vendor_data != transformed_data assert transformed_data["country"] == "US"
def test_boolean(vendor_data, vendor_schema): vendor_schema["properties"]["is_1099_eligible"] = { "type": "function", "source": ["boolean", ["is_1099_eligible"], ["T", "F"]], } transformed_data = transform(vendor_data, vendor_schema) assert vendor_data != transformed_data assert transformed_data["is_1099_eligible"] == "F"
def test_root_field_rename(purchase_order_data, purchase_order_schema): pos = purchase_order_schema # Rename date to po_date pos["properties"]["po_date"] = pos["properties"]["date"] del pos["properties"]["date"] transformed_data = transform(purchase_order_data, purchase_order_schema) assert purchase_order_data != transformed_data assert "po_date" in transformed_data assert transformed_data["po_date"] == purchase_order_data["date"]
def test_default_value_when_source_is_empty(purchase_order_data, purchase_order_schema): pos = purchase_order_schema # Add a default value for comment pos["properties"]["comment"] = { "type": "function", "source": ["default_value", ["comment"], ["no comment"]], } transformed_data = transform(purchase_order_data, purchase_order_schema) assert purchase_order_data != transformed_data assert transformed_data["comment"] == "no comment"
def test_string_literal(purchase_order_data, purchase_order_schema): pos = purchase_order_schema # Add a new field `foo` with default value `bar` pos["properties"]["foo"] = { "type": "function", "source": ["string_literal", [], ["bar"]], } transformed_data = transform(purchase_order_data, purchase_order_schema) assert purchase_order_data != transformed_data assert "foo" in transformed_data assert transformed_data["foo"] == "bar"
def test_format_date(purchase_order_data, purchase_order_schema): pos = purchase_order_schema # Format date in DD-MM-YYYY (02-06-2018) pos["properties"]["date"] = { "type": "function", "source": ["format_date", ["date"], ["DD-MM-YYYY"]], } transformed_data = transform(purchase_order_data, purchase_order_schema) assert purchase_order_data != transformed_data assert transformed_data["date"] == "02-06-2018" # Format unix timestamp pos["properties"]["date"] = { "type": "function", "source": ["format_date", ["date"], ["X"]], } transformed_data = transform(purchase_order_data, purchase_order_schema) assert type(transformed_data["date"]) == int assert isinstance(transformed_data["date"], int)
def test_wrap_fields_in_an_object(purchase_order_data, purchase_order_schema): vendor_properties = purchase_order_schema["properties"]["vendor"][ "properties"] vendor_properties["phones"] = { "type": "object", "source": "`this`", "properties": { "phoneOne": { "type": "string", "source": "phoneOne" }, "phoneTwo": { "type": "string", "source": "phoneTwo" }, }, } transformed_data = transform(purchase_order_data, purchase_order_schema) vendor = transformed_data["vendor"] source_vendor = purchase_order_data["vendor"] assert vendor["phones"]["phoneOne"] == source_vendor["phoneOne"] assert vendor["phones"]["phoneTwo"] == source_vendor["phoneTwo"]
def test_identical_transformation(purchase_order_data, purchase_order_schema): transformed_data = transform(purchase_order_data, purchase_order_schema) assert purchase_order_data == transformed_data