Skip to content

Commit

Permalink
Merge pull request #190 from aodn/testing/189-add-search-page-tests
Browse files Browse the repository at this point in the history
Playwright: Add search page tests
  • Loading branch information
utas-raymondng authored Oct 22, 2024
2 parents 0ed1fc0 + 0411ced commit 059c31c
Show file tree
Hide file tree
Showing 32 changed files with 7,009 additions and 889 deletions.
6 changes: 6 additions & 0 deletions playwright/core/enums/search_sort_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class SearchSortType(Enum):
TITLE = 'Title'
MODIFIED = 'Modified'
13 changes: 9 additions & 4 deletions playwright/mocks/api/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from utils.json_utils import filter_collection_by_id, load_json_data


def handle_collections_bbox_api(route: Route) -> None:
json_data = load_json_data('collections_bbox.json')
def handle_collections_centroid_api(route: Route) -> None:
json_data = load_json_data('collections_centroid.json')
route.fulfill(json=json_data)


Expand All @@ -16,8 +16,8 @@ def handle_collections_all_api(route: Route) -> None:
route.fulfill(json=json_data)


def handle_collections_update_bbox_api(route: Route) -> None:
json_data = load_json_data('collections_update_bbox.json')
def handle_collections_update_centroid_api(route: Route) -> None:
json_data = load_json_data('collections_update_centroid.json')
route.fulfill(json=json_data)


Expand All @@ -39,3 +39,8 @@ def handle_collections_popup_api(route: Route) -> None:

collection = filter_collection_by_id(data, data_id)
route.fulfill(json=collection)


def handle_collections_show_more_api(route: Route) -> None:
json_data = load_json_data('show_more_results.json')
route.fulfill(json=json_data)
65 changes: 65 additions & 0 deletions playwright/mocks/api/search_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
from datetime import datetime, timezone
from json import JSONDecodeError
from typing import Any, Dict, List

from playwright.sync_api import Route

from utils.json_utils import load_json_data


def handle_sort_by_relevance(route: Route) -> None:
json_data = load_json_data('sorted_by_relevance.json')
route.fulfill(json=json_data)


def handle_sort_by_title(route: Route) -> None:
json_data = load_json_data('sorted_by_relevance.json')
json_data['collections'].sort(key=lambda x: x['title'])
route.fulfill(json=json_data)


def handle_sort_by_modified(route: Route) -> None:
json_data = load_json_data('sorted_by_relevance.json')
sorted_json = sort_collections_by_temporal_end(json_data)
route.fulfill(json=sorted_json)


def get_latest_end_date(temporal_data: List[Dict[str, Any]]) -> datetime:
"""Extracts the latest 'end' date from the 'temporal' array.
If no 'end' date is available, returns the current time."""
current_time = datetime.now(timezone.utc)
end_dates = []

# Collect valid 'end' dates
for period in temporal_data:
if 'end' in period and period['end']:
end_dates.append(
datetime.fromisoformat(period['end'].replace('Z', '+00:00'))
)

# Return the latest date, or current time if none are available
return max(end_dates, default=current_time)


def sort_collections_by_temporal_end(data: Dict[str, Any]) -> Dict[str, Any]:
"""Sorts the collections based on the 'temporal'->'end' date in corresponding JSON files."""

# Helper to fetch temporal end date from corresponding file
def fetch_temporal_end_date(collection: Dict[str, Any]) -> datetime:
collection_id = collection['id']
json_file_path = os.path.join('dataset_detail', f'{collection_id}.json')

# Load corresponding JSON data
try:
file_data = load_json_data(json_file_path)
temporal_data = file_data.get('properties', {}).get('temporal', [])
return get_latest_end_date(temporal_data)
except JSONDecodeError as error:
raise ValueError(
f'The file contains invalid JSON data: {json_file_path}'
) from error

# Sort collections based on the temporal end date from the corresponding file
data['collections'].sort(key=fetch_temporal_end_date, reverse=True)
return data
11 changes: 7 additions & 4 deletions playwright/mocks/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,30 @@ def unroute_autocomplete(

def route_collection(
self,
bbox_handler: Callable,
centroid_handler: Callable,
all_handler: Callable,
popup_handler: Optional[Callable] = None,
) -> None:
self.route(Routes.COLLECTION_BBOX, bbox_handler)
self.route(Routes.COLLECTION_CENTROID, centroid_handler)
self.route(Routes.COLLECTION_ALL, all_handler)
if popup_handler is not None:
self.route(Routes.COLLECTION_POPUP, popup_handler)
self.route(Routes.COLLECTION_SELECTED, popup_handler)

def unroute_collection(
self,
bbox_handler: Optional[Callable] = None,
centroid_handler: Optional[Callable] = None,
all_handler: Optional[Callable] = None,
popup_handler: Optional[Callable] = None,
) -> None:
self.unroute(Routes.COLLECTION_BBOX, bbox_handler)
self.unroute(Routes.COLLECTION_CENTROID, centroid_handler)
self.unroute(Routes.COLLECTION_ALL, all_handler)
self.unroute(Routes.COLLECTION_POPUP, popup_handler)
self.unroute(Routes.COLLECTION_SELECTED, popup_handler)

def route_collection_all(self, handler_function: Callable) -> None:
self.route(Routes.COLLECTION_ALL, handler_function)

def route_collection_detail(self, handler_function: Callable) -> None:
self.route(Routes.COLLECTION_DETAIL, handler_function)

Expand Down
800 changes: 2 additions & 798 deletions playwright/mocks/mock_data/collections_all.json

Large diffs are not rendered by default.

Loading

0 comments on commit 059c31c

Please sign in to comment.