def test_step_resource_transform(): source = Package("data/package/datapackage.json") target = transform( source, steps=[ steps.resource_update(name="data", title="It's our data"), steps.resource_remove(name="data2"), steps.resource_add(name="data2", path="data2.csv"), steps.resource_transform( name="data", steps=[ steps.cell_set(field_name="description", value="Zeroed"), steps.cell_set(field_name="amount", value=0), ], ), steps.resource_transform( name="data2", steps=[ steps.cell_set(field_name="comment", value="It works!"), ], ), ], ) assert target.resource_names == ["data", "data2"] assert target.get_resource("data").read_rows() == [ {"id": "A3001", "name": "Taxes", "description": "Zeroed", "amount": 0}, {"id": "A5032", "name": "Parking Fees", "description": "Zeroed", "amount": 0}, ] assert target.get_resource("data2").read_rows() == [ {"parent": "A3001", "comment": "It works!"}, {"parent": "A3001", "comment": "It works!"}, {"parent": "A5032", "comment": "It works!"}, ]
def test_step_cell_set(): source = Resource(path="data/transform.csv") source.infer() target = transform( source, steps=[ steps.cell_set(field_name="population", value=100), ], ) assert target.schema == source.schema assert target.read_rows() == [ { "id": 1, "name": "germany", "population": 100 }, { "id": 2, "name": "france", "population": 100 }, { "id": 3, "name": "spain", "population": 100 }, ]
def test_step_table_write(tmpdir): path = str(tmpdir.join("table.json")) # Write source = Resource("data/transform.csv") transform( source, steps=[ steps.cell_set(field_name="population", value=100), steps.table_write(path=path), ], ) # Read resource = Resource(path=path) assert resource.read_rows() == [ { "id": 1, "name": "germany", "population": 100 }, { "id": 2, "name": "france", "population": 100 }, { "id": 3, "name": "spain", "population": 100 }, ]
def test_step_table_validate(): source = Resource("data/transform.csv") target = transform( source, steps=[ steps.cell_set(field_name="population", value="bad"), steps.table_validate(), ], ) assert target.schema == { "fields": [ { "name": "id", "type": "integer" }, { "name": "name", "type": "string" }, { "name": "population", "type": "integer" }, ] } with pytest.raises(FrictionlessException) as excinfo: target.read_rows() error = excinfo.value.error assert error.code == "step-error" assert error.note.count('type is "integer/default"')
def test_step_table_validate(): source = Resource(path="data/transform.csv") source.infer(only_sample=True) target = transform( source, steps=[ steps.cell_set(field_name="population", value="bad"), steps.table_validate(), ], ) assert target.schema == source.schema with pytest.raises(exceptions.FrictionlessException) as excinfo: target.read_rows() error = excinfo.value.error assert error.code == "step-error" assert error.note.count('type is "integer/default"')
def test_step_cell_set(): source = Resource(path="data/transform.csv") target = transform( source, steps=[ steps.cell_set(field_name="population", value=100), ], ) assert target.schema == { "fields": [ {"name": "id", "type": "integer"}, {"name": "name", "type": "string"}, {"name": "population", "type": "integer"}, ] } assert target.read_rows() == [ {"id": 1, "name": "germany", "population": 100}, {"id": 2, "name": "france", "population": 100}, {"id": 3, "name": "spain", "population": 100}, ]