blob: f075b4b264d7be64358ffeb7ef5c0fd1bdae4e41 [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
8#include <experimental/iterator>
Václav Kubernát26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +010010
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áted824d02020-06-09 15:48:30 +020020#include "pretty_printers.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +010021#include "sysrepo_subscription.hpp"
22#include "utils.hpp"
23
Václav Kubernátab612e92019-11-26 19:51:31 +010024
25TEST_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át715c85c2020-04-14 01:46:08 +020032 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Václav Kubernátab612e92019-11-26 19:51:31 +010033#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át2db124c2020-05-28 21:58:36 +020043 dataPath_ listPath;
44 listPath.m_scope = Scope::Absolute;
Václav Kubernátc15fe822020-06-04 11:28:39 +020045 std::vector<ListInstance> expected;
Václav Kubernátab612e92019-11-26 19:51:31 +010046
47 SECTION("example-schema:person")
48 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020049 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át2db124c2020-05-28 21:58:36 +020052 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"person"}});
Václav Kubernátab612e92019-11-26 19:51:31 +010053 expected = {
54 {{"name", std::string{"Jan Novak"}}},
55 {{"name", std::string{"Tomas"}}},
56 {{"name", std::string{"Vaclav"}}}
57 };
58 }
59
Václav Kubernát45e55462020-02-04 11:19:32 +010060 SECTION("example-schema:person - no instances")
61 {
Václav Kubernát2db124c2020-05-28 21:58:36 +020062 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"person"}});
Václav Kubernát45e55462020-02-04 11:19:32 +010063 expected = {
64 };
65 }
66
Václav Kubernátab612e92019-11-26 19:51:31 +010067 SECTION("example-schema:selectedNumbers")
68 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020069 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át2db124c2020-05-28 21:58:36 +020072 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"selectedNumbers"}});
Václav Kubernátab612e92019-11-26 19:51:31 +010073 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átcbf288b2020-06-18 20:44:39 +020082 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át2db124c2020-05-28 21:58:36 +020085 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"animalWithColor"}});
Václav Kubernátab612e92019-11-26 19:51:31 +010086 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átcbf288b2020-06-18 20:44:39 +020095 datastore.createItem("/example-schema:animalWithColor[name='D\"o\"g'][color=\"b'r'own\"]");
Václav Kubernát2db124c2020-05-28 21:58:36 +020096 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"animalWithColor"}});
Václav Kubernátab612e92019-11-26 19:51:31 +010097 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átcbf288b2020-06-18 20:44:39 +0200104 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át2db124c2020-05-28 21:58:36 +0200107 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"ports"}});
Václav Kubernátab612e92019-11-26 19:51:31 +0100108 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átcbf288b2020-06-18 20:44:39 +0200117 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átab612e92019-11-26 19:51:31 +0100128
129 SECTION("outer list")
130 {
Václav Kubernát2db124c2020-05-28 21:58:36 +0200131 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list_{"org"}});
Václav Kubernátab612e92019-11-26 19:51:31 +0100132 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átab612e92019-11-26 19:51:31 +0100143 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át2db124c2020-05-28 21:58:36 +0200171 listPath.m_nodes.push_back(dataNode_{{"example-schema"}, list});
172 listPath.m_nodes.push_back(dataNode_{list_{"people"}});
Václav Kubernátab612e92019-11-26 19:51:31 +0100173 }
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átab612e92019-11-26 19:51:31 +0100184
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át2db124c2020-05-28 21:58:36 +0200210 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átab612e92019-11-26 19:51:31 +0100213 }
214 }
215
216 SECTION("/other-module:parking-lot/example-schema:cars - list coming from an augment")
217 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200218 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átab612e92019-11-26 19:51:31 +0100220
Václav Kubernát2db124c2020-05-28 21:58:36 +0200221 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átab612e92019-11-26 19:51:31 +0100223 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át2db124c2020-05-28 21:58:36 +0200232 auto keys = dataquery.listKeys(listPath);
Václav Kubernátab612e92019-11-26 19:51:31 +0100233 std::sort(keys.begin(), keys.end());
234 REQUIRE(keys == expected);
235 }
236
237 waitForCompletionAndBitMore(seq1);
238}