Skip to content

Commit 4b5b425

Browse files
stewegsamuel-gauthier
authored andcommitted
schema: add {min,max}_elements for SList and SLeafList
This patch add ability to get minumum and maximum number of elements, and add unit tests. The yolo-nodetypes module is used twice and is now loaded in the list test setUp. Fixes: #89 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 95012d9 commit 4b5b425

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

libyang/schema.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,16 @@ def must_conditions(self) -> Iterator[str]:
13251325
for must in ly_array_iter(pdata.musts):
13261326
yield c2str(must.arg.str)
13271327

1328+
def max_elements(self) -> int:
1329+
return (
1330+
self.cdata_leaflist.max
1331+
if self.cdata_leaflist.max != (2**32 - 1)
1332+
else None
1333+
)
1334+
1335+
def min_elements(self) -> int:
1336+
return self.cdata_leaflist.min
1337+
13281338
def __str__(self):
13291339
return "%s %s" % (self.name(), self.type().name())
13301340

@@ -1423,6 +1433,12 @@ def uniques(self) -> Iterator[List[SNode]]:
14231433
nodes.append(SNode.new(self.context, node))
14241434
yield nodes
14251435

1436+
def max_elements(self) -> int:
1437+
return self.cdata_list.max if self.cdata_list.max != (2**32 - 1) else None
1438+
1439+
def min_elements(self) -> int:
1440+
return self.cdata_list.min
1441+
14261442
def __str__(self):
14271443
return "%s [%s]" % (self.name(), ", ".join(k.name() for k in self.keys()))
14281444

tests/test_schema.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class ListTest(unittest.TestCase):
291291
def setUp(self):
292292
self.ctx = Context(YANG_DIR)
293293
self.ctx.load_module("yolo-system")
294+
self.ctx.load_module("yolo-nodetypes")
294295
self.list = next(self.ctx.find_path(self.PATH["LOG"]))
295296

296297
def tearDown(self):
@@ -329,7 +330,6 @@ def test_list_parent(self):
329330
self.assertEqual(parent.name(), "conf")
330331

331332
def test_list_uniques(self):
332-
self.ctx.load_module("yolo-nodetypes")
333333
list1 = next(self.ctx.find_path("/yolo-nodetypes:conf/list1"))
334334
self.assertIsInstance(list1, SList)
335335
uniques = list(list1.uniques())
@@ -344,6 +344,17 @@ def test_list_uniques(self):
344344
uniques = list(list2.uniques())
345345
self.assertEqual(len(uniques), 0)
346346

347+
def test_list_min_max(self):
348+
list1 = next(self.ctx.find_path("/yolo-nodetypes:conf/list1"))
349+
self.assertIsInstance(list1, SList)
350+
self.assertEqual(list1.min_elements(), 2)
351+
self.assertEqual(list1.max_elements(), 10)
352+
353+
list2 = next(self.ctx.find_path("/yolo-nodetypes:conf/list2"))
354+
self.assertIsInstance(list2, SList)
355+
self.assertEqual(list2.min_elements(), 0)
356+
self.assertEqual(list2.max_elements(), None)
357+
347358

348359
# -------------------------------------------------------------------------------------
349360
class RpcTest(unittest.TestCase):
@@ -531,3 +542,14 @@ def test_leaflist_defaults(self):
531542
leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/ratios"))
532543
for d in leaflist.defaults():
533544
self.assertIsInstance(d, float)
545+
546+
def test_leaf_list_min_max(self):
547+
leaflist1 = next(self.ctx.find_path("/yolo-nodetypes:conf/leaf-list1"))
548+
self.assertIsInstance(leaflist1, SLeafList)
549+
self.assertEqual(leaflist1.min_elements(), 3)
550+
self.assertEqual(leaflist1.max_elements(), 11)
551+
552+
leaflist2 = next(self.ctx.find_path("/yolo-nodetypes:conf/leaf-list2"))
553+
self.assertIsInstance(leaflist2, SLeafList)
554+
self.assertEqual(leaflist2.min_elements(), 0)
555+
self.assertEqual(leaflist2.max_elements(), None)

tests/yang/yolo/yolo-nodetypes.yang

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ module yolo-nodetypes {
3232
list list1 {
3333
key leaf1;
3434
unique "leaf2 leaf3";
35+
min-elements 2;
36+
max-elements 10;
3537
leaf leaf1 {
3638
type string;
3739
}
@@ -49,5 +51,15 @@ module yolo-nodetypes {
4951
type string;
5052
}
5153
}
54+
55+
leaf-list leaf-list1 {
56+
type string;
57+
min-elements 3;
58+
max-elements 11;
59+
}
60+
61+
leaf-list leaf-list2 {
62+
type string;
63+
}
5264
}
5365
}

0 commit comments

Comments
 (0)