-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial fix for issue #13 Update: Issue 13 fixed as best we're going to make it
- Loading branch information
Showing
4 changed files
with
105 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import json | ||
import unittest | ||
|
||
from jsonasobj import JsonObj, as_json | ||
|
||
|
||
class ShapeAssociation(JsonObj): | ||
def __init__(self, | ||
nodeSelector, shapeLabel, | ||
status=None, reason=None, | ||
appinfo=None) -> None: | ||
|
||
self.nodeSelector = nodeSelector | ||
self.shapeLabel = shapeLabel | ||
self.status = status if status is not None else "C", | ||
self.reason = reason | ||
self.appinfo = appinfo | ||
super().__init__() | ||
|
||
|
||
expected = { | ||
"nodeSelector": "http://example.org/people/42", | ||
"shapeLabel": "http://example.org/model/Person", | ||
"status": [ | ||
"C" | ||
], | ||
"reason": "cause", | ||
"appinfo": None | ||
} | ||
|
||
|
||
class PositionalTestCase(unittest.TestCase): | ||
def test_positional(self): | ||
""" jsonasobj has to support positional constructors """ | ||
s = ShapeAssociation("http://example.org/people/42", 'http://example.org/model/Person', reason='cause') | ||
self.assertEqual(expected, json.loads(as_json(s))) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import unittest | ||
from typing import Optional | ||
|
||
from jsonasobj import JsonObj, as_json | ||
|
||
|
||
class MyObj(JsonObj): | ||
def __init__(self, a: JsonObj): | ||
super().__init__() | ||
self.a = a | ||
|
||
|
||
class RecursiveObject(JsonObj): | ||
def __init__(self, parent: Optional["RecursiveObject"] = None): | ||
super().__init__() | ||
self.parent = parent | ||
|
||
class RecursiveObject2(JsonObj): | ||
_idempotent = False | ||
def __init__(self, parent: Optional["RecursiveObject"] = None): | ||
super().__init__() | ||
self.parent = parent | ||
|
||
|
||
class DangerousConstructor(unittest.TestCase): | ||
def test_unintended_recursion(self): | ||
o1 = JsonObj(x=1) | ||
o2 = MyObj(o1) | ||
self.assertNotEqual(id(o1), id(o2)) | ||
|
||
|
||
def test_deliberate_recursion(self): | ||
grandfather = RecursiveObject() | ||
father = RecursiveObject(grandfather) | ||
me = RecursiveObject(father) | ||
self.assertEqual(id(me), id(me.parent)) | ||
|
||
grandfather = RecursiveObject2() | ||
father = RecursiveObject2(grandfather) | ||
me = RecursiveObject2(father) | ||
self.assertNotEqual(id(me), id(me.parent)) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |