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

[WIP] Remove MongoDB dependency from core tests #582

Merged
merged 26 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6b6669d
Remove MongoDB dependencies from tests/functional/commands
breuleux Mar 29, 2021
0bb0c5e
Remove MongoDB dependencies from tests/functional/branching
breuleux Mar 29, 2021
9ba9e7a
Remove MongoDB dependencies from tests/functional/parsing
breuleux Mar 29, 2021
1fa5341
Remove MongoDB dependencies from tests/functional/demo
breuleux Mar 29, 2021
5b2970f
Remove MongoDB dependencies from tests/unittest/core (partial)
breuleux Mar 29, 2021
3d8a3b5
Fix formatting
breuleux Mar 30, 2021
a566afe
Remove MongoDB dependencies from tests/functional/client
breuleux Mar 30, 2021
2c2ae14
Remove MongoDB dependencies from tests/unittests/core/worker/test_pro…
breuleux Mar 30, 2021
385164b
Remove MongoDB dependencies from tests/unittests/core/io
breuleux Mar 30, 2021
a021142
Fix parent_id in child_config fixture
breuleux Mar 30, 2021
2720be0
Remove create_db_instance fixture
breuleux Mar 30, 2021
e1777b9
Remove database from yaml configs
breuleux Mar 30, 2021
97aa169
Change default storage to PickledDB
breuleux Mar 30, 2021
569dfd7
Remove MongoDB dependencies
abergeron Mar 30, 2021
93ed65d
Revert "Change default storage to PickledDB"
breuleux Mar 30, 2021
699012c
Revert "Remove database from yaml configs"
breuleux Mar 30, 2021
67ca3c7
Use storage instead of pdatabase in evc
breuleux Mar 31, 2021
6ad9a1c
Nuke test_experiment_tree
breuleux Mar 31, 2021
10e0126
Remove pdatabase
breuleux Mar 31, 2021
055e1e6
Remove more uses of _fetch_trials
breuleux Mar 31, 2021
40c3b00
Remove useless usefixtures
breuleux Mar 31, 2021
f1bceb0
Merge branch 'develop' into rm_mongo_dep
breuleux Mar 31, 2021
9b35f07
Avoid OrionState in hacked_exp fixture to fix storage fixture issue
breuleux Mar 31, 2021
10243f4
Fix database_test::TestReadOnlyDatabase.test_read
breuleux Mar 31, 2021
cbf9e86
Re-trigger tests
breuleux Mar 31, 2021
0f81bd9
Fix bug in update_heartbeat
breuleux Apr 1, 2021
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
2 changes: 1 addition & 1 deletion src/orion/core/io/database/pickleddb.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def _dump_database(self, database):
log.error(
"Document in (collection: %s) is not pickable\ndoc: %s",
collection,
doc.to_dict(),
doc.to_dict() if hasattr(doc, "to_dict") else str(doc),
)

key, value = find_unpickable_field(doc)
Expand Down
2 changes: 1 addition & 1 deletion src/orion/storage/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def count_broken_trials(self, experiment):
def update_heartbeat(self, trial):
"""Update trial's heartbeat"""
return self.update_trial(
trial, heartbeat=datetime.datetime.utcnow(), status="reserved"
trial, heartbeat=datetime.datetime.utcnow(), where={"status": "reserved"}
)

def fetch_trials_by_status(self, experiment, status):
Expand Down
26 changes: 7 additions & 19 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from orion.core.io.database.pickleddb import PickledDB
from orion.core.utils.singleton import update_singletons
from orion.core.worker.trial import Trial
from orion.storage.base import Storage
from orion.storage.base import Storage, get_storage, setup_storage
from orion.storage.legacy import Legacy
from orion.testing import OrionState

Expand Down Expand Up @@ -315,24 +315,6 @@ def update_metadata(metadata):
monkeypatch.setattr(resolve_config, "update_metadata", update_metadata)


@pytest.fixture()
def create_db_instance(null_db_instances, clean_db):
"""Create and save a singleton database instance."""
try:
database = {
"type": "MongoDB",
"name": "orion_test",
"username": "user",
"password": "pass",
}
db = Storage(of_type="legacy", database=database)
except ValueError:
db = Storage()

db = db._db
return db


@pytest.fixture()
def script_path():
"""Return a script path for mock"""
Expand Down Expand Up @@ -372,6 +354,12 @@ def setup_pickleddb_database():
del os.environ["ORION_DB_ADDRESS"]


@pytest.fixture(scope="function")
def storage(setup_pickleddb_database):
setup_storage()
yield get_storage()


@pytest.fixture()
def with_user_userxyz(monkeypatch):
"""Make ``getpass.getuser()`` return ``'userxyz'``."""
Expand Down
5 changes: 0 additions & 5 deletions tests/functional/branching/orion_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ algorithms: random

producer:
strategy: NoParallelStrategy

database:
type: 'mongodb'
name: 'orion_test'
host: 'mongodb://user:pass@localhost'
14 changes: 8 additions & 6 deletions tests/functional/branching/test_branching.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def execute(command, assert_code=0):


@pytest.fixture
def init_full_x(create_db_instance, monkeypatch):
def init_full_x(setup_pickleddb_database, monkeypatch):
"""Init original experiment"""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))
name = "full_x"
Expand Down Expand Up @@ -984,7 +984,9 @@ def test_auto_resolution_with_fidelity(init_full_x_full_y, monkeypatch):
]


def test_init_w_version_from_parent_w_children(create_db_instance, monkeypatch, capsys):
def test_init_w_version_from_parent_w_children(
setup_pickleddb_database, monkeypatch, capsys
):
"""Test that init of experiment from version with children fails."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))
execute(
Expand All @@ -1007,7 +1009,7 @@ def test_init_w_version_from_parent_w_children(create_db_instance, monkeypatch,
assert "Experiment name" in captured.err


def test_init_w_version_from_exp_wout_child(create_db_instance, monkeypatch):
def test_init_w_version_from_exp_wout_child(setup_pickleddb_database, monkeypatch):
"""Test that init of experiment from version without child works."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))
execute(
Expand All @@ -1026,7 +1028,7 @@ def test_init_w_version_from_exp_wout_child(create_db_instance, monkeypatch):
assert len(list(exp))


def test_init_w_version_gt_max(create_db_instance, monkeypatch):
def test_init_w_version_gt_max(setup_pickleddb_database, monkeypatch):
"""Test that init of experiment from version higher than max works."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))
execute(
Expand All @@ -1045,7 +1047,7 @@ def test_init_w_version_gt_max(create_db_instance, monkeypatch):
assert len(list(exp))


def test_init_check_increment_w_children(create_db_instance, monkeypatch):
def test_init_check_increment_w_children(setup_pickleddb_database, monkeypatch):
"""Test that incrementing version works with not same-named children."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))
execute(
Expand All @@ -1064,7 +1066,7 @@ def test_init_check_increment_w_children(create_db_instance, monkeypatch):
assert len(list(exp))


def test_branch_from_selected_version(create_db_instance, monkeypatch):
def test_branch_from_selected_version(setup_pickleddb_database, monkeypatch):
"""Test that branching from a version passed with `--version` works."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))
execute(
Expand Down
104 changes: 44 additions & 60 deletions tests/functional/client/test_cli_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
from orion.core.worker.consumer import Consumer


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_interrupt(database, monkeypatch, capsys):
def test_interrupt(storage, monkeypatch, capsys):
"""Test interruption from within user script."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -37,17 +35,15 @@ def test_interrupt(database, monkeypatch, capsys):
assert captured.out == "Orion is interrupted.\n"
assert captured.err == ""

exp = list(database.experiments.find({"name": "voila_voici"}))
exp = list(storage.fetch_experiments({"name": "voila_voici"}))
exp = exp[0]
exp_id = exp["_id"]
trials = list(database.trials.find({"experiment": exp_id}))
trials = list(storage.fetch_trials(uid=exp_id))
assert len(trials) == 1
assert trials[0]["status"] == "interrupted"
assert trials[0].status == "interrupted"


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_interrupt_diff_code(database, monkeypatch, capsys):
def test_interrupt_diff_code(storage, monkeypatch, capsys):
"""Test interruption from within user script with custom int code"""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand Down Expand Up @@ -80,12 +76,12 @@ def empty_env(self, trial, results_file=None):

assert error_code == 0

exp = list(database.experiments.find({"name": "voila_voici"}))
exp = list(storage.fetch_experiments({"name": "voila_voici"}))
exp = exp[0]
exp_id = exp["_id"]
trials = list(database.trials.find({"experiment": exp_id}))
trials = list(storage.fetch_trials(uid=exp_id))
assert len(trials) == 2
assert trials[0]["status"] == "broken"
assert trials[0].status == "broken"

# This time we use true `get_execution_environment which pass properly int code to child.
error_code = orion.core.cli.main(
Expand All @@ -108,12 +104,12 @@ def empty_env(self, trial, results_file=None):
assert "Orion is interrupted.\n" in captured.out
assert captured.err == ""

exp = list(database.experiments.find({"name": "voila_voici"}))
exp = list(storage.fetch_experiments({"name": "voila_voici"}))
exp = exp[0]
exp_id = exp["_id"]
trials = list(database.trials.find({"experiment": exp_id}))
trials = list(storage.fetch_trials(uid=exp_id))
assert len(trials) == 3
assert trials[-1]["status"] == "interrupted"
assert trials[-1].status == "interrupted"


# TODO:
Expand All @@ -124,9 +120,7 @@ def empty_env(self, trial, results_file=None):


@pytest.mark.parametrize("fct", ["report_bad_trial", "report_objective"])
@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_report_no_name(database, monkeypatch, fct):
def test_report_no_name(storage, monkeypatch, fct):
"""Test report helper functions with default names"""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -148,21 +142,19 @@ def test_report_no_name(database, monkeypatch, fct):
+ user_args
)

exp = list(database.experiments.find({"name": "voila_voici"}))
exp = list(storage.fetch_experiments({"name": "voila_voici"}))
exp = exp[0]
exp_id = exp["_id"]
trials = list(database.trials.find({"experiment": exp_id}))
trials = list(storage.fetch_trials(uid=exp_id))
assert len(trials) == 2
assert trials[0]["status"] == "completed"
assert trials[0]["results"][0]["name"] == "objective"
assert trials[0]["results"][0]["type"] == "objective"
assert trials[0]["results"][0]["value"] == 1.0
assert trials[0].status == "completed"
assert trials[0].results[0].name == "objective"
assert trials[0].results[0].type == "objective"
assert trials[0].results[0].value == 1.0


@pytest.mark.parametrize("fct", ["report_bad_trial", "report_objective"])
@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_report_with_name(database, monkeypatch, fct):
def test_report_with_name(storage, monkeypatch, fct):
"""Test report helper functions with custom names"""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -186,21 +178,19 @@ def test_report_with_name(database, monkeypatch, fct):
+ user_args
)

exp = list(database.experiments.find({"name": "voila_voici"}))
exp = list(storage.fetch_experiments({"name": "voila_voici"}))
exp = exp[0]
exp_id = exp["_id"]
trials = list(database.trials.find({"experiment": exp_id}))
trials = list(storage.fetch_trials(uid=exp_id))
assert len(trials) == 2
assert trials[0]["status"] == "completed"
assert trials[0]["results"][0]["name"] == "metric"
assert trials[0]["results"][0]["type"] == "objective"
assert trials[0]["results"][0]["value"] == 1.0
assert trials[0].status == "completed"
assert trials[0].results[0].name == "metric"
assert trials[0].results[0].type == "objective"
assert trials[0].results[0].value == 1.0


@pytest.mark.parametrize("fct", ["report_bad_trial", "report_objective"])
@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_report_with_bad_objective(database, monkeypatch, fct):
def test_report_with_bad_objective(storage, monkeypatch, fct):
"""Test report helper functions with bad objective types"""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -226,9 +216,7 @@ def test_report_with_bad_objective(database, monkeypatch, fct):
assert "must contain a type `objective` with type float/int" in str(exc.value)


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_report_with_bad_trial_no_objective(database, monkeypatch):
def test_report_with_bad_trial_no_objective(storage, monkeypatch):
"""Test bad trial report helper function with default objective."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -248,20 +236,18 @@ def test_report_with_bad_trial_no_objective(database, monkeypatch):
+ user_args
)

exp = list(database.experiments.find({"name": "voila_voici"}))
exp = list(storage.fetch_experiments({"name": "voila_voici"}))
exp = exp[0]
exp_id = exp["_id"]
trials = list(database.trials.find({"experiment": exp_id}))
trials = list(storage.fetch_trials(uid=exp_id))
assert len(trials) == 2
assert trials[0]["status"] == "completed"
assert trials[0]["results"][0]["name"] == "objective"
assert trials[0]["results"][0]["type"] == "objective"
assert trials[0]["results"][0]["value"] == 1e10
assert trials[0].status == "completed"
assert trials[0].results[0].name == "objective"
assert trials[0].results[0].type == "objective"
assert trials[0].results[0].value == 1e10


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_report_with_bad_trial_with_data(database, monkeypatch):
def test_report_with_bad_trial_with_data(storage, monkeypatch):
"""Test bad trial report helper function with additional data."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -283,24 +269,22 @@ def test_report_with_bad_trial_with_data(database, monkeypatch):
+ user_args
)

exp = list(database.experiments.find({"name": "voila_voici"}))
exp = list(storage.fetch_experiments({"name": "voila_voici"}))
exp = exp[0]
exp_id = exp["_id"]
trials = list(database.trials.find({"experiment": exp_id}))
trials = list(storage.fetch_trials(uid=exp_id))
assert len(trials) == 2
assert trials[0]["status"] == "completed"
assert trials[0]["results"][0]["name"] == "objective"
assert trials[0]["results"][0]["type"] == "objective"
assert trials[0]["results"][0]["value"] == 1e10
assert trials[0].status == "completed"
assert trials[0].results[0].name == "objective"
assert trials[0].results[0].type == "objective"
assert trials[0].results[0].value == 1e10

assert trials[0]["results"][1]["name"] == "another"
assert trials[0]["results"][1]["type"] == "constraint"
assert trials[0]["results"][1]["value"] == 1.0
assert trials[0].results[1].name == "another"
assert trials[0].results[1].type == "constraint"
assert trials[0].results[1].value == 1.0


@pytest.mark.usefixtures("clean_db")
@pytest.mark.usefixtures("null_db_instances")
def test_no_report(database, monkeypatch, capsys):
def test_no_report(storage, monkeypatch, capsys):
"""Test script call without any results reported."""
monkeypatch.chdir(os.path.dirname(os.path.abspath(__file__)))

Expand Down
Loading