Skip to content

Commit

Permalink
DAS-2146: Adds tests for asset_from_item
Browse files Browse the repository at this point in the history
Tests each case for finding correct asset in Item.
  • Loading branch information
flamingbear committed May 28, 2024
1 parent 55574f7 commit 6dbde03
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
11 changes: 8 additions & 3 deletions harmony_browse_image_generator/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ def validate_message(self):
)

def asset_from_item(self, item: Item) -> Asset:
"""Returns the correct asset from a stac Item.
"""Returns the correct browse asset from a stac Item.
This is used to select which asset is used by HyBIG to generate
the browse image following these steps:
1. If found, return the first asset with 'visual' in any of the item's values' roles.
2. If found, return the first asset that has 'data' in its item's values' roles.
3. Rasie a StopIteration error.
Find an asset with 'visual' in any of the item's values' roles.
If not found return the asset that has 'data' in its item's values' roles.
"""
try:
return next(
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/test_adapter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from datetime import datetime
from unittest import TestCase
from unittest.mock import Mock

from harmony.message import Message
from harmony.util import config
from pystac import Asset, Item

from harmony_browse_image_generator.adapter import BrowseImageGeneratorAdapter
from harmony_browse_image_generator.exceptions import HyBIGInvalidMessageError
Expand Down Expand Up @@ -152,3 +155,52 @@ def test_create_output_stac_items(self):
self.assertEqual(
output_stac_item.assets['auxiliary'].title, 'browse.png.aux.xml'
)


class TestAdapterAssetFromItem(TestCase):
"""A class testing asset_from_item class."""

def setUp(self):
self.adapter = BrowseImageGeneratorAdapter({}, {})

def item_fixture(self, assets: dict) -> Item:
item = Item(Mock(), None, None, datetime.now(), {})
item.assets = assets
return item

def test_asset_from_item_with_visual_role(self):
visual_asset = Asset(Mock(), roles=['visual'])
other_asset = Asset(Mock(), roles=['data'])
item = self.item_fixture({'data': other_asset, 'visual': visual_asset})

expected = visual_asset

actual = self.adapter.asset_from_item(item)

self.assertEqual(expected, actual)

def test_asset_from_item_with_data_role(self):
data_asset = Asset(Mock(), roles=['data'])
other_asset = Asset(Mock(), roles=['something'])
item = self.item_fixture({'data': data_asset, 'other': other_asset})
expected = data_asset

actual = self.adapter.asset_from_item(item)

self.assertEqual(expected, actual)

def test_asset_from_item_no_roles(self):
asset_1 = Asset(Mock())
asset_2 = Asset(Mock())

item = self.item_fixture({'first': asset_1, 'second': asset_2})
with self.assertRaises(StopIteration):
self.adapter.asset_from_item(item)

def test_asset_from_item_no_matching_roles(self):
asset_1 = Asset(Mock(), roles=['something'])
asset_2 = Asset(Mock(), roles=['or another'])

item = self.item_fixture({'first': asset_1, 'second': asset_2})
with self.assertRaises(StopIteration):
self.adapter.asset_from_item(item)

0 comments on commit 6dbde03

Please sign in to comment.