<commit_before>from rest_framework.test import APITestCase
from django.core.urlresolvers import reverse
from rest_framework import status

from django.utils import timezone as tz
from django.db.models import query

from msgvis.apps.corpus import models as corpus_models
from msgvis.apps.corpus import utils as corpus_utils
from msgvis.apps.questions import models as questions_models
from msgvis.apps.dimensions import models as dimensions_models
import mock

from msgvis.apps.api.tests import api_time_format, django_time_format


class ResearchQuestionsViewTest(APITestCase):
    def setUp(self):
        self.dataset = corpus_models.Dataset.objects.create(name="Api test dataset")

        # Create some dimension keys
        self.dimension_keys = ['time', 'hashtags']
        primary_dim = dimensions_models.DimensionKey.objects.create(key=self.dimension_keys[0])
        secondary_dim = dimensions_models.DimensionKey.objects.create(key=self.dimension_keys[1])

        # Create an article
        article = questions_models.Article.objects.create(
            authors="something",
            link="a link",
            title="some title",
            venue="a venue"
        )

        # Generate some sample research questions
        self.sample_questions = [
            questions_models.Question.objects.create(
                text="hello",
                source=article,
            ),
            questions_models.Question.objects.create(
                text="goodbye",
                source=article,
            ),
        ]

        for q in self.sample_questions:
            q.add_dimension(primary_dim)
            q.add_dimension(secondary_dim)

    @mock.patch('msgvis.apps.questions.models.Question.get_sample_questions')
    def test_get_sample_questions_api(self, get_sample_questions):
        primary_dimension_key = self.dimension_keys[0]
        secondary_dimension_key = self.dimension_keys[1]

        get_sample_questions.return_value = self.sample_questions

        url = reverse('research-questions')
        data = {
            "dimensions": [primary_dimension_key, secondary_dimension_key]
        }

        expected_response = {
            'dimensions': [primary_dimension_key, secondary_dimension_key],
            'questions': [
                {
                    'id': q.id,
                    'text': q.text,
                    'dimensions': [d.key for d in q.dimensions.all()],
                    'source': {
                        'id': q.source.id,
                        'authors': q.source.authors,
                        'link': q.source.link,
                        'title': q.source.title,
                        'year': q.source.year,
                        'venue': q.source.venue,
                    }
                } for q in self.sample_questions
            ]
        }

        response = self.client.post(url, data, format='json')

        self.assertEquals(response.data, expected_response)
        self.assertEquals(response.status_code, status.HTTP_200_OK)

        get_sample_questions.assert_called_once_with(primary_dimension_key, secondary_dimension_key)


class ExampleMessagesViewTest(APITestCase):
    def setUp(self):
        self.dataset = corpus_models.Dataset.objects.create(name="Api test dataset")

        sender = self.dataset.person_set.create(
            username='a person',
            full_name='<NAME>',
            original_id=2353583,
        )

        # Create a couple of messages
        self.sample_messages = [
            self.dataset.message_set.create(
                text="i am a message",
                time=tz.now(),
                sender=sender,
            ),
            self.dataset.message_set.create(
                text="another message #hashtag #mention http://url.com",
                time=tz.now(),
                sender=sender,
            )
        ]


    @mock.patch.object(corpus_models.Dataset, 'get_example_messages')
    def test_get_example_messages_api(self, get_example_messages):
        # fake the actual example finding
        get_example_messages.return_value = self.sample_messages

        url = reverse('example-messages')
        data = {
            "dataset": self.dataset.id,
            "filters": [
                {
                    "dimension": "time",
                    "min_time": "2015-02-02T01:19:08Z",
                    "max_time": "2015-02-02T01:19:09Z"
                }
            ],
            "focus": [
                {
                    "dimension": "time",
                    "value": "2015-02-02T01:19:09Z"
                }
            ]
        }

        expected_response = {
            "dataset": data['dataset'],
            "filters": data['filters'],
            "focus": data['focus'],
            "messages": [
                {
                    'id': m.id,
                    'dataset': m.dataset_id,
                    'text': m.text,
                    'time': api_time_format(m.time),
                    'sender': {
                        'id': m.sender.id,
                        'dataset': m.sender.dataset_id,
                        'original_id': m.sender.original_id,
                        'username': m.sender.username,
                        'full_name': m.sender.full_name,
                        'profile_image_local_name': m.sender.profile_image_local_name
                    },
                    'original_id': None,
                    'embedded_html': corpus_utils.render_html_tag(m.text),
                    'media_url': m.media_url
                } for m in self.sample_messages
            ]
        }

        response = self.client.post(url, data, format='json')

        self.assertEquals(response.data, expected_response)
        self.assertEquals(response.status_code, status.HTTP_200_OK)

        self.assertEquals(get_example_messages.call_count, 1)


class DataTableViewTest(APITestCase):
    def setUp(self):
        self.dataset = corpus_models.Dataset.objects.create(name="Api test dataset")

        sender = self.dataset.person_set.create(
            username='a person',
            full_name='a more important person',
            original_id=2353583,
        )

        # Create a couple of messages
        self.sample_messages = [
            self.dataset.message_set.create(
                text="i am a message",
                sender=sender,<commit_msg>fix test<commit_after>from rest_framework.test import APITestCase
from django.core.urlresolvers import reverse
from rest_framework import status

from django.utils import timezone as tz
from django.db.models import query

from msgvis.apps.corpus import models as corpus_models
from msgvis.apps.corpus import utils as corpus_utils
from msgvis.apps.questions import models as questions_models
from msgvis.apps.dimensions import models as dimensions_models
import mock

from msgvis.apps.api.tests import api_time_format, django_time_format


class ResearchQuestionsViewTest(APITestCase):
    def setUp(self):
        self.dataset = corpus_models.Dataset.objects.create(name="Api test dataset")

        # Create some dimension keys
        self.dimension_keys = ['time', 'hashtags']
        primary_dim = dimensions_models.DimensionKey.objects.create(key=self.dimension_keys[0])
        secondary_dim = dimensions_models.DimensionKey.objects.create(key=self.dimension_keys[1])

        # Create an article
        article = questions_models.Article.objects.create(
            authors="something",
            link="a link",
            title="some title",
            venue="a venue"
        )

        # Generate some sample research questions
        self.sample_questions = [
            questions_models.Question.objects.create(
                text="hello",
                source=article,
            ),
            questions_models.Question.objects.create(
                text="goodbye",
                source=article,
            ),
        ]

        for q in self.sample_questions:
            q.add_dimension(primary_dim)
            q.add_dimension(secondary_dim)

    @mock.patch('msgvis.apps.questions.models.Question.get_sample_questions')
    def test_get_sample_questions_api(self, get_sample_questions):
        primary_dimension_key = self.dimension_keys[0]
        secondary_dimension_key = self.dimension_keys[1]

        get_sample_questions.return_value = self.sample_questions

        url = reverse('research-questions')
        data = {
            "dimensions": [primary_dimension_key, secondary_dimension_key]
        }

        expected_response = {
            'dimensions': [primary_dimension_key, secondary_dimension_key],
            'questions': [
                {
                    'id': q.id,
                    'text': q.text,
                    'dimensions': [d.key for d in q.dimensions.all()],
                    'source': {
                        'id': q.source.id,
                        'authors': q.source.authors,
                        'link': q.source.link,
                        'title': q.source.title,
                        'year': q.source.year,
                        'venue': q.source.venue,
                    }
                } for q in self.sample_questions
            ]
        }

        response = self.client.post(url, data, format='json')

        self.assertEquals(response.data, expected_response)
        self.assertEquals(response.status_code, status.HTTP_200_OK)

        get_sample_questions.assert_called_once_with(primary_dimension_key, secondary_dimension_key)


class ExampleMessagesViewTest(APITestCase):
    def setUp(self):
        self.dataset = corpus_models.Dataset.objects.create(name="Api test dataset")

        sender = self.dataset.person_set.create(
            username='a person',
            full_name='<NAME>',
            original_id=2353583,
        )

        # Create a couple of messages
        self.sample_messages = [
            self.dataset.message_set.create(
                text="i am a message",
                time=tz.now(),
                sender=sender,
            ),
            self.dataset.message_set.create(
                text="another message #hashtag #mention http://url.com",
                time=tz.now(),
                sender=sender,
            )
        ]


    @mock.patch.object(corpus_models.Dataset, 'get_example_messages')
    def test_get_example_messages_api(self, get_example_messages):
        # fake the actual example finding
        get_example_messages.return_value = self.sample_messages

        url = reverse('example-messages')
        data = {
            "dataset": self.dataset.id,
            "filters": [
                {
                    "dimension": "time",
                    "min_time": "2015-02-02T01:19:08Z",
                    "max_time": "2015-02-02T01:19:09Z"
                }
            ],
            "focus": [
                {
                    "dimension": "time",
                    "value": "2015-02-02T01:19:09Z"
                }
            ]
        }

        expected_response = {
            "dataset": data['dataset'],
            "filters": data['filters'],
            "focus": data['focus'],
            "messages": {
                "count": 2,
                "next": None,
                "previous": None,
                "results":[
                    {
                        'id': m.id,
                        'dataset': m.dataset_id,
                        'text': m.text,
                        'time': api_time_format(m.time),
                        'sender': {
                            'id': m.sender.id,
                            'dataset': m.sender.dataset_id,
                            'original_id': m.sender.original_id,
                            'username': m.sender.username,
                            'full_name': m.sender.full_name,
                            'profile_image_local_name': m.sender.profile_image_local_name
                        },
                        'original_id': None,
                        'embedded_html': corpus_utils.render_html_tag(m.text),
                        'media_url': m.media_url
                    } for m in self.sample_messages
                ]
            }
        }

        response = self.client.post(url, data, format='json')

        self.assertEquals(response.data, expected_response)
        self.assertEquals(response.status_code, status.HTTP_200_OK)

        self.assertEquals(get_example_messages.call_count, 1)


class DataTableViewTest(APITestCase):
    def setUp(self):
        self.dataset = corpus_models.Dataset.objects.create(name="Api test dataset")

        sender = self.dataset.person_set.create(
            username='a person',
            full_name='<NAME> important person',
            original_id=2353583,
        )

        # Create a couple of messages
        self.sample_messages = [
            self.dataset.message_set.create(
                text="i am a message",
                sender=sender,
{'fraction_non_alphanumeric': 0.09707620290077508, 'fraction_numerical': 0.01035991098150564, 'mean_word_length': 1.6732307692307693, 'pattern_counts': {'":': 30, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '955761', 'n_tokens_mistral': 3637, 'n_tokens_neox': 3343, 'n_words': 610}