summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDaniel Rocco <drocco@gmail.com>2015-06-16 08:35:42 -0400
committerDaniel Rocco <drocco@gmail.com>2015-06-16 08:35:42 -0400
commit40149ad648fa84f4597fcb3f3182b83ee62de035 (patch)
tree40c4f7bd4171d333098d3045b6c79323fb351d81 /tests
parentbbd1ac231e8ca1922e75fe6337f338af6c7a2038 (diff)
Add tests for fuzzy ranking adjustments.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_fuzzy_completion.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/test_fuzzy_completion.py b/tests/test_fuzzy_completion.py
new file mode 100644
index 00000000..5fcd3d05
--- /dev/null
+++ b/tests/test_fuzzy_completion.py
@@ -0,0 +1,54 @@
+from __future__ import unicode_literals
+import pytest
+from prompt_toolkit.completion import Completion
+from prompt_toolkit.document import Document
+
+
+@pytest.fixture
+def completer():
+ import pgcli.pgcompleter as pgcompleter
+ return pgcompleter.PGCompleter()
+
+
+def test_ranking_ignores_identifier_quotes(completer):
+ """When calculating result rank, identifier quotes should be ignored.
+
+ The result ranking algorithm ignores identifier quotes. Without this
+ correction, the match "user", which Postgres requires to be quoted
+ since it is also a reserved word, would incorrectly fall below the
+ match user_action because the literal quotation marks in "user"
+ alter the position of the match.
+
+ This test checks that the fuzzy ranking algorithm correctly ignores
+ quotation marks when computing match ranks.
+
+ """
+
+ text = 'user'
+ collection = ['user_action', '"user"']
+
+ result = [match.text for match in completer.find_matches(text, collection)]
+
+ assert result == ['"user"', 'user_action']
+
+
+def test_ranking_based_on_shortest_match(completer):
+ """Fuzzy result rank should be based on shortest match.
+
+ Result ranking in fuzzy searching is partially based on the length
+ of matches: shorter matches are considered more relevant than
+ longer ones. When searching for the text 'user', the length
+ component of the match 'user_group' could be either 4 ('user') or
+ 7 ('user_gr').
+
+ This test checks that the fuzzy ranking algorithm uses the shorter
+ match when calculating result rank.
+
+ """
+
+ text = 'user'
+ collection = ['api_user', 'user_group']
+
+ result = [match.text for match in completer.find_matches(text, collection)]
+
+ assert result == ['user_group', 'api_user']