Source

importdatetimeimportrandomimportpytestimportmockimportirc.clientdeftest_version():assert'VERSION'invars(irc.client)assertisinstance(irc.client.VERSION,tuple)assertirc.client.VERSION,"No VERSION detected."deftest_delayed_command_order():""" delayed commands should be sorted by delay time """null=lambda:Nonedelays=[random.randint(0,99)forxinxrange(5)]cmds=sorted([irc.client.DelayedCommand(delay,null,tuple())fordelayindelays])assert[c.delay.secondsforcincmds]==sorted(delays)deftest_periodic_command_fixed_delay():""" Test that we can construct a periodic command with a fixed initial delay. """fd=irc.client.PeriodicCommandFixedDelay.at_time(at=datetime.datetime.now(),delay=datetime.timedelta(seconds=2),function=lambda:None,arguments=[],)assertfd.due()==Trueassertfd.next().due()==False@mock.patch('irc.connection.socket')deftest_privmsg_sends_msg(socket_mod):pytest.xfail("Fails because server finds 'write' method on mock socket")server=irc.client.IRC().server()server.connect('foo',6667,'bestnick')server.privmsg('#best-channel','You are great')socket_mod.socket.return_value.send.assert_called_with(b'PRIVMSG #best-channel :You are great\r\n')@mock.patch('irc.connection.socket')deftest_privmsg_fails_on_embedded_carriage_returns(socket_mod):server=irc.client.IRC().server()server.connect('foo',6667,'bestnick')withpytest.raises(ValueError):server.privmsg('#best-channel','You are great\nSo are you')