def test_can_make_message_with_existing_contact(self): contact1 = Mock(Contact) contact1.name.return_value = "Justin Powers" contact2 = Mock(Contact) contact2.name.return_value = "Lydia Powers" json = { "text": "message text", "timestamp": "2009-05-23 12:12:01", "sender": {"name": "Justin Powers", "tags": ["tag1", "tag2"]} } Contact.all_contacts = set([contact1, contact2]) message = Message.from_json(json) self.assertEqual(message._text, "message text") self.assertEqual(message._timestamp, datetime(2009, 5, 23, 12, 12, 1)) self.assertIs(message._sender, contact1)
def test_json_to_message_requires_text_key(self): with self.assertRaises(ValueError): Message.from_json({"wrong": "txt", "timestamp": "", "sender": ""})
def test_json_to_message_requires_dict(self): with self.assertRaises(TypeError): Message.from_json("some string")
def test_json_to_message_requires_sender_key(self): with self.assertRaises(ValueError): Message.from_json({"text": "txt", "timestamp": "", "wrong": ""})