Skip to content

refactor tests to not need sleep, use fixture for setup/teardown #69

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

Merged
merged 2 commits into from
Mar 22, 2022

Conversation

philvarner
Copy link
Collaborator

@philvarner philvarner commented Mar 22, 2022

Related Issue(s):

Description:

  • refactor tests to not need sleep
  • use fixture for setup/teardown of most tests
  • remove use of execute methods in ES DSL

PR Checklist:

  • Code is formatted and linted (run pre-commit run --all-files)
  • Tests pass (run make test)
  • Documentation has been updated to reflect changes, if applicable, and docs build successfully (run make docs)
  • Changes are added to the changelog

…ost tests, remove use of execute methods in ES DSL
addopts = -sv
asyncio_mode = auto
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not having this gave a warning, and we'll need it for making everything async

@@ -29,6 +29,7 @@
"requests",
"ciso8601",
"overrides",
"starlette",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have use some classes out of this package, but it was not explicitly deps

Copy link
Collaborator

@jonhealy1 jonhealy1 Mar 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently had to add this too for a fresh pgstac stac-fastapi backend that we're testing at work

@@ -243,9 +243,8 @@ def post_search(self, search_request, **kwargs) -> ItemCollection:
for sort in search_request.sortby:
if sort.field == "datetime":
sort.field = "properties__datetime"
field = sort.field + ".keyword"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all sortable fields are keyword by default, so they don't need this set.

else:
item = self.database.prep_create_item(item=item, base_url=base_url)
self.database.create_item(item=item, base_url=base_url)
self.database.create_item(item, refresh=kwargs.get("refresh", False))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't wired this through to the API yet, but calling this with "refresh=true" will cause it to refresh the index, so the item is immediately visible to a subsequent search

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@@ -79,7 +89,11 @@ def get_item_collection(

# search = search.sort({"id.keyword" : {"order" : "asc"}})
search = search.query()[0:limit]
collection_children = search.execute().to_dict()

body = search.to_dict()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switched this from using the ES DSL method on search to generating the search and calling client.search, because we're going to convert this to be async next, and that doesn't work with the DSL execute and count methods.

{"id": {"order": "desc"}},
{"collection": {"order": "desc"}},
)
return Search().sort(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client and index no longer need to be defined in the search b/c we're not executing on it.

search = search.query(collection_filter)

return search
collections_query = [Q("term", **{"collection": cid}) for cid in collection_ids]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

term is an exact match, rather than match_phrase which does something else

item = load_test_data("test_item.json")
es_transactions.create_item(item, request=MockStarletteRequest)

def test_app_search_response(app_client, ctx):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ctx fixture clears the indexes, loads an item, runs the tests, and then clears the indexes again

@@ -123,238 +116,146 @@ def test_app_context_extension(load_test_data, app_client, es_transactions, es_c
assert "context" in resp_json
assert resp_json["context"]["returned"] == resp_json["context"]["matched"] == 1

es_transactions.delete_collection(collection["id"], request=MockStarletteRequest)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed tons of these calls, as it's handled by ctx now

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

@philvarner philvarner changed the title refactor tests to not need sleep, use fixture for setup/teardown of m… refactor tests to not need sleep, use fixture for setup/teardown Mar 22, 2022
@philvarner philvarner marked this pull request as ready for review March 22, 2022 01:19
Copy link
Collaborator

@jonhealy1 jonhealy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@philvarner philvarner merged commit a466f75 into main Mar 22, 2022
@philvarner philvarner deleted the pv/refactor-tests-and-remove-es-dsl branch March 22, 2022 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants