Beispiel #1
0
    def parse(self, path):
        if not path:
            raise SearchParserError('No condition given')

        sp = qvarn.SearchParameters()

        pairs = list(self._parse_simple(path))

        for operator, args in pairs:
            if operator == 'show_all':
                sp.set_show_all()
            elif operator == 'show':
                for field in args:
                    sp.add_show_field(field)
            elif operator == 'sort':
                for field in args:
                    sp.add_sort_key(field)
            elif operator == 'offset':
                sp.set_offset(int(args[0]))
            elif operator == 'limit':
                sp.set_limit(int(args[0]))
            else:
                klass = self.conditions[operator][1]
                cond = klass(*args)
                sp.add_cond(cond)

        self._check_params(sp)

        return sp
Beispiel #2
0
 def test_adds_cond(self):
     cond = qvarn.Yes()
     sp = qvarn.SearchParameters()
     self.assertEqual(sp.cond, None)
     sp.add_cond(cond)
     self.assertEqual(sp.cond, cond)
     sp.add_cond(cond)
     self.assertTrue(isinstance(sp.cond, qvarn.All))
     self.assertEqual(len(sp.cond.conds), 2)
     self.assertEqual(sp.cond.conds, [cond, cond])
     sp.add_cond(cond)
     self.assertTrue(isinstance(sp.cond, qvarn.All))
     self.assertEqual(len(sp.cond.conds), 3)
     self.assertEqual(sp.cond.conds, [cond, cond, cond])
Beispiel #3
0
 def test_setting_show_all_when_fields_are_set_raises_error(self):
     sp = qvarn.SearchParameters()
     sp.add_show_field('foo')
     with self.assertRaises(qvarn.SearchParserError):
         sp.set_show_all()
Beispiel #4
0
 def test_set_show_all(self):
     sp = qvarn.SearchParameters()
     sp.set_show_all()
     self.assertEqual(sp.show_all, True)
Beispiel #5
0
 def test_adds_show_field(self):
     sp = qvarn.SearchParameters()
     sp.add_show_field('foo')
     self.assertEqual(sp.show_fields, ['foo'])
Beispiel #6
0
 def test_adds_sort_key(self):
     sp = qvarn.SearchParameters()
     sp.add_sort_key('foo')
     self.assertEqual(sp.sort_keys, ['foo'])
Beispiel #7
0
 def test_raises_error_if_setting_limit_a_second_time(self):
     sp = qvarn.SearchParameters()
     sp.set_limit(42)
     with self.assertRaises(qvarn.SearchParserError):
         sp.set_limit(42)
Beispiel #8
0
 def test_sets_limit(self):
     sp = qvarn.SearchParameters()
     sp.set_limit(42)
     self.assertEqual(sp.limit, 42)
Beispiel #9
0
 def test_sets_offset(self):
     sp = qvarn.SearchParameters()
     sp.set_offset(42)
     self.assertEqual(sp.offset, 42)
Beispiel #10
0
 def test_has_correct_initial_state(self):
     sp = qvarn.SearchParameters()
     self.assertEqual(sp.sort_keys, [])
     self.assertEqual(sp.show_fields, [])
     self.assertEqual(sp.show_all, False)
     self.assertEqual(sp.cond, None)