<commit_before>from django.test import TestCase, RequestFactory
from elasticsearch import Elasticsearch
import json, time

from . import views_model, views_auth


"""
User story #1: A client can search any keyword, using different query
specifiers (i.e. general, or specific model name). The general index
should include results from all models.
"""
class SearchTestCase(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

    def test_general_search(self):
        post_data = {
            'search_query': 'tp3ks',
            'query_specifier': 'general',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 3)

    def test_course_search(self):
        post_data = {
            'search_query': 'internet scale app',
            'query_specifier': 'course',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 1)
        self.assertEqual(resp_data['hits'][0]['model'], 'api.Course')
        c_label = 'CS 4501: Internet Scale App (<NAME>)'
        self.assertEqual(resp_data['hits'][0]['label'], c_label)

    def test_instructor_search(self):
        post_data = {
            'search_query': 'tp3ks',
            'query_specifier': 'instructor',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 1)
        self.assertEqual(resp_data['hits'][0]['model'], 'api.Instructor')
        ins_label = '<NAME> (tp3ks)'
        self.assertEqual(resp_data['hits'][0]['label'], ins_label)

    def test_student_search(self):
        post_data = {
            'search_query': 'tq7bw',
            'query_specifier': 'student',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 1)
        self.assertEqual(resp_data['hits'][0]['model'], 'api.Student')
        stud_label = 'Tong Qiu (tq7bw)'
        self.assertEqual(resp_data['hits'][0]['label'], stud_label)


"""
User story #2: A user, when creating an account or listing properly,
should see his new account/listing being added to the search index.
"""
class CreateAndSearchTestCase(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

        # create a course CS 6501
        course_data = {
            'mnemonic': 'CS',
            'number': '6501',
            'section': '008',
            'instructor': 'dee2b',
            'title': 'Understanding and Securing TLS',
            'id': '20529',
            'max_students': 30,
        }
        request = self.factory.post('/course/create/', course_data)
        views_model.course_create(request)

        # create a student yz9fy
        student_data = {
            'last_name': 'Zheng',
            'first_name': 'Ian',
            'id': 'yz9fy',
            'username': 'zeizyy',
            'password': '<PASSWORD>',
            'password_again': '<PASSWORD>',
        }
        request = self.factory.post('/student/signup/', student_data)
        views_auth.signup(request, 1)

        # sleeps for 15s to wait for instance creation to complete
        # this looks reaaaaaaaally stupid, but have to keep it...
        time.sleep(15)

    def tearDown(self):
        # delete both instances for testing
        url = 'http://models-api:8000/api/course/delete/'
        post_data = {'id': '20529'}<commit_msg>updated exp test<commit_after>from django.test import TestCase, RequestFactory
from elasticsearch import Elasticsearch
import json, time

from . import views_model, views_auth


"""
User story #1: A client can search any keyword, using different query
specifiers (i.e. general, or specific model name). The general index
should include results from all models.
"""
class SearchTestCase(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

    def test_general_search(self):
        post_data = {
            'search_query': 'tp3ks',
            'query_specifier': 'general',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 2)

    def test_course_search(self):
        post_data = {
            'search_query': 'tp3ks',
            'query_specifier': 'course',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 1)
        self.assertEqual(resp_data['hits'][0]['model'], 'api.Course')
        label = 'CS 4501: Special Topics in Computer Science (<NAME>)'
        self.assertEqual(resp_data['hits'][0]['label'], label)

    def test_instructor_search(self):
        post_data = {
            'search_query': 'tp3ks',
            'query_specifier': 'instructor',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 1)
        self.assertEqual(resp_data['hits'][0]['model'], 'api.Instructor')
        ins_label = '<NAME> (tp3ks)'
        self.assertEqual(resp_data['hits'][0]['label'], ins_label)

    def test_student_search(self):
        post_data = {
            'search_query': 'tq7bw',
            'query_specifier': 'student',
        }
        request = self.factory.post('/search/', data=post_data)
        response = views_model.search(request)
        resp_data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(resp_data['status_code'], 200)
        self.assertEqual(resp_data['size'], 1)
        self.assertEqual(resp_data['hits'][0]['model'], 'api.Student')
        stud_label = 'Tong Qiu (tq7bw)'
        self.assertEqual(resp_data['hits'][0]['label'], stud_label)


"""
User story #2: A user, when creating an account or listing properly,
should see his new account/listing being added to the search index.
"""
class CreateAndSearchTestCase(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

        # create a course CS 6501
        course_data = {
            'mnemonic': 'CS',
            'number': '6501',
            'section': '008',
            'instructor': 'dee2b',
            'title': 'Understanding and Securing TLS',
            'id': '20529',
            'max_students': 30,
        }
        request = self.factory.post('/course/create/', course_data)
        views_model.course_create(request)

        # create a student yz9fy
        student_data = {
            'last_name': 'Zheng',
            'first_name': 'Ian',
            'id': 'yz9fy',
            'username': 'zeizyy',
            'password': '<PASSWORD>',
            'password_again': '<PASSWORD>',
        }
        request = self.factory.post('/student/signup/', student_data)
        views_auth.signup(request, 1)

        # sleeps for 30s to wait for instance creation to complete
        # this looks reaaaaaaaally stupid, but have to keep it...
        time.sleep(30)

    def tearDown(self):
        # delete both instances for testing
        url = 'http://models-api:8000/api/course/delete/'
        post_data = {'id': '20529'}
{'fraction_non_alphanumeric': 0.1362085769980507, 'fraction_numerical': 0.01766569200779727, 'mean_word_length': 2.6242825607064018, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2174658', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2388, 'n_words': 521}