コード例 #1
0
def test_update(app, field_ids, expected_field_ids):

    type_id = 2

    with app.app_context():
        count = TypeFieldAssociation.query.count()
        model = ProductType.query.filter_by(type_id=type_id).one()
        expected_len_change = len(model.fields) - len(expected_field_ids)

    with app.app_context():
        model = ops.update_product_type(
            type_id=type_id, name="compass", field_ids=field_ids
        )
        ops.commit()
        assert model.name == "compass"

    with app.app_context():
        model = ProductType.query.filter_by(type_id=type_id).one()
        assert model.name == "compass"
        actual_field_ids = [f.field.field_id for f in model.fields]
        assert actual_field_ids == expected_field_ids

        # assert unused TypeFieldAssociation is deleted
        actual_len_change = count - TypeFieldAssociation.query.count()
        assert expected_len_change == actual_len_change
コード例 #2
0
def test_update_error(app, field_ids):

    type_id = 2

    with app.app_context():
        count = TypeFieldAssociation.query.count()

    with app.app_context():
        with pytest.raises(exc.NoResultFound):
            model = ops.update_product_type(
                type_id=type_id,
                name="compass",
                field_ids=field_ids,
            )
            ops.commit()
            # commit() won't be called because of the exception

    with app.app_context():
        model = ProductType.query.filter_by(type_id=type_id).one()
        assert model.name == "beam"
        actual_field_ids = [f.field.field_id for f in model.fields]
        expected_field_ids = [1, 2, 3, 7, 9]
        assert actual_field_ids == expected_field_ids

        assert TypeFieldAssociation.query.count() == count
コード例 #3
0
ファイル: conftest.py プロジェクト: simonsobs/acondbs
def app(app_users):
    y = app_users

    with y.app_context():
        ops.create_log(id_=1, level="DEBUG", message="A debug message!")
        ops.create_log(id_=2, level="ERROR", message="An error message!")
        ops.commit()

    yield y
コード例 #4
0
ファイル: test_product.py プロジェクト: simonsobs/acondbs
def _test_convert(
    app,
    updating_git_hub_user_id=None,
):

    product_id = 1
    type_id = 2
    kwargs = {"product_id": product_id, "type_id": type_id}

    if updating_git_hub_user_id:
        kwargs["updating_git_hub_user_id"] = updating_git_hub_user_id

    attr_names = [
        a.attribute_class.backref_column
        for a in FieldType.__members__.values()
    ]
    # e.g., 'attributes_unicode_text', 'attributes_boolean'

    with app.app_context():
        model = Product.query.filter_by(product_id=product_id).one()
        assert not model.type_.type_id == type_id
        value_dict = {
            e.field_id: e.value
            for attr in attr_names for e in getattr(model, attr)
        }

        product_type = ProductType.query.filter_by(type_id=type_id).one()
        expected_field_values = [(assoc.field_id,
                                  value_dict.get(assoc.field_id))
                                 for assoc in product_type.fields]

    with app.app_context():
        model = ops.convert_product_type(**kwargs)
        assert model
        ops.commit()

    with app.app_context():
        model = Product.query.filter_by(product_id=product_id).one()
        assert model.type_.type_id == type_id

        if updating_git_hub_user_id:
            updating_git_hub_user = GitHubUser.query.filter_by(
                user_id=updating_git_hub_user_id).one()
            assert model.updating_git_hub_user == updating_git_hub_user
        else:
            assert model.updating_git_hub_user is None

        expected_ids = [(a.iid, a.field_id) for a in model.type_.fields]
        attrs = [e for attr in attr_names for e in getattr(model, attr)]
        for attr in attrs:
            assert attr.type_field_association.type_ == model.type_
        actual_ids = [(e.type_field_association.iid, e.field_id)
                      for e in attrs]
        assert set(expected_ids) == set(actual_ids)
        actual_field_values = [(a.field_id, a.value) for a in attrs]
        assert set(expected_field_values) == set(actual_field_values)
コード例 #5
0
def test_create_error_neither(app):
    type_ = {"name": "doctor"}

    with app.app_context():
        count = ProductRelationType.query.count()
        with pytest.raises(ValueError):
            ops.create_product_relation_type(type_)
            ops.commit()

    with app.app_context():
        assert ProductRelationType.query.count() == count
コード例 #6
0
def test_create_error_both(app):
    type_ = {"name": "doctor"}
    reverse = {"name": "patient"}

    with app.app_context():
        count = ProductRelationType.query.count()
        with pytest.raises(ValueError):
            ops.create_product_relation_type(type_, reverse, self_reverse=True)
            ops.commit()

    with app.app_context():
        assert ProductRelationType.query.count() == count
コード例 #7
0
def test_create(app):
    with app.app_context():
        count = ProductFilePath.query.count()
        model = ops.create_product_file_path(path="/new/path")
        assert model.path == "/new/path"
        ops.commit()
        path_id = model.path_id
        assert path_id

    with app.app_context():
        assert ProductFilePath.query.count() == (count + 1)
        model = ProductFilePath.query.filter_by(path_id=path_id).one()
        assert model.path == "/new/path"
コード例 #8
0
ファイル: test_log.py プロジェクト: simonsobs/acondbs
def test_create(app):
    with app.app_context():
        count = Log.query.count()
        model = ops.create_log(level="ERROR", message="An exception occurred")
        assert model.message == "An exception occurred"
        ops.commit()
        id_ = model.id_
        assert id_

    with app.app_context():
        assert Log.query.count() == (count + 1)
        model = Log.query.filter_by(id_=id_).one()
        assert model.message == "An exception occurred"
コード例 #9
0
def test_create(app):
    with app.app_context():
        count = ProductRelation.query.count()
        model = ops.create_product_relation(
            type_id=1, self_product_id=1, other_product_id=4
        )
        ops.commit()
        relation_id = model.relation_id
        assert relation_id

    with app.app_context():
        assert ProductRelation.query.count() == (count + 2)
        model = ProductRelation.query.filter_by(relation_id=relation_id).one()
        assert model.reverse
コード例 #10
0
ファイル: test_field.py プロジェクト: simonsobs/acondbs
def test_update(app):

    with app.app_context():
        model = Field.query.filter_by(name="number").one()
        field_id = model.field_id

    with app.app_context():
        model = ops.update_field(field_id=field_id, name="number_renamed")
        ops.commit()

    with app.app_context():
        model = Field.query.filter_by(field_id=field_id).one()
        assert model.name == "number_renamed"
        assert model.field_id == field_id
コード例 #11
0
ファイル: test_log.py プロジェクト: simonsobs/acondbs
def test_delete(app):
    with app.app_context():
        model = ops.create_log(level="ERROR", message="To be deleted")
        ops.commit()
        id_ = model.id_

    with app.app_context():
        count = Log.query.count()
        ops.delete_log(id_=id_)
        ops.commit()

    with app.app_context():
        assert Log.query.count() == (count - 1)
        model = Log.query.filter_by(id_=id_).one_or_none()
        assert model is None
コード例 #12
0
def test_create_self_reverse(app):
    type_ = {"name": "spouse"}

    with app.app_context():
        count = ProductRelationType.query.count()
        model = ops.create_product_relation_type(type_, self_reverse=True)
        ops.commit()
        type_id = model.type_id
        assert type_id

    with app.app_context():
        assert ProductRelationType.query.count() == (count + 1)
        model = ProductRelationType.query.filter_by(type_id=type_id).one()
        assert model.name == "spouse"
        assert model.reverse is model
コード例 #13
0
def test_update(app):

    type_id = 1

    with app.app_context():
        count = ProductRelationType.query.count()
        model = ops.update_product_relation_type(type_id=type_id,
                                                 name="renamed")
        ops.commit()
        assert model.type_id == type_id

    with app.app_context():
        assert ProductRelationType.query.count() == count
        model = ProductRelationType.query.filter_by(type_id=type_id).one()
        assert model.name == "renamed"
コード例 #14
0
def test_create_error(app, field_ids):

    with app.app_context():
        count = ProductType.query.count()
        with pytest.raises(exc.NoResultFound):
            model = ops.create_product_type(
                name="derived_map",
                field_ids=field_ids,
            )
            ops.commit()

    with app.app_context():
        assert ProductType.query.count() == count
        model = ProductType.query.filter_by(name="derived_map").one_or_none()
        assert model is None
コード例 #15
0
def test_delete(app):
    with app.app_context():
        count = ProductRelation.query.count()
        model = ProductRelation.query.first()
        relation_id = model.relation_id

        ops.delete_product_relation(relation_id=relation_id)
        ops.commit()

    with app.app_context():
        assert ProductRelation.query.count() == (count - 2)
        model = ProductRelation.query.filter_by(
            relation_id=relation_id
        ).one_or_none()
        assert model is None
コード例 #16
0
def test_delete(app):
    with app.app_context():
        model = ops.create_product_file_path(path="/to/delete")
        ops.commit()
        path_id = model.path_id

    with app.app_context():
        count = ProductFilePath.query.count()
        model = ops.delete_product_file_path(path_id=path_id)
        ops.commit()

    with app.app_context():
        assert ProductFilePath.query.count() == (count - 1)
        model = ProductFilePath.query.filter_by(path_id=path_id).one_or_none()
        assert model is None
コード例 #17
0
def test_create(app):
    type_ = {"name": "doctor"}
    reverse = {"name": "patient"}

    with app.app_context():
        count = ProductRelationType.query.count()
        model = ops.create_product_relation_type(type_, reverse)
        ops.commit()
        type_id = model.type_id
        assert type_id

    with app.app_context():
        assert ProductRelationType.query.count() == (count + 2)
        model = ProductRelationType.query.filter_by(type_id=type_id).one()
        assert model.name == "doctor"
        assert model.reverse.name == "patient"
コード例 #18
0
ファイル: test_field.py プロジェクト: simonsobs/acondbs
def test_create(app_empty, type_):
    app = app_empty

    with app.app_context():
        model = ops.create_field(name="new_field", type_=type_)
        assert model.name == "new_field"
        assert model.type_ == FieldType.Float
        ops.commit()
        field_id = model.field_id
        assert field_id

    with app.app_context():
        model = Field.query.filter_by(field_id=field_id).one()
        assert model.name == "new_field"
        assert model.type_ == FieldType.Float
        assert model.field_id == field_id
コード例 #19
0
ファイル: test_product.py プロジェクト: simonsobs/acondbs
def test_delete(app):

    with app.app_context():
        model = ops.create_product(type_id=1, name="to_be_deleted")
        ops.commit()
        product_id = model.product_id

    with app.app_context():
        count = Product.query.count()
        model = ops.delete_product(product_id=product_id)
        ops.commit()

    with app.app_context():
        model = Product.query.filter_by(product_id=product_id).one_or_none()
        assert model is None
        assert Product.query.count() == (count - 1)
コード例 #20
0
def test_updated(app):
    with app.app_context():
        count = ProductFilePath.query.count()

        model = ProductFilePath.query.first()
        path_id = model.path_id
        assert model.path != "/new/path"

        model = ops.update_product_file_path(path_id=path_id, path="/new/path")
        assert model.path == "/new/path"

        ops.commit()

    with app.app_context():
        assert ProductFilePath.query.count() == count
        model = ProductFilePath.query.filter_by(path_id=path_id).one()
        assert model.path == "/new/path"
コード例 #21
0
def test_delete(app):

    with app.app_context():
        model = ops.create_product_relation_type(
            type_={"name": "to_be_deleted"},
            reverse={"name": "reverse"},
        )
        ops.commit()
        type_id = model.type_id

    with app.app_context():
        count = ProductRelationType.query.count()
        ops.delete_product_relation_type(type_id=type_id)
        ops.commit()

    with app.app_context():
        assert ProductRelationType.query.count() == count - 2
コード例 #22
0
ファイル: test_field.py プロジェクト: simonsobs/acondbs
def test_delete(app):

    with app.app_context():
        model = ops.create_field(name="to_be_deleted", type_=FieldType.Float)
        ops.commit()
        field_id = model.field_id

    with app.app_context():
        count = Field.query.count()
        ret = ops.delete_field(field_id=field_id)
        ops.commit()
        assert ret is None

    with app.app_context():
        model = Field.query.filter_by(field_id=field_id).one_or_none()
        assert model is None
        assert Field.query.count() == count - 1
コード例 #23
0
def test_delete(app):

    with app.app_context():
        model = ops.create_product_type(
            name="to_be_deleted",
            field_ids=[1, 4, 5],
        )
        ops.commit()
        type_id = model.type_id

    with app.app_context():
        count = ProductType.query.count()
        model = ops.delete_product_type(type_id=type_id)
        ops.commit()

    with app.app_context():
        model = ProductType.query.filter_by(type_id=type_id).one_or_none()
        assert model is None
        assert ProductType.query.count() == (count - 1)
コード例 #24
0
def app(app_users):
    y = app_users

    config_json = json.dumps(
        {
            "headTitle": "Head Title",
            "toolbarTitle": "Toolbar Title",
            "devtoolLoadingstate": True,
            "productCreationDialog": False,
            "productUpdateDialog": True,
            "productDeletionDialog": True,
        },
        indent=2,
    )

    with y.app_context():
        ops.save_web_config(json=config_json)
        ops.commit()

    yield y
コード例 #25
0
def app(app_users):
    y = app_users
    with y.app_context():
        ops.create_field(
            field_id=1,
            name="contact",
            type_=ops.FieldType.UnicodeText,
        )
        ops.create_field(
            field_id=2,
            name="produced_by",
            type_=ops.FieldType.UnicodeText,
        )
        ops.create_field(
            field_id=3,
            name="date_produced",
            type_=ops.FieldType.Date,
        )
        ops.commit()
    yield y
コード例 #26
0
def test_create(app, field_ids):

    with app.app_context():
        count = ProductType.query.count()
        model = ops.create_product_type(
            name="derived_map",
            field_ids=field_ids,
        )
        assert model.name == "derived_map"
        ops.commit()
        type_id = model.type_id
        assert type_id

    with app.app_context():
        assert ProductType.query.count() == (count + 1)
        model = ProductType.query.filter_by(type_id=type_id).one()
        assert model.name == "derived_map"
        expected_field_ids = sorted(set(field_ids)) if field_ids else []
        actual_field_ids = [f.field.field_id for f in model.fields]
        assert actual_field_ids == expected_field_ids
コード例 #27
0
def app(app_users):

    y = app_users

    # map1 -> beam1
    #            |
    #            +---> beam2
    #
    # map2
    # map3

    with y.app_context():
        ops.create_product_relation_type(
            type_={
                "type_id": 1,
                "name": "parent"
            },
            reverse={
                "type_id": 2,
                "name": "child"
            },
        )

        ops.create_product_type(type_id=1, name="map")
        ops.create_product_type(type_id=2, name="beam")

        ops.commit()

    with y.app_context():
        ops.create_product(
            product_id=1,
            type_id=1,
            name="map1",
        )
        ops.create_product(
            product_id=2,
            type_id=1,
            name="map2",
        )
        ops.create_product(
            product_id=3,
            type_id=1,
            name="map3",
        )
        ops.create_product(
            product_id=4,
            type_id=2,
            name="beam1",
        )
        ops.create_product(
            product_id=5,
            type_id=2,
            name="beam2",
        )
        ops.commit()

    with y.app_context():
        ops.create_product_relation(
            relation_id=2,
            type_id=1,
            self_product_id=4,
            other_product_id=1,
        )
        ops.create_product_relation(
            relation_id=4,
            type_id=1,
            self_product_id=5,
            other_product_id=4,
        )
        ops.commit()

    yield y
コード例 #28
0
def app(app_empty):

    y = app_empty

    user1 = GitHubUser(login="******", git_hub_id="04:User1")
    token1 = GitHubToken(token="39d86487d76a84087f1da599c872dac4473e5f07",
                         scope="",
                         user=user1)

    with y.app_context():
        sa.session.add(user1)
        sa.session.add(token1)
        sa.session.commit()

    # map1 -> beam1
    #   |        |
    #   +--------+---> beam2
    #
    # map2
    # map3

    with y.app_context():
        ops.create_product_relation_type(
            type_={
                "type_id": 1,
                "name": "parent",
            },
            reverse={
                "type_id": 2,
                "name": "child",
            },
        )
        ops.commit()

    with y.app_context():
        ops.create_field(
            field_id=1,
            name="contact",
            type_=ops.FieldType.UnicodeText,
        )
        ops.create_field(
            field_id=2,
            name="produced_by",
            type_=ops.FieldType.UnicodeText,
        )
        ops.create_field(
            field_id=3,
            name="date_produced",
            type_=ops.FieldType.Date,
        )
        ops.commit()

    with y.app_context():
        ops.create_product_type(
            type_id=1,
            name="map",
            field_ids=[1, 2, 3],
        )
        ops.create_product_type(
            type_id=2,
            name="beam",
            field_ids=[1, 2, 3],
        )
        ops.commit()

    with y.app_context():
        ops.create_product(
            type_id=1,
            product_id=1,
            name="map1",
            attributes={3: datetime.date(2020, 2, 1)},
            paths=["site1:/path/to/map1", "site2:/another/way/map1"],
        )
        ops.create_product(
            type_id=1,
            product_id=2,
            name="map2",
            attributes={3: datetime.date(2020, 2, 10)},
            paths=["site1:/path/to/map2"],
        )
        ops.create_product(
            type_id=1,
            product_id=3,
            name="map3",
            attributes={3: datetime.date(2020, 3, 19)},
            paths=["site1:/path/to/map3", "site2:/another/way/map3"],
        )
        ops.create_product(
            type_id=2,
            product_id=4,
            name="beam1",
            attributes={3: datetime.date(2020, 2, 5)},
            paths=["site1:/path/to/beam1", "site2:/another/way/beam1"],
        )
        ops.create_product(
            type_id=2,
            product_id=5,
            name="beam2",
            attributes={3: datetime.date(2020, 3, 4)},
            paths=["site1:/path/to/beam2"],
        )
        ops.commit()

    with y.app_context():
        ops.create_product_relation(
            type_id=1,
            self_product_id=4,
            other_product_id=1,
        )
        ops.create_product_relation(
            type_id=1,
            self_product_id=5,
            other_product_id=1,
        )
        ops.create_product_relation(
            type_id=1,
            self_product_id=5,
            other_product_id=4,
        )
        ops.commit()

    yield y
コード例 #29
0
ファイル: test_product.py プロジェクト: simonsobs/acondbs
def _test_create(
    app,
    paths=None,
    relations=None,
    attributes=None,
    posting_git_hub_user_id=None,
):

    kwargs = {"type_id": 1, "name": "new-product"}

    if paths is not None:
        kwargs["paths"] = paths

    if relations is not None:
        kwargs["relations"] = relations

    if attributes is not None:
        kwargs["attributes"] = attributes

    if posting_git_hub_user_id:
        kwargs["posting_git_hub_user_id"] = posting_git_hub_user_id

    with app.app_context():
        count = Product.query.count()

        model = ops.create_product(**kwargs)
        assert model.name == "new-product"
        ops.commit()
        product_id = model.product_id
        assert product_id

    with app.app_context():
        assert Product.query.count() == (count + 1)

        model = Product.query.filter_by(product_id=product_id).one()
        assert model.name == "new-product"

        if posting_git_hub_user_id:
            posting_git_hub_user = GitHubUser.query.filter_by(
                user_id=posting_git_hub_user_id).one()
            assert model.posting_git_hub_user == posting_git_hub_user
        else:
            assert model.posting_git_hub_user is None

        if paths is not None:
            expected = _normalize_paths(paths)
            actual = [p.path for p in model.paths]
            assert actual == expected
        else:
            assert model.paths == []

        if relations is not None:
            expected = [(r["type_id"], r["product_id"]) for r in relations]
            actual = [(r.type_id, r.other_product_id) for r in model.relations]
            assert actual == expected
        else:
            assert model.relations == []

        if attributes is None:
            attributes = {}
        expected_ids = [(a.iid, a.field_id) for a in model.type_.fields]
        # list of (type_field_association.iid, field.field_id)
        expected = {
            fid: (aid, attributes.get(fid))
            for aid, fid in expected_ids
        }
        actual = _extract_attributes(model)
        # actual_field_ids = list(actual.keys())
        assert actual == expected
コード例 #30
0
ファイル: test_product.py プロジェクト: simonsobs/acondbs
def _test_update(
    app,
    paths=None,
    relations=None,
    attributes=None,
    updating_git_hub_user_id=None,
):

    product_id = 1
    kwargs = {"product_id": product_id, "name": "new-name"}

    if paths is not None:
        kwargs["paths"] = paths

    if relations is not None:
        kwargs["relations"] = relations

    if attributes is not None:
        kwargs["attributes"] = attributes

    if updating_git_hub_user_id:
        kwargs["updating_git_hub_user_id"] = updating_git_hub_user_id

    with app.app_context():
        count = Product.query.count()

        model = Product.query.filter_by(product_id=product_id).one()
        paths_old = [p.path for p in model.paths]
        path_ids_old = {p.path: p.path_id for p in model.paths}

        relations_old = {(r.type_id, r.other_product_id)
                         for r in model.relations}
        relation_ids_old = {(r.type_id, r.other_product_id): r.relation_id
                            for r in model.relations}

        attributes_old = _extract_attributes(model)

        model = ops.update_product(**kwargs)
        assert model.name == "new-name"
        ops.commit()

    with app.app_context():
        assert Product.query.count() == count

        model = Product.query.filter_by(product_id=product_id).one()
        assert model.name == "new-name"

        if updating_git_hub_user_id:
            updating_git_hub_user = GitHubUser.query.filter_by(
                user_id=updating_git_hub_user_id).one()
            assert model.updating_git_hub_user == updating_git_hub_user
        else:
            assert model.updating_git_hub_user is None

        # NOTE: The order of the paths is being tested. However, it is
        # probably not possible to control the order. The order in a
        # Python list is not preserved once committed in the DB.
        if paths is not None:
            kept = [p for p in paths_old if p in paths]
            expected = kept + _normalize_paths(paths)
            expected = list(dict.fromkeys(expected))  # uniq order preserved
        else:
            expected = paths_old
        actual = [p.path for p in model.paths]
        assert actual == expected
        expected_path_ids = {
            k: v
            for k, v in path_ids_old.items() if k in expected
        }
        path_ids = {
            p.path: p.path_id
            for p in model.paths if p.path in path_ids_old
        }
        assert path_ids == expected_path_ids

        if relations is not None:
            expected = {(r["type_id"], r["product_id"]) for r in relations}
        else:
            expected = relations_old
        actual = {(r.type_id, r.other_product_id) for r in model.relations}
        assert actual == expected
        expected_relation_ids = {
            k: v
            for k, v in relation_ids_old.items() if k in expected
        }
        relation_ids = {(r.type_id, r.other_product_id): r.relation_id
                        for r in model.relations
                        if (r.type_id, r.other_product_id) in relation_ids_old}
        assert relation_ids == expected_relation_ids

        if attributes is None:
            attributes = {}
        expected = attributes_old.copy()
        update = {k: (expected[k][0], v) for k, v in attributes.items()}
        expected.update(update)
        actual = _extract_attributes(model)
        assert actual == expected