def test_keyword_upper_casing(self):
        new_completer = PGCompleter(smart_completion=True,
                                    settings={'keyword_casing': 'upper'})
        text = 'sel'
        position = len(text)
        result = new_completer.get_completions(
            Document(text=text, cursor_position=position), self.complete_event)

        # then completions should now be lower case
        self.assertEqual(result, [
            Completion(
                text='SELECT', start_position=-3, display_meta="keyword")
        ])
    def test_keyword_auto_casing(self):
        new_completer = PGCompleter(smart_completion=True,
                                    settings={'keyword_casing': 'auto'})

        # if text is lower case
        text = 'sel'
        position = len(text)
        result = new_completer.get_completions(
            Document(text=text, cursor_position=position), self.complete_event)

        # then completions should be lower case as well
        self.assertEqual(result, [
            Completion(
                text='select', start_position=-3, display_meta="keyword")
        ])
Example #3
0
 def setUp(self):
     self.completer = PGCompleter()
Example #4
0
class TestFuzzyCompletion(unittest.TestCase):
    """Methods for testing fuzzy completion"""
    def setUp(self):
        self.completer = PGCompleter()

    def test_ranking_ignores_identifier_quotes(self):
        """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"']
        matches = self.completer.find_matches(text, collection)
        self.assertEqual(len(matches), 2)

    def test_ranking_based_on_shortest_match(self):
        """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']
        matches = self.completer.find_matches(text, collection)
        self.assertGreater(matches[1].priority, matches[0].priority)

    @parameterized.expand([
        param(['user_action', 'user']),
        param(['user_group', 'user']),
        param(['user_group', 'user_action']),
    ])
    def test_should_break_ties_using_lexical_order(self, collection):
        """Fuzzy result rank should use lexical order to break ties.

        When fuzzy matching, if multiple matches have the same match length and
        start position, present them in lexical (rather than arbitrary) order. For
        example, if we have tables 'user', 'user_action', and 'user_group', a
        search for the text 'user' should present these tables in this order.

        The input collections to this test are out of order; each run checks that
        the search text 'user' results in the input tables being reordered
        lexically.

        """
        text = 'user'
        matches = self.completer.find_matches(text, collection)
        self.assertGreater(matches[1].priority, matches[0].priority)

    def test_matching_should_be_case_insensitive(self):
        """Fuzzy matching should keep matches even if letter casing doesn't match.

        This test checks that variations of the text which have different casing
        are still matched.
        """
        text = 'foo'
        collection = ['Foo', 'FOO', 'fOO']
        matches = self.completer.find_matches(text, collection)
        self.assertEqual(len(matches), 3)
Example #5
0
 def setUp(self):
     self.completer = PGCompleter(smart_completion=False)
     self.complete_event = Mock()
Example #6
0
class TestNaiveCompletion(unittest.TestCase):
    """Methods for testing non-smart completion"""
    def setUp(self):
        self.completer = PGCompleter(smart_completion=False)
        self.complete_event = Mock()

    def test_empty_string_completion(self):
        text = ''
        position = 0
        result = set(
            self.completer.get_completions(
                Document(text=text, cursor_position=position),
                self.complete_event))
        self.assertSetEqual(
            result, set(map(Completion, self.completer.all_completions)))

    def test_select_keyword_completion(self):
        text = 'SEL'
        position = len('SEL')
        result = set(
            self.completer.get_completions(
                Document(text=text, cursor_position=position),
                self.complete_event))
        self.assertSetEqual(
            result, set([Completion(text='SELECT', start_position=-3)]))

    def test_function_name_completion(self):
        text = 'SELECT MA'
        position = len('SELECT MA')
        result = set(
            self.completer.get_completions(
                Document(text=text, cursor_position=position),
                self.complete_event))
        self.assertSetEqual(
            result,
            set([
                Completion(text='MATERIALIZED VIEW', start_position=-2),
                Completion(text='MAX', start_position=-2),
                Completion(text='MAXEXTENTS', start_position=-2)
            ]))

    def test_column_name_completion(self):
        text = 'SELECT  FROM users'
        position = len('SELECT ')
        result = set(
            self.completer.get_completions(
                Document(text=text, cursor_position=position),
                self.complete_event))
        self.assertSetEqual(
            result, set(map(Completion, self.completer.all_completions)))

    # {{ PGToolsService EDIT }}
    # Disabling as we will not support cli-only features
    # def test_paths_completion(self):
    #     text = '\i '
    #     position = len(text)
    #     result = set(self.completer.get_completions(
    #         Document(text=text, cursor_position=position),
    #         self.complete_event,
    #         smart_completion=True))
    #     # Set comparison: > means "is superset"
    #     self.assertTrue(result > set([Completion(text="setup.py", start_position=0)]))

    def test_alter_well_known_keywords_completion(self):
        text = 'ALTER '
        position = len(text)
        result = set(
            self.completer.get_completions(Document(text=text,
                                                    cursor_position=position),
                                           self.complete_event,
                                           smart_completion=True))
        # Set comparison: > means "is superset"
        self.assertTrue(result > set([
            Completion(text="DATABASE", display_meta='keyword'),
            Completion(text="TABLE", display_meta='keyword'),
            Completion(text="SYSTEM", display_meta='keyword'),
        ]))
        self.assertTrue(
            Completion(text="CREATE", display_meta="keyword") not in result)

    def test_keyword_lower_casing(self):
        new_completer = PGCompleter(smart_completion=True,
                                    settings={'keyword_casing': 'lower'})
        text = 'SEL'
        position = len(text)
        result = set(
            new_completer.get_completions(
                Document(text=text, cursor_position=position),
                self.complete_event))

        # then completions should now be lower case
        self.assertSetEqual(
            result,
            set([
                Completion(text='select',
                           start_position=-3,
                           display_meta="keyword")
            ]))

    def test_keyword_upper_casing(self):
        new_completer = PGCompleter(smart_completion=True,
                                    settings={'keyword_casing': 'upper'})
        text = 'sel'
        position = len(text)
        result = set(
            new_completer.get_completions(
                Document(text=text, cursor_position=position),
                self.complete_event))

        # then completions should now be lower case
        self.assertSetEqual(
            result,
            set([
                Completion(text='SELECT',
                           start_position=-3,
                           display_meta="keyword")
            ]))

    def test_keyword_auto_casing(self):
        new_completer = PGCompleter(smart_completion=True,
                                    settings={'keyword_casing': 'auto'})

        # if text is lower case
        text = 'sel'
        position = len(text)
        result = set(
            new_completer.get_completions(
                Document(text=text, cursor_position=position),
                self.complete_event))

        # then completions should be lower case as well
        self.assertSetEqual(
            result,
            set([
                Completion(text='select',
                           start_position=-3,
                           display_meta="keyword")
            ]))