Exemplo n.º 1
0
def test_main_logging_argparse(monkeypatch, caplog):
    monkeypatch.setattr(logging.config, "dictConfig", MagicMock())
    monkeypatch.setattr(main, "valid_file", MagicMock(return_value=True))
    monkeypatch.setattr(main, "run_cli", MagicMock())
    monkeypatch.setattr(main, "start_ert_server", MagicMock())
    monkeypatch.setattr(main, "ErtPluginContext", MagicMock())
    monkeypatch.setattr(sys, "argv", ["ert", "test_run", "config.ert"])
    with caplog.at_level(logging.INFO):
        main.main()
    assert "mode='test_run'" in caplog.text
Exemplo n.º 2
0
def test_main_logging(monkeypatch, caplog):
    parser_mock = MagicMock()
    parser_mock.func.side_effect = ValueError("This is a test")
    monkeypatch.setattr(logging.config, "dictConfig", MagicMock())
    monkeypatch.setattr(main, "ert_parser",
                        MagicMock(return_value=parser_mock))
    monkeypatch.setattr(main, "start_ert_server", MagicMock())
    monkeypatch.setattr(main, "ErtPluginContext", MagicMock())
    monkeypatch.setattr(sys, "argv", ["ert", "test_run", "config.ert"])
    with pytest.raises(SystemExit,
                       match='ERT crashed unexpectedly with "This is a test"'):
        main.main()
    assert 'ERT crashed unexpectedly with "This is a test"' in caplog.text
    assert "Traceback" in caplog.text
Exemplo n.º 3
0
def test_main_logging_config(monkeypatch, caplog, tmp_path):
    content = "Content of config.ert\nMore content."
    ert_config = tmp_path / "config.ert"
    with open(ert_config, "w", encoding="utf-8") as file_obj:
        file_obj.write(content)
    monkeypatch.setattr(logging.config, "dictConfig", MagicMock())
    monkeypatch.setattr(main, "run_cli", MagicMock())
    monkeypatch.setattr(main, "start_ert_server", MagicMock())
    monkeypatch.setattr(main, "ErtPluginContext", MagicMock())
    monkeypatch.setattr(sys, "argv", ["ert", "test_run", str(ert_config)])
    with caplog.at_level(logging.INFO):
        main.main()
    assert f"Content of the configuration file ({str(ert_config)}):" in caplog.text
    assert content in caplog.text
Exemplo n.º 4
0
def test_api_database_url_forwarded(monkeypatch):
    monkeypatch.setattr(logging.config, "dictConfig", MagicMock())

    monkeypatch.setattr(main, "start_ert_server", MagicMock())
    monkeypatch.setattr(main, "ErtPluginContext", MagicMock())
    mocked_start_server = MagicMock()
    monkeypatch.setattr(
        "ert_shared.services.storage_service.BaseService.start_server",
        mocked_start_server,
    )
    monkeypatch.setattr(
        sys,
        "argv",
        ["ert", "api", "--database-url", "TEST_DATABASE_URL"],
    )

    main.main()
    mocked_start_server.assert_called_once_with(
        res_config=None, database_url="TEST_DATABASE_URL")
Exemplo n.º 5
0
def test_api_database_default(monkeypatch):
    monkeypatch.setattr(logging.config, "dictConfig", MagicMock())

    monkeypatch.setattr(main, "start_ert_server", MagicMock())
    monkeypatch.setattr(main, "ErtPluginContext", MagicMock())
    mocked_start_server = MagicMock()
    monkeypatch.setattr(
        "ert_shared.services.storage_service.BaseService.start_server",
        mocked_start_server,
    )
    monkeypatch.setattr(
        sys,
        "argv",
        ["ert", "api"],
    )

    main.main()
    # We expect default value from Storage class, validate that no explicit
    # value is given for database_url
    mocked_start_server.assert_called_once_with(res_config=None)
Exemplo n.º 6
0
def test_stea_fm_single_real(monkeypatch, httpserver, ecl_case, ecl_base,
                             fm_options):
    httpserver.expect_request(
        "/foobar/api/v1/Alternative/56892/1/summary",
        query_string="ConfigurationDate=2018-11-01T00:00:00",
    ).respond_with_json({
        "AlternativeId": 1,
        "AlternativeVersion": 1,
        "Profiles": [{
            "Id": "FOPT",
            "Unit": "SM3"
        }],
    })
    httpserver.expect_request(
        "/foobar/api/v1/Calculate/", method="POST").respond_with_json(
            {
                SteaKeys.KEY_VALUES: [{
                    SteaKeys.TAX_MODE: SteaKeys.CORPORATE,
                    SteaKeys.VALUES: {
                        "NPV": 30
                    }
                }]
            }, )
    server = httpserver.url_for("/foobar")
    # Mock a forward model configuration file
    Path("stea_conf.yml").write_text(
        base_stea_config.format(ecl_case) + f"\nstea_server: {server}")

    # Write an ERT config file
    Path("config.ert").write_text(
        base_ert_config.format(ecl_base=ecl_base, fm_options=fm_options))

    monkeypatch.setattr(sys, "argv",
                        ["ert", "test_run", "config.ert", "--verbose"])
    main()

    assert Path("realization-0/iter-0/NPV_0").read_text()[0:3] == "30\n"
    assert Path("realization-0/iter-0/stea_response.json").exists()