def test_sort_by_deps_not_included(self, open_url_mock):
        calls = [
            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
            MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'),
            get_schema_response('/test:deps')
        ]
        open_url_mock.side_effect = lambda *args, **kwargs: mock_call(calls, *args, **kwargs)

        parent = "/test:deps"
        schema_data = json.loads(
            SCHEMA_DATA['/test:deps'])
        schema = schema_data['data']

        values = {
            'a': '1',
            'b': '2'
        }

        vb = nso.ValueBuilder(nso.JsonRpc('http://localhost:8080/jsonrpc', 10, False))
        vb.build(parent, None, values, schema)
        values = list(vb.values)
        self.assertEqual(2, len(values))
        value = values[0]
        self.assertEqual('{0}/a'.format(parent), value.path)
        self.assertEqual('1', value.value)
        value = values[1]
        self.assertEqual('{0}/b'.format(parent), value.path)
        self.assertEqual('2', value.value)

        self.assertEqual(0, len(calls))
    def test_leaf_list_type_45(self, open_url_mock):
        calls = [
            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
            MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'),
            get_schema_response('/test:test/device-list')
        ]
        open_url_mock.side_effect = lambda *args, **kwargs: mock_call(calls, *args, **kwargs)

        parent = "/test:test"
        schema_data = json.loads(
            SCHEMA_DATA['/test:test'])
        schema = schema_data['data']

        vb = nso.ValueBuilder(nso.JsonRpc('http://localhost:8080/jsonrpc', 10, False))
        vb.build(parent, None, {'device-list': ['one', 'two']}, schema)
        values = list(vb.values)
        self.assertEqual(3, len(values))
        value = values[0]
        self.assertEqual('{0}/device-list'.format(parent), value.path)
        self.assertEqual(nso.State.ABSENT, value.state)
        value = values[1]
        self.assertEqual('{0}/device-list{{one}}'.format(parent), value.path)
        self.assertEqual(nso.State.PRESENT, value.state)
        value = values[2]
        self.assertEqual('{0}/device-list{{two}}'.format(parent), value.path)
        self.assertEqual(nso.State.PRESENT, value.state)

        self.assertEqual(0, len(calls))
    def test_nested_choice(self, open_url_mock):
        calls = [
            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
            MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'),
            get_schema_response('/test:test'),
            MockResponse('exists', {'path': '/test:test{direct}'}, 200, '{"result": {"exists": true}}'),
            MockResponse('exists', {'path': '/test:test{nested}'}, 200, '{"result": {"exists": true}}')
        ]
        open_url_mock.side_effect = lambda *args, **kwargs: mock_call(calls, *args, **kwargs)

        parent = "/test:test"
        schema_data = json.loads(
            SCHEMA_DATA['/test:test'])
        schema = schema_data['data']

        vb = nso.ValueBuilder(nso.JsonRpc('http://localhost:8080/jsonrpc', 10, False))
        vb.build(parent, None, [{'name': 'direct', 'direct-child': 'direct-value'},
                                {'name': 'nested', 'nested-child': 'nested-value'}], schema)
        values = list(vb.values)
        self.assertEqual(2, len(values))
        value = values[0]
        self.assertEqual('{0}{{direct}}/direct-child'.format(parent), value.path)
        self.assertEqual('set', value.state)
        self.assertEqual('direct-value', value.value)

        value = values[1]
        self.assertEqual('{0}{{nested}}/nested-child'.format(parent), value.path)
        self.assertEqual('set', value.state)
        self.assertEqual('nested-value', value.value)

        self.assertEqual(0, len(calls))
    def test_identityref_key(self, open_url_mock):
        calls = [
            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
            MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'),
            get_schema_response('/an:id-name-values/id-name-value'),
            MockResponse('get_module_prefix_map', {}, 200, '{{"result": {0}}}'.format(MODULE_PREFIX_MAP)),
            MockResponse('exists', {'path': '/an:id-name-values/id-name-value{an:id-one}'}, 200, '{"result": {"exists": true}}')
        ]
        open_url_mock.side_effect = lambda *args, **kwargs: mock_call(calls, *args, **kwargs)

        parent = "/an:id-name-values"
        schema_data = json.loads(
            SCHEMA_DATA['/an:id-name-values/id-name-value'])
        schema = schema_data['data']

        vb = nso.ValueBuilder(nso.JsonRpc('http://localhost:8080/jsonrpc', 10, False))
        vb.build(parent, 'id-name-value', [{'name': 'ansible-nso:id-one', 'value': '1'}], schema)
        values = list(vb.values)
        self.assertEqual(1, len(values))
        value = values[0]
        self.assertEqual('{0}/id-name-value{{an:id-one}}/value'.format(parent), value.path)
        self.assertEqual('set', value.state)
        self.assertEqual('1', value.value)

        self.assertEqual(0, len(calls))
    def test_exists_data_not_found(self, open_url_mock):
        calls = [
            MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'),
            MockResponse('exists', {'path': '/list{missing-parent}/list{child}'}, 200, '{"error":{"type":"data.not_found"}}')
        ]
        open_url_mock.side_effect = lambda *args, **kwargs: mock_call(calls, *args, **kwargs)
        client = nso.JsonRpc('http://localhost:8080/jsonrpc', 10, False)
        self.assertEqual(False, client.exists('/list{missing-parent}/list{child}'))

        self.assertEqual(0, len(calls))
    def test_exists(self, open_url_mock):
        calls = [
            MockResponse('new_trans', {}, 200, '{"result": {"th": 1}}'),
            MockResponse('exists', {'path': '/exists'}, 200, '{"result": {"exists": true}}'),
            MockResponse('exists', {'path': '/not-exists'}, 200, '{"result": {"exists": false}}')
        ]
        open_url_mock.side_effect = lambda *args, **kwargs: mock_call(calls, *args, **kwargs)
        client = nso.JsonRpc('http://localhost:8080/jsonrpc', 10, False)
        self.assertEqual(True, client.exists('/exists'))
        self.assertEqual(False, client.exists('/not-exists'))

        self.assertEqual(0, len(calls))