def test_findCreateModelInstance_valid_create(modelCatalog, myModelID):
    model_catalog = modelCatalog
    model_id = myModelID
    model = model_catalog.get_model(model_id=model_id)
    test_model = sample.SampleModel(
        model_uuid=model_id,
        model_version=model["instances"][0]["version"] + "_new")
    model_instance = model_catalog.find_model_instance_else_add(test_model)
    assert isinstance(uuid.UUID(model_instance["id"], version=4), uuid.UUID)
def test_findCreateModelInstance_valid_exist_instance_id(
        modelCatalog, myModelID):
    model_catalog = modelCatalog
    model_id = myModelID
    model = model_catalog.get_model(model_id=model_id)
    test_model = sample.SampleModel(
        model_instance_uuid=model["instances"][0]["id"])
    model_instance_id = model_catalog.find_model_instance_else_add(test_model)
    assert isinstance(uuid.UUID(model_instance_id, version=4), uuid.UUID)
def test_run_test_offline(modelCatalog, testLibrary, myModelID, myTestID):
    model_catalog = modelCatalog
    model_id = myModelID
    test_library = testLibrary
    test_id = myTestID

    sleep(10)
    test = test_library.get_test_definition(test_id=test_id)
    test_config_file = utils.prepare_run_test_offline(test_id=test_id, test_version=test["instances"][0]["version"], client_obj=test_library)
    model = model_catalog.get_model(model_id=model_id)
    test_model = sample.SampleModel(model_instance_uuid=model["instances"][0]["id"])
    test_result_file = utils.run_test_offline(model=test_model, test_config_file=test_config_file)
    assert os.path.isfile(test_result_file)
def test_run_test_combined(modelCatalog, testLibrary, myModelID, myTestID):
    model_catalog = modelCatalog
    model_id = myModelID
    test_library = testLibrary
    test_id = myTestID

    sleep(20)
    test = test_library.get_test_definition(test_id=test_id)
    model = model_catalog.get_model(model_id=model_id)
    test_model = sample.SampleModel(model_instance_uuid=model["instances"][0]["id"])

    result, score = utils.run_test(model=test_model, test_id=test_id, test_version=test["instances"][0]["version"], client_obj=test_library)
    assert isinstance(uuid.UUID(result["id"], version=4), uuid.UUID)
    assert isinstance(score, sciunit.Score)
def test_register_result_valid(modelCatalog, testLibrary, myModelID, myTestID):
    model_catalog = modelCatalog
    model_id = myModelID
    test_library = testLibrary
    test_id = myTestID
    sleep(20)
    model = model_catalog.get_model(model_id=model_id)
    model = sample.SampleModel(model_uuid=model_id,
                               model_version=model["instances"][0]["version"])

    test_name = "Test_{}_{}_py{}_getValTest_1".format(
        datetime.now().strftime("%Y-%m-%d_%H:%M:%S"), test_library.environment,
        platform.python_version())
    test = test_library.add_test(
        name="IGNORE - Test Test - " + test_name,
        alias=test_name,
        author={
            "family_name": "Tester",
            "given_name": "Validation"
        },
        species="Mus musculus",
        age="",
        brain_region="basal ganglia",
        cell_type="granule cell",
        recording_modality="electron microscopy",
        test_type="network structure",
        score_type="Other",
        description="Later",
        data_location=
        "https://object.cscs.ch/v1/AUTH_c0a333ecf7c045809321ce9d9ecdfdea/sp6_validation_data/test.txt",
        data_type="Mean, SD",
        publication="Testing et al., 2019",
        instances=[{
            "version": "1.0",
            "repository":
            "https://github.com/HumanBrainProject/hbp-validation-client.git",
            "path": "hbp_validation_framework.sample.SampleTest"
        }])
    sleep(20)
    test = test_library.get_validation_test(test_id=test["id"])

    score = test.judge(model)

    timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
    folder_name = "results_{}_{}_{}".format(model.name, model.model_uuid[:8],
                                            timestamp)

    result = test_library.register_result(score, collab_id="model-validation")
    assert isinstance(uuid.UUID(result["id"], version=4), uuid.UUID)
def test_upload_test_result(modelCatalog, testLibrary, myModelID, myTestID):
    model_catalog = modelCatalog
    model_id = myModelID
    test_library = testLibrary
    test_id = myTestID

    test = test_library.get_test_definition(test_id=test_id)
    test_config_file = utils.prepare_run_test_offline(
        test_id=test_id,
        test_version=test["codes"][0]["version"],
        client_obj=test_library)
    model = model_catalog.get_model(model_id=model_id)
    test_model = sample.SampleModel(
        model_instance_uuid=model["instances"][0]["id"])
    test_result_file = utils.run_test_offline(
        model=test_model, test_config_file=test_config_file)
    result_id, score = utils.upload_test_result(
        test_result_file=test_result_file, client_obj=test_library)
    assert isinstance(uuid.UUID(result_id, version=4), uuid.UUID)
    assert isinstance(score, sciunit.Score)
def test_delete_superUser(request):
    ENVIRONMENT = request.config.getoption("--environment")

    if HBP_USERNAME and HBP_PASSWORD:
        model_catalog = ModelCatalog(username=HBP_USERNAME,
                                     password=HBP_PASSWORD,
                                     environment=ENVIRONMENT)
    elif TOKEN:
        model_catalog = ModelCatalog(token=TOKEN, environment=ENVIRONMENT)
    else:
        raise Exception(
            "Credentials not provided. Please define environment variables (HBP_AUTH_TOKEN or HBP_USER and HBP_PASS"
        )

    model_name = "Model_{}_{}_py{}_superuser1".format(
        datetime.now().strftime("%Y-%m-%d_%H:%M:%S"),
        model_catalog.environment, platform.python_version())
    model = model_catalog.register_model(
        collab_id="model-validation",
        name="IGNORE - Test Model - " + model_name,
        alias=model_name,
        author={
            "family_name": "Tester",
            "given_name": "Validation"
        },
        organization="HBP-SP6",
        private=False,
        cell_type="granule cell",
        model_scope="single cell",
        abstraction_level="spiking neurons",
        brain_region="basal ganglia",
        species="Mus musculus",
        owner={
            "family_name": "Tester",
            "given_name": "Validation"
        },
        license="BSD 3-Clause",
        description="This is a test entry! Please ignore.",
        instances=[{
            "source": "https://www.abcde.com",
            "version": "1.0",
            "parameters": ""
        }],
        images=[{
            "url":
            "http://www.neuron.yale.edu/neuron/sites/default/themes/xchameleon/logo.png",
            "caption": "NEURON Logo"
        }])

    model_instance_id = model["instances"][0]["id"]
    #model_image_id = model["images"][0]["id"]
    model_obj = sample.SampleModel(
        model_uuid=model["id"], model_version=model["instances"][0]["version"])

    test_library = TestLibrary.from_existing(model_catalog)
    test_name = "Test_{}_{}_py{}_superuser2".format(
        datetime.now().strftime("%Y-%m-%d_%H:%M:%S"), test_library.environment,
        platform.python_version())
    test = test_library.add_test(
        name="IGNORE - Test Test - " + test_name,
        alias=test_name,
        author={
            "family_name": "Tester",
            "given_name": "Validation"
        },
        species="Mus musculus",
        age="",
        brain_region="basal ganglia",
        cell_type="granule cell",
        recording_modality="electron microscopy",
        test_type="network structure",
        score_type="Other",
        description="Later",
        data_location=
        "https://object.cscs.ch/v1/AUTH_c0a333ecf7c045809321ce9d9ecdfdea/sp6_validation_data/test.txt",
        data_type="Mean, SD",
        publication="Testing et al., 2019",
        instances=[{
            "version": "1.0",
            "repository":
            "https://github.com/HumanBrainProject/hbp-validation-client.git",
            "path": "hbp_validation_framework.sample.SampleTest"
        }])

    test_instance_id = test["instances"][0]["id"]
    sleep(20)
    test_obj = test_library.get_validation_test(test_id=test["id"])

    score = test_obj.judge(model_obj)
    timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
    folder_name = "results_{}_{}_{}".format(model_obj.name,
                                            model_obj.model_uuid[:8],
                                            timestamp)
    result = test_library.register_result(
        score, collab_id="model-validation")  # Collab ID = model-validation

    test_library.delete_result(result_id=result["id"])
    sleep(20)
    with pytest.raises(Exception) as excinfo:
        result = test_library.get_result(result_id=result["id"])
    assert "not found" in str(excinfo.value)

    model_catalog.delete_model_instance(instance_id=model_instance_id)
    sleep(20)
    with pytest.raises(Exception) as excinfo:
        model_instance = model_catalog.get_model_instance(
            instance_id=model_instance_id)
    assert "Error in retrieving model instance." in str(excinfo.value)

    model_catalog.delete_model(model_id=model["id"])
    sleep(20)
    with pytest.raises(Exception) as excinfo:
        model = model_catalog.get_model(model_id=model["id"])
    assert "Error in retrieving model." in str(excinfo.value)

    test_library.delete_test_instance(instance_id=test_instance_id)
    sleep(20)
    with pytest.raises(Exception) as excinfo:
        test_instance = test_library.get_test_instance(
            instance_id=test_instance_id)
    assert "Error in retrieving test instance." in str(excinfo.value)

    test_library.delete_test(test_id=test["id"])
    sleep(20)
    with pytest.raises(Exception) as excinfo:
        test = test_library.get_test_definition(test_id=test["id"])
    assert "Error in retrieving test" in str(excinfo.value)