Skip to content

Commit

Permalink
test(tests): added extract resource test [2024-11-25]
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Nov 25, 2024
1 parent 97e1d79 commit cefb472
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
27 changes: 16 additions & 11 deletions HerdingCats/explorer/cat_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ def package_show_info_json(self, package_name: Union[str, dict, Any]) -> List[Di
url = f"{base_url}?{urlencode(params)}" if params else base_url

try:
print(url)
response = self.cat_session.session.get(url)
response.raise_for_status()
data = response.json()
Expand Down Expand Up @@ -531,11 +530,17 @@ def package_search_condense_json_unpacked(
url to download
# Example usage...
if __name__ == "__main__":
with CatSession("data.london.gov.uk") as session:
explorer = CatExplorer(session)
condensed_results = explorer.package_search_condense_json_unpacked("police")
pprint(condensed_results)
import HerdingCats as hc
from pprint import pprint
def main():
with hc.CatSession(hc.CkanDataCatalogues.LONDON_DATA_STORE) as session:
explore = hc.CkanCatExplorer(session)
packages_search = explore.package_search_condense_json_unpacked("police", 50)
pprint(packages_search)
if __name__ =="__main__":
main()
"""
base_url = self.cat_session.base_url + CkanApiPaths.PACKAGE_SEARCH
Expand Down Expand Up @@ -790,6 +795,11 @@ def extract_resource_url(
Returns:
List[format, url]: The format of the resource and the URL.
[
'spreadsheet',
'https://data.london.gov.uk/download/violence-reduction-unit/1ef840d0-2c02-499c-ae40-382005b2a0c7/VRU%2520Dataset%2520Q1%2520April-Nov%25202023.xlsx'
]
Example:
if __name__ == "__main__":
Expand All @@ -801,11 +811,6 @@ def extract_resource_url(
dl_link = explore.extract_resource_url(info, "VRU Q1 2023-24 Dataset")
print(dl_link)
[
'spreadsheet',
'https://data.london.gov.uk/download/violence-reduction-unit/1ef840d0-2c02-499c-ae40-382005b2a0c7/VRU%2520Dataset%2520Q1%2520April-Nov%25202023.xlsx'
]
"""

for item in package_info:
Expand Down
32 changes: 32 additions & 0 deletions tests/ckan/test_ckan_extract_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from HerdingCats.session.cat_session import CatSession
from HerdingCats.explorer.cat_explore import CkanCatExplorer
from HerdingCats.errors.cats_errors import CatExplorerError
from loguru import logger

CATALOGUES = ["https://data.london.gov.uk"]
TEST_PACKAGE = "violence-reduction-unit"
TEST_RESOURCE = "VRU Q1 2023-24 Dataset"

def test_extract_resource_url():
"""Test successful resource URL extraction"""
with CatSession(CATALOGUES[0]) as cat_session:
explorer = CkanCatExplorer(cat_session)
try:
# First get package info
package_info = explorer.package_show_info_json(TEST_PACKAGE)

# Extract resource URL
result = explorer.extract_resource_url(package_info, TEST_RESOURCE)

logger.success(result)

# Assertions
assert result is not None, "Should return result for known resource"
assert isinstance(result, list), "Should return a list"
assert len(result) == 2, "Should return format and URL"
assert isinstance(result[0], str), "Format should be string"
assert isinstance(result[1], str), "URL should be string"

except Exception as e:
pytest.fail(f"Failed to extract resource URL: {str(e)}")

0 comments on commit cefb472

Please sign in to comment.