def test_notebooks_field_circular_dependency(monkeypatch): """ Test that NotebooksField raises a ValidationError if notebook specifications have circular dependencies. """ monkeypatch.setattr("pathlib.Path.exists", lambda self: True) notebooks = { "notebook1": { "filename": "NOTEBOOK1.ipynb", "parameters": { "param": "notebook2" }, }, "notebook2": { "filename": "NOTEBOOK2.ipynb", "parameters": { "param": "notebook1" }, }, } notebooks_field = NotebooksField() # Can't set context directly on a Field - must be set on the parent Schema notebooks_field._bind_to_schema( "notebooks", Schema(context={"inputs_dir": "DUMMY_INPUTS_DIR"})) with pytest.raises(ValidationError) as exc_info: deserialised_notebooks = notebooks_field.deserialize(notebooks) assert ("Notebook specifications contain circular dependencies." in exc_info.value.messages)
def test_notebooks_field_invalid_keys(monkeypatch, key, message): """ Test that NotebooksField raises a ValidationError if a notebook key is not a string, or has a disallowed value. """ monkeypatch.setattr("pathlib.Path.exists", lambda self: True) notebooks = {key: {"filename": "NOTEBOOK1.ipynb"}} notebooks_field = NotebooksField() # Can't set context directly on a Field - must be set on the parent Schema notebooks_field._bind_to_schema( "notebooks", Schema(context={"inputs_dir": "DUMMY_INPUTS_DIR"})) with pytest.raises(ValidationError) as exc_info: deserialised_notebooks = notebooks_field.deserialize(notebooks) assert message in exc_info.value.messages[key]["key"]
def test_notebooks_field_deserialise(monkeypatch): """ Test that NotebooksField deserialises a dict of notebook specifications as an OrderedDict. """ monkeypatch.setattr("pathlib.Path.exists", lambda self: True) notebooks = { "notebook1": { "filename": "NOTEBOOK1.ipynb" }, "notebook2": { "filename": "NOTEBOOK2.ipynb", "parameters": { "other_notebook": "notebook1" }, }, } notebooks_field = NotebooksField() # Can't set context directly on a Field - must be set on the parent Schema notebooks_field._bind_to_schema( "notebooks", Schema(context={"inputs_dir": "DUMMY_INPUTS_DIR"})) deserialised_notebooks = notebooks_field.deserialize(notebooks) assert isinstance(deserialised_notebooks, OrderedDict) assert deserialised_notebooks == notebooks