2020-02-04 09:33:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from benedict.core import keylists as _keylists
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class keylists_test_case(unittest.TestCase):
|
2022-02-13 10:35:43 +00:00
|
|
|
"""
|
|
|
|
This class describes a keylists test case.
|
|
|
|
"""
|
2020-02-04 09:33:20 +00:00
|
|
|
|
|
|
|
def test_keylists(self):
|
|
|
|
i = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": 1,
|
|
|
|
"b": {
|
|
|
|
"c": {
|
|
|
|
"x": 2,
|
|
|
|
"y": 3,
|
2020-02-04 09:33:20 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"d": {
|
|
|
|
"x": 4,
|
|
|
|
"y": 5,
|
2020-02-04 09:33:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
o = _keylists(i)
|
|
|
|
r = [
|
2022-02-13 10:35:43 +00:00
|
|
|
["a"],
|
|
|
|
["b"],
|
|
|
|
["b", "c"],
|
|
|
|
["b", "c", "x"],
|
|
|
|
["b", "c", "y"],
|
|
|
|
["b", "d"],
|
|
|
|
["b", "d", "x"],
|
|
|
|
["b", "d", "y"],
|
2020-02-04 09:33:20 +00:00
|
|
|
]
|
2020-02-04 11:45:08 +00:00
|
|
|
for k in r:
|
|
|
|
self.assertTrue(k in o)
|
2020-02-04 09:33:20 +00:00
|
|
|
|
|
|
|
def test_keylists_with_non_string_keys(self):
|
|
|
|
i = {
|
|
|
|
True: {
|
|
|
|
True: 1,
|
|
|
|
},
|
|
|
|
False: {
|
|
|
|
False: 1,
|
|
|
|
},
|
|
|
|
None: {
|
|
|
|
None: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
o = _keylists(i)
|
|
|
|
r = [
|
|
|
|
[False],
|
|
|
|
[False, False],
|
|
|
|
[None],
|
|
|
|
[None, None],
|
|
|
|
[True],
|
|
|
|
[True, True],
|
|
|
|
]
|
|
|
|
for k in r:
|
|
|
|
self.assertTrue(k in o)
|
2020-09-21 14:41:44 +00:00
|
|
|
|
|
|
|
def test_keylists_with_lists_and_indexes_included(self):
|
|
|
|
i = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": 1,
|
|
|
|
"b": {
|
|
|
|
"c": {
|
|
|
|
"x": 2,
|
|
|
|
"y": 3,
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"d": {
|
|
|
|
"x": 4,
|
|
|
|
"y": 5,
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"e": [
|
2020-09-21 14:41:44 +00:00
|
|
|
{
|
2022-02-13 10:35:43 +00:00
|
|
|
"x": 1,
|
|
|
|
"y": -1,
|
|
|
|
"z": [1, 2, 3],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
|
|
|
{
|
2022-02-13 10:35:43 +00:00
|
|
|
"x": 2,
|
|
|
|
"y": -2,
|
|
|
|
"z": [2, 3, 4],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
|
|
|
{
|
2022-02-13 10:35:43 +00:00
|
|
|
"x": 3,
|
|
|
|
"y": -3,
|
|
|
|
"z": [3, 4, 5],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
o = _keylists(i, indexes=True)
|
2020-09-22 12:11:50 +00:00
|
|
|
o.sort()
|
2020-09-21 14:41:44 +00:00
|
|
|
r = [
|
2022-02-13 10:35:43 +00:00
|
|
|
["a"],
|
|
|
|
["b"],
|
|
|
|
["b", "c"],
|
|
|
|
["b", "c", "x"],
|
|
|
|
["b", "c", "y"],
|
|
|
|
["b", "d"],
|
|
|
|
["b", "d", "x"],
|
|
|
|
["b", "d", "y"],
|
|
|
|
["b", "e"],
|
|
|
|
["b", "e[0]"],
|
|
|
|
["b", "e[0]", "x"],
|
|
|
|
["b", "e[0]", "y"],
|
|
|
|
["b", "e[0]", "z"],
|
|
|
|
["b", "e[0]", "z[0]"],
|
|
|
|
["b", "e[0]", "z[1]"],
|
|
|
|
["b", "e[0]", "z[2]"],
|
|
|
|
["b", "e[1]"],
|
|
|
|
["b", "e[1]", "x"],
|
|
|
|
["b", "e[1]", "y"],
|
|
|
|
["b", "e[1]", "z"],
|
|
|
|
["b", "e[1]", "z[0]"],
|
|
|
|
["b", "e[1]", "z[1]"],
|
|
|
|
["b", "e[1]", "z[2]"],
|
|
|
|
["b", "e[2]"],
|
|
|
|
["b", "e[2]", "x"],
|
|
|
|
["b", "e[2]", "y"],
|
|
|
|
["b", "e[2]", "z"],
|
|
|
|
["b", "e[2]", "z[0]"],
|
|
|
|
["b", "e[2]", "z[1]"],
|
|
|
|
["b", "e[2]", "z[2]"],
|
2020-09-21 14:41:44 +00:00
|
|
|
]
|
|
|
|
self.assertEqual(o, r)
|
|
|
|
|
|
|
|
def test_keylists_with_nested_lists_and_indexes_included(self):
|
|
|
|
i = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": {
|
|
|
|
"b": [
|
2020-09-21 14:41:44 +00:00
|
|
|
[1, 2],
|
|
|
|
[3, 4, 5],
|
|
|
|
[
|
|
|
|
{
|
2022-02-13 10:35:43 +00:00
|
|
|
"x": 1,
|
|
|
|
"y": -1,
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
],
|
2020-09-21 14:41:44 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
o = _keylists(i, indexes=True)
|
2020-09-22 12:11:50 +00:00
|
|
|
o.sort()
|
2020-09-21 14:41:44 +00:00
|
|
|
r = [
|
2022-02-13 10:35:43 +00:00
|
|
|
["a"],
|
|
|
|
["a", "b"],
|
|
|
|
["a", "b[0]"],
|
|
|
|
["a", "b[0][0]"],
|
|
|
|
["a", "b[0][1]"],
|
|
|
|
["a", "b[1]"],
|
|
|
|
["a", "b[1][0]"],
|
|
|
|
["a", "b[1][1]"],
|
|
|
|
["a", "b[1][2]"],
|
|
|
|
["a", "b[2]"],
|
|
|
|
["a", "b[2][0]"],
|
|
|
|
["a", "b[2][0]", "x"],
|
|
|
|
["a", "b[2][0]", "y"],
|
2020-09-21 14:41:44 +00:00
|
|
|
]
|
|
|
|
self.assertEqual(o, r)
|
|
|
|
|
|
|
|
def test_keylists_with_lists_and_indexes_not_included(self):
|
|
|
|
i = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": 1,
|
|
|
|
"b": {
|
|
|
|
"c": {
|
|
|
|
"x": 2,
|
|
|
|
"y": 3,
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"d": {
|
|
|
|
"x": 4,
|
|
|
|
"y": 5,
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"e": [
|
2020-09-21 14:41:44 +00:00
|
|
|
{
|
2022-02-13 10:35:43 +00:00
|
|
|
"x": 1,
|
|
|
|
"y": -1,
|
|
|
|
"z": [1, 2, 3],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
|
|
|
{
|
2022-02-13 10:35:43 +00:00
|
|
|
"x": 2,
|
|
|
|
"y": -2,
|
|
|
|
"z": [2, 3, 4],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
|
|
|
{
|
2022-02-13 10:35:43 +00:00
|
|
|
"x": 3,
|
|
|
|
"y": -3,
|
|
|
|
"z": [3, 4, 5],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
],
|
2020-09-21 14:41:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
o = _keylists(i, indexes=False)
|
2020-09-22 12:11:50 +00:00
|
|
|
o.sort()
|
2020-09-21 14:41:44 +00:00
|
|
|
r = [
|
2022-02-13 10:35:43 +00:00
|
|
|
["a"],
|
|
|
|
["b"],
|
|
|
|
["b", "c"],
|
|
|
|
["b", "c", "x"],
|
|
|
|
["b", "c", "y"],
|
|
|
|
["b", "d"],
|
|
|
|
["b", "d", "x"],
|
|
|
|
["b", "d", "y"],
|
|
|
|
["b", "e"],
|
2020-09-21 14:41:44 +00:00
|
|
|
]
|
|
|
|
self.assertEqual(o, r)
|