Пример #1
0
def test_array_model_properties(dataset):
    model = dataset.create_model(
        "Array_Properties",
        description="Description",
        schema=[
            ModelProperty("name", data_type=str, title=True),
            ModelProperty(
                "int_array",
                data_type=ModelPropertyEnumType(data_type=int, multi_select=True),
            ),
        ],
    )

    int_array = model.get_property("int_array")
    assert int_array.type == int
    assert int_array.multi_select == True
    assert int_array.enum == None

    record = model.create_record({"name": "A", "int_array": [1, 2, 3]})
    bulk_records = model.create_records(
        [
            {"name": "B", "int_array": [1, 2, 3]},
            {"name": "B", "int_array": [1, 2, 3]},
            {"name": "B", "int_array": [1, 2, 3]},
        ]
    )

    gotten = model.get_all()
    assert all(x.values["int_array"] == [1, 2, 3] for x in gotten)
Пример #2
0
def test_related_records_pagination(dataset):
    patient = dataset.create_model(
        "patient",
        description="patient",
        schema=[
            ModelProperty(
                "name", data_type=ModelPropertyType(data_type=str), title=True
            )
        ],
    )

    visit = dataset.create_model(
        "visit",
        description="visit",
        schema=[
            ModelProperty(
                "field", data_type=ModelPropertyType(data_type=str), title=True
            )
        ],
    )

    attends = dataset.create_relationship_type("attends", "an attendance")

    fred = patient.create_record({"name": "Fred"})
    visits = visit.create_records([{"field": str(i)} for i in range(200)])

    fred.relate_to(visits, attends)

    # Get all records
    gotten = fred.get_related()
    assert len(gotten) == 200
    assert [int(r.get("field")) for r in gotten] == list(range(200))
Пример #3
0
def test_retrieve_linked_properties(dataset):
    # make a model and add a linked property
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    source.add_linked_property("link", target, "my linked property")

    new_source = dataset.get_model(source.type)
    assert "link" in new_source.linked
Пример #4
0
def test_delete_linked_property(dataset):
    # make a model and add a linked property
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    source.add_linked_property("link", target, "my linked property")

    # delete the link
    source.remove_linked_property("link")

    # Make sure changes were saved
    assert "link" not in source.linked
Пример #5
0
def simple_graph(client):
    """
    Creates a small test graph in an independent dataset to de-couple
    from other tests
    """
    test_dataset = create_test_dataset(client)
    model_1 = test_dataset.create_model(
        "Model_A",
        description="model a",
        schema=[
            ModelProperty(
                "prop1", data_type=ModelPropertyType(data_type=str), title=True
            )
        ],
    )

    model_2 = test_dataset.create_model(
        "Model_B",
        description="model b",
        schema=[
            ModelProperty(
                "prop1", data_type=ModelPropertyType(data_type=str), title=True
            )
        ],
    )

    relationship = test_dataset.create_relationship_type(
        "New_Relationship_{}".format(current_ts()), "a new relationship"
    )

    model_instance_1 = model_1.create_record({"prop1": "val1"})
    model_instance_2 = model_2.create_record({"prop1": "val1"})
    model_instance_1.relate_to(model_instance_2, relationship)

    graph = Graph(
        test_dataset,
        models=[model_1, model_2],
        model_records=[model_instance_1, model_instance_2],
        relationships=[relationship],
        relationship_records=None,
    )
    yield graph

    ds_id = test_dataset.id
    client._api.datasets.delete(test_dataset)
    all_dataset_ids = [x.id for x in client.datasets()]
    assert ds_id not in all_dataset_ids
    assert not test_dataset.exists
Пример #6
0
def test_enum_model_properties(dataset):
    model = dataset.create_model(
        "Enum_Props",
        description="a new description",
        schema=[
            ModelProperty("name", data_type=str, title=True),
            ModelProperty(
                "int_enum",
                data_type=ModelPropertyEnumType(
                    data_type=float, enum=[1.0, 2.0, 3.0], unit="cm", multi_select=False
                ),
            ),
            ModelProperty(
                "str_enum",
                data_type=ModelPropertyEnumType(
                    data_type=str, enum=["foo", "bar", "baz"], multi_select=True
                ),
            ),
        ],
    )

    assert model == dataset.get_model(model.id)

    int_enum = model.get_property("int_enum")
    str_enum = model.get_property("str_enum")

    assert int_enum.type == float
    assert int_enum.multi_select == False
    assert int_enum.enum == [1.0, 2.0, 3.0]
    assert int_enum.unit == "cm"

    assert str_enum.type == unicode
    assert str_enum.multi_select == True
    assert str_enum.enum == ["foo", "bar", "baz"]

    record = model.create_record(
        {"name": "A", "int_enum": 1.0, "str_enum": ["foo", "bar"]}
    )

    gotten = model.get_all()[0]
    assert gotten.values["int_enum"] == 1.0
    assert set(gotten.values["str_enum"]) == set(["foo", "bar"])

    with pytest.raises(Exception, match=r"Value '5' is not a member*"):
        model.create_record({"name": "A", "int_enum": 5, "str_enum": ["foo", "bar"]})

    with pytest.raises(Exception, match=r"Value 'blah' is not a member*"):
        model.create_record({"name": "A", "int_enum": 1.0, "str_enum": ["blah"]})
Пример #7
0
def test_make_linked_property(dataset):
    # make a new model
    model = dataset.create_model("my_model_{}".format(make_id()),
                                 schema=[ModelProperty("name", title=True)])

    # create a linked property linking to that model,
    # and make sure the link initialized correctly
    link1 = LinkedModelProperty("link1", model, "1st linked property")

    dct1 = link1.as_dict()
    assert dct1["name"] == "link1"
    assert dct1["displayName"] == "1st linked property"
    assert dct1["to"] == model.id

    # make a linked property from a dict,
    # and make sure it initialized correctly
    link2 = LinkedModelProperty.from_dict({
        "link": {
            "name": "link2",
            "displayName": "2nd linked property",
            "to": model.id,
            "id": "XXX-XXX-XXX",
            "position": 0,
        }
    })
    assert link2.id == "XXX-XXX-XXX"
    assert link2.position == 0

    dct2 = link2.as_dict()
    assert dct2["name"] == "link2"
    assert dct2["displayName"] == "2nd linked property"
    assert dct2["to"] == model.id
Пример #8
0
def test_get_link(dataset):
    # make a model and add a linked property
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    prop = source.add_linked_property("link", target, "my linked property")

    # make records and link them
    source_rec = source.create_record({"name": "source_record"})
    target_rec = target.create_record({"name": "target_record"})
    link = source_rec.add_linked_value(target_rec, prop)
    link2 = source_rec.get_linked_value("link")
    link3 = source_rec.get_linked_value(link.id)
    assert link.id == link2.id == link3.id
    assert link.source_model.id == source.id
    assert link.target_model.id == target.id
Пример #9
0
def test_remove_link(dataset):
    # make a model and add a linked property
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    prop = source.add_linked_property("link", target, "my linked property")

    # make records and link them
    source_rec = source.create_record({"name": "source_record"})
    target_rec = target.create_record({"name": "target_record"})
    link = source_rec.add_linked_value(target_rec, prop)

    # delete the link
    source_rec.delete_linked_value(link.id)
    assert not any(link.target == target_rec.id
                   for link in source_rec.get_linked_values())
Пример #10
0
def test_get_connected(dataset):
    model_1 = dataset.create_model(
        "Model_A",
        description="model a",
        schema=[
            ModelProperty(
                "prop1", data_type=ModelPropertyType(data_type=str), title=True
            )
        ],
    )

    related_models = model_1.get_connected()
    # For a single, unconnected model, it should return nothing
    assert len(related_models) == 0

    model_2 = dataset.create_model(
        "Model_B",
        description="model b",
        schema=[
            ModelProperty(
                "prop1", data_type=ModelPropertyType(data_type=str), title=True
            )
        ],
    )

    relationship = dataset.create_relationship_type(
        "New_Relationship_{}".format(current_ts()), "a new relationship"
    )

    model_instance_1 = model_1.create_record({"prop1": "val1"})
    model_instance_2 = model_2.create_record({"prop1": "val1"})
    model_instance_1.relate_to(model_instance_2, relationship)

    related_models = model_1.get_connected()
    assert len(related_models) == 2

    # For a connected model, return all connections + the model itself
    related_models = model_2.get_connected()
    assert len(related_models) == 2

    # Check that get_connected_models from the dataset object also works
    related_models = dataset.get_connected_models(model_1.id)
    assert len(related_models) == 2

    related_models = dataset.get_connected_models(model_2.id)
    assert len(related_models) == 2
Пример #11
0
def test_edit_linked_property(dataset):
    # make a model and add a linked property
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    link = source.add_linked_property("link", target, "my linked property")

    # edit the linked property and update model
    link.display_name = "updated linked property"
    link.position = 99
    source.update()

    # Make sure changes were saved
    new_link = source.get_linked_property("link")
    assert new_link.position == 99
    assert new_link.display_name == "updated linked property"
Пример #12
0
def test_add_linked_property(dataset):
    # Create two models and link one to the other
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    source.add_linked_property("link", target, "my linked property")

    # Make sure newly created link is accessible through the API
    assert any(l.name == "link" and l.target == target.id
               for l in dataset.get_topology()["linked_properties"])
    assert "link" in source.linked

    # Prevent user from adding duplicate linked properties
    with pytest.raises(Exception):
        source.add_linked_property("link", source, "duplicate linked property")
    assert not any(l.display_name == "duplicate linked property"
                   for l in dataset.get_topology()["linked_properties"])
Пример #13
0
def test_add_linked_property_bulk(dataset):
    # Link one model to three others
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    link1 = LinkedModelProperty("link1", target, "bulk-added")
    link2 = LinkedModelProperty("link2", target, "bulk-added")
    link3 = LinkedModelProperty("link3", target, "bulk-added")
    source.add_linked_properties([link1, link2, link3])

    # Make sure newly created links are accessible through the API
    bulk = [
        x for x in dataset.get_topology()["linked_properties"]
        if x.display_name == "bulk-added"
    ]
    assert len(bulk) == 3
    assert len(source.linked) == 3
Пример #14
0
def test_complex_model_properties(dataset):
    model_with_complex_props = dataset.create_model(
        "Complex_Props",
        description="a new description",
        schema=[
            ModelProperty(
                "name", data_type=ModelPropertyType(data_type=str), title=True
            ),
            ModelProperty("age", data_type=ModelPropertyType(data_type=int)),
            ModelProperty(
                "email", data_type=ModelPropertyType(data_type=str, format="email")
            ),
        ],
    )

    assert dataset.get_model(model_with_complex_props.id) == model_with_complex_props

    # Add a property
    model_with_complex_props.add_property(
        "weight", ModelPropertyType(data_type=float, unit="kg"), display_name="Weight"
    )

    updated_model = dataset.get_model(model_with_complex_props.id)

    weight_property = updated_model.get_property("weight")
    assert weight_property.display_name == "Weight"
    assert weight_property.type == float
    assert weight_property._type.data_type == float
    assert weight_property._type.format == None
    assert weight_property.unit == "kg"

    email_property = model_with_complex_props.get_property("email")
    assert email_property.type == unicode
    assert email_property._type.data_type == unicode
    assert email_property._type.format.lower() == "email"
    assert email_property.unit == None

    good_values = {"name": "Bob", "age": 1, "email": "*****@*****.**", "weight": 10}
    good_record = updated_model.create_record(good_values)

    bad_values = {"name": "Bob", "age": 1, "email": "123455", "weight": 10}

    with pytest.raises(Exception):
        bad_record = updated_model.create_record(bad_values)
Пример #15
0
 def get_properties(self, dataset, concept):
     dataset_id = self._get_id(dataset)
     concept_id = self._get_id(concept)
     resp = self._get(
         self._uri(
             "/{dataset_id}/concepts/{id}/properties",
             dataset_id=dataset_id,
             id=concept_id,
         ))
     return [ModelProperty.from_dict(r) for r in resp]
Пример #16
0
def test_add_link(dataset):
    # make a model and add a linked property
    source = dataset.create_model("source_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    target = dataset.create_model("target_model_{}".format(make_id()),
                                  schema=[ModelProperty("name", title=True)])
    prop = source.add_linked_property("link", target, "my linked property")

    # make records and link them
    source_rec = source.create_record({"name": "source_record"})
    target_rec = target.create_record({"name": "target_record"})
    link = source_rec.add_linked_value(target_rec, prop)
    assert link.id == source_rec.get_linked_value("link").id

    # prevent duplicate links from being created
    target_rec2 = target.create_record({"name": "second_target"})
    link2 = source_rec.add_linked_value(target_rec2, prop)
    links = source_rec.get_linked_values()
    assert len(links) == 1
    assert links[0].target_record_id == target_rec2.id
Пример #17
0
def test_model_title_is_a_required_property_by_default(dataset):
    model = dataset.create_model(
        "Concept_title_model",
        description="",
        schema=[
            ModelProperty("name", data_type="string", title=True),
        ],
    )

    model = dataset.get_model(model)
    assert model.get_property("name").title is True
    assert model.get_property("name").required is True
Пример #18
0
 def apply(self, dataset, template):
     org_id = self._get_int_id(self.session._context)
     dataset_id = self._get_int_id(dataset)
     template_id = self._get_id(template)
     resp = self._post(
         self._uri(
             "/organizations/{orgId}/templates/{templateId}/datasets/{datasetId}",
             orgId=org_id,
             templateId=template_id,
             datasetId=dataset_id,
         ))
     return [ModelProperty.from_dict(t) for t in resp]
Пример #19
0
def test_stringified_boolean_values(dataset):
    ppatient = dataset.create_model(
        "potential_patient",
        description="potential patient",
        schema=[
            ModelProperty(
                "name", data_type=ModelPropertyType(data_type=str), title=True
            ),
            ModelProperty("sick", data_type=ModelPropertyType(data_type=bool)),
        ],
    )

    ppatient.create_record({"name": "Fred", "sick": "false"})
    ppatient.create_record({"name": "Joe", "sick": "true"})
    ppatient.create_record({"name": "Adele", "sick": "plop"})

    # Get all records
    gotten = ppatient.get_all()
    assert len(gotten) == 3
    assert gotten[0]._values["sick"].value == False
    assert gotten[1]._values["sick"].value == True
    assert gotten[2]._values["sick"].value == True
Пример #20
0
def test_model_property_default_field_inherits_from_required_field(dataset):
    """
    Unless explictly set, the `default` field on a property takes the value of
    the `required` field.
    """
    model = dataset.create_model(
        "Default_properties_model",
        description="",
        schema=[
            ModelProperty("name", data_type="string", title=True),
            ModelProperty("required", data_type="string", required=True),
            ModelProperty("not_required", data_type="string", required=False),
        ],
    )

    model = dataset.get_model(model)
    assert model.get_property("required").required is True
    assert model.get_property("required").default is True

    assert model.get_property("not_required").required is False
    assert model.get_property("not_required").default is False

    model.create_record({"name": "Record #1", "required": "yes"})
Пример #21
0
 def update_properties(self, dataset, concept):
     assert isinstance(concept, Model), "concept must be type Model"
     assert concept.schema, "concept schema cannot be empty"
     data = concept.as_dict()["schema"]
     dataset_id = self._get_id(dataset)
     resp = self._put(
         self._uri(
             "/{dataset_id}/concepts/{id}/properties",
             dataset_id=dataset_id,
             id=concept.id,
         ),
         json=data,
     )
     return [ModelProperty.from_dict(r) for r in resp]
Пример #22
0
def test_simple_model_properties(dataset):

    # Define properties as tuples
    model_with_basic_props_1 = dataset.create_model(
        "Basic_Props_1",
        description="a new description",
        schema=[
            ("an_integer", int, "An Integer", True),
            ("a_bool", bool),
            ("a_string", str),
            ("a_date", datetime.datetime),
        ],
    )

    assert dataset.get_model(model_with_basic_props_1.id) == model_with_basic_props_1

    # Add a property with a description
    model_with_basic_props_1.add_property(
        "a_new_property", float, display_name="Weight", description="some metric"
    )

    updated_model_1 = dataset.get_model(model_with_basic_props_1.id)

    test_prop = updated_model_1.get_property("a_new_property")
    assert test_prop.display_name == "Weight"
    assert test_prop.description == "some metric"

    # Define properties as ModelProperty objects
    model_with_basic_props_2 = dataset.create_model(
        "Basic_Props_2",
        description="a new description",
        schema=[
            ModelProperty("name", data_type=str, title=True, required=True),
            ModelProperty("age", data_type=int),
            ModelProperty("DOB", data_type=datetime.datetime),
        ],
    )

    assert dataset.get_model(model_with_basic_props_2.id) == model_with_basic_props_2

    # Add a property
    model_with_basic_props_2.add_property(
        "weight2", ModelPropertyType(data_type=float), display_name="Weight"
    )

    updated_model_2 = dataset.get_model(model_with_basic_props_2.id)

    assert updated_model_2.get_property("weight2").display_name == "Weight"
    assert updated_model_2.get_property("name").required == True

    # Define properties as ModelProperty objects with ModelPropertyType data_type
    model_with_basic_props_3 = dataset.create_model(
        "Basic_Props_3",
        description="a new description",
        schema=[
            ModelProperty(
                "name", data_type=ModelPropertyType(data_type=str), title=True
            ),
            ModelProperty("age", data_type=ModelPropertyType(data_type=int)),
            ModelProperty("DOB", data_type=ModelPropertyType(data_type=str)),
        ],
    )

    assert dataset.get_model(model_with_basic_props_3.id) == model_with_basic_props_3

    # Add a property
    model_with_basic_props_3.add_property(
        "weight3", ModelPropertyType(data_type=float), display_name="Weight"
    )

    updated_model_3 = dataset.get_model(model_with_basic_props_3.id)

    assert updated_model_3.get_property("weight3").display_name == "Weight"

    # Reverse look up property data types
    ModelProperty("name", data_type="string", title=True, required=True)
    model_with_basic_props_4 = dataset.create_model(
        "Basic_Props_4",
        description="a new description",
        schema=[
            ModelProperty("name", data_type="string", title=True, required=True),
            ModelProperty("age", data_type="long"),
            ModelProperty("DOB", data_type="date"),
        ],
    )

    assert dataset.get_model(model_with_basic_props_4.id) == model_with_basic_props_4

    # Add a property
    model_with_basic_props_4.add_property(
        "weight4", ModelPropertyType(data_type="double"), display_name="Weight"
    )

    updated_model_4 = dataset.get_model(model_with_basic_props_4.id)

    assert updated_model_4.get_property("weight4").display_name == "Weight"
    assert updated_model_4.get_property("name").required == True