def test_send_peh_upon_connection(self): '''To test client protocol we isloate it from the ClientFactory''' with patch.object(datetime, 'datetime', Mock(wraps=datetime.datetime)) as patched: fixed_date = datetime.datetime(2014, 1, 1, 12, 0, 0) patched.now.return_value = fixed_date factory = ClientFactory() factory.comaster = self.comaster factory.protocol = MaraClientProtocol proto = factory.buildProtocol(('127.0.0.1', 0)) proto.construct = MaraFrame # Disable unnesesary behaviour def stop(): proto.stop() reactor.stop() proto.sendPoll = MagicMock(side_effect=stop) transport = proto_helpers.StringTransport() proto.makeConnection(transport) bytes_sent_to_device = transport.value() result = MaraFrame.parse(bytes_sent_to_device) self.assertEqual(result.dest, 0xFF) self.assertEqual(result.source, 2) # We don't need to check BCC since it's already coded into MaraFrame self.assertEqual(result.peh, fixed_date) reactor.run() # Shuld have stopped self.assertEqual(self.comaster.update_peh_timestamp.call_count, 1) self.assertEqual(self.comaster.update_peh_timestamp.call_args[0][0], fixed_date)