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