Skip to content

Commit

Permalink
Ensure in json-ietf the Containers with Presence are shown (#351)
Browse files Browse the repository at this point in the history
* Update UnitTest for Presence on Empty Containers

* Ensure in json-ietf the Containers with Presence are shown.
  • Loading branch information
JoseIgnacioTamayo authored Aug 5, 2024
1 parent f3dda7c commit 5ffbcbf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pyangbind/lib/serialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ def generate_ietf_tree(obj, parent_namespace=None, flt=False, with_defaults=None
d[yname] = generate_ietf_tree(
element, parent_namespace=element._namespace, flt=flt, with_defaults=with_defaults
)
if not len(d[yname]):
present = getattr(element, "_cpresent", False)
if not len(d[yname]) and not present:
del d[yname]
elif generated_by == "YANGListType":
d[yname] = [
Expand Down
23 changes: 17 additions & 6 deletions tests/presence/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,49 +97,60 @@ def test_009_presence_get(self):

def test_010_presence_serialise(self):
self.instance.parent.child._set_present()
self.instance.p_container._set_present()
expectedJ = """
{
"parent": {
"child": {}
}
},
"p-container": {}
}"""
self.assertEqual(json.loads(pbJ.dumps(self.instance)), json.loads(expectedJ))
self.instance.parent.child._set_present(False)
expectedJ = "{}"
expectedJ = """
{
"p-container": {}
}"""
self.assertEqual(json.loads(pbJ.dumps(self.instance)), json.loads(expectedJ))

def test_011_presence_serialise_ietf(self):
self.instance.parent.child._set_present()
self.instance.p_container._set_present()
expectedJ = """
{
"presence:parent": {
"child": {}
}
},
"presence:p-container": {}
}"""
self.assertEqual(json.loads(pbJ.dumps(self.instance, mode="ietf")), json.loads(expectedJ))
self.instance.parent.child._set_present(False)
expectedJ = "{}"
expectedJ = """{"presence:p-container": {}}"""
self.assertEqual(json.loads(pbJ.dumps(self.instance, mode="ietf")), json.loads(expectedJ))

def test_012_presence_deserialise(self):
inputJ = """
{
"parent": {
"child": {}
}
},
"p-container": {}
}"""
x = pbJ.loads(inputJ, self.bindings, "presence")
self.assertIs(x.parent.child._present(), True)
self.assertIs(x.p_container._present(), True)

def test_013_presence_deserialise(self):
inputJ = """
{
"presence:parent": {
"child": {}
}
},
"presence:p-container": {}
}"""
x = pbJ.loads_ietf(inputJ, self.bindings, "presence")
self.assertIs(x.parent.child._present(), True)
self.assertIs(x.p_container._present(), True)


class SplitClassesPresenceTests(PresenceTests):
Expand Down

0 comments on commit 5ffbcbf

Please sign in to comment.