Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Mar 14, 2023
1 parent f76e895 commit 060369e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/test_canonicaljson.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import Mock

from math import inf, nan

Expand All @@ -22,6 +23,7 @@
iterencode_canonical_json,
iterencode_pretty_printed_json,
set_json_library,
register_preserialisation_callback,
)

import unittest
Expand Down Expand Up @@ -150,3 +152,40 @@ def test_set_json(self) -> None:
from canonicaljson import json # type: ignore[attr-defined]

set_json_library(json)

def test_encode_unknown_class_raises(self) -> None:
class C:
pass

with self.assertRaises(Exception):
encode_canonical_json(C())

def test_preserialisation_callback(self) -> None:
class C:
pass

register_preserialisation_callback(C, lambda c: "I am a C instance")

result = encode_canonical_json(C())
self.assertEqual(result, b'"I am a C instance"')

def test_cannot_register_preserialisation_callback_for_object(self) -> None:
with self.assertRaises(Exception):
register_preserialisation_callback(
object, lambda c: "shouldn't be able to do this"
)

def test_most_recent_preserialisation_callback_called(self) -> None:
class C:
pass

callback1 = Mock(return_value="callback 1 was called")
callback2 = Mock(return_value="callback 2 was called")

register_preserialisation_callback(C, callback1)
register_preserialisation_callback(C, callback2)

encode_canonical_json(C())

callback1.assert_not_called()
callback2.assert_called_once()

0 comments on commit 060369e

Please sign in to comment.