Skip to content

Commit

Permalink
Improve get cases (#110)
Browse files Browse the repository at this point in the history
* Fix error in docstring and add tests for get_cases

* Remove unused tests

* test against Sumo dev environment

* use search, not searchroot
  • Loading branch information
perolavsvendsen authored Dec 23, 2022
1 parent f63d1d4 commit 14def25
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 121 deletions.
13 changes: 9 additions & 4 deletions src/fmu/sumo/explorer/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ def get_case_by_id(self, sumo_id: str) -> Case:

return Case(self.sumo, hits[0])

def get_cases(self, status=None, fields=None, users=None) -> DocumentCollection:
def get_cases(
self,
status: List[str] = None,
fields: List[str] = None,
users: List[str] = None,
) -> DocumentCollection:
"""Returns all cases in given sumo environment.
args:
status (str or None): filter on status
fields (list or None): filter on field(s)
users (list or None): filter on user(s)
status (list[str] or None): filter on status
fields (list[str] or None): filter on field(s)
users (list[str] or None): filter on user(s)
"""
query_string = "class:case"

Expand Down
4 changes: 2 additions & 2 deletions src/fmu/sumo/uploader/caseondisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ def _get_sumo_parent_id(self):
pass # Not a valid uuid, will call Sumo

# No valid cached file, need to call Sumo to get the parent id
query = f"fmu.case.uuid:{self.fmu_case_uuid}"
query = f"class:case AND fmu.case.uuid:{self.fmu_case_uuid}"
search_results = self.sumo_connection.api.get(
"/searchroot", query=query, size=2, **{"from": 0}
"/search", query=query, size=2, **{"from": 0}
)

# To catch crazy rare situation when index is empty (first upload to new index)
Expand Down
2 changes: 2 additions & 0 deletions tests/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ def add_path():

from fmu.sumo.explorer import Explorer
import fmu.sumo.explorer._utils as ut
from fmu.sumo.explorer._document_collection import DocumentCollection
from fmu.sumo.explorer._case import Case
151 changes: 41 additions & 110 deletions tests/test_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from pathlib import Path
from uuid import UUID
import pytest
from context import Explorer, ut
from context import Explorer, Case, DocumentCollection, ut


TEST_DATA = Path("data")
logging.basicConfig(level="DEBUG")
LOGGER = logging.getLogger()
LOGGER.debug("Tjohei")


@pytest.fixture(name="the_logger")
Expand All @@ -22,38 +21,24 @@ def fixture_the_logger():

@pytest.fixture(name="case_name")
def fixture_case_name():
"""Returns case name
"""
"""Returns case name"""
return "21.x.0.dev_rowh2022_08-17"


@pytest.fixture(name="test_explorer")
def fixture_test_explorer(token):
@pytest.fixture(name="explorer")
def fixture_explorer(token):
"""Returns explorer"""
return Explorer("test", token=token)


# @pytest.fixture(name="prod_explorer")
# def fixture_prod_explorer(token):
# """Returns explorer"""
# return Explorer("prod", token=token)
return Explorer("dev", token=token)


@pytest.fixture(name="test_case")
def fixture_test_case(test_explorer, case_name):
"""Basis for test of method get_case_by_name for Explorer,
but also other attributes
but also other attributes
"""
return test_explorer.get_case_by_name(case_name)


# @pytest.fixture(name="sum_case")
# def fixture_sum_case(token):
# """Gets case with summary data from prod"""
# exp = Explorer("prod",token=token)
# return exp.get_case_by_name("drogon_design_2022_11-01")


def write_json(result_file, results):
"""writes json files to disc
args:
Expand Down Expand Up @@ -114,20 +99,10 @@ def assert_dict_equality(results, correct):
correct (dict): the one to compare to
"""
incorrect_mess = (
f"the dictionary produced ({results}) is not equal to \n" +
f" ({correct})")
f"the dictionary produced ({results}) is not equal to \n" + f" ({correct})"
)
assert results == correct, incorrect_mess

# Come back to this
# def test_logger(caplog):
# """Tests the defined logger in explorer"""
# logger_name = "tests"
# logger = ut.init_logging(logger_name, "debug")
# message = "works!"
# logger.debug(message)
# with caplog:
# assert caplog.record_tuples == [(logger_name, logging.DEBUG, message)]


def test_cast_toomany_warning():
"""Tests custom made warning"""
Expand Down Expand Up @@ -173,94 +148,50 @@ def test_toolowsize_warning_content():
assert warn_message == test_message, assert_mess


# def test_sumo_id_attribute(sum_case):
# """Tests getting sumo_id
# args
# test_explorer (sumo.Explorer):
# """
# assert_correct_uuid(sum_case.sumo_id)


# def test_get_dict_of_case_names(prod_explorer):
# """tests method get_dict_of_cases
# """

# assert_uuid_dict(prod_explorer.get_dict_of_case_names())


# def test_func_get_surface_object_ids(the_logger, sum_case):
# """Tests method get_object_blob_ids"""
def test_get_cases(explorer):
"""Test the get_cases method."""

# results = ut.get_object_ids(sum_case, data_type="surface", content="depth",
# name="VOLANTIS GP. Base",
# tag="FACIES_Fraction_Offshore", iteration=0,
# )
# # |result_file = "dict_of_surface_blob_ids.json"
cases = explorer.get_cases()
assert isinstance(cases, DocumentCollection)
assert isinstance(cases[0], Case)

# # write_json(result_file, results)
# # correct = read_json(result_file)

# assert len(results) == 155
# assert_uuid_dict(results)
# assert_dict_equality(results, correct)


# def test_funct_get_surface_object_ids_w_aggregation(sum_case):

# """Tests function get_object_blob_ids with aggregation"""
# results = ut.get_aggregated_object_ids(sum_case, data_type="surface", content="depth",
# name="VOLANTIS GP. Base",
# tag="FACIES_Fraction_Offshore", iteration=0,
# aggregation="*"
# )
# assert len(results.keys()) == 1
# for surf_name in results:
# assert len(results[surf_name]) == 7
# assert isinstance(surf_name, str)
# assert_uuid_dict(results[surf_name])


# def test_func_get_sum_object_ids(the_logger, sum_case):
# """Tests method get_object_blob_ids"""
# results = ut.get_object_ids(sum_case, data_type="table",
# content="timeseries",
# )
# # result_file = "dict_of_sum_blob_ids.json"

# # write_json(result_file, results)

# # correct = read_json(result_file)

# assert len(results) == 974
# assert_uuid_dict(results)
# # assert_dict_equality(results, correct)
def test_get_cases_fields(explorer):
"""Test get_cases method with the fields argument.
Shall be case insensitive.
"""

# def test_method_get_surface_object_ids(the_logger, sum_case):
# """Tests method get_object_blob_ids"""
cases = explorer.get_cases(fields=["dRoGoN"])
for case in cases:
assert case.field_name.lower() == "drogon"

# results = sum_case.get_object_ids("VOLANTIS GP. Base",
# "FACIES_Fraction_Offshore")

# # result_file = "dict_of_surface_blob_ids.json"
def test_get_cases_status(explorer):
"""Test the get_cases method with the status argument."""

# # write_json(result_file, results)
# # correct = read_json(result_file)
cases = explorer.get_cases(status=["keep"])
for case in cases:
assert case.status == "keep"

# assert len(results) == 155
# assert_uuid_dict(results)
# # assert_dict_equality(results, correct)

def test_get_cases_user(explorer):
"""Test the get_cases method with the users argument."""

# def test_method_get_sum_object_ids(the_logger, sum_case):
# """Tests method get_object_blob_ids"""
# results = sum_case.get_summary_object_ids()
# # result_file = "dict_of_sum_blob_ids.json"
cases = explorer.get_cases(users=["peesv"])
for case in cases:
assert case.user == "peesv"

# # write_json(result_file, results)

# # |correct = read_json(result_file)
def test_get_cases_combinations(explorer):
"""Test the get_cases method with combined arguments."""

# assert len(results) == 974
# assert_uuid_dict(results)
# # assert_dict_equality(results, correct)
cases = explorer.get_cases(
fields=["Drogon", "Johan_Sverdrup"], users=["peesv", "dbs"], status=["keep"]
)
for case in cases:
assert (
case.user in ["peesv", "dbs"]
and case.field_name.lower() in ["drogon", "johan_sverdrup"]
and case.status == "keep"
)
10 changes: 5 additions & 5 deletions tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def test_case(token):
sumo_connection=sumo_connection,
)

query = f"fmu.case.uuid:{e.fmu_case_uuid}"
query = f"class:case AND fmu.case.uuid:{e.fmu_case_uuid}"

# assert that it is not there in the first place
logger.debug("Asserting that the test case is not already there")
search_results = sumo_connection.api.get(
"/searchroot", query=query, size=100, **{"from": 0}
"/search", query=query, size=100, **{"from": 0}
)
logger.debug("search results: %s", str(search_results))
if not search_results:
Expand All @@ -103,7 +103,7 @@ def test_case(token):
# assert that it is there now
time.sleep(3) # wait 3 seconds
search_results = sumo_connection.api.get(
"/searchroot", query=query, size=100, **{"from": 0}
"/search", query=query, size=100, **{"from": 0}
)
hits = search_results.get("hits").get("hits")
logger.debug(search_results.get("hits"))
Expand Down Expand Up @@ -246,9 +246,9 @@ def test_teardown(token):
time.sleep(30) # Sumo removes the container

# Assert children is not on Sumo
query = f"{e.fmu_case_uuid}"
query = f"class:case AND {e.fmu_case_uuid}"
search_results = sumo_connection.api.get(
"/searchroot", query=query, size=100, **{"from": 0}
"/search", query=query, size=100, **{"from": 0}
)
total = search_results["hits"]["total"]["value"]
assert total == 0
Expand Down

0 comments on commit 14def25

Please sign in to comment.