Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <experimental/iterator> |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 10 | |
| 11 | #ifdef sysrepo_BACKEND |
| 12 | #include "sysrepo_access.hpp" |
| 13 | #elif defined(netconf_BACKEND) |
| 14 | #include "netconf_access.hpp" |
| 15 | #include "netopeer_vars.hpp" |
| 16 | #else |
| 17 | #error "Unknown backend" |
| 18 | #endif |
| 19 | #include "data_query.hpp" |
Václav Kubernát | ed824d0 | 2020-06-09 15:48:30 +0200 | [diff] [blame] | 20 | #include "pretty_printers.hpp" |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 21 | #include "sysrepo_subscription.hpp" |
| 22 | #include "utils.hpp" |
| 23 | |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 24 | |
| 25 | TEST_CASE("data query") |
| 26 | { |
| 27 | trompeloeil::sequence seq1; |
| 28 | SysrepoSubscription subscriptionExample("example-schema"); |
| 29 | SysrepoSubscription subscriptionOther("other-module"); |
| 30 | |
| 31 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 32 | SysrepoAccess datastore("netconf-cli-test", Datastore::Running); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 33 | #elif defined(netconf_BACKEND) |
| 34 | NetconfAccess datastore(NETOPEER_SOCKET_PATH); |
| 35 | #else |
| 36 | #error "Unknown backend" |
| 37 | #endif |
| 38 | |
| 39 | DataQuery dataquery(datastore); |
| 40 | |
| 41 | SECTION("listKeys") |
| 42 | { |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 43 | dataPath_ listPath; |
| 44 | listPath.m_scope = Scope::Absolute; |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 45 | std::vector<ListInstance> expected; |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 46 | |
| 47 | SECTION("example-schema:person") |
| 48 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame^] | 49 | datastore.createItem("/example-schema:person[name='Vaclav']"); |
| 50 | datastore.createItem("/example-schema:person[name='Tomas']"); |
| 51 | datastore.createItem("/example-schema:person[name='Jan Novak']"); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 52 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"person"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 53 | expected = { |
| 54 | {{"name", std::string{"Jan Novak"}}}, |
| 55 | {{"name", std::string{"Tomas"}}}, |
| 56 | {{"name", std::string{"Vaclav"}}} |
| 57 | }; |
| 58 | } |
| 59 | |
Václav Kubernát | 45e5546 | 2020-02-04 11:19:32 +0100 | [diff] [blame] | 60 | SECTION("example-schema:person - no instances") |
| 61 | { |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 62 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"person"}}); |
Václav Kubernát | 45e5546 | 2020-02-04 11:19:32 +0100 | [diff] [blame] | 63 | expected = { |
| 64 | }; |
| 65 | } |
| 66 | |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 67 | SECTION("example-schema:selectedNumbers") |
| 68 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame^] | 69 | datastore.createItem("/example-schema:selectedNumbers[value='45']"); |
| 70 | datastore.createItem("/example-schema:selectedNumbers[value='99']"); |
| 71 | datastore.createItem("/example-schema:selectedNumbers[value='127']"); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 72 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"selectedNumbers"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 73 | expected = { |
| 74 | {{"value", int8_t{127}}}, |
| 75 | {{"value", int8_t{45}}}, |
| 76 | {{"value", int8_t{99}}} |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | SECTION("example-schema:animalWithColor") |
| 81 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame^] | 82 | datastore.createItem("/example-schema:animalWithColor[name='Dog'][color='brown']"); |
| 83 | datastore.createItem("/example-schema:animalWithColor[name='Dog'][color='white']"); |
| 84 | datastore.createItem("/example-schema:animalWithColor[name='Cat'][color='grey']"); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 85 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"animalWithColor"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 86 | expected = { |
| 87 | {{"name", std::string{"Cat"}}, {"color", std::string{"grey"}}}, |
| 88 | {{"name", std::string{"Dog"}}, {"color", std::string{"brown"}}}, |
| 89 | {{"name", std::string{"Dog"}}, {"color", std::string{"white"}}} |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | SECTION("example-schema:animalWithColor - quotes in values") |
| 94 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame^] | 95 | datastore.createItem("/example-schema:animalWithColor[name='D\"o\"g'][color=\"b'r'own\"]"); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 96 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"animalWithColor"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 97 | expected = { |
| 98 | {{"name", std::string{"D\"o\"g"}}, {"color", std::string{"b'r'own"}}} |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | SECTION("example-schema:ports") |
| 103 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame^] | 104 | datastore.createItem("/example-schema:ports[name='A']"); |
| 105 | datastore.createItem("/example-schema:ports[name='B']"); |
| 106 | datastore.createItem("/example-schema:ports[name='E']"); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 107 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"ports"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 108 | expected = { |
| 109 | {{"name", enum_{"A"}}}, |
| 110 | {{"name", enum_{"B"}}}, |
| 111 | {{"name", enum_{"E"}}}, |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | SECTION("example-schema:org/example:people - nested list") |
| 116 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame^] | 117 | datastore.createItem("/example-schema:org[department='accounting']"); |
| 118 | datastore.createItem("/example-schema:org[department='sales']"); |
| 119 | datastore.createItem("/example-schema:org[department='programmers']"); |
| 120 | datastore.createItem("/example-schema:org[department='accounting']/people[name='Alice']"); |
| 121 | datastore.createItem("/example-schema:org[department='accounting']/people[name='Bob']"); |
| 122 | datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']"); |
| 123 | datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']"); |
| 124 | datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']/computers[type='laptop']"); |
| 125 | datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']/computers[type='server']"); |
| 126 | datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']/computers[type='PC']"); |
| 127 | datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']/computers[type='server']"); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 128 | |
| 129 | SECTION("outer list") |
| 130 | { |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 131 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"org"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 132 | expected = { |
| 133 | {{"department", std::string{"accounting"}}}, |
| 134 | {{"department", std::string{"sales"}}}, |
| 135 | {{"department", std::string{"programmers"}}}, |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | SECTION("nested list") |
| 140 | { |
| 141 | listElement_ list; |
| 142 | list.m_name = "org"; |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 143 | SECTION("accounting department") |
| 144 | { |
| 145 | list.m_keys = { |
| 146 | {"department", std::string{"accounting"}} |
| 147 | }; |
| 148 | expected = { |
| 149 | {{"name", std::string{"Alice"}}}, |
| 150 | {{"name", std::string{"Bob"}}}, |
| 151 | }; |
| 152 | } |
| 153 | SECTION("sales department") |
| 154 | { |
| 155 | list.m_keys = { |
| 156 | {"department", std::string{"sales"}} |
| 157 | }; |
| 158 | expected = { |
| 159 | {{"name", std::string{"Alice"}}}, |
| 160 | {{"name", std::string{"Cyril"}}}, |
| 161 | }; |
| 162 | } |
| 163 | SECTION("programmers department") |
| 164 | { |
| 165 | list.m_keys = { |
| 166 | {"department", std::string{"programmers"}} |
| 167 | }; |
| 168 | expected = { |
| 169 | }; |
| 170 | } |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 171 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list}); |
| 172 | listPath.m_nodes.push_back(dataNode_{list_{"people"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | SECTION("THREE MF NESTED LISTS") |
| 176 | { |
| 177 | listElement_ listOrg; |
| 178 | listOrg.m_name = "org"; |
| 179 | listOrg.m_keys = { |
| 180 | {"department", std::string{"sales"}} |
| 181 | }; |
| 182 | |
| 183 | listElement_ listPeople; |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 184 | |
| 185 | SECTION("alice computers") |
| 186 | { |
| 187 | listPeople.m_name = "people"; |
| 188 | listPeople.m_keys = { |
| 189 | {"name", std::string{"Alice"}} |
| 190 | }; |
| 191 | expected = { |
| 192 | {{"type", enum_{"laptop"}}}, |
| 193 | {{"type", enum_{"server"}}}, |
| 194 | }; |
| 195 | |
| 196 | } |
| 197 | |
| 198 | SECTION("cyril computers") |
| 199 | { |
| 200 | listPeople.m_name = "people"; |
| 201 | listPeople.m_keys = { |
| 202 | {"name", std::string{"Cyril"}} |
| 203 | }; |
| 204 | expected = { |
| 205 | {{"type", enum_{"PC"}}}, |
| 206 | {{"type", enum_{"server"}}}, |
| 207 | }; |
| 208 | } |
| 209 | |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 210 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, listOrg}); |
| 211 | listPath.m_nodes.push_back(dataNode_{listPeople}); |
| 212 | listPath.m_nodes.push_back(dataNode_{list_{"computers"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | SECTION("/other-module:parking-lot/example-schema:cars - list coming from an augment") |
| 217 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame^] | 218 | datastore.createItem("/other-module:parking-lot/example-schema:cars[id='1']"); |
| 219 | datastore.createItem("/other-module:parking-lot/example-schema:cars[id='2']"); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 220 | |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 221 | listPath.m_nodes.push_back(dataNode_{{"other-module"}, container_{"parking-lot"}}); |
| 222 | listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"cars"}}); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 223 | expected = { |
| 224 | {{"id", int32_t{1}}}, |
| 225 | {{"id", int32_t{2}}}, |
| 226 | }; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | datastore.commitChanges(); |
| 231 | std::sort(expected.begin(), expected.end()); |
Václav Kubernát | 2db124c | 2020-05-28 21:58:36 +0200 | [diff] [blame] | 232 | auto keys = dataquery.listKeys(listPath); |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 233 | std::sort(keys.begin(), keys.end()); |
| 234 | REQUIRE(keys == expected); |
| 235 | } |
| 236 | |
| 237 | waitForCompletionAndBitMore(seq1); |
| 238 | } |