def test_datemath_in_field(self): tree = SearchField("foo", Word(r"2015-12-19||+2\d")) parsed = parser.parse(r'foo:2015-12-19||+2\d') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed) tree = SearchField("foo", Word(r"now+2h+20m\h")) parsed = parser.parse(r'foo:now+2h+20m\h') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed)
def test_reserved_ko(self): """Test reserved word hurt as they hurt lucene """ with self.assertRaises(ParseError): parser.parse('foo:NOT') with self.assertRaises(ParseError): parser.parse('foo:AND') with self.assertRaises(ParseError): parser.parse('foo:OR') with self.assertRaises(ParseError): parser.parse('OR') with self.assertRaises(ParseError): parser.parse('AND')
def test_flavours(self): tree = SearchField( "somedate", Range(Word("now/d-1d+7H"), Word("now/d+7H"), True, True)) parsed = parser.parse('somedate:[now/d-1d+7H TO now/d+7H]') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_phrase(self): tree = (AndOperation(Phrase('"a phrase (AND a complicated~ one)"'), Phrase('"Another one"'))) parsed = parser.parse( '"a phrase (AND a complicated~ one)" AND "Another one"') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_date_in_range(self): # juste one funky expression tree = SearchField( "foo", Range(Word(r"2015-12-19||+2\d"), Word(r"now+3d+12h\h"))) parsed = parser.parse(r'foo:[2015-12-19||+2\d TO now+3d+12h\h]') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed)
def test_range(self): tree = (AndOperation( SearchField("foo", Range(Word("10"), Word("100"), True, True)), SearchField("bar", Range(Word("a*"), Word("*"), True, False)))) parsed = parser.parse('foo:[10 TO 100] AND bar:[a* TO *}') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_boost(self): tree = (UnknownOperation( Boost(Phrase('"foo bar"'), Decimal("3.0")), Boost(Group(AndOperation(Word('baz'), Word('bar'))), Decimal("2.1")))) parsed = parser.parse('"foo bar"^3 (baz AND bar)^2.1') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_approx(self): tree = (UnknownOperation(Proximity(Phrase('"foo bar"'), 3), Proximity(Phrase('"foo baz"'), 1), Fuzzy(Word('baz'), Decimal("0.3")), Fuzzy(Word('fou'), Decimal("0.5")))) parsed = parser.parse('"foo bar"~3 "foo baz"~ baz~0.3 fou~') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_date_in_field(self): tree = SearchField("foo", Word("2015-12-19")) parsed = parser.parse('foo:2015-12-19') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed) tree = SearchField("foo", Word("2015-12-19T22:30")) parsed = parser.parse('foo:2015-12-19T22:30') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed) tree = SearchField("foo", Word("2015-12-19T22:30:45")) parsed = parser.parse('foo:2015-12-19T22:30:45') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed) tree = SearchField("foo", Word("2015-12-19T22:30:45.234Z")) parsed = parser.parse('foo:2015-12-19T22:30:45.234Z') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed)
def test_reserved_ok(self): """Test reserved word do not hurt in certain positions """ tree = SearchField("foo", Word("TO")) parsed = parser.parse('foo:TO') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed) tree = SearchField("foo", Word("TO*")) parsed = parser.parse('foo:TO*') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed) tree = SearchField("foo", Word("NOT*")) parsed = parser.parse('foo:NOT*') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed) tree = SearchField("foo", Phrase('"TO AND OR"')) parsed = parser.parse('foo:"TO AND OR"') self.assertEqual(str(tree), str(parsed)) self.assertEqual(tree, parsed)
def test_groups(self): tree = (OrOperation( Word('test'), Group( AndOperation( SearchField( "subject", FieldGroup(OrOperation(Word('foo'), Word('bar')))), Word('baz'))))) parsed = parser.parse('test OR (subject:(foo OR bar) AND baz)') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_combinations(self): # self.assertEqual(parser.parse("subject:test desc:(house OR car)").pval, "") tree = (UnknownOperation( SearchField("subject", Word("test")), AndOperation( SearchField( "desc", FieldGroup(OrOperation(Word("house"), Word("car")))), Not(Proximity(Phrase('"approximatly this"'), 3))))) parsed = parser.parse( 'subject:test desc:(house OR car) AND NOT "approximatly this"~3') self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_simplest(self): tree = (AndOperation(Word("foo"), Word("bar"))) parsed = parser.parse("foo AND bar") self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_simple_field(self): tree = (SearchField("subject", Word("test"))) parsed = parser.parse("subject:test") self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_field_with_number(self): # non regression for issue #10 tree = (SearchField("field_42", Word("42"))) parsed = parser.parse("field_42:42") self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_minus(self): tree = (AndOperation(Prohibit(Word("test")), Prohibit(Word("foo")), Not(Word("bar")))) parsed = parser.parse("-test AND -foo AND NOT bar") self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_plus(self): tree = (AndOperation(Plus(Word("test")), Word("foo"), Plus(Word("bar")))) parsed = parser.parse("+test AND foo AND +bar") self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)
def test_implicit_operations(self): tree = (UnknownOperation(Word("foo"), Word("bar"))) parsed = parser.parse("foo bar") self.assertEqual(str(parsed), str(tree)) self.assertEqual(parsed, tree)