def test_get_time(self): self.message_to_server = create_presence() time_1 = self.message_to_server['time'] time.sleep(1) self.message_to_server = create_presence() time_2 = self.message_to_server['time'] self.assertNotEqual(time_1, time_2) self.transport.close()
class TestClient(unittest.TestCase): """Класс тестирования""" correct_result = { 'action': 'presence', 'time': 1, 'user': { 'account_name': 'Guest' } } test_create_presence = client.create_presence() test_create_presence['time'] = 1 def test_type_result(self): self.assertIsInstance(client.create_presence(), dict) def test_correct(self): self.assertEqual(self.test_create_presence, self.correct_result) def test_response_200(self): self.assertEqual(client.server_response_handler({'response': 200}), '200 : OK') def test_response_400(self): self.assertEqual( client.server_response_handler({ 'response': 400, 'error': 'Bad Request' }), '400 : Bad Request')
def test_def_presense(self): """Тест коректного запроса""" # {'action': 'presence', 'time': 1573760672.167031, 'user': {'account_name': 'Guest'}} test = create_presence() test[TIME] = 1.1 # время необходимо приравнять принудительно # иначе тест никогда не будет пройден self.assertEqual(test, {ACTION: PRESENCE, TYPE: 'status', TIME: 1.1, USER: {ACCOUNT_NAME: 'Guest', TYPE: 'Yep, I am here!'}})
def test_process_ans(self): server_address, server_port = DEFAULT_IP_ADDRESS, DEFAULT_PORT self.transport.connect((server_address, server_port)) self.message_to_server = create_presence() send_message(self.transport, self.message_to_server) self.answer = get_message(self.transport) self.assertEqual(self.answer[RESPONSE], 200) self.transport.close()
def test_create_presence(self): self.message_to_server = create_presence() self.assertIsInstance(self.message_to_server, dict) self.assertEqual(self.message_to_server['action'], 'presence') self.assertIsInstance(self.message_to_server['time'], str) self.assertEqual(self.message_to_server['user']['account_name'], 'Guest') self.transport.close()
def test_create_presence(self): """Тест создания запроса приветствия серверу""" self.assertEqual(create_presence(), { ACTION: PRESENCE, TIME: time.time(), USER: { ACCOUNT_NAME: 'Guest' } })
def test_presence(self): test = create_presence() test[TIME] = 1.1 # присваиваем значения для прохождения теста self.assertEqual(test, { ACTION: PRESENCE, TIME: 1.1, USER: { ACCOUNT_NAME: 'Guest' } })
def test_create_presence(self): test_presence = client.create_presence() check_presence = get_base_msg() check_presence[ACTION] = PRESENCE check_presence[USER] = { ACCOUNT_NAME: 'Guest' # другие имена пока не проверяем } self.assertEqual(test_presence, check_presence)
def test_presence(self): test = create_presence() test[TIME] = 1.0 self.assertEqual(test, { ACTION: PRESENCE, TIME: 1.0, USER: { ACCOUNT_NAME: 'User' } })
def test_correct_presence(self): defer = create_presence() defer[TIME] = 2.995 # присвоение обязательно из-за рандомного времени self.assertEqual(defer, { ACTION: PRESENCE, TIME: 2.995, USER: { ACCOUNT_NAME: 'User' } })
def test_def_presense(self): test = create_presence('Guest') test[TIME] = 1.1 self.assertEqual(test, { ACTION: PRESENCE, TIME: 1.1, USER: { ACCOUNT_NAME: 'Guest' } })
def test_correct_request(self): test = create_presence() test[TIME] = 2 self.assertEqual(test, { ACTION: PRESENCE, TIME: 2, USER: { ACCOUNT_NAME: 'Client' } })
def test_def_presense(self): """Тест коректного запроса""" test = create_presence() test[TIME] = 1.1 self.assertEqual(test, { ACTION: PRESENCE, TIME: 1.1, USER: { ACCOUNT_NAME: 'Guest' } })
def test_create_presence_2(self): """Тест функции генерации запроса о присутствии клиента""" request = cl.create_presence('Tom') request['time'] = 2.2 self.assertEqual(request, { 'action': 'presence', 'time': 2.2, 'user': { 'account_name': 'Tom' } })
def test_def_presense(self): test = create_presence('Guest') test[ TIME] = 1.1 # время необходимо приравнять принудительно иначе тест никогда не будет пройден self.assertEqual(test, { ACTION: PRESENCE, TIME: 1.1, USER: { ACCOUNT_NAME: 'Guest' } })
def test_def_presense_wrong_login(self): """Тест коректного запроса""" test = create_presence() test[TIME] = 0 self.assertNotEqual(test, { ACTION: PRESENCE, TIME: 0, USER: { LOGIN: '******' } })
def test_smoke_def_create_presence(self): """Тест на 'дымок' ф-ии создания сообщения""" test_notification = create_presence() test_notification[TIME] = 1.0 self.assertEqual(test_notification, { ACTION: PRESENCE, TIME: 1.0, USER: { ACCOUNT_NAME: 'Guest' } })
def test_def_presense(self): """Тест коректного запроса""" test = create_presence() test[TIME] = 0 # время необходимо приравнять принудительно # иначе тест никогда не будет пройден self.assertEqual(test, { ACTION: PRESENCE, TIME: 0, USER: { LOGIN: '******' } })
def test_create_presence(self): test_msg = create_presence() test_msg[TIME] = '100.10' expect_msg = { ACTION: PRESENCE, TIME: '100.10', TYPE: STATUS, USER: { ACCOUNT_NAME: NOT_LOGGED_USER, STATUS: 'Presense status test?' } } self.assertEqual(test_msg, expect_msg)
def test_create_presence(): # без параметров message = create_presence() assert message['action'] == "presence" # берем разницу во времени assert abs(message['time'] - time.time()) < 0.1 assert message["user"]["account_name"] == 'Guest' # с именем message = create_presence('test_user_name') assert message["user"]["account_name"] == 'test_user_name' # неверный тип with raises(TypeError): create_presence(200) with raises(TypeError): create_presence(None) # Имя пользователя слишком длинное with raises(UsernameToLongError): create_presence('11111111111111111111111111')
def test_create_presence_all(self): #неверный тип with self.assertRaises(TypeError): create_presence(777) with self.assertRaises(TypeError): create_presence(None) #слишком длинное имя with self.assertRaises(UserNameToLongError): create_presence('0123456789012345678901234567') # запуск функции без параметра res = create_presence() self.assertEqual(res[USER][ACCOUNT_NAME], 'Guest') # запуск с параметром res = create_presence('Natali') self.assertEqual(res[USER][ACCOUNT_NAME], 'Natali')
from socket import socket, AF_INET, SOCK_STREAM from utils import translate_message from utils import send_message, get_message from client import create_presence, read_messages, write_messages client = socket(AF_INET, SOCK_STREAM) # Создать сокет TCP addr = 'localhost' port = 7777 mode = 'r' # Соединиться с сервером client.connect((addr, port)) # Создаем сообщение presence = create_presence() # Отсылаем сообщение send_message(client, presence) # Получаем ответ response = get_message(client) # Проверяем ответ response = translate_message(response) if response['response'] == 'ok': # в зависимости от режима мы будем или слушать или отправлять сообщения if mode == 'r': read_messages(client) elif mode == 'w': write_messages(client) else: raise Exception('Не верный режим чтения/записи')
def test_create_presence_time(self): self.assert_(abs(create_presence()['time'] - time.time()) < 0.1)
def test_type_result(self): self.assertIsInstance(client.create_presence(), dict)
def test_create_presence_acc_int(self): with self.assertRaises(TypeError): create_presence(200)
def test_create_presence_acc_toolong(self): with self.assertRaises(UsernameToLongError): create_presence('11111111111111111111111111')
def test_create_presence_user(self): self.assertTrue(create_presence()['user']['account_name'], not None)
def test_create_presence_time(self): self.assertEqual(create_presence()['time'], time.strftime("%d-%m-%Y %H:%M:%S", time.localtime()))
def test_create_presence_acc_none(self): with self.assertRaises(TypeError): create_presence(None)
def test_create_presence(self): """Тест функции генерации запроса о присутствии клиента""" result = cl.create_presence() self.assertIsInstance(result, dict)