<commit_before>from __future__ import unicode_literals

from django_webtest import WebTest

from .factories import (
    AreaExtraFactory, CandidacyExtraFactory, MembershipFactory,
    PersonExtraFactory, PostExtraFactory,
)
from .uk_examples import UK2015ExamplesMixin

from candidates.models import LoggedAction


class TestAPI(UK2015ExamplesMixin, WebTest):

    def setUp(self):
        super(TestAPI, self).setUp()

        person_extra = PersonExtraFactory.create(
            base__id='2009',
            base__name='<NAME>'
        )
        dulwich_not_stand = PersonExtraFactory.create(
            base__id='4322',
            base__name='<NAME>'
        )
        edinburgh_candidate = PersonExtraFactory.create(
            base__id='818',
            base__name='<NAME>'
        )
        edinburgh_winner = PersonExtraFactory.create(
            base__id='5795',
            base__name='<NAME>'
        )
        edinburgh_may_stand = PersonExtraFactory.create(
            base__id='5163',
            base__name='<NAME>'
        )

        CandidacyExtraFactory.create(
            election=self.election,
            base__person=person_extra.base,
            base__post=self.dulwich_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base
            )
        MembershipFactory.create(
            person=person_extra.base,
            organization=self.labour_party_extra.base
        )

        CandidacyExtraFactory.create(
            election=self.earlier_election,
            base__person=dulwich_not_stand.base,
            base__post=self.dulwich_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base,
            )
        dulwich_not_stand.not_standing.add(self.election)

        CandidacyExtraFactory.create(
            election=self.election,
            base__person=edinburgh_winner.base,
            base__post=self.edinburgh_east_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base,
            elected=True,
            )

        CandidacyExtraFactory.create(
            election=self.election,
            base__person=edinburgh_candidate.base,
            base__post=self.edinburgh_east_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base
            )
        MembershipFactory.create(
            person=edinburgh_candidate.base,
            organization=self.labour_party_extra.base
        )

        MembershipFactory.create(
            person=edinburgh_winner.base,
            organization=self.labour_party_extra.base
        )
        CandidacyExtraFactory.create(
            election=self.earlier_election,
            base__person=edinburgh_may_stand.base,
            base__post=self.edinburgh_east_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base
            )

    def test_api_basic_response(self):
        response = self.app.get(
            '/api/v0.9/'
        )
        self.assertEqual(response.status_code, 200)
        json = response.json

        self.assertEqual(
            json['persons'],
            'http://localhost:80/api/v0.9/persons/'
        )
        self.assertEqual(json['areas'], 'http://localhost:80/api/v0.9/areas/')
        self.assertEqual(
            json['organizations'],
            'http://localhost:80/api/v0.9/organizations/'
        )
        self.assertEqual(
            json['elections'],
            'http://localhost:80/api/v0.9/elections/'
        )
        self.assertEqual(json['posts'], 'http://localhost:80/api/v0.9/posts/')

        persons_resp = self.app.get('/api/v0.9/persons/')
        self.assertEqual(persons_resp.status_code, 200)

        areas_resp = self.app.get('/api/v0.9/areas/')
        self.assertEqual(areas_resp.status_code, 200)

        organizations_resp = self.app.get('/api/v0.9/organizations/')
        self.assertEqual(organizations_resp.status_code, 200)

        elections_resp = self.app.get('/api/v0.9/elections/')
        self.assertEqual(elections_resp.status_code, 200)

        posts_resp = self.app.get('/api/v0.9/posts/')
        self.assertEqual(posts_resp.status_code, 200)

    def test_api_errors(self):
        response = self.app.get(
            '/api/',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.get(
            '/api/v0.8',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.get(
            '/api/v0.9/person/',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.get(
            '/api/v0.9/persons/4000/',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.post(
            '/api/v0.9/persons/',
            {},
            expect_errors=True
        )
        self.assertEqual(response.status_code, 403)

    def test_api_persons(self):
        persons_resp = self.app.get('/api/v0.9/persons/')

        persons = persons_resp.json

        self.assertEqual(persons['count'], len(persons['results']))
        self.assertEqual(persons['count'], 5)

    def test_api_person(self):
        person_resp = self.app.get('/api/v0.9/persons/2009/')

        self.assertEqual(person_resp.status_code, 200)

        person = person_resp.json
        self.assertEqual(person['id'], 2009)
        self.assertEqual(person['name'], '<NAME>')

        memberships = person['memberships']

        self.assertEqual(len(memberships), 2)
        self.assertEqual(memberships[1]['role'], 'Candidate')

        self.assertEqual(len(person['versions']), 0)

    def test_api_areas(self):
        areas_resp = self.app.get('/api/v0.9/areas/')

        areas = areas_resp.json

        self.assertEqual(areas['count'], len(areas['results']))
        self.assertEqual(areas['count'], 4)<commit_msg>API: fix a test failure due to non-deterministic ordering<commit_after>from __future__ import unicode_literals

from django_webtest import WebTest

from .factories import (
    AreaExtraFactory, CandidacyExtraFactory, MembershipFactory,
    PersonExtraFactory, PostExtraFactory,
)
from .uk_examples import UK2015ExamplesMixin

from candidates.models import LoggedAction


class TestAPI(UK2015ExamplesMixin, WebTest):

    def setUp(self):
        super(TestAPI, self).setUp()

        person_extra = PersonExtraFactory.create(
            base__id='2009',
            base__name='<NAME>'
        )
        dulwich_not_stand = PersonExtraFactory.create(
            base__id='4322',
            base__name='<NAME>'
        )
        edinburgh_candidate = PersonExtraFactory.create(
            base__id='818',
            base__name='<NAME>'
        )
        edinburgh_winner = PersonExtraFactory.create(
            base__id='5795',
            base__name='<NAME>'
        )
        edinburgh_may_stand = PersonExtraFactory.create(
            base__id='5163',
            base__name='<NAME>'
        )

        CandidacyExtraFactory.create(
            election=self.election,
            base__person=person_extra.base,
            base__post=self.dulwich_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base
            )
        MembershipFactory.create(
            person=person_extra.base,
            organization=self.labour_party_extra.base
        )

        CandidacyExtraFactory.create(
            election=self.earlier_election,
            base__person=dulwich_not_stand.base,
            base__post=self.dulwich_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base,
            )
        dulwich_not_stand.not_standing.add(self.election)

        CandidacyExtraFactory.create(
            election=self.election,
            base__person=edinburgh_winner.base,
            base__post=self.edinburgh_east_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base,
            elected=True,
            )

        CandidacyExtraFactory.create(
            election=self.election,
            base__person=edinburgh_candidate.base,
            base__post=self.edinburgh_east_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base
            )
        MembershipFactory.create(
            person=edinburgh_candidate.base,
            organization=self.labour_party_extra.base
        )

        MembershipFactory.create(
            person=edinburgh_winner.base,
            organization=self.labour_party_extra.base
        )
        CandidacyExtraFactory.create(
            election=self.earlier_election,
            base__person=edinburgh_may_stand.base,
            base__post=self.edinburgh_east_post_extra.base,
            base__on_behalf_of=self.labour_party_extra.base
            )

    def test_api_basic_response(self):
        response = self.app.get(
            '/api/v0.9/'
        )
        self.assertEqual(response.status_code, 200)
        json = response.json

        self.assertEqual(
            json['persons'],
            'http://localhost:80/api/v0.9/persons/'
        )
        self.assertEqual(json['areas'], 'http://localhost:80/api/v0.9/areas/')
        self.assertEqual(
            json['organizations'],
            'http://localhost:80/api/v0.9/organizations/'
        )
        self.assertEqual(
            json['elections'],
            'http://localhost:80/api/v0.9/elections/'
        )
        self.assertEqual(json['posts'], 'http://localhost:80/api/v0.9/posts/')

        persons_resp = self.app.get('/api/v0.9/persons/')
        self.assertEqual(persons_resp.status_code, 200)

        areas_resp = self.app.get('/api/v0.9/areas/')
        self.assertEqual(areas_resp.status_code, 200)

        organizations_resp = self.app.get('/api/v0.9/organizations/')
        self.assertEqual(organizations_resp.status_code, 200)

        elections_resp = self.app.get('/api/v0.9/elections/')
        self.assertEqual(elections_resp.status_code, 200)

        posts_resp = self.app.get('/api/v0.9/posts/')
        self.assertEqual(posts_resp.status_code, 200)

    def test_api_errors(self):
        response = self.app.get(
            '/api/',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.get(
            '/api/v0.8',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.get(
            '/api/v0.9/person/',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.get(
            '/api/v0.9/persons/4000/',
            expect_errors=True
        )
        self.assertEqual(response.status_code, 404)

        response = self.app.post(
            '/api/v0.9/persons/',
            {},
            expect_errors=True
        )
        self.assertEqual(response.status_code, 403)

    def test_api_persons(self):
        persons_resp = self.app.get('/api/v0.9/persons/')

        persons = persons_resp.json

        self.assertEqual(persons['count'], len(persons['results']))
        self.assertEqual(persons['count'], 5)

    def test_api_person(self):
        person_resp = self.app.get('/api/v0.9/persons/2009/')

        self.assertEqual(person_resp.status_code, 200)

        person = person_resp.json
        self.assertEqual(person['id'], 2009)
        self.assertEqual(person['name'], '<NAME>')

        memberships = sorted(
            person['memberships'],
            key=lambda m: m['role']
        )

        self.assertEqual(len(memberships), 2)
        self.assertEqual(memberships[1]['role'], 'Candidate')

        self.assertEqual(len(person['versions']), 0)

    def test_api_areas(self):
        areas_resp = self.app.get('/api/v0.9/areas/')

        areas = areas_resp.json

        self.assertEqual(areas['count'], len(areas['results']))
        self.assertEqual(areas['count'], 4)
{'fraction_non_alphanumeric': 0.11064857214220299, 'fraction_numerical': 0.0209807676296728, 'mean_word_length': 2.1502753737214793, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7121737', 'n_tokens_mistral': 3783, 'n_tokens_neox': 3570, 'n_words': 413}