diff --git a/ckanext/validation/tests/test_interfaces.py b/ckanext/validation/tests/test_interfaces.py index cadca410..a621d0fb 100644 --- a/ckanext/validation/tests/test_interfaces.py +++ b/ckanext/validation/tests/test_interfaces.py @@ -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): @@ -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() @@ -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): @@ -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() diff --git a/ckanext/validation/tests/test_jobs.py b/ckanext/validation/tests/test_jobs.py index 8e770eb0..9d0eba60 100644 --- a/ckanext/validation/tests/test_jobs.py +++ b/ckanext/validation/tests/test_jobs.py @@ -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"]) @@ -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"]) @@ -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() @@ -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") @@ -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") @@ -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") @@ -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({}) @@ -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") diff --git a/ckanext/validation/tests/test_logic.py b/ckanext/validation/tests/test_logic.py index 90046f29..e2bf3a5e 100644 --- a/ckanext/validation/tests/test_logic.py +++ b/ckanext/validation/tests/test_logic.py @@ -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" @@ -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"}])