Skip to content

Commit

Permalink
fix: correct mock patchs in tests now we have moved to package.validate
Browse files Browse the repository at this point in the history
  • Loading branch information
ChasNelson1990 committed Jul 21, 2023
1 parent dfa701d commit 71595be
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
8 changes: 4 additions & 4 deletions ckanext/validation/tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TestInterfaceSync():
@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_create_sync', True)
@mock.patch('ckanext.validation.jobs.validate',
@mock.patch('frictionless.Package.validate',
return_value=VALID_REPORT)
def test_can_validate_called_on_create_sync(self, mock_validation):

Expand All @@ -73,7 +73,7 @@ def test_can_validate_called_on_create_sync(self, mock_validation):
@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_create_sync', True)
@mock.patch('ckanext.validation.jobs.validate')
@mock.patch('frictionless.Package.validate')
def test_can_validate_called_on_create_sync_no_validation(self, mock_validation):

dataset = factories.Dataset()
Expand All @@ -91,7 +91,7 @@ def test_can_validate_called_on_create_sync_no_validation(self, mock_validation)
@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_sync', True)
@mock.patch('ckanext.validation.jobs.validate',
@mock.patch('frictionless.Package.validate',
return_value=VALID_REPORT)
def test_can_validate_called_on_update_sync(self, mock_validation):

Expand All @@ -113,7 +113,7 @@ def test_can_validate_called_on_update_sync(self, mock_validation):
@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_sync', True)
@mock.patch('ckanext.validation.jobs.validate')
@mock.patch('frictionless.Package.validate')
def test_can_validate_called_on_update_sync_no_validation(self, mock_validation):

dataset = factories.Dataset()
Expand Down
67 changes: 44 additions & 23 deletions ckanext/validation/tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ def mock_get_resource_uploader(data_dict):
class TestValidationJob(object):

@pytest.mark.ckan_config("ckanext.validation.run_on_create_async", False)
@mock.patch("ckanext.validation.jobs.validate", return_value=VALID_REPORT)
@mock.patch("frictionless.Package.validate", return_value=VALID_REPORT)
@mock.patch("frictionless.Resource.__create__")
@mock.patch.object(Session, "commit")
@mock.patch.object(ckantoolkit, "get_action")
def test_job_run_no_schema(self, mock_get_action, mock_commit, mock_validate):
def test_job_run_no_schema(self, mock_get_action, mock_commit, mock_frictionless_resource, mock_validate):

org = factories.Organization()
dataset = factories.Dataset(private=True, owner_org=org["id"])
Expand All @@ -51,14 +52,21 @@ def test_job_run_no_schema(self, mock_get_action, mock_commit, mock_validate):

run_validation_job(resource)

assert mock_validate.call_args[0][0] == "http://example.com/file.csv"
assert mock_validate.call_args[1]["format"] == "csv"
assert mock_validate.call_args[1]["schema"] is None

@mock.patch("ckanext.validation.jobs.validate", return_value=VALID_REPORT)
assert mock_frictionless_resource.call_count == 1
assert mock_frictionless_resource.call_args[1]["path"] == "http://example.com/file.csv"
assert mock_frictionless_resource.call_args[1]["format"] == "csv"
assert "schema" not in mock_frictionless_resource.call_args[1]

assert mock_validate.call_count == 1
# assert mock_validate.call_args[1]["checklist"] is {}
assert mock_validate.call_args[1]["limit_errors"] is None
assert mock_validate.call_args[1]["limit_rows"] is None

@mock.patch("frictionless.Package.validate", return_value=VALID_REPORT)
@mock.patch("frictionless.Resource.__create__")
@mock.patch.object(Session, "commit")
@mock.patch.object(ckantoolkit, "get_action")
def test_job_run_schema(self, mock_get_action, mock_commit, mock_validate):
def test_job_run_schema(self, mock_get_action, mock_commit, mock_frictionless_resource, mock_validate):

org = factories.Organization()
dataset = factories.Dataset(private=True, owner_org=org["id"])
Expand All @@ -79,18 +87,25 @@ def test_job_run_schema(self, mock_get_action, mock_commit, mock_validate):

run_validation_job(resource)

assert mock_validate.call_args[0][0] == "http://example.com/file.csv"
assert mock_validate.call_args[1]["format"] == "csv"
assert mock_validate.call_args[1]["schema"].to_dict() == schema

@mock.patch("ckanext.validation.jobs.validate", return_value=VALID_REPORT)
assert mock_frictionless_resource.call_count == 1
assert mock_frictionless_resource.call_args[1]["path"] == "http://example.com/file.csv"
assert mock_frictionless_resource.call_args[1]["format"] == "csv"
assert mock_frictionless_resource.call_args[1]["schema"].to_dict() == schema

assert mock_validate.call_count == 1
# assert mock_validate.call_args[1]["checklist"] is {}
assert mock_validate.call_args[1]["limit_errors"] is None
assert mock_validate.call_args[1]["limit_rows"] is None

@mock.patch("frictionless.Package.validate", return_value=VALID_REPORT)
@mock.patch("frictionless.Resource.__create__")
@mock.patch.object(
uploader, "get_resource_uploader", return_value=mock_get_resource_uploader({})
)
@mock.patch.object(Session, "commit")
@mock.patch.object(ckantoolkit, "get_action")
def test_job_run_uploaded_file(
self, mock_get_action, mock_commit, mock_uploader, mock_validate
self, mock_get_action, mock_commit, mock_uploader, mock_frictionless_resource, mock_validate
):

org = factories.Organization()
Expand All @@ -106,11 +121,17 @@ def test_job_run_uploaded_file(

run_validation_job(resource)

assert mock_validate.call_args[0][0] == "/tmp/example/{}".format(resource["id"])
assert mock_validate.call_args[1]["format"] == "csv"
assert mock_validate.call_args[1]["schema"] is None

@mock.patch("ckanext.validation.jobs.validate", return_value=VALID_REPORT)
assert mock_frictionless_resource.call_count == 1
assert mock_frictionless_resource.call_args[1]["path"] == "/tmp/example/{}".format(resource["id"])
assert mock_frictionless_resource.call_args[1]["format"] == "csv"
assert "schema" not in mock_frictionless_resource.call_args[1]

assert mock_validate.call_count == 1
# assert mock_validate.call_args[1]["checklist"] is {}
assert mock_validate.call_args[1]["limit_errors"] is None
assert mock_validate.call_args[1]["limit_rows"] is None

@mock.patch("frictionless.Package.validate", return_value=VALID_REPORT)
def test_job_run_valid_stores_validation_object(self, mock_validate):

resource = factories.Resource(url="http://example.com/file.csv", format="csv")
Expand All @@ -127,7 +148,7 @@ def test_job_run_valid_stores_validation_object(self, mock_validate):
assert json.loads(validation.report) == VALID_REPORT
assert validation.finished

@mock.patch("ckanext.validation.jobs.validate", return_value=INVALID_REPORT)
@mock.patch("frictionless.Package.validate", return_value=INVALID_REPORT)
def test_job_run_invalid_stores_validation_object(self, mock_validate):

resource = factories.Resource(url="http://example.com/file.csv", format="csv")
Expand All @@ -144,7 +165,7 @@ def test_job_run_invalid_stores_validation_object(self, mock_validate):
assert json.loads(validation.report) == INVALID_REPORT
assert validation.finished

@mock.patch("ckanext.validation.jobs.validate", return_value=ERROR_REPORT)
@mock.patch("frictionless.Package.validate", return_value=ERROR_REPORT)
def test_job_run_error_stores_validation_object(self, mock_validate):

resource = factories.Resource(url="http://example.com/file.csv", format="csv")
Expand All @@ -162,7 +183,7 @@ def test_job_run_error_stores_validation_object(self, mock_validate):
assert validation.finished

@mock.patch(
"ckanext.validation.jobs.validate", return_value=VALID_REPORT_LOCAL_FILE
"frictionless.Package.validate", return_value=VALID_REPORT_LOCAL_FILE
)
@mock.patch.object(
uploader, "get_resource_uploader", return_value=mock_get_resource_uploader({})
Expand All @@ -182,7 +203,7 @@ def test_job_run_uploaded_file_replaces_paths(self, mock_uploader, mock_validate
report = json.loads(validation.report)
assert report["tasks"][0]["place"].startswith("http")

@mock.patch("ckanext.validation.jobs.validate", return_value=VALID_REPORT)
@mock.patch("frictionless.Package.validate", return_value=VALID_REPORT)
def test_job_run_valid_stores_status_in_resource(self, mock_validate):

resource = factories.Resource(url="http://example.com/file.csv", format="csv")
Expand Down
4 changes: 2 additions & 2 deletions ckanext/validation/tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ def test_validation_passes_on_upload(self):
assert resource["validation_status"] == "success"
assert "validation_timestamp" in resource

@mock.patch("ckanext.validation.jobs.validate", return_value=VALID_REPORT)
@mock.patch("frictionless.Package.validate", return_value=VALID_REPORT)
def test_validation_passes_with_url(self, mock_validate):

url = "https://example.com/valid.csv"
Expand Down Expand Up @@ -672,7 +672,7 @@ def test_validation_passes_on_upload(self):
assert resource["validation_status"] == "success"
assert "validation_timestamp" in resource

@mock.patch("ckanext.validation.jobs.validate", return_value=VALID_REPORT)
@mock.patch("frictionless.Package.validate", return_value=VALID_REPORT)
def test_validation_passes_with_url(self, mock_validate):

dataset = factories.Dataset(resources=[{"url": "https://example.com/data.csv"}])
Expand Down

0 comments on commit 71595be

Please sign in to comment.