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