예제 #1
0
def test_instance_parameter(api_mock):
    koi.init()

    # create pool and get the first instance of the first model
    pool = koi.create_api_object_pool(host="http://base", username="******", password="******")

    model = next(pool.get_all_models(), None)

    assert model is not None

    inst = next(model.instances, None)

    assert inst is not None

    # get the current values of the our instance parameters
    value1 = inst.parameter["param1"]
    value2 = inst.parameter["param2"]

    # increment the parameter values
    inst.parameter["param1"] = value1 + 1
    inst.parameter["param2"] = value2 + 1.0

    # check the new values
    assert inst.parameter["param1"] == value1 + 1
    assert inst.parameter["param2"] == value2 + 1.0

    # test the type checking
    with pytest.raises(TypeError):
        inst.parameter["param1"] = 15.0

    with pytest.raises(TypeError):
        inst.parameter["param2"] = "15"
예제 #2
0
def test_user_check_grant_revoke_general_access(api_mock):
    koi.init()

    pool = koi.create_api_object_pool(host="http://base",
                                      username="******",
                                      password="******")

    roles = list(pool.get_all_general_roles())

    user = next(pool.get_all_users())

    helper_check_role(user, roles[1])

    koi.deinit()
예제 #3
0
def run_all_models(persistence=None):
    koi.init()

    if persistence:
        pool = koi.create_api_object_pool(
            host="http://base",
            username="******",
            password="******",
            persistance_file=persistence,
        )
    else:
        pool = koi.create_api_object_pool(
            host="http://base",
            username="******",
            password="******"
        )
    models = pool.get_all_models()
    for model in models:
        instances = model.instances
        for instance in instances:
            koi.control.train(instance, dev=True)

    koi.deinit()
예제 #4
0
def test_offline_model_inference(offlineOrConnectionError, api_mock):
    persistence = io.BytesIO()

    koi_core.init()
    pool = koi_core.create_api_object_pool(
        host="http://base",
        username="******",
        password="******",
        persistance_file=persistence,
    )
    instance: Instance = pool.instance(
        InstanceId(
            uuid.UUID("00000000-0001-1000-8000-000000000000"),
            uuid.UUID("00000000-0002-1000-8000-000000000000"),
        ))
    koi_core.control.infer(instance, [], dev=True)
    koi_core.deinit()

    if offlineOrConnectionError:
        api_mock.set_offline()
    else:
        api_mock.set_connectionError()
    persistence.seek(0)
    koi_core.init()
    pool = koi_core.create_api_object_pool(
        host="http://base",
        username="******",
        password="******",
        persistance_file=persistence,
    )
    instance = pool.instance(
        InstanceId(
            uuid.UUID("00000000-0001-1000-8000-000000000000"),
            uuid.UUID("00000000-0002-1000-8000-000000000000"),
        ))
    koi_core.control.infer(instance, [], dev=True)
    koi_core.deinit()
예제 #5
0
def test_get_roles(api_mock):
    koi.init()

    pool = koi.create_api_object_pool(host="http://base",
                                      username="******",
                                      password="******")

    general_roles = list(pool.get_all_general_roles())
    model_roles = list(pool.get_all_model_roles())
    instance_roles = list(pool.get_all_instance_roles())

    assert len(general_roles) == 2
    assert len(model_roles) == 2
    assert len(instance_roles) == 2

    koi.deinit()
예제 #6
0
def test_offline_api(offlineOrConnectionError, api_mock):
    persistence = io.BytesIO()

    koi_core.init()
    pool = koi_core.create_api_object_pool(
        host="http://base",
        username="******",
        password="******",
        persistance_file=persistence,
    )
    instance: Instance = pool.instance(
        InstanceId(
            uuid.UUID("00000000-0001-1000-8000-000000000000"),
            uuid.UUID("00000000-0002-1000-8000-000000000000"),
        ))
    koi_core.control.infer(instance, [], dev=True)
    koi_core.deinit()

    api_mock.requests_mock.reset_mock()
    persistence.seek(0)
    koi_core.init()
    pool = koi_core.create_offline_object_pool(persistance_file=persistence)
    instance = pool.instance(
        InstanceId(
            uuid.UUID("00000000-0001-1000-8000-000000000000"),
            uuid.UUID("00000000-0002-1000-8000-000000000000"),
        ))
    koi_core.control.infer(instance, [], dev=True)
    koi_core.deinit()
    assert api_mock.requests_mock.call_count == 0, "The Offline API should not make any HTTP Request"

    if offlineOrConnectionError:
        api_mock.set_offline()
    else:
        api_mock.set_connectionError()
    persistence.seek(0)
    koi_core.init()
    pool = koi_core.create_offline_object_pool(persistance_file=persistence)
    instance = pool.instance(
        InstanceId(
            uuid.UUID("00000000-0001-1000-8000-000000000000"),
            uuid.UUID("00000000-0002-1000-8000-000000000000"),
        ))
    koi_core.control.infer(instance, [], dev=True)
    koi_core.deinit()
예제 #7
0
def test_user_check_grant_revoke_model_access(api_mock):
    koi.init()

    pool = koi.create_api_object_pool(host="http://base",
                                      username="******",
                                      password="******")

    roles = list(pool.get_all_model_roles())

    user = next(pool.get_all_users())

    model = next(pool.get_all_models())

    assert user.has_role(roles[0], model) is True

    helper_check_role(user, roles[1], model)

    koi.deinit()
예제 #8
0
def test_offline_authentication(offlineOrConnectionError, api_mock):
    koi_core.init()
    pool = koi_core.create_api_object_pool(host="http://base",
                                           username="******",
                                           password="******")

    # check that the api reports as online
    assert pool.api.online is True

    # check if the api detects the offline server when trying to authenticate
    if offlineOrConnectionError:
        api_mock.set_offline()
    else:
        api_mock.set_connectionError()
    with pytest.raises(KoiApiOfflineException):
        pool.api.authenticate()
    assert pool.api.online is False

    koi_core.deinit()
예제 #9
0
def test_sample_filtering(testserver):
    try:
        koi.init()
    except koi.exceptions.KoiInitializationError:
        ...

    pool = koi.create_api_object_pool(*testserver)

    model = pool.new_model()
    model.code = Dummy()
    model.finalized = True

    # create two instances and finalize
    inst = model.new_instance()
    inst.finalized = True

    for _ in range(1):
        new_sample = inst.new_sample()
        new_sample.tags.add("A")
        new_sample.finalized = True

    for _ in range(3):
        new_sample = inst.new_sample()
        new_sample.tags.add("A")
        new_sample.tags.add("B")
        new_sample.finalized = True

    for _ in range(5):
        new_sample = inst.new_sample()
        new_sample.tags.add("B")
        new_sample.finalized = True

    all_a = inst.get_samples(filter_include=["A", ])
    assert len(all_a) == 1+3

    all_b = inst.get_samples(filter_include=["B", ])
    assert len(all_b) == 3+5

    only_a = inst.get_samples(filter_include=["A", ], filter_exclude=["B", ])
    assert len(only_a) == 1

    only_b = inst.get_samples(filter_include=["B", ], filter_exclude=["A", ])
    assert len(only_b) == 5
예제 #10
0
def test_get_users(api_mock):
    koi.init()
    # create pool
    pool = koi.create_api_object_pool(host="http://base",
                                      username="******",
                                      password="******")

    users = list(pool.get_all_users())
    assert len(users) > 0

    user = users[0]

    assert user.name == "admin"

    user = users[2]
    assert user.name == "user2"
    user.name = "user2_new"
    assert user.name == "user2_new"

    koi.deinit()
예제 #11
0
def test_offline_detection(offlineOrConnectionError, api_mock):
    koi_core.init()
    pool = koi_core.create_api_object_pool(host="http://base",
                                           username="******",
                                           password="******")

    # check that the api reports as online and continous to do so after authentication
    assert pool.api.online is True
    pool.get_all_models()  # make sure the models are cached
    assert pool.api.online is True

    # check if the api detects the offline server when doing something
    if offlineOrConnectionError:
        api_mock.set_offline()
    else:
        api_mock.set_connectionError()
    pool.get_all_models(
    )  # just do something, so that the API can detect the absence of the server
    assert pool.api.online is False

    koi_core.deinit()
예제 #12
0
def test_instance_merging(testserver):
    try:
        koi.init()
    except koi.exceptions.KoiInitializationError:
        ...

    pool = koi.create_api_object_pool(*testserver)

    # create an empty model
    model = pool.new_model()
    model.code = Dummy()
    model.finalized = True

    # create two instances and finalize
    inst1 = model.new_instance()
    inst2 = model.new_instance()
    inst1.finalized = True
    inst2.finalized = True

    # give them some descriptors
    inst1.descriptors["c"].append("1")
    inst1.descriptors["c"].append("2")
    inst2.descriptors["c"].append("2")
    inst2.descriptors["c"].append("3")

    inst1.descriptors["a"].append("1")
    inst2.descriptors["b"].append("2")

    # create samples for these instances
    sample1 = inst1.new_sample()
    sample2 = inst2.new_sample()

    # give them some tags and labels
    sample1.tags.add("keep this")
    sample2.tags.add("keep this")

    sample1.labels["class1"].append(b"keep this label")
    sample2.labels["class1"].append(b"keep this label")
    sample2.labels["class2"].append(b"keep this label")

    # finalize the samples and add some non mergeable content
    sample1.finalized = True
    sample2.finalized = True

    sample1.tags.add("do not keep this")
    sample2.tags.add("do not keep this")

    sample1.labels["class1"].append(b"do not keep this")
    sample2.labels["class3"].append(b"do not keep this")

    # build a mergeable instance
    inst3 = model.new_instance()
    inst3.finalized = True

    # merge and get the samples
    inst3.merge([inst1, inst2])
    samples = list(inst3.get_samples())

    # get the descriptors after merging
    desc_a = [x.raw.decode() for x in inst3.descriptors["a"]]
    desc_b = [x.raw.decode() for x in inst3.descriptors["b"]]
    desc_c = [x.raw.decode() for x in inst3.descriptors["c"]]

    # check if we merged the descriptor correctly
    assert desc_a == ["1"]
    assert desc_b == ["2"]
    assert desc_c == ["1", "2", "3"]

    # the merged instance schould have two samples
    assert len(samples) == 2

    # check if we merged the tags correctly
    for sample in samples:
        assert "keep this" in sample.tags
        assert "do not keep this" not in sample.tags

        labels = [x.raw for x in sample.labels["class1"]]
        assert b"do not keep this" not in labels
        assert b"keep this label" in labels

        labels = [x.raw for x in sample.labels["class2"]]
        assert b"do not keep this" not in labels

        labels = [x.raw for x in sample.labels["class3"]]
        assert b"do not keep this" not in labels