File: test_query.py

package info (click to toggle)
knack 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 696 kB
  • sloc: python: 6,261; sh: 8; makefile: 4
file content (83 lines) | stat: -rw-r--r-- 3,014 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import unittest
from unittest import mock

from knack.events import EVENT_PARSER_GLOBAL_CREATE
from knack.query import CLIQuery
from tests.util import MockContext


class TestQueryEventHandling(unittest.TestCase):

    def setUp(self):
        self.mock_ctx = MockContext()
        self.cli_query = CLIQuery(cli_ctx=self.mock_ctx)

    def test_cli_ctx_type_error(self):
        with self.assertRaises(TypeError):
            CLIQuery(cli_ctx=object())

    def test_query_argument_registrations(self):
        parser_arg_group_mock = mock.MagicMock()
        self.mock_ctx.raise_event(EVENT_PARSER_GLOBAL_CREATE, arg_group=parser_arg_group_mock)
        parser_arg_group_mock.add_argument.assert_any_call('--query',
                                                           metavar=mock.ANY,
                                                           dest=mock.ANY,
                                                           help=mock.ANY,
                                                           type=mock.ANY)


class TestQuery(unittest.TestCase):
    '''Tests for the values that can be passed to the --query parameter.
    These tests ensure that we are handling invalid queries correctly and raising appropriate errors
    that argparse can then handle.
    (We are not testing JMESPath itself here)
    '''

    def test_query_valid_1(self):
        query = 'length(@)'
        # Should not raise any exception as it is valid
        CLIQuery.jmespath_type(query)

    def test_query_valid_2(self):
        query = "[?propertyX.propertyY.propertyZ=='AValue'].[col1,col2]"
        # Should not raise any exception as it is valid
        CLIQuery.jmespath_type(query)

    def test_query_empty(self):
        query = ''
        with self.assertRaises(ValueError):
            CLIQuery.jmespath_type(query)

    def test_query_unbalanced(self):
        query = 'length(@'
        with self.assertRaises(ValueError):
            CLIQuery.jmespath_type(query)

    def test_query_invalid_1(self):
        query = '[?asdf=asdf]'
        with self.assertRaises(ValueError):
            CLIQuery.jmespath_type(query)

    def test_query_invalid_2(self):
        query = '[?name=My Value]'
        with self.assertRaises(ValueError):
            CLIQuery.jmespath_type(query)

    def test_query_invalid_3(self):
        query = "[].location='westus'"
        with self.assertRaises(ValueError):
            CLIQuery.jmespath_type(query)

    def test_query_invalid_4(self):
        query = "length([?contains('id', 'Publishers'])"
        with self.assertRaises(ValueError):
            CLIQuery.jmespath_type(query)


if __name__ == '__main__':
    unittest.main()