Example #1
0
    def test_many(self):
        self.maxDiff = None
        pattern = QueryPattern('the_table',
                               [qcreq('animal'),
                                qcopt('adverb'),
                                qcreq('verb'),
                                qcreq('object'),
                                qcopt('ftw')])
        fox = Phrase('animal', { 'english' : 'the fox' }, [])
        mermaid = Phrase('animal', { 'english' : 'the mermaid' }, [])
        really = Phrase('adverb', { 'english' : 'really' }, [])
        totally = Phrase('adverb', { 'english' : 'totally' }, [])
        eats = Phrase('verb', { 'english' : 'eats' }, [])
        consumes = Phrase('verb', { 'english' : 'consumes' }, [])
        coconut = Phrase('object', { 'english' : 'a coconut' }, [])
        lamb = Phrase('object', { 'english' : 'a lamb' }, [])
        ftw = Phrase('ftw', { 'english' : 'FOR THE WIN' }, [])

        self.assertEqual(pattern.resolve([fox, mermaid, ftw]), [])
        self.assertEqual(pattern.resolve([fox, eats, lamb]), [Query('the_table', [fox, eats, lamb])])
        self.assertEqual(pattern.resolve([fox, mermaid, eats, coconut]), [Query('the_table', [fox, eats, coconut]),
                                                                          Query('the_table', [mermaid, eats, coconut])])

        result = pattern.resolve([fox, consumes, really, totally, lamb, ftw])
        target = [Query('the_table', [fox, really, consumes, lamb]),
                  Query('the_table', [fox, consumes, lamb]),
                  Query('the_table', [fox, totally, consumes, lamb]),
                  Query('the_table', [fox, consumes, lamb, ftw]),
                  Query('the_table', [fox, really, consumes, lamb, ftw]),
                  Query('the_table', [fox, totally, consumes, lamb, ftw])]
        self.assertEqual(len(result), len(target))
        for t in target:
            self.assertTrue(t in result)
Example #2
0
 def test_two(self):
     pattern = QueryPattern('the table', [qcreq('t1'), qcopt('t2')])
     t1 = Phrase('t1', {}, [])
     t2 = Phrase('t2', {}, [])
     self.assertEqual(pattern.resolve([t1]), [Query('the table', [t1])])
     self.assertEqual(pattern.resolve([t2]), [])
     self.assertEqual(pattern.resolve([t1, t2]), [Query('the table', [t1, t2]),
                                                  Query('the table', [t1])])
Example #3
0
 def test_single_optional(self):
     pattern = QueryPattern('the_table', [qcopt('t')])
     phrase = Phrase('t', {}, [])
     result = pattern.resolve([phrase])
     target = [Query('the_table', [phrase]), Query('the_table', [])]
     self.assertEqual(result, target)
Example #4
0
 def test_single_required(self):
     pattern = QueryPattern('the_table', [qcreq('t')])
     phrase = Phrase('t', {}, [])
     self.assertEqual(pattern.resolve([phrase]), [Query('the_table', [phrase])])
Example #5
0
 def test_empty_phrases_optional(self):
     pattern = QueryPattern('the_table', [qcopt('t')])
     self.assertEqual(pattern.resolve([]), [])
Example #6
0
 def test_empty_phrases_required(self):
     pattern = QueryPattern('the_table', [qcreq('t')])
     self.assertEqual(pattern.resolve([]), [])