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