예제 #1
0
    def test_has_and_any(self):
        """Tests for the ``"has"`` and ``"any"`` operators.

        The `any` operator returns all instances for which any related instance
        in a given collection has some property. The `has` operator returns all
        instances for which a related instance has a given property.

        """
        # create test computers
        computer1 = self.Computer(name=u'c1', vendor=u'foo')
        computer2 = self.Computer(name=u'c2', vendor=u'bar')
        computer3 = self.Computer(name=u'c3', vendor=u'bar')
        computer4 = self.Computer(name=u'c4', vendor=u'bar')
        computer5 = self.Computer(name=u'c5', vendor=u'foo')
        computer6 = self.Computer(name=u'c6', vendor=u'foo')
        self.session.add_all((computer1, computer2, computer3, computer4,
                                 computer5, computer6))
        self.session.commit()
        # add the computers to three test people
        person1, person2, person3 = self.people[:3]
        person1.computers = [computer1, computer2, computer3]
        person2.computers = [computer4]
        person3.computers = [computer5, computer6]
        self.session.commit()
        # test 'any'
        d = dict(filters=[dict(name='computers__vendor', val=u'foo',
                               op='any')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 2)
        # test 'has'
        d = dict(filters=[dict(name='owner__name', op='has', val=u'Lincoln')])
        result = search(self.session, self.Computer, d)
        self.assertEqual(len(result), 3)
예제 #2
0
    def test_has_and_any_suboperators(self):
        """Tests for the ``"has"`` and ``"any"`` operators with suboperators.

        The `any` operator returns all instances for which any related instance
        in a given collection has some property. The `has` operator returns all
        instances for which a related instance has a given property.

        """
        # create test computers
        computer1 = self.Computer(name=u'c1', vendor=u'foo')
        computer2 = self.Computer(name=u'c2', vendor=u'bar')
        computer3 = self.Computer(name=u'c3', vendor=u'bar')
        computer4 = self.Computer(name=u'c4', vendor=u'bar')
        computer5 = self.Computer(name=u'c5', vendor=u'foo')
        computer6 = self.Computer(name=u'c6', vendor=u'foo')
        self.session.add_all((computer1, computer2, computer3, computer4,
                              computer5, computer6))
        self.session.commit()
        # add the computers to three test people
        person1, person2, person3 = self.people[:3]
        person1.computers = [computer1, computer2, computer3]
        person2.computers = [computer4]
        person3.computers = [computer5, computer6]
        self.session.commit()
        # test 'any'
        val = dict(name='vendor', op='like', val=u'%o%')
        d = dict(filters=[dict(name='computers', op='any', val=val)])
        result = search(self.session, self.Person, d)
        assert result.count() == 2
        # test 'has'
        val = dict(name='name', op='like', val=u'%incol%')
        d = dict(filters=[dict(name='owner', op='has', val=val)])
        result = search(self.session, self.Computer, d)
        assert result.count() == 3
예제 #3
0
    def test_has_and_any(self):
        """Tests for the ``"has"`` and ``"any"`` operators.

        The `any` operator returns all instances for which any related instance
        in a given collection has some property. The `has` operator returns all
        instances for which a related instance has a given property.

        """
        # create test computers
        computer1 = self.Computer(name=u"c1", vendor=u"foo")
        computer2 = self.Computer(name=u"c2", vendor=u"bar")
        computer3 = self.Computer(name=u"c3", vendor=u"bar")
        computer4 = self.Computer(name=u"c4", vendor=u"bar")
        computer5 = self.Computer(name=u"c5", vendor=u"foo")
        computer6 = self.Computer(name=u"c6", vendor=u"foo")
        self.session.add_all((computer1, computer2, computer3, computer4, computer5, computer6))
        self.session.commit()
        # add the computers to three test people
        person1, person2, person3 = self.people[:3]
        person1.computers = [computer1, computer2, computer3]
        person2.computers = [computer4]
        person3.computers = [computer5, computer6]
        self.session.commit()
        # test 'any'
        d = dict(filters=[dict(name="computers__vendor", val=u"foo", op="any")])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 2)
        # test 'has'
        d = dict(filters=[dict(name="owner__name", op="has", val=u"Lincoln")])
        result = search(self.session, self.Computer, d)
        self.assertEqual(len(result), 3)
예제 #4
0
    def test_has_and_any(self):
        """Tests for the ``"has"`` and ``"any"`` operators.

        The `any` operator returns all instances for which any related instance
        in a given collection has some property. The `has` operator returns all
        instances for which a related instance has a given property.

        """
        # create test computers
        computer1 = self.Computer(name=u'c1', vendor=u'foo')
        computer2 = self.Computer(name=u'c2', vendor=u'bar')
        computer3 = self.Computer(name=u'c3', vendor=u'bar')
        computer4 = self.Computer(name=u'c4', vendor=u'bar')
        computer5 = self.Computer(name=u'c5', vendor=u'foo')
        computer6 = self.Computer(name=u'c6', vendor=u'foo')
        self.session.add_all(
            (computer1, computer2, computer3, computer4, computer5, computer6))
        self.session.commit()
        # add the computers to three test people
        person1, person2, person3 = self.people[:3]
        person1.computers = [computer1, computer2, computer3]
        person2.computers = [computer4]
        person3.computers = [computer5, computer6]
        self.session.commit()
        # test 'any'
        d = dict(
            filters=[dict(name='computers__vendor', val=u'foo', op='any')])
        result = search(self.session, self.Person, d)
        assert result.count() == 2
        # test 'has'
        d = dict(filters=[dict(name='owner__name', op='has', val=u'Lincoln')])
        result = search(self.session, self.Computer, d)
        assert result.count() == 3
예제 #5
0
    def test_has_and_any_suboperators(self):
        """Tests for the ``"has"`` and ``"any"`` operators with suboperators.

        The `any` operator returns all instances for which any related instance
        in a given collection has some property. The `has` operator returns all
        instances for which a related instance has a given property.

        """
        # create test computers
        computer1 = self.Computer(name=u'c1', vendor=u'foo')
        computer2 = self.Computer(name=u'c2', vendor=u'bar')
        computer3 = self.Computer(name=u'c3', vendor=u'bar')
        computer4 = self.Computer(
            name=u'c4',
            vendor=None,
            programs=[self.ComputerProgram(program=self.Program())])
        computer5 = self.Computer(
            name=u'c5',
            vendor=u'foo',
            programs=[self.ComputerProgram(program=self.Program())])
        computer6 = self.Computer(name=u'c6', vendor=u'foo')
        self.session.add_all(
            (computer1, computer2, computer3, computer4, computer5, computer6))
        self.session.commit()
        # add the computers to three test people
        person1, person2, person3 = self.people[:3]
        person1.computers = [computer1, computer2, computer3]
        person2.computers = [computer4]
        person3.computers = [computer5, computer6]
        self.session.commit()
        # test 'any'
        val = dict(name='vendor', op='like', val=u'%o%')
        d = dict(filters=[dict(name='computers', op='any', val=val)])
        result = search(self.session, self.Person, d)
        assert result.count() == 2
        val = dict(name='vendor', op='is_null')
        d = dict(filters=[dict(name='computers', op='any', val=val)])
        result = search(self.session, self.Person, d)
        assert result.count() == 1
        # test 'has'
        val = dict(name='name', op='like', val=u'%incol%')
        d = dict(filters=[dict(name='owner', op='has', val=val)])
        result = search(self.session, self.Computer, d)
        assert result.count() == 3
        val = dict(name='vendor', op='is_null')
        d = dict(filters=[dict(name='computer', op='has', val=val)])
        result = search(self.session, self.ComputerProgram, d)
        assert result.count() == 1
예제 #6
0
    def test_search(self):
        """Tests that asking for a single result raises an error unless the
        result of the query truly has only a single element.

        """
        # tests getting multiple results
        d = {"single": True, "filters": [{"name": "name", "val": u"%y%", "op": "like"}]}
        with self.assertRaises(MultipleResultsFound):
            search(self.session, self.Person, d)

        # tests getting no results
        d = {"single": True, "filters": [{"name": "name", "val": u"bogusname", "op": "=="}]}
        with self.assertRaises(NoResultFound):
            search(self.session, self.Person, d)

        # tests getting exactly one result
        d = {"single": True, "filters": [{"name": "name", "val": u"Lincoln", "op": "=="}]}
        result = search(self.session, self.Person, d)
        self.assertEqual(result.name, u"Lincoln")
예제 #7
0
    def test_search(self):
        """Tests that asking for a single result raises an error unless the
        result of the query truly has only a single element.

        """
        # tests getting multiple results
        d = {
            'single': True,
            'filters': [{
                'name': 'name',
                'val': u'%y%',
                'op': 'like'
            }]
        }
        with self.assertRaises(MultipleResultsFound):
            search(self.session, self.Person, d)

        # tests getting no results
        d = {
            'single': True,
            'filters': [{
                'name': 'name',
                'val': u'bogusname',
                'op': '=='
            }]
        }
        with self.assertRaises(NoResultFound):
            search(self.session, self.Person, d)

        # tests getting exactly one result
        d = {
            'single': True,
            'filters': [{
                'name': 'name',
                'val': u'Lincoln',
                'op': '=='
            }]
        }
        result = search(self.session, self.Person, d)
        assert result.name == u'Lincoln'
예제 #8
0
    def test_search(self):
        """Tests that asking for a single result raises an error unless the
        result of the query truly has only a single element.

        """
        # tests getting multiple results
        d = {'single': True,
             'filters': [{'name': 'name', 'val': u'%y%', 'op': 'like'}]}
        with self.assertRaises(MultipleResultsFound):
            search(self.session, self.Person, d)

        # tests getting no results
        d = {'single': True,
             'filters': [{'name': 'name', 'val': u'bogusname', 'op': '=='}]}
        with self.assertRaises(NoResultFound):
            search(self.session, self.Person, d)

        # tests getting exactly one result
        d = {'single': True,
             'filters': [{'name': 'name', 'val': u'Lincoln', 'op': '=='}]}
        result = search(self.session, self.Person, d)
        self.assertEqual(result.name, u'Lincoln')
예제 #9
0
    def test_operators(self):
        """Tests for each of the individual operators in
        :data:`flask_restless.search.OPERATORS`.

        """
        for op in '==', 'eq', 'equals', 'equal_to':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 1)
            self.assertEqual(result[0].name, u'Lincoln')
        for op in '!=', 'ne', 'neq', 'not_equal_to', 'does_not_equal':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), len(self.people) - 1)
            self.assertNotIn(u'Lincoln', (p.name for p in result))
        for op in '>', 'gt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        for op in '<', 'lt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 2)
        for op in '>=', 'ge', 'gte', 'geq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        for op in '<=', 'le', 'lte', 'leq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name='name', op='like', val=u'%y%')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name='name', op='ilike', val=u'%Y%')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name='age', op='in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 2)
        d = dict(filters=[dict(name='age', op='not_in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name='birth_date', op='is_null')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 4)
        d = dict(filters=[dict(name='birth_date', op='is_not_null')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 1)
예제 #10
0
    def test_operators(self):
        """Tests for each of the individual operators in
        :data:`flask_restless.search.OPERATORS`.

        """
        for op in "==", "eq", "equals", "equal_to":
            d = dict(filters=[dict(name="name", op=op, val=u"Lincoln")])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 1)
            self.assertEqual(result[0].name, u"Lincoln")
        for op in "!=", "ne", "neq", "not_equal_to", "does_not_equal":
            d = dict(filters=[dict(name="name", op=op, val=u"Lincoln")])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), len(self.people) - 1)
            self.assertNotIn(u"Lincoln", (p.name for p in result))
        for op in ">", "gt":
            d = dict(filters=[dict(name="age", op=op, val=20)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        for op in "<", "lt":
            d = dict(filters=[dict(name="age", op=op, val=20)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 2)
        for op in ">=", "ge", "gte", "geq":
            d = dict(filters=[dict(name="age", op=op, val=23)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        for op in "<=", "le", "lte", "leq":
            d = dict(filters=[dict(name="age", op=op, val=23)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name="name", op="like", val=u"%y%")])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name="age", op="in", val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 2)
        d = dict(filters=[dict(name="age", op="not_in", val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name="birth_date", op="is_null")])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 4)
        d = dict(filters=[dict(name="birth_date", op="is_not_null")])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 1)
예제 #11
0
    def test_operators(self):
        """Tests for each of the individual operators in
        :data:`flask_restless.search.OPERATORS`.

        """
        for op in '==', 'eq', 'equals', 'equal_to':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            assert result.count() == 1
            assert result[0].name == u'Lincoln'
        for op in '!=', 'ne', 'neq', 'not_equal_to', 'does_not_equal':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            assert result.count() == len(self.people) - 1
            assert u'Lincoln' not in (p.name for p in result)
        for op in '>', 'gt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            assert result.count() == 3
        for op in '<', 'lt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            assert result.count() == 2
        for op in '>=', 'ge', 'gte', 'geq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            assert result.count() == 3
        for op in '<=', 'le', 'lte', 'leq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            assert result.count() == 3
        d = dict(filters=[dict(name='name', op='like', val=u'%y%')])
        result = search(self.session, self.Person, d)
        assert result.count() == 3
        d = dict(filters=[dict(name='name', op='ilike', val=u'%Y%')])
        result = search(self.session, self.Person, d)
        assert result.count() == 3
        d = dict(filters=[dict(name='age', op='in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        assert result.count() == 2
        d = dict(filters=[dict(name='age', op='not_in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        assert result.count() == 3
        d = dict(filters=[dict(name='birth_date', op='is_null')])
        result = search(self.session, self.Person, d)
        assert result.count() == 4
        d = dict(filters=[dict(name='birth_date', op='is_not_null')])
        result = search(self.session, self.Person, d)
        assert result.count() == 1
예제 #12
0
    def test_operators(self):
        """Tests for each of the individual operators in
        :data:`flask_restless.search.OPERATORS`.

        """
        for op in '==', 'eq', 'equals', 'equal_to':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            assert result.count() == 1
            assert result[0].name == u'Lincoln'
        for op in '!=', 'ne', 'neq', 'not_equal_to', 'does_not_equal':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            assert result.count() == len(self.people) - 1
            assert u'Lincoln' not in (p.name for p in result)
        for op in '>', 'gt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            assert result.count() == 3
        for op in '<', 'lt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            assert result.count() == 2
        for op in '>=', 'ge', 'gte', 'geq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            assert result.count() == 3
        for op in '<=', 'le', 'lte', 'leq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            assert result.count() == 3
        d = dict(filters=[dict(name='name', op='like', val=u'%y%')])
        result = search(self.session, self.Person, d)
        assert result.count() == 3
        d = dict(filters=[dict(name='name', op='ilike', val=u'%Y%')])
        result = search(self.session, self.Person, d)
        assert result.count() == 3
        d = dict(filters=[dict(name='age', op='in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        assert result.count() == 2
        d = dict(filters=[dict(name='age', op='not_in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        assert result.count() == 3
        d = dict(filters=[dict(name='birth_date', op='is_null')])
        result = search(self.session, self.Person, d)
        assert result.count() == 4
        d = dict(filters=[dict(name='birth_date', op='is_not_null')])
        result = search(self.session, self.Person, d)
        assert result.count() == 1
        d = dict(filters=[dict(name='birth_date', op='eq', val=None)])
        assert_raises(TypeError, search, self.session, self.Person, d)
예제 #13
0
    def test_operators(self):
        """Tests for each of the individual operators in
        :data:`flask_restless.search.OPERATORS`.

        """
        for op in '==', 'eq', 'equals', 'equal_to':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 1)
            self.assertEqual(result[0].name, u'Lincoln')
        for op in '!=', 'ne', 'neq', 'not_equal_to', 'does_not_equal':
            d = dict(filters=[dict(name='name', op=op, val=u'Lincoln')])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), len(self.people) - 1)
            self.assertNotIn(u'Lincoln', (p.name for p in result))
        for op in '>', 'gt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        for op in '<', 'lt':
            d = dict(filters=[dict(name='age', op=op, val=20)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 2)
        for op in '>=', 'ge', 'gte', 'geq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        for op in '<=', 'le', 'lte', 'leq':
            d = dict(filters=[dict(name='age', op=op, val=23)])
            result = search(self.session, self.Person, d)
            self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name='name', op='like', val=u'%y%')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name='age', op='in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 2)
        d = dict(filters=[dict(name='age', op='not_in', val=[19, 21, 23])])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 3)
        d = dict(filters=[dict(name='birth_date', op='is_null')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 4)
        d = dict(filters=[dict(name='birth_date', op='is_not_null')])
        result = search(self.session, self.Person, d)
        self.assertEqual(len(result), 1)