Skip to content

Commit

Permalink
changed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yishain11 committed Aug 2, 2024
1 parent ad06432 commit 05e6c13
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 44 deletions.
9 changes: 9 additions & 0 deletions config/CONSTANTS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HELP_COMMAND_TEXT = (
"Available commands:\n"
"/start - Start the bot\n"
"/help - Show this help message\n"
"/ip - Get public ip\n"
"/question - Get a question from the server\n "
"/ip - get public ip\n"
"/api - connect to server"
)
12 changes: 2 additions & 10 deletions handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from telegram.ext import ContextTypes, CallbackContext
import requests
from dotenv import load_dotenv
from config import CONSTANTS

load_dotenv()

Expand All @@ -15,16 +16,7 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N


async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
help_text = (
"Available commands:\n"
"/start - Start the bot\n"
"/help - Show this help message\n"
"/ip - Get public ip\n"
"/question - Get a question from the server\n "
"/ip - get public ip\n"
"/api - connect to server"
)
await update.message.reply_text(help_text)
await update.message.reply_text(CONSTANTS.HELP_COMMAND_TEXT)


async def get_public_ip_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> bool:
Expand Down
Empty file removed handlers/tmp.py
Empty file.
74 changes: 40 additions & 34 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from telegram.ext import ContextTypes
from genie_bot import *
import statistics
from config import CONSTANTS

@pytest.mark.asyncio
async def test_start_command():
Expand All @@ -27,14 +28,7 @@ async def test_help_command():

await help_command(update, context)

help_text = (
"Available commands:\n"
"/start - Start the bot\n"
"/help - Show this help message\n"
"/ip - get public ip\n"
"/api - connect to server"
)
update.message.reply_text.assert_called_once_with(help_text)
update.message.reply_text.assert_called_once_with(CONSTANTS.HELP_COMMAND_TEXT)

@pytest.mark.asyncio
async def test_get_public_ip_command_success():
Expand Down Expand Up @@ -77,41 +71,53 @@ async def test_get_public_ip_command_failure():
assert result is False

@patch('requests.post')
def test_question_command_success(mock_post, mock_update, mock_context):
async def test_question_command_success():
mock_response = MagicMock()
mock_response.json.return_value = {"response": "Here is your coding question."}
mock_post.return_value = mock_response

question_command(mock_update, mock_context)
mock_update = AsyncMock(Update)
mock_context = AsyncMock(ContextTypes.DEFAULT_TYPE)
message = AsyncMock(Message)

mock_update.message = message
await question_command(mock_update, mock_context)
mock_update.message.reply_text.assert_called_with("Server response: Here is your coding question.")

@patch('requests.post')
def test_question_command_failure(mock_post, mock_update, mock_context):
mock_post.side_effect = requests.exceptions.RequestException("Network error")

question_command(mock_update, mock_context)
mock_update.message.reply_text.assert_called_with("An error occurred: Network error")
async def test_question_command_failure():
mock_response = MagicMock()
mock_response.json.return_value = {"response": "Here is your coding question."}
mock_update = AsyncMock(Update)
mock_context = AsyncMock(ContextTypes.DEFAULT_TYPE)

with patch('genie_bot.requests.post') as mock_post:
mock_post.side_effect = requests.exceptions.RequestException("Network error")
await question_command(mock_update, mock_context)
mock_update.message.reply_text.assert_called_with("An error occurred: Network error")

@patch('requests.post')
def test_question_command_latency(mock_post, mock_update, mock_context):
async def test_question_command_latency():
mock_response = MagicMock()
mock_response.json.return_value = {"response": "Here is your coding question."}
mock_post.return_value = mock_response

latencies = []
num_tests = 10

for _ in range(num_tests):
start_time = time.time()
question_command(mock_update, mock_context)
end_time = time.time()
latencies.append(end_time - start_time)

average_latency = statistics.mean(latencies)
print(f"Average latency: {average_latency:.4f} seconds")

assert average_latency > 0 # Ensure latency is measured
mock_update.message.reply_text.assert_called_with("Server response: Here is your coding question.")
mock_update = AsyncMock(Update)
mock_context = AsyncMock(ContextTypes.DEFAULT_TYPE)
mock_response = MagicMock()
mock_response.json.return_value = {"response": "Here is your coding question."}
with patch('genie_bot.requests.post') as mock_post:
mock_post.return_value = mock_response
latencies = []
num_tests = 10

for _ in range(num_tests):
start_time = time.time()
await question_command(mock_update, mock_context)
end_time = time.time()
latencies.append(end_time - start_time)

average_latency = statistics.mean(latencies)
print(f"Average latency: {average_latency:.4f} seconds")

assert average_latency > 0 # Ensure latency is measured
mock_update.message.reply_text.assert_called_with("Server response: Here is your coding question.")

@pytest.mark.asyncio
async def test_api_command_success():
Expand Down
Empty file removed tests/tmp.py
Empty file.

0 comments on commit 05e6c13

Please sign in to comment.