

def test_advanced_joins():
    (app, db, admin) = setup()

    class Model1(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        val1 = db.Column(db.String(20))
        test = db.Column(db.String(20))

    class Model2(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        val2 = db.Column(db.String(20))
        model1_id = db.Column(db.Integer, db.ForeignKey(Model1.id))
        model1 = db.relationship(Model1, backref='model2')

    class Model3(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        val2 = db.Column(db.String(20))
        model2_id = db.Column(db.Integer, db.ForeignKey(Model2.id))
        model2 = db.relationship(Model2, backref='model3')
    view1 = CustomModelView(Model1, db.session)
    admin.add_view(view1)
    view2 = CustomModelView(Model2, db.session)
    admin.add_view(view2)
    view3 = CustomModelView(Model3, db.session)
    admin.add_view(view3)
    (attr, path) = tools.get_field_with_path(Model2, 'model1.val1')
    eq_(attr, Model1.val1)
    eq_(path, [Model2.model1])
    (attr, path) = tools.get_field_with_path(Model1, 'model2.val2')
    eq_(attr, Model2.val2)
    eq_(id(path[0]), id(Model1.model2))
    (attr, path) = tools.get_field_with_path(Model3, 'model2.model1.val1')
    eq_(attr, Model1.val1)
    eq_(path, [Model3.model2, Model2.model1])
    query = view3.get_query()
    joins = {
        
    }
    (q1, joins, alias) = view3._apply_path_joins(query, joins, path)
    ok_(((True, Model3.model2) in joins))
    ok_(((True, Model2.model1) in joins))
    ok_((alias is not None))
    (attr, path) = tools.get_field_with_path(Model2, 'model1.test')
    (q2, joins, alias) = view2._apply_path_joins(query, joins, path)
    eq_(len(joins), 2)
    for p in q2._join_entities:
        ok_((p in q1._join_entities))
    ok_((alias is not None))
    (attr, path) = tools.get_field_with_path(Model2, Model1.test)
    eq_(attr, Model1.test)
    eq_(path, [Model1.__table__])
    (q3, joins, alias) = view2._apply_path_joins(view2.get_query(), joins, path)
    eq_(len(joins), 3)
    ok_((alias is None))

dataset/ETHPy150Open flask-admin/flask-admin/flask_admin/tests/sqla/test_basic.py test_advanced_joins/original