blob: 642a0e01b862914a4aad6e0a9612d485caf464ef [file] [log] [blame]
Václav Kubernátab612e92019-11-26 19:51:31 +01001/*
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átb4e5b182020-11-16 19:55:09 +01008#include "trompeloeil_doctest.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +01009#include <experimental/iterator>
Václav Kubernát654303f2020-07-31 13:16:54 +020010#include <sysrepo-cpp/Session.hpp>
Václav Kubernátab612e92019-11-26 19:51:31 +010011
12#ifdef sysrepo_BACKEND
13#include "sysrepo_access.hpp"
14#elif defined(netconf_BACKEND)
15#include "netconf_access.hpp"
Václav Kubernát74487df2020-06-04 01:29:28 +020016#elif defined(yang_BACKEND)
17#include "yang_access.hpp"
18#include "yang_access_test_vars.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +010019#else
20#error "Unknown backend"
21#endif
22#include "data_query.hpp"
Václav Kubernáted824d02020-06-09 15:48:30 +020023#include "pretty_printers.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +010024#include "sysrepo_subscription.hpp"
25#include "utils.hpp"
26
Václav Kubernátab612e92019-11-26 19:51:31 +010027
28TEST_CASE("data query")
29{
30 trompeloeil::sequence seq1;
Václav Kubernát654303f2020-07-31 13:16:54 +020031 {
Václav Kubernátcfdb9222021-07-07 22:36:24 +020032 auto sess = sysrepo::Connection{}.sessionStart();
33 sess.copyConfig(sysrepo::Datastore::Startup, "example-schema", std::chrono::milliseconds{1000});
Václav Kubernát654303f2020-07-31 13:16:54 +020034 }
Václav Kubernátab612e92019-11-26 19:51:31 +010035 SysrepoSubscription subscriptionExample("example-schema");
36 SysrepoSubscription subscriptionOther("other-module");
37
38#ifdef sysrepo_BACKEND
Václav Kubernátf5d75152020-12-03 03:52:34 +010039 SysrepoAccess datastore;
Václav Kubernátab612e92019-11-26 19:51:31 +010040#elif defined(netconf_BACKEND)
Václav Kubernátd1beedc2020-09-07 12:09:05 +020041 const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET");
42 NetconfAccess datastore(NETOPEER_SOCKET);
Václav Kubernát74487df2020-06-04 01:29:28 +020043#elif defined(yang_BACKEND)
44 YangAccess datastore;
45 datastore.addSchemaDir(schemaDir);
46 datastore.addSchemaFile(exampleSchemaFile);
Václav Kubernátab612e92019-11-26 19:51:31 +010047#else
48#error "Unknown backend"
49#endif
50
51 DataQuery dataquery(datastore);
52
53 SECTION("listKeys")
54 {
Václav Kubernát2db124c2020-05-28 21:58:36 +020055 dataPath_ listPath;
56 listPath.m_scope = Scope::Absolute;
Václav Kubernátc15fe822020-06-04 11:28:39 +020057 std::vector<ListInstance> expected;
Václav Kubernátab612e92019-11-26 19:51:31 +010058
59 SECTION("example-schema:person")
60 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020061 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átfaacd022020-07-08 16:44:38 +020064 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernátab612e92019-11-26 19:51:31 +010065 expected = {
66 {{"name", std::string{"Jan Novak"}}},
67 {{"name", std::string{"Tomas"}}},
68 {{"name", std::string{"Vaclav"}}}
69 };
70 }
71
Václav Kubernát45e55462020-02-04 11:19:32 +010072 SECTION("example-schema:person - no instances")
73 {
Václav Kubernátfaacd022020-07-08 16:44:38 +020074 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernát45e55462020-02-04 11:19:32 +010075 expected = {
76 };
77 }
78
Václav Kubernátab612e92019-11-26 19:51:31 +010079 SECTION("example-schema:selectedNumbers")
80 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020081 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átfaacd022020-07-08 16:44:38 +020084 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"selectedNumbers"});
Václav Kubernátab612e92019-11-26 19:51:31 +010085 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átcbf288b2020-06-18 20:44:39 +020094 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átfaacd022020-07-08 16:44:38 +020097 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"animalWithColor"});
Václav Kubernátab612e92019-11-26 19:51:31 +010098 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át1db04ca2020-07-08 17:23:26 +0200107 datastore.createItem(R"(/example-schema:animalWithColor[name='D"o"g'][color="b'r'own"])");
Václav Kubernátfaacd022020-07-08 16:44:38 +0200108 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"animalWithColor"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100109 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átcbf288b2020-06-18 20:44:39 +0200116 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átfaacd022020-07-08 16:44:38 +0200119 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"ports"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100120 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átcbf288b2020-06-18 20:44:39 +0200129 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átab612e92019-11-26 19:51:31 +0100140
141 SECTION("outer list")
142 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200143 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"org"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100144 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átab612e92019-11-26 19:51:31 +0100155 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átfaacd022020-07-08 16:44:38 +0200183 listPath.m_nodes.emplace_back(module_{"example-schema"}, list);
184 listPath.m_nodes.emplace_back(list_{"people"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100185 }
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átab612e92019-11-26 19:51:31 +0100196
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átab612e92019-11-26 19:51:31 +0100207 }
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átfaacd022020-07-08 16:44:38 +0200221 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átab612e92019-11-26 19:51:31 +0100224 }
225 }
226
227 SECTION("/other-module:parking-lot/example-schema:cars - list coming from an augment")
228 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200229 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átab612e92019-11-26 19:51:31 +0100231
Václav Kubernátfaacd022020-07-08 16:44:38 +0200232 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átab612e92019-11-26 19:51:31 +0100234 expected = {
235 {{"id", int32_t{1}}},
236 {{"id", int32_t{2}}},
237 };
Václav Kubernátab612e92019-11-26 19:51:31 +0100238 }
239
240 datastore.commitChanges();
241 std::sort(expected.begin(), expected.end());
Václav Kubernát2db124c2020-05-28 21:58:36 +0200242 auto keys = dataquery.listKeys(listPath);
Václav Kubernátab612e92019-11-26 19:51:31 +0100243 std::sort(keys.begin(), keys.end());
244 REQUIRE(keys == expected);
245 }
246
Václav Kubernát0f362fb2020-06-24 15:16:56 +0200247 SECTION("empty datastore")
248 {
249 dataPath_ listPath;
250 listPath.m_scope = Scope::Absolute;
Václav Kubernátfaacd022020-07-08 16:44:38 +0200251 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernát0f362fb2020-06-24 15:16:56 +0200252 auto keys = dataquery.listKeys(listPath);
253 REQUIRE(keys == std::vector<ListInstance>{});
254 }
255
Václav Kubernátab612e92019-11-26 19:51:31 +0100256 waitForCompletionAndBitMore(seq1);
257}