Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deflake serializer tests. #449

Merged
merged 4 commits into from
Aug 14, 2024
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
22 changes: 14 additions & 8 deletions app/tests/tests_09/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
from datetime import timedelta
from datetime import datetime
from pprint import pformat
from urllib.parse import urlparse

Expand All @@ -14,6 +14,8 @@
from stac_api.utils import fromisoformat
from stac_api.utils import get_link
from stac_api.utils import get_provider
from stac_api.utils import isoformat
from stac_api.utils import utc_aware

from tests.utils import get_http_error_description

Expand Down Expand Up @@ -437,13 +439,17 @@ def _check_value(self, path, key, value, current, ignore):
else:
self._check_stac_list(path, sorted(value), sorted(current[key]))
elif key in ['created', 'updated']:
# created and updated time are automatically set therefore don't do an exact
# test as we can't guess the exact time.
self.assertAlmostEqual(
fromisoformat(value),
fromisoformat(current[key]),
delta=timedelta(seconds=3),
msg=f'{path}: current datetime value is not equal to the expected'
# Created and updated time are automatically set therefore don't do an exact
# test as we can't guess the exact time. So we just check these timestamps
# are from after the start of the test and before "now".
self.assertLessEqual(
value,
current[key],
msg=f'{path}: current datetime value is before test start time'
)
now = isoformat(utc_aware(datetime.now()))
self.assertGreaterEqual(
now, current[key], msg=f'{path}: current datetime value is after test end time'
)
elif key == 'href':
self.assertEqual(
Expand Down
12 changes: 6 additions & 6 deletions app/tests/tests_09/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CollectionSerializationTestCase(StacBaseTransactionTestCase):
@mock_s3_asset_file
def setUp(self):
self.data_factory = Factory()
self.collection_created = utc_aware(datetime.now())
self.collection_created_after = utc_aware(datetime.now())
self.collection = self.data_factory.create_collection_sample(db_create=True)
self.item = self.data_factory.create_item_sample(
collection=self.collection.model, db_create=True
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_collection_serialization(self):

expected = self.collection.get_json('serialize')
expected.update({
'created': isoformat(self.collection_created),
'created': isoformat(self.collection_created_after),
'crs': ['http://www.opengis.net/def/crs/OGC/1.3/CRS84'],
'extent': {
'spatial': {
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_collection_serialization(self):
'geoadmin:variant': ['kgrs'],
'proj:epsg': [2056],
},
'updated': isoformat(self.collection_created)
'updated': isoformat(self.collection_created_after)
})
self.check_stac_collection(expected, python_native)

Expand All @@ -144,7 +144,7 @@ class EmptyCollectionSerializationTestCase(StacBaseTransactionTestCase):

def setUp(self):
self.data_factory = Factory()
self.collection_created = utc_aware(datetime.now())
self.collection_created_after = utc_aware(datetime.now())
self.collection = self.data_factory.create_collection_sample(db_create=True)
self.maxDiff = None # pylint: disable=invalid-name

Expand All @@ -166,7 +166,7 @@ def test_empty_collection_serialization(self):

expected = self.collection.get_json('serialize')
expected.update({
'created': isoformat(self.collection_created),
'created': isoformat(self.collection_created_after),
'crs': ['http://www.opengis.net/def/crs/OGC/1.3/CRS84'],
'extent': {
'spatial': {
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_empty_collection_serialization(self):
],
'stac_version': STAC_VERSION,
'summaries': {},
'updated': isoformat(self.collection_created)
'updated': isoformat(self.collection_created_after)
})
self.check_stac_collection(expected, python_native)

Expand Down
22 changes: 14 additions & 8 deletions app/tests/tests_10/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
from datetime import timedelta
from datetime import datetime
from pprint import pformat
from urllib.parse import urlparse

Expand All @@ -14,6 +14,8 @@
from stac_api.utils import fromisoformat
from stac_api.utils import get_link
from stac_api.utils import get_provider
from stac_api.utils import isoformat
from stac_api.utils import utc_aware

from tests.utils import get_http_error_description

Expand Down Expand Up @@ -438,13 +440,17 @@ def _check_value(self, path, key, value, current, ignore):
else:
self._check_stac_list(path, sorted(value), sorted(current[key]))
elif key in ['created', 'updated']:
# created and updated time are automatically set therefore don't do an exact
# test as we can't guess the exact time.
self.assertAlmostEqual(
fromisoformat(value),
fromisoformat(current[key]),
delta=timedelta(seconds=2),
msg=f'{path}: current datetime value is not equal to the expected'
# Created and updated time are automatically set therefore don't do an exact
# test as we can't guess the exact time. So we just check these timestamps
# are from after the start of the test and before "now".
self.assertLessEqual(
value,
current[key],
msg=f'{path}: current datetime value is before test start time'
)
now = isoformat(utc_aware(datetime.now()))
self.assertGreaterEqual(
now, current[key], msg=f'{path}: current datetime value is after test end time'
)
elif key == 'href':
self.assertEqual(
Expand Down
12 changes: 6 additions & 6 deletions app/tests/tests_10/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CollectionSerializationTestCase(StacBaseTransactionTestCase):
@mock_s3_asset_file
def setUp(self):
self.data_factory = Factory()
self.collection_created = utc_aware(datetime.now())
self.collection_created_after = utc_aware(datetime.now())
self.collection = self.data_factory.create_collection_sample(db_create=True)
self.item = self.data_factory.create_item_sample(
collection=self.collection.model, db_create=True
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_collection_serialization(self):

expected = self.collection.get_json('serialize')
expected.update({
'created': isoformat(self.collection_created),
'created': isoformat(self.collection_created_after),
'crs': ['http://www.opengis.net/def/crs/OGC/1.3/CRS84'],
'extent': {
'spatial': {
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_collection_serialization(self):
'geoadmin:variant': ['kgrs'],
'proj:epsg': [2056],
},
'updated': isoformat(self.collection_created)
'updated': isoformat(self.collection_created_after)
})
self.check_stac_collection(expected, python_native)

Expand All @@ -141,7 +141,7 @@ class EmptyCollectionSerializationTestCase(StacBaseTransactionTestCase):

def setUp(self):
self.data_factory = Factory()
self.collection_created = utc_aware(datetime.now())
self.collection_created_after = utc_aware(datetime.now())
self.collection = self.data_factory.create_collection_sample(db_create=True)
self.maxDiff = None # pylint: disable=invalid-name

Expand All @@ -163,7 +163,7 @@ def test_empty_collection_serialization(self):

expected = self.collection.get_json('serialize')
expected.update({
'created': isoformat(self.collection_created),
'created': isoformat(self.collection_created_after),
'crs': ['http://www.opengis.net/def/crs/OGC/1.3/CRS84'],
'extent': {
'spatial': {
Expand Down Expand Up @@ -216,7 +216,7 @@ def test_empty_collection_serialization(self):
],
'stac_version': STAC_VERSION,
'summaries': {},
'updated': isoformat(self.collection_created)
'updated': isoformat(self.collection_created_after)
})
self.check_stac_collection(expected, python_native)

Expand Down