<commit_before>import time
import unittest
from bson.objectid import ObjectId
from apps.chat.models import *
from mongoengine import *


class ChatsTestCase(unittest.TestCase):
    def setUp(self):
        self.chat_token = Chats.create_chat()['chat_token']
        self.user_token = Chats.join_to_chat(self.chat_token)
        self.chat = Chats.objects.get_all_by_token(self.chat_token)

    def test_create_chat(self):
        self.assertTrue(self.chat)

    def test_join_to_chat_success(self):
        self.assertEquals(24, len(str(self.chat.user_tokens[1])))
        self.assertEquals(2, len(self.chat.user_tokens))

    def test_join_to_chat_failed(self):
        actual = Chats.join_to_chat('543e33a2e3ce324d374246fc')
        self.assertFalse(actual)

    def test_join_to_chat_twice_failed(self):
        actual = Chats.join_to_chat(str(self.chat_token))
        self.assertFalse(actual)

    def test_validate_chat_token_success(self):
        tokens = Chats.create_chat()
        actual = Chats.validate_chat_token(tokens['chat_token'])
        self.assertTrue(actual)

    def test_validate_chat_token_failed(self):
        actual = Chats.validate_chat_token('543e33a2e3ce324d374246fc')
        self.assertFalse(actual)

    def test_get_chat_success(self):
        chat = Chats.get_chat(self.chat_token, self.user_token)
        self.assertEquals(self.chat_token, str(chat.id))

    def test_get_chat_failed(self):
        chat = Chats.get_chat('543e33a2e3ce324d374246fc', '543e33a2e3ce324d374246fc')
        self.assertFalse(chat)

    def test_add_message_success(self):
        messages = [{'msg': 'First message'}, {'msg': 'Second message'}]
        result = self.chat.add_message(user_token=self.user_token, messages=messages)
        self.assertTrue(result)

    def test_add_message_failed(self):
        messages = [{'msg': 'Lorem ipsum dolor sit amet, abhorreant appellantur ex vis. Ad eos dicam quaeque. \
                            Sed ferri tamquam te, scaevola ocurreret conclusionemque in pro. Lorem ipsum dolor \
                            sit amet, abhorreant appellantur ex vis. Ad eos dicam quaeque. Sed ferri tamquam te, \
                            scaevola ocurreret conclusionemque in pro.'}]
        result = self.chat.add_message(user_token=self.user_token, messages=messages)
        self.assertFalse(result)

    def test_delete_message_success(self):
        messages = [{'_id': str(self.chat.messages[0]._id)}]
        result = self.chat.delete_message(messages)
        self.assertTrue(result)

    def test_delete_message_failed(self):
        messages = [{'_id': '0' * 10}]
        result = self.chat.delete_message(messages)
        self.assertFalse(result)

    def test_close_chat_success(self):
        result = self.chat.close_chat(self.user_token)
        self.assertTrue(result)

    def test_close_auto_chat(self):
        result = self.chat.close_auto_chat()
        self.assertTrue(result)

    def test_delete_closed_chat(self):
        chat_token = Chats.create_chat()['chat_token']
        chat = Chats.objects.get_all_by_token(chat_token)
        chat.status = 'closed'
        chat.save()
        id = chat.id
        chat.delete_closed_chat()
        chat = Chats.objects.filter(id=id)
        self.assertEquals([1], list(chat))

    def test_create_long_polling(self):
        expected = self.chat.create_long_polling(self.user_token)
        self.chat.reload()
        actual = self.chat.long_polling[0]._id
        self.assertEquals(expected, str(actual))

    def test_get_long_polling_success(self):
        self.chat.create_long_polling(self.user_token)
        self.chat.reload()
        long_polling = self.chat.get_long_polling(self.user_token)
        self.assertEquals(24, len(str(long_polling._id)))

    def test_get_long_polling_failed(self):
        self.chat.create_long_polling(self.user_token)
        self.chat.reload()
        long_polling = self.chat.get_long_polling(self.chat_token)
        self.assertFalse(long_polling)

    def test_auto_close_long_polling(self):
        talker_token = self.chat.get_talker_token(self.user_token)
        self.chat.create_long_polling(self.user_token)<commit_msg>add test_delete_closed_chat<commit_after>import time
import unittest
from bson.objectid import ObjectId
from apps.chat.models import *
from mongoengine import *


class ChatsTestCase(unittest.TestCase):
    def setUp(self):
        self.chat_token = Chats.create_chat()['chat_token']
        self.user_token = Chats.join_to_chat(self.chat_token)
        self.chat = Chats.objects.get_all_by_token(self.chat_token)

    def test_create_chat(self):
        self.assertTrue(self.chat)

    def test_join_to_chat_success(self):
        self.assertEquals(24, len(str(self.chat.user_tokens[1])))
        self.assertEquals(2, len(self.chat.user_tokens))

    def test_join_to_chat_failed(self):
        actual = Chats.join_to_chat('543e33a2e3ce324d374246fc')
        self.assertFalse(actual)

    def test_join_to_chat_twice_failed(self):
        actual = Chats.join_to_chat(str(self.chat_token))
        self.assertFalse(actual)

    def test_validate_chat_token_success(self):
        tokens = Chats.create_chat()
        actual = Chats.validate_chat_token(tokens['chat_token'])
        self.assertTrue(actual)

    def test_validate_chat_token_failed(self):
        actual = Chats.validate_chat_token('543e33a2e3ce324d374246fc')
        self.assertFalse(actual)

    def test_get_chat_success(self):
        chat = Chats.get_chat(self.chat_token, self.user_token)
        self.assertEquals(self.chat_token, str(chat.id))

    def test_get_chat_failed(self):
        chat = Chats.get_chat('543e33a2e3ce324d374246fc', '543e33a2e3ce324d374246fc')
        self.assertFalse(chat)

    def test_add_message_success(self):
        messages = [{'msg': 'First message'}, {'msg': 'Second message'}]
        result = self.chat.add_message(user_token=self.user_token, messages=messages)
        self.assertTrue(result)

    def test_add_message_failed(self):
        messages = [{'msg': 'Lorem ipsum dolor sit amet, abhorreant appellantur ex vis. Ad eos dicam quaeque. \
                            Sed ferri tamquam te, scaevola ocurreret conclusionemque in pro. Lorem ipsum dolor \
                            sit amet, abhorreant appellantur ex vis. Ad eos dicam quaeque. Sed ferri tamquam te, \
                            scaevola ocurreret conclusionemque in pro.'}]
        result = self.chat.add_message(user_token=self.user_token, messages=messages)
        self.assertFalse(result)

    def test_delete_message_success(self):
        messages = [{'_id': str(self.chat.messages[0]._id)}]
        result = self.chat.delete_message(messages)
        self.assertTrue(result)

    def test_delete_message_failed(self):
        messages = [{'_id': '0' * 10}]
        result = self.chat.delete_message(messages)
        self.assertFalse(result)

    def test_close_chat_success(self):
        result = self.chat.close_chat(self.user_token)
        self.assertTrue(result)

    def test_close_auto_chat(self):
        result = self.chat.close_auto_chat()
        self.assertTrue(result)

    def test_delete_closed_chat(self):
        chat_token = Chats.create_chat()['chat_token']
        chat = Chats.objects.get_all_by_token(chat_token)
        chat.status = 'closed'
        chat.save()
        id = chat.id
        chat.delete_closed_chat()
        chat = Chats.objects.filter(id=id)
        self.assertEquals([], list(chat))

    def test_create_long_polling(self):
        expected = self.chat.create_long_polling(self.user_token)
        self.chat.reload()
        actual = self.chat.long_polling[0]._id
        self.assertEquals(expected, str(actual))

    def test_get_long_polling_success(self):
        self.chat.create_long_polling(self.user_token)
        self.chat.reload()
        long_polling = self.chat.get_long_polling(self.user_token)
        self.assertEquals(24, len(str(long_polling._id)))

    def test_get_long_polling_failed(self):
        self.chat.create_long_polling(self.user_token)
        self.chat.reload()
        long_polling = self.chat.get_long_polling(self.chat_token)
        self.assertFalse(long_polling)

    def test_auto_close_long_polling(self):
        talker_token = self.chat.get_talker_token(self.user_token)
        self.chat.create_long_polling(self.user_token)
{'fraction_non_alphanumeric': 0.10161001788908766, 'fraction_numerical': 0.018008348240906382, 'mean_word_length': 3.2014028056112225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2988443', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2717, 'n_words': 410}