Example #1
0
    def test__get_related_model_for_field(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)
        restaurant = properties['restaurant']
        model = SQLAlchemyFilterBackend._get_related_model_for_field(
            restaurant)

        assert model is Restaurant
    def test_empty(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(
            alchemy_db.query(Place),
            context={'context': 'here'},
        )

        assert 'WHERE 0 = 1' in six.text_type(backend.empty())
    def test_filter_no_specs(self, alchemy_db):
        qs = alchemy_db.query(Place)

        backend = SQLAlchemyFilterBackend(qs)
        backend.bind([])

        assert backend.filter() is qs
    def test_filter_no_specs(self, alchemy_db):
        qs = alchemy_db.query(Place)

        backend = SQLAlchemyFilterBackend(qs)
        backend.bind([])

        assert backend.filter() is qs
    def test__get_column_for_field(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)
        name = properties['name']
        column = SQLAlchemyFilterBackend._get_column_for_field(name)

        assert column.key == 'name'
        assert isinstance(column.type, String)
        assert column.table is Waiter.__table__
    def test__get_column_for_field(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)
        name = properties['name']
        column = SQLAlchemyFilterBackend._get_column_for_field(name)

        assert column.key == 'name'
        assert isinstance(column.type, String)
        assert column.table is Waiter.__table__
    def test_init(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(alchemy_db.query(Place),
                                          context={"context": "here"})

        assert backend.model is Place
        assert backend.context == {"context": "here"}

        with pytest.raises(AssertionError):
            SQLAlchemyFilterBackend(alchemy_db.query(Place, Restaurant))
    def _test_build_clause(self, alchemy_db, name, lookup, value, expected, is_negated=False):
        backend = SQLAlchemyFilterBackend(
            alchemy_db.query(Place),
        )

        clause, to_join = backend.build_clause(
            FilterSpec(['restaurant', 'waiter_set', name], lookup, value, is_negated)
        )

        assert to_join == [Place.restaurant, Restaurant.waiter_set]
        assert_alchemy_expressions_equal(clause, expected)
    def test_filter_already_eagerloaded(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(
            alchemy_db.query(Place).options(joinedload(Place.restaurant).joinedload(Restaurant.waiter_set)),
        )
        backend.bind([
            FilterSpec(['restaurant', 'waiter_set', 'name'], 'exact', 'John', False),
        ])

        filtered = backend.filter()

        sql = six.text_type(filtered)
        assert 'place JOIN one_to_one' not in sql
        assert 'place LEFT OUTER JOIN one_to_one' in sql
    def test_filter_already_eagerloaded(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(
            alchemy_db.query(Place).options(
                joinedload(Place.restaurant).joinedload(
                    Restaurant.waiter_set)), )
        backend.bind([
            FilterSpec(['restaurant', 'waiter_set', 'name'], 'exact', 'John',
                       False),
        ])

        filtered = backend.filter()

        sql = six.text_type(filtered)
        assert 'place JOIN one_to_one' not in sql
        assert 'place LEFT OUTER JOIN one_to_one' in sql
    def _test_build_clause(self,
                           alchemy_db,
                           name,
                           lookup,
                           value,
                           expected,
                           is_negated=False):
        backend = SQLAlchemyFilterBackend(alchemy_db.query(Place), )

        clause, to_join = backend.build_clause(
            FilterSpec(['restaurant', 'waiter_set', name], lookup, value,
                       is_negated))

        assert to_join == [Place.restaurant, Restaurant.waiter_set]
        assert_alchemy_expressions_equal(clause, expected)
    def test_filter(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(alchemy_db.query(Place))
        backend.bind([
            FilterSpec(["restaurant", "waiter_set", "name"], "exact", "John",
                       False)
        ])

        filtered = backend.filter()

        sql = six.text_type(filtered)
        # eagerloads via outerjoin
        assert "LEFT OUTER JOIN one_to_one_restaurant" not in sql
        assert "LEFT OUTER JOIN one_to_one_waiter" not in sql
        # for filtering via inner join
        assert "JOIN one_to_one_restaurant" in sql
        assert "JOIN one_to_one_waiter" in sql
    def test_filter(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(alchemy_db.query(Place), )
        backend.bind([
            FilterSpec(['restaurant', 'waiter_set', 'name'], 'exact', 'John',
                       False),
        ])

        filtered = backend.filter()

        assert six.text_type(filtered) == (
            'SELECT one_to_one_place.id AS one_to_one_place_id, '
            'one_to_one_place.name AS one_to_one_place_name, '
            'one_to_one_place.address AS one_to_one_place_address \n'
            'FROM one_to_one_place '
            'JOIN one_to_one_restaurant '
            'ON one_to_one_restaurant.place_id = one_to_one_place.id '
            'JOIN one_to_one_waiter '
            'ON one_to_one_waiter.restaurant_id = one_to_one_restaurant.place_id '
            '\nWHERE one_to_one_waiter.name = :name_1')
    def test_filter(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(
            alchemy_db.query(Place),
        )
        backend.bind([
            FilterSpec(['restaurant', 'waiter_set', 'name'], 'exact', 'John', False),
        ])

        filtered = backend.filter()

        assert six.text_type(filtered) == (
            'SELECT one_to_one_place.id AS one_to_one_place_id, '
            'one_to_one_place.name AS one_to_one_place_name, '
            'one_to_one_place.address AS one_to_one_place_address \n'
            'FROM one_to_one_place '
            'JOIN one_to_one_restaurant '
            'ON one_to_one_restaurant.place_id = one_to_one_place.id '
            'JOIN one_to_one_waiter '
            'ON one_to_one_waiter.restaurant_id = one_to_one_restaurant.place_id '
            '\nWHERE one_to_one_waiter.name = :name_1'
        )
    def test_get_model(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(alchemy_db.query(Place))

        assert backend.get_model() is Place
    def test_empty(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(alchemy_db.query(Place),
                                          context={"context": "here"})

        assert "WHERE 0 = 1" in six.text_type(backend.empty())
    def test_get_model(self, alchemy_db):
        backend = SQLAlchemyFilterBackend(alchemy_db.query(Place))

        assert backend.get_model() is Place
    def test__get_related_model_for_field(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)
        restaurant = properties['restaurant']
        model = SQLAlchemyFilterBackend._get_related_model_for_field(restaurant)

        assert model is Restaurant
    def test__get_attribute_for_field(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)
        name = properties['name']
        attr = SQLAlchemyFilterBackend._get_attribute_for_field(name)

        assert attr is Waiter.name
    def test__get_properties_for_model(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)

        assert set(properties) == {'restaurant', 'id', 'restaurant_id', 'name'}
    def test__get_properties_for_model(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)

        assert set(properties) == {"restaurant", "id", "restaurant_id", "name"}
    def test__get_attribute_for_field(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)
        name = properties['name']
        attr = SQLAlchemyFilterBackend._get_attribute_for_field(name)

        assert attr is Waiter.name
    def test__get_properties_for_model(self):
        properties = SQLAlchemyFilterBackend._get_properties_for_model(Waiter)

        assert set(properties) == {'restaurant', 'id', 'restaurant_id', 'name'}