mock ¶
Mock Plugin for Tux Bot.
This plugin provides error testing and debugging functionality, allowing developers to trigger various error conditions and test error handling mechanisms in the bot.
Classes:
-
MockParameter–Mock parameter object for testing purposes.
-
MockFlag–Mock flag object for testing purposes.
-
MockObject–A simple mock object that accepts arbitrary attributes.
-
ErrorTestDefinition–Defines how to construct and test a specific error type.
-
ErrorTestRegistry–Dynamic registry of errors that can be tested, based on the actual error handler.
-
Mock–Mock plugin for Tux Bot.
Functions:
-
setup–Cog setup for event handler.
Classes¶
MockParameter ¶
MockParameter(name: str)
MockFlag ¶
MockFlag(name: str)
MockObject ¶
MockObject(**kwargs: Any)
ErrorTestDefinition ¶
ErrorTestDefinition(
error_class: type[Exception],
args: tuple[Any, ...] = (),
kwargs: dict[str, Any] | None = None,
description: str = "",
category: str = "General",
)
Defines how to construct and test a specific error type.
Initialize an error test definition.
Parameters:
-
error_class(type[Exception]) –The exception class to test.
-
args(tuple[Any, ...], default:()) –Positional arguments for error construction.
-
kwargs(dict[str, Any] | None, default:None) –Keyword arguments for error construction.
-
description(str, default:'') –Description of the error test.
-
category(str, default:'General') –Category for organizing error tests.
Methods:
-
create_error–Create an instance of this error for testing.
-
get_config–Get the error handler configuration for this error type.
ErrorTestRegistry ¶
ErrorTestRegistry()
Dynamic registry of errors that can be tested, based on the actual error handler.
Initialize the error test registry.
Methods:
-
get_test_names–Get all test names.
-
get_test_names_by_category–Get test names grouped by category.
-
get_test–Get a specific test by name.
Functions¶
_build_test_registry ¶
_build_test_registry() -> None
Build test cases dynamically from ERROR_CONFIG_MAP.
_add_test ¶
_add_test(
name: str,
error_class: type[Exception],
args: tuple[Any, ...] = (),
description: str = "",
category: str = "General",
) -> None
Add a test case to the registry.
_add_app_command_test ¶
Add app command specific test cases.
_get_realistic_app_command_args ¶
_add_traditional_command_test ¶
Add traditional command specific test cases.
_get_realistic_traditional_command_args ¶
_add_discord_api_test ¶
Add Discord API specific test cases.
_get_realistic_discord_args ¶
_add_httpx_test ¶
Add HTTPX error test cases.
_add_builtin_test ¶
Add Python built-in error test cases for unhandled error testing.
_get_realistic_httpx_args ¶
_get_realistic_builtin_args ¶
_add_custom_test ¶
Add custom error test cases for tux module errors.
_get_realistic_custom_args ¶
get_test_names ¶
get_test_names_by_category ¶
get_test ¶
get_test(name: str) -> ErrorTestDefinition | None
Get a specific test by name.
Returns:
-
ErrorTestDefinition | None–The test definition if found, None otherwise.
Mock ¶
Mock(bot: Tux)
Bases: BaseCog
Mock plugin for Tux Bot.
Initialize the Mock cog.
Parameters:
-
bot(Tux) –The bot instance.
Methods:
-
get_config–Get a configuration value from CONFIG with support for nested keys.
-
__repr__–Return a string representation of the cog instance.
-
unload_if_missing_config–Check if required configuration is missing and log warning.
-
mock–Command group for mocking various bot behaviors.
-
error_name_autocomplete–Autocomplete function for error names based on the selected category.
-
mock_error–Raise a specified error to test the global error handler.
-
error_type_autocomplete–Autocomplete function for error types with category information.
-
mock_test–Alternative error testing command with autocomplete support.
Attributes:
-
db(DatabaseCoordinator) –Get the database coordinator for accessing database controllers.
Attributes¶
db property ¶
db: DatabaseCoordinator
Get the database coordinator for accessing database controllers.
Returns:
-
DatabaseCoordinator–Coordinator providing access to all database controllers.
Examples:
>>> await self.db.guild_config.get_guild_config(guild_id)
>>> await self.db.cases.create_case(...)
Notes
This property provides convenient access to database operations without needing to access self.bot.db directly.
Functions¶
_setup_command_usage ¶
_setup_command_usage() -> None
Generate usage strings for all commands in this cog that lack explicit usage.
The generated usage follows the pattern: <qualified_name> <param tokens>
Where: - Required parameters are denoted as <name: Type> - Optional parameters are denoted as [name: Type] - The prefix is intentionally omitted (provided by ctx.prefix)
Examples:
ban <member: Member> [reason: str] config set <key: str> <value: str>
Notes
Respects explicit usage strings if already set on a command. Errors during generation are logged but don't prevent cog loading.
_generate_usage ¶
Generate a usage string with support for flags and positional parameters.
This method inspects the command's callback signature to detect: - FlagConverter parameters (e.g., --flag value) - Positional parameters (e.g., <required> or [optional])
Parameters:
-
command(Command) –The command to generate usage for.
Returns:
-
str–Generated usage string, or qualified command name as fallback.
Notes
Delegates to shared usage generator for consistency across all cogs. Falls back gracefully to command name if generation fails.
get_config ¶
Get a configuration value from CONFIG with support for nested keys.
Parameters:
-
key(str) –The configuration key to retrieve. Supports dot notation for nested values (e.g.,
"BOT_INFO.BOT_NAME"). -
default(Any, default:None) –Default value to return if key is not found, by default None.
Returns:
-
Any–The configuration value or default if not found.
Examples:
>>> self.get_config("BOT_INFO.BOT_NAME")
'Tux'
>>> self.get_config("MISSING_KEY", "fallback")
'fallback'
Notes
Errors during retrieval are logged but don't raise exceptions. Returns the default value on any error.
__repr__ ¶
__repr__() -> str
Return a string representation of the cog instance.
Returns:
-
str–String representation in format
<CogName bot=BotUser>.
unload_if_missing_config ¶
Check if required configuration is missing and log warning.
This allows cogs to detect missing configuration at load time and return early from init to prevent partial initialization.
Parameters:
-
condition(bool) –True if config is missing (should unload), False otherwise.
-
config_name(str) –Name of the missing configuration for logging purposes.
Returns:
-
bool–True if config is missing (caller should return early), False if config is present.
Examples:
>>> def __init__(self, bot: Tux):
... super().__init__(bot)
... if self.unload_if_missing_config(not CONFIG.GITHUB_TOKEN, "GITHUB_TOKEN"):
... return # Exit early, cog will be partially loaded but won't register commands
... self.github_client = GitHubClient()
Notes
When this returns True, the cog's init should return early to avoid initializing services that depend on the missing config. The cog will be loaded but commands won't be registered properly, preventing runtime errors.
For complete cog unloading, the bot owner should remove the cog from the modules directory or use the reload system to unload it programmatically.
_unload_self async ¶
_unload_self(extension_name: str) -> None
Perform the actual cog unload operation.
Parameters:
-
extension_name(str) –Full extension name to unload.
Notes
This is called as a background task by unload_if_missing_config(). Errors during unload are logged but don't raise exceptions.
_create_error_info_embed async ¶
_create_error_info_embed(
test_name: str, test_def: ErrorTestDefinition, ctx: Context[Tux]
) -> Embed
Create an informative embed showing error details and expected handler behavior.
Returns:
-
Embed–The informational embed.
_send_test_summary async ¶
Send a summary of available error tests.
mock async ¶
Command group for mocking various bot behaviors.
Requires System Administrator permissions (Level 8).
error_name_autocomplete async ¶
error_name_autocomplete(interaction: Interaction, current: str) -> list[Choice[str]]
mock_error async ¶
Raise a specified error to test the global error handler.
This command shows detailed information about how the error will be handled, then raises the error to demonstrate the actual behavior.
Parameters:
-
ctx(Context[Tux]) –The command invocation context.
-
category(str) –The category of error to test (required for slash commands).
-
error_name(str, default:None) –The specific error name to test. If not provided, shows available errors in the category.
Notes
This command intentionally raises various exceptions based on the input. These exceptions will propagate up to the global ErrorHandler cog. Requires System Administrator permissions (Level 8).
_send_category_summary async ¶
Send a summary of available errors in a specific category.
_send_error_not_in_category async ¶
Send an error message when the error is not found in the specified category.
_send_error_not_found async ¶
Send an error message when the error is not found at all.