示例#1
0
def test_repr(app_empty):
    app = app_empty  # noqa: F841

    field1 = Field(name="field1")
    repr(field1)

    field1.type_ = FieldType.UnicodeText
    repr(field1)
示例#2
0
def test_nullable(app_empty):
    app = app_empty  # noqa: F841

    # without type_
    with app.app_context():
        field = Field(name="field")
        sa.session.add(field)
        with pytest.raises(exc.IntegrityError):
            sa.session.commit()

    # without name
    with app.app_context():
        field = Field(type_=FieldType.UnicodeText)
        sa.session.add(field)
        with pytest.raises(exc.IntegrityError):
            sa.session.commit()
示例#3
0
def test_obj(app_empty, field_type, AttributeClass, value):
    app = app_empty  # noqa: F841
    field = Field(name="field1", type_=field_type)
    assoc = TypeFieldAssociation(field=field)
    attr = AttributeClass(
        type_field_association=assoc, field=field, value=value
    )  # noqa: F841
示例#4
0
def test_commit(app_empty, field_type, AttributeClass, value):
    app = app_empty
    with app.app_context():
        field = Field(name="field1", type_=field_type)
        assoc = TypeFieldAssociation(field=field)
        product_type = ProductType(name="map")
        product_type.fields = [assoc]
        sa.session.add(product_type)
        sa.session.commit()

    with app.app_context():
        product_type = ProductType.query.filter_by(name="map").one()
        assoc = product_type.fields[0]
        field = assoc.field
        product = Product(name="product1", type_=product_type)
        attr = AttributeClass(
            product=product,
            type_field_association=assoc,
            field=field,
            value=value,
        )
        sa.session.add(attr)
        sa.session.commit()

    with app.app_context():
        attr = AttributeClass.query.one()
        assert attr.__class__ is AttributeClass
        assert attr.type_field_association.field.name == "field1"
        assert attr.type_field_association.field.type_ is field_type
        assert attr.field.name == "field1"
        assert attr.field.type_ is field_type
        assert attr.value == value
        assert getattr(attr.product, AttributeClass.backref_column) == [attr]
        assert getattr(attr.field, AttributeClass.backref_column) == [attr]
示例#5
0
def test_unique_constraint(app_empty):
    # A type cannot have multiple same field.
    app = app_empty
    with app.app_context():
        field1 = Field(name="field1", type_=FieldType.UnicodeText)
        assoc1 = TypeFieldAssociation(field=field1)
        assoc2 = TypeFieldAssociation(field=field1)
        type1 = ProductType(name="type1", fields=[assoc1, assoc2])
        sa.session.add(type1)
        with pytest.raises(exc.IntegrityError):
            sa.session.commit()
示例#6
0
def test_repr(app_empty, field_type, AttributeClass, value):
    app = app_empty  # noqa: F841

    attr1 = AttributeClass(value=value)
    repr(attr1)

    field1 = Field(name="attr1", type_=field_type)
    assoc1 = TypeFieldAssociation(field=field1)
    attr1.type_field_association = assoc1
    attr1.field = field1
    repr(attr1)
示例#7
0
def test_commit_with_field_id(app_empty):
    app = app_empty

    with app.app_context():
        field1 = Field(field_id=259, name="field1", type_=FieldType.Date)
        sa.session.add(field1)
        sa.session.commit()

    with app.app_context():
        field1 = Field.query.filter_by(field_id=259).one()
        assert field1.name == "field1"
        assert field1.type_ is FieldType.Date
示例#8
0
def test_repr(app_empty):
    app = app_empty  # noqa: F841

    model = TypeFieldAssociation()
    repr(model)

    field1 = Field(name="field1", type_=FieldType.UnicodeText)
    type1 = ProductType(name="type1")

    model.type_ = type1
    model.field = field1
    repr(model)
示例#9
0
def app(app_empty):

    y = app_empty

    field1 = Field(name="field1", type_=FieldType.UnicodeText)
    field2 = Field(name="field2", type_=FieldType.Integer)

    assoc1 = TypeFieldAssociation(iid=1, field=field1)
    assoc2 = TypeFieldAssociation(iid=2, field=field2)

    type1 = ProductType(
        name="type1",
        fields=[assoc1, assoc2],
    )

    with y.app_context():
        sa.session.add(field1)
        sa.session.add(field2)
        sa.session.add(type1)
        sa.session.commit()

    yield y
示例#10
0
def test_enum_by_int(app_empty):
    """Test if an enum can be given by a number

    No. It raises an exception at a commit

    TODO: move this test to tests.models.example
    """
    app = app_empty

    with app.app_context():
        field1 = Field(name="field1", type_=1)
        sa.session.add(field1)
        with pytest.raises(exc.StatementError):
            sa.session.commit()
示例#11
0
def test_column(app_empty):
    app = app_empty

    with app.app_context():
        field1 = Field(name="field1", type_=FieldType.UnicodeText)
        type1 = ProductType(name="type1")
        model = TypeFieldAssociation(
            order=123,
            type_=type1,
            field=field1,
        )
        sa.session.add(model)
        sa.session.commit()
        iid = model.iid

    with app.app_context():
        model = TypeFieldAssociation.query.filter_by(iid=iid).one()
        assert model.iid == iid
        assert model.order == 123
示例#12
0
def test_delte_orphan_type_field_association_and_field(app_empty):
    app = app_empty

    with app.app_context():
        field1 = Field(name="field1", type_=FieldType.UnicodeText)
        assoc1 = TypeFieldAssociation(field=field1)
        type1 = ProductType(type_id=1, name="type1", fields=[assoc1])

        product1 = Product(name="product1", type_=type1)
        field1_attribute_class = field1.type_.attribute_class
        attr1 = field1_attribute_class(
            product=product1,
            type_field_association=assoc1,
            field=field1,
            value="value1",
        )

        sa.session.add(product1)
        sa.session.commit()

        field1_id = field1.field_id
        assoc1_id = assoc1.iid
        product1_id = product1.product_id
        attr1_id = attr1.iid

    with app.app_context():
        assoc1 = TypeFieldAssociation.query.filter_by(iid=assoc1_id).one()
        field1 = Field.query.filter_by(field_id=field1_id).one()
        sa.session.delete(assoc1)
        sa.session.delete(field1)
        sa.session.commit()

    with app.app_context():
        product1 = Product.query.filter_by(product_id=product1_id).one()
        attr1 = field1_attribute_class.query.filter_by(
            iid=attr1_id
        ).one_or_none()
        assert attr1 is None
示例#13
0
def app(app_empty):

    y = app_empty

    # create fields
    field1 = Field(name="attr1", type_=FieldType.UnicodeText)
    field2 = Field(name="date_produced", type_=FieldType.Date)
    field3 = Field(name="time_posted", type_=FieldType.DateTime)
    fields = [field1, field2, field3]

    # create product types
    Map = ProductType(
        type_id=1,
        name="map",
        order=2,
        indef_article="a",
        singular="map",
        plural="maps",
        icon="mdi-map",
        fields=[TypeFieldAssociation(field=f) for f in fields],
    )
    Beam = ProductType(
        type_id=2,
        name="beam",
        order=1,
        indef_article="a",
        singular="beam",
        plural="beams",
        icon="mdi-spotlight-beam",
        fields=[TypeFieldAssociation(field=f) for f in fields],
    )

    # create products
    map1 = Product(product_id=1, name="map1", type_=Map)
    values = (
        "value1",
        datetime.date(2020, 2, 1),
        datetime.datetime(2020, 2, 1, 9, 10, 25),
    )
    for assoc, value in zip(Map.fields, values):
        assoc.field.type_.attribute_class(
            product=map1,
            type_field_association=assoc,
            field=assoc.field,
            value=value,
        )

    map2 = Product(product_id=2, name="map2", type_=Map)
    values = (
        "value2",
        datetime.date(2020, 2, 10),
        datetime.datetime(2020, 2, 10, 13, 20, 2),
    )
    for assoc, value in zip(Map.fields, values):
        assoc.field.type_.attribute_class(
            product=map2,
            type_field_association=assoc,
            field=assoc.field,
            value=value,
        )

    map3 = Product(product_id=3, name="map3", type_=Map)
    values = (
        "value3",
        datetime.date(2020, 3, 19),
        datetime.datetime(2020, 3, 20, 8, 45, 30),
    )
    for assoc, value in zip(Map.fields, values):
        assoc.field.type_.attribute_class(
            product=map3,
            type_field_association=assoc,
            field=assoc.field,
            value=value,
        )

    beam1 = Product(product_id=4, name="beam1", type_=Beam)
    values = (
        "value4",
        datetime.date(2020, 2, 5),
        datetime.datetime(2020, 2, 5, 12, 3, 48),
    )
    for assoc, value in zip(Map.fields, values):
        assoc.field.type_.attribute_class(
            product=beam1,
            type_field_association=assoc,
            field=assoc.field,
            value=value,
        )

    beam2 = Product(product_id=5, name="beam2", type_=Beam)
    values = (
        "value5",
        datetime.date(2020, 3, 4),
        datetime.datetime(2020, 3, 4, 19, 22, 5),
    )
    for assoc, value in zip(Map.fields, values):
        assoc.field.type_.attribute_class(
            product=beam2,
            type_field_association=assoc,
            field=assoc.field,
            value=value,
        )

    with y.app_context():
        sa.session.add(Map)
        sa.session.add(Beam)
        sa.session.commit()

    yield y
示例#14
0
def test_order_nested(app_empty):
    app = app_empty

    field_attr1 = Field(name="attr1", type_=FieldType.UnicodeText)
    field_attr2 = Field(name="attr2", type_=FieldType.UnicodeText)
    Map = ProductType(type_id=1, name="map")
    assoc1 = TypeFieldAssociation(field=field_attr1)
    assoc2 = TypeFieldAssociation(field=field_attr2)
    Map.fields = [assoc1, assoc2]

    map1 = Product(product_id=1, name="map1", type_=Map)
    map2 = Product(product_id=2, name="map2", type_=Map)
    map3 = Product(product_id=3, name="map3", type_=Map)
    map4 = Product(product_id=4, name="map4", type_=Map)
    map5 = Product(product_id=5, name="map5", type_=Map)

    AttributeUnicodeText(
        product=map1,
        type_field_association=assoc1,
        field=field_attr1,
        value="1",
    )
    AttributeUnicodeText(
        product=map2,
        type_field_association=assoc1,
        field=field_attr1,
        value="2",
    )
    AttributeUnicodeText(
        product=map3,
        type_field_association=assoc1,
        field=field_attr1,
        value="1",
    )
    AttributeUnicodeText(
        product=map4,
        type_field_association=assoc1,
        field=field_attr1,
        value="2",
    )
    AttributeUnicodeText(
        product=map5,
        type_field_association=assoc1,
        field=field_attr1,
        value="1",
    )

    AttributeUnicodeText(
        product=map1,
        type_field_association=assoc2,
        field=field_attr2,
        value="b",
    )
    AttributeUnicodeText(
        product=map2,
        type_field_association=assoc2,
        field=field_attr2,
        value="a",
    )
    AttributeUnicodeText(
        product=map3,
        type_field_association=assoc2,
        field=field_attr2,
        value="c",
    )
    AttributeUnicodeText(
        product=map4,
        type_field_association=assoc2,
        field=field_attr2,
        value="b",
    )
    AttributeUnicodeText(
        product=map5,
        type_field_association=assoc2,
        field=field_attr2,
        value="a",
    )

    with app.app_context():
        sa.session.add(Map)
        sa.session.commit()

    with app.app_context():

        map1 = Product.query.filter_by(product_id=1).one()
        map2 = Product.query.filter_by(product_id=2).one()
        map3 = Product.query.filter_by(product_id=3).one()
        map4 = Product.query.filter_by(product_id=4).one()
        map5 = Product.query.filter_by(product_id=5).one()

        expected = [map2, map4, map5, map1, map3]

        # ORM Alias: https://docs.sqlalchemy.org/en/14/tutorial/data_select.html#orm-entity-aliases
        # refer to the same table multiple times
        AliasedAttributeUnicodeText1 = aliased(AttributeUnicodeText)
        AliasedAttributeUnicodeText2 = aliased(AttributeUnicodeText)

        AliasedTypeFieldAssociation1 = aliased(TypeFieldAssociation)
        AliasedTypeFieldAssociation2 = aliased(TypeFieldAssociation)

        AliasedField1 = aliased(Field)
        AliasedField2 = aliased(Field)

        # sort descending order of attr1 then ascending order of attr2
        query = (
            Product.query.join(ProductType)
            .filter_by(name="map")
            .join(AliasedAttributeUnicodeText1)
            .join(AliasedTypeFieldAssociation1)
            .join(AliasedField1)
            .filter_by(name="attr1")
            .order_by(AliasedAttributeUnicodeText1.value.desc())
            .join(
                AliasedAttributeUnicodeText2, Product.attributes_unicode_text
            )
            .join(AliasedTypeFieldAssociation2)
            .join(AliasedField2)
            .filter_by(name="attr2")
            .order_by(AliasedAttributeUnicodeText2.value)
        )

        # print(sqlparse.format(str(query), reindent=True))

        actual = query.all()

        assert actual == expected