Beispiel #1
0
def test_create_collection_from_path(mock_client, monkeypatch, mocker, page,
                                     page_size, length, entity_name,
                                     call_params):
    """Test create_collection_from_path for different pagination settings"""
    items = [{"name": "name{}".format(i), "bcId": str(i)} for i in range(4)]

    def mock_request_ws(path, **kwargs):
        path, param = path.split("?")
        params = {
            key: int(val)
            for key, val in [pp.split("=") for pp in param.split("&")]
        }
        assert path == "braincube/path/all/summary"
        return {
            "items":
            items[params["offset"]:(params["offset"] + params["size"])]
        }

    rpatch = mocker.patch("braincube_connector.client.request_ws",
                          side_effect=mock_request_ws)
    parameters.set_parameter({"page_size": page_size})
    entities = base_entity.BaseEntity.create_collection_from_path(
        "{webservice}/path/all/summary",
        "{webservice}/path/{bcid}",
        None,
        page=page)

    calls = [
        mocker.call("braincube/path/all/summary{param}".format(param=pp),
                    braincube_name="") for pp in call_params
    ]
    rpatch.assert_has_calls(calls)
    assert len(entities) == length
    entities[-1]._name = entity_name
Beispiel #2
0
def test_get_memory_base_list(mocker, monkeypatch, bc_obj,
                              mock_request_entity):
    parameters.set_parameter({"page_size": 2})
    mb_list = bc_obj.get_memory_base_list(page=10)
    mock_request_entity.assert_called_with(
        "braincube/bcname/braincube/mb/all/summary?offset=20&size=2",
        braincube_name=bc_obj.get_braincube_name(),
    )
Beispiel #3
0
def test_get_name(mocker, create_mock_var):
    mocker.patch(
        "braincube_connector.instances.instances", {"parameter_set": {}}
    )  # Uses a temporary instance for the test.
    var = create_mock_var(name="any", metadata={"standard": "name_standard", "tag": "name_tag"})
    assert var.get_name() == "name_standard"
    parameters.set_parameter({"VariableDescription_name_key": "tag"})
    assert var.get_name() == "name_tag"
Beispiel #4
0
def test_reset(mocker):
    mocker.patch.dict("braincube_connector.parameters._default_parameters",
                      {"page_size": 8})
    mocker.patch.dict("braincube_connector.instances.instances",
                      {"parameter_set": {}})
    parameters.set_parameter({"page_size": 5})
    parameters.reset_parameter()
    assert parameters.get_parameter("page_size") == 8
    assert instances.instances["parameter_set"] == {}
Beispiel #5
0
def test_get_parameters(mocker):
    mocker.patch.dict("braincube_connector.parameters._default_parameters",
                      {"page_size": 8})
    mocker.patch.dict("braincube_connector.instances.instances",
                      {"parameter_set": {}})
    assert parameters.get_parameter("Nonexistent") is None
    assert parameters.get_parameter("page_size") == 8
    parameters.set_parameter({"page_size": 5})
    assert parameters.get_parameter("page_size") == 5
    assert instances.instances["parameter_set"]["page_size"] == 5
Beispiel #6
0
def test_variable(patch_endpoints):
    mb = test_memorybase(patch_endpoints)
    patch_endpoints()

    var = mb.get_variable("0")
    assert var.get_parameter_key("name") == "standard"
    assert var.get_name() == "standard_name"
    parameters.set_parameter({"VariableDescription_name_key": "tag"})
    assert var.get_parameter_key("name") == "tag"
    assert var.get_name() == "tag_name"
Beispiel #7
0
def test_get_name(mocker):
    mocker.patch(
        "braincube_connector.instances.instances",
        {"parameter_set": {}})  # Uses a temporary instance for the test.
    json_dict = {"bcId": "1", "name": "abcd", "tag": "dcba"}
    obj = base_entity.BaseEntity.create_from_json(json_dict, "path/{bcid}",
                                                  None)
    assert obj.get_name() == "abcd"
    parameters.set_parameter({"BaseEntity_name_key": "tag"})
    assert obj.get_name() == "dcba"
Beispiel #8
0
def test_get_datagroup_list(mocker, monkeypatch, mb_obj, mock_request_entity):
    parameters.set_parameter({"page_size": 2})
    group_list = mb_obj.get_datagroup_list(page=0)
    assert group_list[0]._name == "name0"
    request_path = "braincube/bcname/braincube/mb/1/dataGroups/summary?offset=0&size=2"
    mock_request_entity.assert_called_with(request_path, braincube_name="")
    mock_request_entity.reset_mock()
    parameters.set_parameter({"page_size": 4})
    group_list = mb_obj.get_datagroup_list(page=0, page_size=2)
    mock_request_entity.assert_called_with(request_path, braincube_name="")
Beispiel #9
0
def test_get_rule_list(mocker, monkeypatch, mb_obj, mock_request_entity):
    parameters.set_parameter({"page_size": 2})
    rule_list = mb_obj.get_rule_list(page=0)
    assert rule_list[0]._name == "name0"
    request_path = "braincube/bcname/braincube/mb/1/rules/all/selector?offset=0&size=2"
    mock_request_entity.assert_called_with(request_path, braincube_name="")
    mock_request_entity.reset_mock()
    parameters.set_parameter({"page_size": 4})
    rule_list = mb_obj.get_rule_list(page=0, page_size=2)
    mock_request_entity.assert_called_with(request_path, braincube_name="")
Beispiel #10
0
def test_extract_format_data():
    parameters.set_parameter({"parse_date": True})
    formated_dataset = data._extract_format_data(DATASET)
    assert sorted(formated_dataset.keys()) == [1, 2, 3, 4, 5]
    assert formated_dataset[1] == [1, 2, 3, 4]

    assert type(formated_dataset[1][0]) is int
    assert type(formated_dataset[2][0]) is float  # No NaN for int...
    assert type(formated_dataset[3][0]) is float
    assert type(formated_dataset[4][0]) is datetime
    assert type(formated_dataset[5][0]) is str
Beispiel #11
0
def test_get_variable_list(mocker, monkeypatch, mb_obj, mock_request_entity):
    mock_request_entity.return_value = {
        "items": [{
            "standard": "name{}".format(i),
            "bcId": str(i)
        } for i in range(3)]
    }
    parameters.set_parameter({"page_size": 2})
    var_list = mb_obj.get_variable_list(page=0)
    assert var_list[0]._name == "name0"

    request_path = "braincube/bcname/braincube/mb/1/variables/summary?offset=0&size=2"
    mock_request_entity.assert_called_with(request_path, braincube_name="")
    mock_request_entity.reset_mock()
    parameters.set_parameter({"page_size": 4})
    var_list = mb_obj.get_variable_list(page=0, page_size=2)
    mock_request_entity.assert_called_with(request_path, braincube_name="")
Beispiel #12
0
def test_get_data(patch_endpoints):
    mb = test_memorybase(patch_endpoints)
    patch_endpoints()
    parameters.set_parameter({"parse_date": True})
    data = mb.get_data(["101", "102", "103"], label_type="name")
    expected_data = {
        "standard_101": [
            datetime.strptime("20201127_124000", "%Y%m%d_%H%M%S"),
            datetime.strptime("20201127_124001", "%Y%m%d_%H%M%S"),
            None,
            datetime.strptime("20201127_124002", "%Y%m%d_%H%M%S"),
        ],
        "standard_102": [1.1, 1.2, math.nan, 1.4],
        "standard_103": ["A", "B", "NaN", "D"],
    }
    for key, values in expected_data.items():
        for val_retreived, val_expected in zip(data[key], values):
            try:
                assert val_retreived == val_expected
            except AssertionError as err:
                # If a is Nan: a!=a
                if val_retreived == val_retreived or val_expected == val_expected:
                    raise err
Beispiel #13
0
def test_set_parameters(mocker):
    mocker.patch.dict("braincube_connector.instances.instances",
                      {"parameter_set": {}})
    parameters.set_parameter({"page_size": 30})
    assert instances.get_instance("parameter_set")["page_size"] == 30
Beispiel #14
0
def test_extract_format_data_no_parse():
    parameters.set_parameter({"parse_date": False})
    formated_dataset = data._extract_format_data(DATASET)
    assert type(formated_dataset[4][0]) is str