Files
APIcord/tests/test_models.py
T
k.eaven 5669048f89 Initial commit: APIcord Discord API Emulator
- Core infrastructure: config, state management, models
- REST API server (aiohttp) with Discord-compatible endpoints
- WebSocket Gateway server (websockets) with Discord Gateway protocol
- State management with guilds, channels, users, messages, interactions
- Pydantic models for Discord objects (User, Guild, Channel, Message, etc.)
- CLI with start, test, reset, export, validate commands
- YAML-based test scenario framework
- Basic test scenario for /server_status command flow
- Unit tests for models and state manager (19 passing)
- GPL-3.0 license
2026-10-31 11:43:57 +00:00

144 lines
4.0 KiB
Python

"""Tests for Pydantic Models"""
import pytest
from apicord.models.discord import (
User,
BotUser,
Role,
Channel,
ChannelType,
Guild,
Message,
Interaction,
InteractionType,
Snowflake,
)
class TestSnowflake:
def test_snowflake_from_string(self):
sf = Snowflake("123456789012345678")
assert sf == "123456789012345678"
def test_snowflake_from_int(self):
sf = Snowflake(123456789012345678)
assert sf == "123456789012345678"
def test_snowflake_invalid(self):
with pytest.raises(ValueError):
Snowflake.validate("invalid", None)
class TestUser:
def test_user_creation(self):
user = User(
id="123456789012345678",
username="TestUser",
discriminator="1234",
bot=False,
)
assert user.id == "123456789012345678"
assert user.username == "TestUser"
assert user.discriminator == "1234"
assert user.bot is False
def test_user_with_guild_roles(self):
user = User(
id="123456789012345678",
username="TestUser",
discriminator="1234",
guild_roles={"111111111111111111": ["222222222222222222"]},
)
assert "111111111111111111" in user.guild_roles
assert "222222222222222222" in user.guild_roles["111111111111111111"]
class TestBotUser:
def test_bot_user_creation(self):
bot = BotUser(
id="444444444444444444",
username="TestBot",
discriminator="0000",
token="test_token_123",
)
assert bot.id == "444444444444444444"
assert bot.bot is True
assert bot.token == "test_token_123"
class TestRole:
def test_role_creation(self):
role = Role(
id="222222222222222222",
name="Game Server User",
color=3447003,
permissions="104324161",
)
assert role.name == "Game Server User"
assert role.color == 3447003
class TestChannel:
def test_text_channel(self):
channel = Channel(
id="111111111111111111",
type=ChannelType.GUILD_TEXT,
guild_id="123456789012345678",
name="general",
)
assert channel.type == ChannelType.GUILD_TEXT
assert channel.guild_id == "123456789012345678"
assert channel.name == "general"
def test_voice_channel(self):
channel = Channel(
id="111111111111111112",
type=ChannelType.GUILD_VOICE,
guild_id="123456789012345678",
name="voice-chat",
)
assert channel.type == ChannelType.GUILD_VOICE
class TestGuild:
def test_guild_creation(self):
guild = Guild(
id="123456789012345678",
name="Test Guild",
owner_id="111111111111111111",
)
assert guild.name == "Test Guild"
assert guild.owner_id == "111111111111111111"
assert guild.roles == []
assert guild.channels == []
class TestMessage:
def test_message_creation(self):
user = User(
id="123456789012345678",
username="TestUser",
discriminator="1234",
)
message = Message(
id="999999999999999999",
channel_id="111111111111111111",
author=user,
content="Hello, world!",
timestamp="2024-01-01T00:00:00.000000+00:00",
)
assert message.content == "Hello, world!"
assert message.author.username == "TestUser"
class TestInteraction:
def test_interaction_creation(self):
interaction = Interaction(
id="888888888888888888",
type=InteractionType.APPLICATION_COMMAND,
application_id="444444444444444444",
token="interaction_token_123",
)
assert interaction.type == InteractionType.APPLICATION_COMMAND
assert interaction.token == "interaction_token_123"
assert interaction.responded is False