def test_invalid_type(self):
     """
     Initializing the Identity object with an invalid type that can’t be JSON should
     raise a TypeError.
     """
     with self.assertRaises(TypeError):
         from_json(["not", "a", "string"])
 def test_invalid_value(self):
     """
     Initializing the Identity object with an invalid JSON string should raise a
     ValueError.
     """
     with self.assertRaises(ValueError):
         from_json("invalid JSON")
    def test_invalid_format(self):
        """
        Initializing the Identity object with a JSON string that is not
        formatted correctly.
        """
        identity = self._identity()

        dict_ = identity._asdict()
        json = dumps(dict_)

        with self.assertRaises(KeyError):
            from_json(json)
    def test_valid(self):
        """
        Initialize the Identity object with a valid JSON string.
        """
        identity = self._identity()

        dict_ = {"identity": identity._asdict()}
        json = dumps(dict_)

        try:
            self.assertEqual(identity, from_json(json))
        except (TypeError, ValueError):
            self.fail()