Testing Guide
Learn how to test RedHarmony components and functionality.
Unit Testing
Testing Content Generation
def test_content_generation():
content = generate_post_content(
prompt="Test prompt",
subreddit="test",
max_tokens=100
)
assert content is not None
assert len(content) > 0
Testing Database Operations
def test_database_operations():
# Initialize test database
initialize_db()
# Test post creation
post_id = "test123"
save_post(post_id, "testuser", "testsubreddit", "Test Title")
# Verify post exists
conn = sqlite3.connect("reddit_bot.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM posts WHERE post_id = ?", (post_id,))
assert cursor.fetchone() is not None
Integration Testing
Testing Reddit Integration
def test_reddit_integration():
account = {
"subreddits": ["test"],
"prompt": "Test prompt"
}
# Test client creation
reddit = get_reddit_client(account)
assert reddit is not None
# Test subreddit access
assert subreddit_valid(reddit, "test")
Mock Testing
Mocking Reddit API
from unittest.mock import Mock, patch
@patch('praw.Reddit')
def test_post_creation(mock_reddit):
# Setup mock
mock_submission = Mock()
mock_submission.id = "test123"
mock_reddit.subreddit.return_value.submit.return_value = mock_submission
# Test post creation
create_post(mock_reddit, "testuser", "test", "Test Title", "Test Content")
Mocking OpenAI API
@patch('openai.chat.completions.create')
def test_content_generation(mock_openai):
# Setup mock response
mock_openai.return_value.choices[0].message.content = "Test content"
# Test generation
content = openAI_generate("Test prompt")
assert content == "Test content"