From 950e623f4760458a3db03ebdf9a13bb1eed528ca Mon Sep 17 00:00:00 2001 From: Mathieu Darse Date: Wed, 4 Nov 2015 16:52:05 +0100 Subject: [PATCH] Test equality expressions --- .../AST/FieldEqualsExpressionTest.php | 67 +++++++++++++++++++ .../SearchEngine/resources/queries.csv | 5 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/Alchemy/Tests/Phrasea/SearchEngine/AST/FieldEqualsExpressionTest.php diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/AST/FieldEqualsExpressionTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/AST/FieldEqualsExpressionTest.php new file mode 100644 index 0000000000..99dbd0fb6c --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/AST/FieldEqualsExpressionTest.php @@ -0,0 +1,67 @@ +assertTrue(method_exists(FieldEqualsExpression::class, '__toString'), 'Class does not have method __toString'); + $field = $this->prophesize(ASTField::class); + $field->__toString()->willReturn('foo'); + $node = new FieldEqualsExpression($field->reveal(), 'bar'); + $this->assertEquals('(foo == )', (string) $node); + } + + /** + * @dataProvider queryProvider + */ + public function testQueryBuild($index_field, $value, $compatible_value, $private, $expected_json) + { + $structure_field = $this->prophesize(StructureField::class); + $structure_field->isValueCompatible($value)->willReturn($compatible_value); + $structure_field->getIndexField(true)->willReturn($index_field); + $structure_field->isPrivate()->willReturn($private); + if ($private) { + $structure_field->getDependantCollections()->willReturn(['baz', 'qux']); + } + + $ast_field = $this->prophesize(ASTField::class); + $query_context = $this->prophesize(QueryContext::class); + $query_context->get($ast_field->reveal())->willReturn($structure_field); + + $node = new FieldEqualsExpression($ast_field->reveal(), 'bar'); + $query = $node->buildQuery($query_context->reveal()); + + $this->assertEquals(json_decode($expected_json, true), $query); + } + + public function queryProvider() + { + return [ + ['foo.raw', 'bar', true, true, '{ + "filtered": { + "filter": { + "terms": { + "base_id": ["baz","qux"] } }, + "query": { + "term": { + "foo.raw": "bar" } } } }'], + ['foo.raw', 'bar', true, false, '{ + "term": { + "foo.raw": "bar" } }'], + ['foo.raw', 'bar', false, true, 'null'], + ['foo.raw', 'bar', false, false, 'null'], + ]; + } +} diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/resources/queries.csv b/tests/Alchemy/Tests/Phrasea/SearchEngine/resources/queries.csv index 93c9c3a868..3c89dd5483 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/resources/queries.csv +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/resources/queries.csv @@ -48,7 +48,7 @@ foo EXCEPT (bar AND baz)|( EXCEPT ( AND )) foo EXCEPT (bar OR baz)|( EXCEPT ( OR )) foo EXCEPT (bar EXCEPT baz)|( EXCEPT ( EXCEPT )) -# Inequality operators +# Comparison operators foo < 42| foo ≤ 42| foo > 42| @@ -61,6 +61,9 @@ foo < "2015/01/01"| foo ≤ "2015/01/01"| foo > "2015/01/01"| foo ≥ "2015/01/01"| +foo = 42|( == ) +foo = bar|( == ) +foo = "bar"|( == ) # Field narrowing foo:bar|( MATCHES )