<commit_before>from django.test import TestCase

# Create your tests here.<commit_msg>Initial import of choropleths tests.<commit_after>from django.test import TestCase
from django.core.urlresolvers import reverse
from datasets.models import Dataset, DatasetRecord
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password
from .models import Choropleth

from model_mommy import mommy
from model_mommy.recipe import seq


class ChoroplethTestCase(TestCase):

    def setUp(self):
        self.dataset = mommy.make(Dataset)
        records = mommy.make(DatasetRecord, _quantity=10, value=seq(0))
        for record in records:
            self.dataset.records.add(record)
        self.choropleth = mommy.make(Choropleth, dataset=self.dataset)

    def test_has_records_is_true(self):
        self.assertTrue(self.choropleth.has_records())

    def test_has_records_is_false(self):
        choropleth = mommy.make(
                Choropleth,
                dataset=mommy.make(Dataset))
        self.assertFalse(choropleth.has_records())

    def test_has_records_is_false_without_dataset(self):
        choropleth = mommy.make(Choropleth)
        self.assertFalse(choropleth.has_records())

    def test_get_dataset_id(self):
        self.assertTrue(self.choropleth.get_dataset_id())

    def test_get_dataset_id_without_dataset(self):
        choropleth = mommy.make(Choropleth)
        self.assertFalse(choropleth.get_dataset_id())


class ChoroplethViewsTestCase(TestCase):
    def setUp(self):
        self.user1 = mommy.make(User, password=make_password('password'))
        self.user2 = mommy.make(User, password=make_password('password'))
        self.dataset = mommy.make(Dataset, owner=self.user1)
        self.choropleth = mommy.make(Choropleth, dataset=self.dataset, owner=self.user1)

    def test_anon_user_can_view_choropleth_list(self):
        response = self.client.get(reverse('choropleths:choropleth-list'))
        self.assertEqual(response.status_code, 302)

    def test_user_can_view_choropleth_list(self):
        self.client.login(username=self.user1.username, password='password')
        response = self.client.get(reverse('choropleths:choropleth-list'))
        self.assertEqual(response.status_code, 200)

    def test_non_owner_cannot_view_choropleth_detail(self):
        self.client.login(username=self.user2.username, password='password')
        response = self.client.get(reverse(
            'choropleths:choropleth-detail',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 403)

    def test_non_owner_can_view_choropleth_detail(self):
        self.choropleth.published = 1
        self.choropleth.save()
        self.client.login(username=self.user2.username, password='password')
        response = self.client.get(reverse(
            'choropleths:choropleth-detail',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 200)

    def test_anon_user_cannot_view_choropleth_create(self):
        response = self.client.get(reverse(
            'choropleths:choropleth-create',
            args=[self.dataset.id]))
        self.assertEqual(response.status_code, 302)

    def test_non_owner_cannot_view_choropleth_create(self):
        self.client.login(username=self.user2.username, password='password')
        response = self.client.get(reverse(
            'choropleths:choropleth-create',
            args=[self.dataset.id]))
        self.assertEqual(response.status_code, 403)

    def test_owner_can_view_choropleth_create(self):
        self.client.login(username=self.user1.username, password='password')
        response = self.client.get(reverse(
            'choropleths:choropleth-create',
            args=[self.dataset.id]))
        self.assertEqual(response.status_code, 200)

    def test_anon_user_cannot_view_choropleth_edit(self):
        response = self.client.get(reverse(
            'choropleths:choropleth-edit',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 302)

    def test_non_owner_cannot_view_choropleth_edit(self):
        self.client.login(username=self.user2.username, password='password')
        response = self.client.get(reverse(
            'choropleths:choropleth-edit',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 403)

    def test_owner_can_view_choropleth_edit(self):
        self.client.login(username=self.user1.username, password='password')
        response = self.client.get(reverse(
            'choropleths:choropleth-edit',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 200)

    def test_anon_user_can_view_choropleth_view_when_published(self):
        self.choropleth.published = 1
        self.choropleth.save()
        response = self.client.get(reverse(
            'choropleths:choropleth-view',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 200)

    def test_non_owner_can_view_choropleth_view_when_published(self):
        self.client.login(username=self.user2.username, password='password')
        self.choropleth.published = 1
        self.choropleth.save()
        response = self.client.get(reverse(
            'choropleths:choropleth-view',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 200)

    def test_owner_can_view_choropleth_view_when_published(self):
        self.client.login(username=self.user2.username, password='password')
        self.choropleth.published = 1
        self.choropleth.save()
        response = self.client.get(reverse(
            'choropleths:choropleth-view',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 200)

    def test_anon_user_cannot_view_choropleth_view_when_not_published(self):
        response = self.client.get(reverse(
            'choropleths:choropleth-view',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 403)

    def test_non_owner_cannot_view_choropleth_view_when_not_published(self):
        self.client.login(username=self.user2.username, password='password')
        response = self.client.get(reverse(
            'choropleths:choropleth-view',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 403)

    def test_owner_can_view_choropleth_view_when_not_published(self):
        self.client.login(username=self.user2.username, password='password')
        self.choropleth.published = 1
        self.choropleth.save()
        response = self.client.get(reverse(
            'choropleths:choropleth-view',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 200)

    def test_choropleth_export(self):
        response = self.client.get(reverse(
            'choropleths:choropleth-export',
            args=[self.choropleth.id]))
        self.assertEqual(response.status_code, 200)
{'fraction_non_alphanumeric': 0.10518814139110605, 'fraction_numerical': 0.010547320410490307, 'mean_word_length': 3.30226854690374, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5491243', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2098, 'n_words': 271}