Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ max_complexity = 7
[tool.black]
line-length = 119
target-version = ['py312']
include='''src/.*/*.py$'''
include='''(src|tests)/.*\.py$'''
3 changes: 1 addition & 2 deletions tests/integration/test_dataset_unregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def test_remove_unregistered_dataset_from_azure_blob(get_and_clear_up_context):
context["DATA_REGISTRY_BASE_URL"] = "http://localhost:3000/ckan-registration/datasets-01-1-dataset"
checker_run(context, datasets_in_bds)

blob_service_client = \
BlobServiceClient.from_connection_string(context["AZURE_STORAGE_CONNECTION_STRING"])
blob_service_client = BlobServiceClient.from_connection_string(context["AZURE_STORAGE_CONNECTION_STRING"])

xml_container_name = get_azure_container_name(context, "xml")
zip_container_name = get_azure_container_name(context, "zip")
Expand Down
75 changes: 44 additions & 31 deletions tests/integration/test_dataset_upload_to_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@
from utilities.azure import get_azure_blob_name, get_azure_container_name


@pytest.mark.parametrize("artifact_filename", [
("test_foundation_a-dataset-001.xml"),
("test_foundation_a-dataset-001-utf-8-with-bom"),
("test_foundation_a-dataset-001-utf-16-le"),
("test_foundation_a-dataset-001-utf-16-be"),
("test_foundation_a-dataset-001-utf-32-le"),
("test_foundation_a-dataset-001-utf-32-be"),
("test_foundation_a-dataset-001-iso-8859-1"),
])
@pytest.mark.parametrize(
"artifact_filename",
[
("test_foundation_a-dataset-001.xml"),
("test_foundation_a-dataset-001-utf-8-with-bom"),
("test_foundation_a-dataset-001-utf-16-le"),
("test_foundation_a-dataset-001-utf-16-be"),
("test_foundation_a-dataset-001-utf-32-le"),
("test_foundation_a-dataset-001-utf-32-be"),
("test_foundation_a-dataset-001-iso-8859-1"),
],
)
def test_valid_dataset_azure_xml_upload(get_and_clear_up_context, artifact_filename): # noqa: F811

context = get_and_clear_up_context

dataset_id = uuid.UUID("c8a40aa5-9f31-4bcf-a36f-51c1fc2cc159")
context["DATA_REGISTRY_BASE_URL"] = ("http://localhost:3000/ckan-registration/datasets-01-1-dataset/"
"http%3A%2F%2Flocalhost%3A3000%2Fdata%2F{}").format(artifact_filename)
context["DATA_REGISTRY_BASE_URL"] = (
"http://localhost:3000/ckan-registration/datasets-01-1-dataset/http%3A%2F%2Flocalhost%3A3000%2Fdata%2F{}"
).format(artifact_filename)
datasets_in_bds = {}
checker_run(context, datasets_in_bds)

Expand All @@ -39,47 +43,56 @@ def test_valid_dataset_azure_xml_upload(get_and_clear_up_context, artifact_filen
assert dataset_contents_from_disk == dataset_contents_from_azure


@pytest.mark.parametrize("artifact_filename", [
("test_foundation_a-dataset-001.xml"),
("test_foundation_a-dataset-001-utf-8-with-bom"),
("test_foundation_a-dataset-001-utf-16-le"),
("test_foundation_a-dataset-001-utf-16-be"),
("test_foundation_a-dataset-001-utf-32-le"),
("test_foundation_a-dataset-001-utf-32-be"),
("test_foundation_a-dataset-001-iso-8859-1"),
])
@pytest.mark.parametrize(
"artifact_filename",
[
("test_foundation_a-dataset-001.xml"),
("test_foundation_a-dataset-001-utf-8-with-bom"),
("test_foundation_a-dataset-001-utf-16-le"),
("test_foundation_a-dataset-001-utf-16-be"),
("test_foundation_a-dataset-001-utf-32-le"),
("test_foundation_a-dataset-001-utf-32-be"),
("test_foundation_a-dataset-001-iso-8859-1"),
],
)
def test_valid_dataset_azure_zip_upload(get_and_clear_up_context, artifact_filename): # noqa: F811

context = get_and_clear_up_context

dataset_id = uuid.UUID("c8a40aa5-9f31-4bcf-a36f-51c1fc2cc159")
context["DATA_REGISTRY_BASE_URL"] = ("http://localhost:3000/ckan-registration/datasets-01-1-dataset/"
"http%3A%2F%2Flocalhost%3A3000%2Fdata%2F{}").format(artifact_filename)
context["DATA_REGISTRY_BASE_URL"] = (
"http://localhost:3000/ckan-registration/datasets-01-1-dataset/http%3A%2F%2Flocalhost%3A3000%2Fdata%2F{}"
).format(artifact_filename)
datasets_in_bds = {}
checker_run(context, datasets_in_bds)

dataset_contents_from_disk = get_file_contents("tests/artifacts/iati-xml-files/{}".format(artifact_filename))

dataset_contents_from_azure_zipped = download_dataset_from_azure(context, datasets_in_bds[dataset_id], "zip")

dataset_contents_from_azure = unzip_from_buffer("{}.xml".format(datasets_in_bds[dataset_id]["short_name"]),
dataset_contents_from_azure_zipped)
dataset_contents_from_azure = unzip_from_buffer(
"{}.xml".format(datasets_in_bds[dataset_id]["short_name"]), dataset_contents_from_azure_zipped
)

assert dataset_contents_from_disk == dataset_contents_from_azure


@pytest.mark.parametrize("artifact_filename", [
("test_foundation_a-dataset-empty.xml"),
("test_foundation_a-dataset-html.xml"),
("test_foundation_a-dataset.pdf"),
])
@pytest.mark.parametrize(
"artifact_filename",
[
("test_foundation_a-dataset-empty.xml"),
("test_foundation_a-dataset-html.xml"),
("test_foundation_a-dataset.pdf"),
],
)
def test_invalid_dataset_no_azure_xml_upload(get_and_clear_up_context, artifact_filename): # noqa: F811

context = get_and_clear_up_context

dataset_id = uuid.UUID("c8a40aa5-9f31-4bcf-a36f-51c1fc2cc159")
context["DATA_REGISTRY_BASE_URL"] = ("http://localhost:3000/ckan-registration/datasets-01-1-dataset/"
"http%3A%2F%2Flocalhost%3A3000%2Fdata%2F{}").format(artifact_filename)
context["DATA_REGISTRY_BASE_URL"] = (
"http://localhost:3000/ckan-registration/datasets-01-1-dataset/http%3A%2F%2Flocalhost%3A3000%2Fdata%2F{}"
).format(artifact_filename)
datasets_in_bds = {}
checker_run(context, datasets_in_bds)

Expand Down
36 changes: 18 additions & 18 deletions tests/integration/test_prometheus_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_metrics_after_simple_add(get_and_clear_up_context): # noqa: F811
("datasets_unregistered", 0),
("datasets_expired", 0),
("datasets_head_request_non_200", 0),
("datasets_downloads_non_200", 0)
("datasets_downloads_non_200", 0),
]

for expected_result in expected_results:
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_metrics_after_new_registration(get_and_clear_up_context): # noqa: F811
("datasets_unregistered", 0),
("datasets_expired", 0),
("datasets_head_request_non_200", 0),
("datasets_downloads_non_200", 0)
("datasets_downloads_non_200", 0),
]

for expected_result in expected_results:
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_metrics_after_unregistration(get_and_clear_up_context): # noqa: F811
("datasets_unregistered", 1),
("datasets_expired", 0),
("datasets_head_request_non_200", 0),
("datasets_downloads_non_200", 0)
("datasets_downloads_non_200", 0),
]

for expected_result in expected_results:
Expand All @@ -101,14 +101,14 @@ def test_metrics_with_success_then_immediate_404(get_and_clear_up_context): # n
("datasets_unregistered", 0),
("datasets_expired", 0),
("datasets_head_request_non_200", 0),
("datasets_downloads_non_200", 1)
("datasets_downloads_non_200", 1),
]

for expected in expected_results:
assert context["prom_metrics"][expected[0]].set.called, \
"metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.call_args.args == (expected[1],), \
"metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.called, "metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.call_args.args == (expected[1],), "metric name: {}".format(
expected[0]
)


def test_metrics_with_success_then_delay_404(get_and_clear_up_context): # noqa: F811
Expand All @@ -133,14 +133,14 @@ def test_metrics_with_success_then_delay_404(get_and_clear_up_context): # noqa:
("datasets_unregistered", 0),
("datasets_expired", 0),
("datasets_head_request_non_200", 0),
("datasets_downloads_non_200", 1)
("datasets_downloads_non_200", 1),
]

for expected in expected_results:
assert context["prom_metrics"][expected[0]].set.called, \
"metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.call_args.args == (expected[1],), \
"metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.called, "metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.call_args.args == (expected[1],), "metric name: {}".format(
expected[0]
)


def test_metrics_with_only_404(get_and_clear_up_context): # noqa: F811
Expand All @@ -157,11 +157,11 @@ def test_metrics_with_only_404(get_and_clear_up_context): # noqa: F811
("datasets_unregistered", 0),
("datasets_expired", 0),
("datasets_head_request_non_200", 0), # 0 b/c HEAD reqs only after successful dl
("datasets_downloads_non_200", 1)
("datasets_downloads_non_200", 1),
]

for expected in expected_results:
assert context["prom_metrics"][expected[0]].set.called, \
"metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.call_args.args == (expected[1],), \
"metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.called, "metric name: {}".format(expected[0])
assert context["prom_metrics"][expected[0]].set.call_args.args == (expected[1],), "metric name: {}".format(
expected[0]
)
4 changes: 2 additions & 2 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_config_blob_storage_base_url_has_no_trailing_slash_1():

config = get_basic_config()

assert config["WEB_BASE_URL"] == 'http://127.0.0.1:10000/devstoreaccount1'
assert config["WEB_BASE_URL"] == "http://127.0.0.1:10000/devstoreaccount1"


def test_config_blob_storage_base_url_has_no_trailing_slash_2():
Expand All @@ -21,7 +21,7 @@ def test_config_blob_storage_base_url_has_no_trailing_slash_2():

config = get_basic_config()

assert config["WEB_BASE_URL"] == 'http://127.0.0.1:10000/devstoreaccount1'
assert config["WEB_BASE_URL"] == "http://127.0.0.1:10000/devstoreaccount1"


def test_config_dataset_timeouts_loaded():
Expand Down
27 changes: 16 additions & 11 deletions tests/unit/test_dataset_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@
def get_level1_field_blanker(key):
def field_blanker(dict, attribute_value, key=key):
dict[key] = attribute_value

return partial(field_blanker, key=key)


def get_level2_field_blanker(key1, key2):
def field_blanker(dict, attribute_value, key1=key1, key2=key2):
dict[key1][key2] = attribute_value

return partial(field_blanker, key1=key1, key2=key2)


@pytest.mark.parametrize("field_blanker", [
get_level1_field_blanker("id"),
get_level1_field_blanker("name"),
get_level1_field_blanker("organization"),
get_level1_field_blanker("extras"),
get_level2_field_blanker("organization", "id"),
get_level2_field_blanker("organization", "name")
])
@pytest.mark.parametrize(
"field_blanker",
[
get_level1_field_blanker("id"),
get_level1_field_blanker("name"),
get_level1_field_blanker("organization"),
get_level1_field_blanker("extras"),
get_level2_field_blanker("organization", "id"),
get_level2_field_blanker("organization", "name"),
],
)
@pytest.mark.parametrize("attribute_value", [None, "None", ""])
def test_incomplete_necessary_data_from_ckan(field_blanker, attribute_value):

Expand All @@ -40,7 +45,7 @@ def test_incomplete_necessary_data_from_ckan(field_blanker, attribute_value):

ckan_datasets = clean_datasets_metadata(logger, ckan_datasets)

assert(len(ckan_datasets) == 0)
assert len(ckan_datasets) == 0


def test_create_empty_dataset_error_occurred_defaults_to_false():
Expand All @@ -62,5 +67,5 @@ def test_missing_url_from_ckan(resources_value):

registered_datasets_dtos = convert_datasets_metadata(ckan_datasets)

assert(len(registered_datasets_dtos) == 1)
assert(registered_datasets_dtos[uuid.UUID("c8a40aa5-9f31-4bcf-a36f-51c1fc2cc159")]["source_url"] == "")
assert len(registered_datasets_dtos) == 1
assert registered_datasets_dtos[uuid.UUID("c8a40aa5-9f31-4bcf-a36f-51c1fc2cc159")]["source_url"] == ""
30 changes: 18 additions & 12 deletions tests/unit/test_utilities_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
from utilities.http import add_qs_params_to_url, parse_last_modified_header


@pytest.mark.parametrize("input,expected", [
("Fri, 06 Sep 2024 13:08:28 GMT", datetime.datetime(2024, 9, 6, 13, 8, 28, 0, datetime.timezone.utc)),
("Wed, 21 Oct 2015 07:28:00 GMT", datetime.datetime(2015, 10, 21, 7, 28, 0, 0, datetime.timezone.utc)),
("Wed, 21 Oct 2015 07:28:00", None),
("Wed, 21 October 2015 07:28:00 +00:00", None),
])
@pytest.mark.parametrize(
"input,expected",
[
("Fri, 06 Sep 2024 13:08:28 GMT", datetime.datetime(2024, 9, 6, 13, 8, 28, 0, datetime.timezone.utc)),
("Wed, 21 Oct 2015 07:28:00 GMT", datetime.datetime(2015, 10, 21, 7, 28, 0, 0, datetime.timezone.utc)),
("Wed, 21 Oct 2015 07:28:00", None),
("Wed, 21 October 2015 07:28:00 +00:00", None),
],
)
def test_parse_http_last_modified_header(input, expected):
assert parse_last_modified_header(input) == expected


@pytest.mark.parametrize("input,params,expected", [
("http://www.a.com", {}, "http://www.a.com"),
("http://www.a.com", {"one": 1}, "http://www.a.com?one=1"),
("http://www.a.com", {"one": 1, "two": 2}, "http://www.a.com?one=1&two=2"),
("http://www.a.com?one=1", {"one": "updated"}, "http://www.a.com?one=updated"),
])
@pytest.mark.parametrize(
"input,params,expected",
[
("http://www.a.com", {}, "http://www.a.com"),
("http://www.a.com", {"one": 1}, "http://www.a.com?one=1"),
("http://www.a.com", {"one": 1, "two": 2}, "http://www.a.com?one=1&two=2"),
("http://www.a.com?one=1", {"one": "updated"}, "http://www.a.com?one=updated"),
],
)
def test_add_qs_params_to_url(input, params, expected):
assert add_qs_params_to_url(input, params) == expected
Loading
Loading