Skip to content

Commit 6bea3e7

Browse files
committed
993 replace function with fixture
1 parent 0fcff09 commit 6bea3e7

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# TODO move all test case code to this file
22

3+
import json
34
import shutil
45
import uuid
56
from datetime import datetime
67
from pathlib import Path
8+
from typing import Any
79

810
import pytest
911

@@ -61,6 +63,14 @@ def get_data_file(rel_path: str) -> str:
6163
return str(here / "data-files" / rel_path)
6264

6365

66+
@pytest.fixture
67+
def sample_item_dict() -> dict[str, Any]:
68+
m = TestCases.get_path("data-files/item/sample-item.json")
69+
with open(m) as f:
70+
item_dict: dict[str, Any] = json.load(f)
71+
return item_dict
72+
73+
6474
@pytest.fixture
6575
def sample_item() -> Item:
6676
return Item.from_file(TestCases.get_path("data-files/item/sample-item.json"))

tests/test_item.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,8 @@
2626
from tests.utils import TestCases, assert_to_from_dict
2727

2828

29-
def get_example_item_dict() -> dict[str, Any]:
30-
m = TestCases.get_path("data-files/item/sample-item.json")
31-
with open(m) as f:
32-
item_dict: dict[str, Any] = json.load(f)
33-
return item_dict
34-
35-
36-
def test_to_from_dict() -> None:
37-
item_dict = get_example_item_dict()
38-
param_dict = deepcopy(item_dict)
29+
def test_to_from_dict(sample_item_dict: dict[str, Any]) -> None:
30+
param_dict = deepcopy(sample_item_dict)
3931

4032
assert_to_from_dict(Item, param_dict)
4133
item = Item.from_dict(param_dict)
@@ -49,17 +41,16 @@ def test_to_from_dict() -> None:
4941
assert len(item.assets["thumbnail"].extra_fields) == 0
5042

5143
# test that the parameter is preserved
52-
assert param_dict == item_dict
44+
assert param_dict == sample_item_dict
5345

5446
# assert that the parameter is preserved regardless of preserve_dict
5547
Item.from_dict(param_dict, preserve_dict=False)
56-
assert param_dict == item_dict
48+
assert param_dict == sample_item_dict
5749

5850

59-
def test_from_dict_set_root() -> None:
60-
item_dict = get_example_item_dict()
51+
def test_from_dict_set_root(sample_item_dict: dict[str, Any]) -> None:
6152
catalog = pystac.Catalog(id="test", description="test desc")
62-
item = Item.from_dict(item_dict, root=catalog)
53+
item = Item.from_dict(sample_item_dict, root=catalog)
6354
assert item.get_root() is catalog
6455

6556

@@ -85,23 +76,20 @@ def test_set_self_href_none_ignores_relative_asset_hrefs() -> None:
8576
assert not is_absolute_href(asset.href)
8677

8778

88-
def test_asset_absolute_href() -> None:
79+
def test_asset_absolute_href(sample_item: Item) -> None:
8980
item_path = TestCases.get_path("data-files/item/sample-item.json")
90-
item_dict = get_example_item_dict()
91-
item = Item.from_dict(item_dict)
92-
item.set_self_href(item_path)
81+
sample_item.set_self_href(item_path)
9382
rel_asset = Asset("./data.geojson")
94-
rel_asset.set_owner(item)
83+
rel_asset.set_owner(sample_item)
9584
expected_href = make_posix_style(
9685
os.path.abspath(os.path.join(os.path.dirname(item_path), "./data.geojson"))
9786
)
9887
actual_href = rel_asset.get_absolute_href()
9988
assert expected_href == actual_href
10089

10190

102-
def test_asset_absolute_href_no_item_self() -> None:
103-
item_dict = get_example_item_dict()
104-
item = Item.from_dict(item_dict)
91+
def test_asset_absolute_href_no_item_self(sample_item_dict: dict[str, Any]) -> None:
92+
item = Item.from_dict(sample_item_dict)
10593
assert item.get_self_href() is None
10694

10795
rel_asset = Asset("./data.geojson")
@@ -160,10 +148,8 @@ def test_clearing_collection() -> None:
160148
assert item.get_collection() is collection
161149

162150

163-
def test_datetime_ISO8601_format() -> None:
164-
item_dict = get_example_item_dict()
165-
item = Item.from_dict(item_dict)
166-
formatted_time = item.to_dict()["properties"]["datetime"]
151+
def test_datetime_ISO8601_format(sample_item: Item) -> None:
152+
formatted_time = sample_item.to_dict()["properties"]["datetime"]
167153
assert "2016-05-03T13:22:30.040000Z" == formatted_time
168154

169155

0 commit comments

Comments
 (0)