Mocks#
Test mocks: they fake objects for testing.
New in version 7.0.
- class sopel.tests.mocks.MockIRCBackend(*args, **kwargs)#
Fake IRC connection backend for testing purpose.
- Parameters:
bot (
sopel.bot.Sopel) – a Sopel instance
This backend doesn’t require an actual connection. Instead, it stores every message sent in the
message_sentlist.You can use the
rawlist()function to compare the messages easily, and theclear_message_sent()method to clear previous messages:>>> from sopel.tests import rawlist, mocks >>> backend = mocks.MockIRCBackend(bot=None) >>> backend.irc_send(b'PRIVMSG #channel :Hi!\r\n') >>> backend.message_sent == rawlist('PRIVMSG #channel :Hi!') True >>> backend.clear_message_sent() [b'PRIVMSG #channel :Hi!\r\n'] >>> backend.message_sent []
See also
The
parent classcontains all the methods that can be used on this test backend.- clear_message_sent()#
Clear and return previous messages sent.
- Returns:
a copy of the cleared messages sent
- Return type:
New in version 7.1.
- connected#
Convenient status flag.
Set to
Trueto make the bot think it is connected.
- irc_send(data)#
Store
dataintomessage_sent.
- is_connected()#
Tell if the backend is connected or not.
- message_sent#
List of raw messages sent by the bot.
This list will be populated each time the
irc_send()method is used: it will contain the raw IRC lines the bot wanted to send.You can clear this list with the
clear_message_sent()method, or use therawlist()function to compare it.
- on_irc_error(pretrigger)#
Action to perform when the server sends an error event.
- Parameters:
pretrigger – PreTrigger object with the error event
On IRC error, if
bot.hasquitis set, the backend should close the connection so the bot can quit or reconnect as required.
- run_forever()#
Run the backend forever (blocking call).
This method is responsible for initiating the connection to the server, and it must call
bot.on_connectonce connected, orbot.on_closeif it fails to connect.Upon successful connection, it must run forever, listening to the server and allowing the bot to use
send_command()in a thread-safe way.
- class sopel.tests.mocks.MockIRCServer(bot, join_threads=True)#
Fake IRC Server that can send messages to a test bot.
- Parameters:
bot (
sopel.bot.Sopel) – test bot instance to send messages tojoin_threads (bool) – whether message functions should join running threads before returning (default:
True)
This mock object helps developers when they want to simulate an IRC server sending messages to the bot.
The default
join_threadsbehavior is suitable for testing most common plugin callables, and ensures that all callables dispatched by thebotin response to messages sent via thisMockIRCServerare finished running before execution can continue. If set toFalse, the mock server will not wait for the bot to finish processing threaded callables before returning.Note
You can override
join_threadson a per-method-call basis with theblockingarguments to the instance methods below.The
IRCFactoryfactory can be used to create such mock object, either directly or by usingpy.testand theircfactory()fixture.New in version 7.1: The
join_threadsparameter.- channel_joined(channel, users=None, *, blocking=None)#
Send events as if the bot just joined a channel.
- Parameters:
This will send 2 messages to the bot:
a
RPL_NAMREPLYevent (353), giving information aboutuserspresent inchannela
RPL_ENDOFNAMESevent (366) for completion
Use this to emulate when the bot joins a channel, and the server replies with the list of connected users:
factory.channel_joined('#test', ['Owner', '@ChanServ'])
In this example, the bot will know that there are 2 other users present in
#test: “Owner” (a regular user) and “ChanServ” (which is a channel operator). Note that the bot itself will be added to the list of users automatically, and you should not pass it in theusersparameter.This is particularly useful to populate the bot’s memory of who is in a channel.
If
blockingisTrue, this method will wait to join all running triggers’ threads before returning. Setting it toFalsewill skip this step. If not specified, thisMockIRCServerinstance’sjoin_threadsargument will be obeyed.New in version 7.1: The
blockingparameter.See also
The
join_threadsargument toMockIRCServer.Note
To add a user to a channel after using this method, you should use the
join()method.
- property chanserv#
ChanServ’s message prefix.
- join(user, channel, *, blocking=None)#
Send a
channelJOIN event fromuser.- Parameters:
This will send a
JOINmessage as ifuserjust joined the channel:factory.join(MockUser('NewUser'), '#test')
If
blockingisTrue, this method will wait to join all running triggers’ threads before returning. Setting it toFalsewill skip this step. If not specified, thisMockIRCServerinstance’sjoin_threadsargument will be obeyed.New in version 7.1: The
blockingparameter.See also
The
join_threadsargument toMockIRCServer.See also
This function is a shortcut to call the bot with the result from the user factory’s
join()method.
- mode_set(channel, flags, users, *, blocking=None)#
Send a MODE event for a
channel- Parameters:
This will send a MODE message as if
ChanServadded/removed channel modes for a set ofusers. This method assumes theflagsparameter follows the IRC specification for MODE:factory.mode_set('#test', '+vo-v', ['UserV', UserOP', 'UserAnon'])
If
blockingisTrue, this method will wait to join all running triggers’ threads before returning. Setting it toFalsewill skip this step. If not specified, thisMockIRCServerinstance’sjoin_threadsargument will be obeyed.New in version 7.1: The
blockingparameter.See also
The
join_threadsargument toMockIRCServer.
- pm(user, text, *, blocking=None)#
Send a
PRIVMSGto the bot by auser.- Parameters:
This will send a
PRIVMSGmessage as forwarded by the server for ausersending it to the bot:factory.pm(MockUser('NewUser'), 'A private word.')
If
blockingisTrue, this method will wait to join all running triggers’ threads before returning. Setting it toFalsewill skip this step. If not specified, thisMockIRCServerinstance’sjoin_threadsargument will be obeyed.New in version 7.1: The
blockingparameter.See also
The
join_threadsargument toMockIRCServer.See also
This function is a shortcut to call the bot with the result from the user factory’s
privmsg()method, using the bot’s nick as recipient.
- say(user, channel, text, *, blocking=None)#
Send a
PRIVMSGtochannelbyuser.- Parameters:
This will send a
PRIVMSGmessage as ifusersent it to thechannel, and the server forwarded it to its clients:factory.say(MockUser('NewUser'), '#test', '.shrug')
If
blockingisTrue, this method will wait to join all running triggers’ threads before returning. Setting it toFalsewill skip this step. If not specified, thisMockIRCServerinstance’sjoin_threadsargument will be obeyed.New in version 7.1: The
blockingparameter.See also
The
join_threadsargument toMockIRCServer.See also
This function is a shortcut to call the bot with the result from the user’s
privmsg()method.
- class sopel.tests.mocks.MockUser(nick=None, user=None, host=None)#
Fake user that can generate messages to send to a bot.
The
UserFactoryfactory can be used to create such mock object, either directly or by usingpy.testand theuserfactory()fixture.- join(channel)#
Generate a
JOINcommand forwarded by the server for the user.
- property prefix#
User’s hostmask as seen by other users on the server.
When the server forwards a User’s command, it uses this prefix.
- privmsg(recipient, text)#
Generate a
PRIVMSGcommand forwarded by a server for the user.