<commit_before>from django.utils import unittest
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.utils import simplejson
from itertools import *

# FIXME better url treatment
# FIXME relying on ordering when doing api set calls
# FIXME should construct data through posts

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AgoraTest))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(UserTest))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(MiscTest))
    return suite

API_ROOT = '/api/v1/'

class RootTestCase(TestCase):
    fixtures = ['test_users.json', 'test_agoras.json']

    def login(self, user, passw):
        loggedIn = self.client.login(username=user, password=<PASSWORD>)
        self.assertTrue(loggedIn)

    def get(self, url, code = 200):
        response = self.client.get(API_ROOT + url)
        self.assertEqual(response.status_code, code)

        return response.content

    def getAndParse(self, url, code = 200):
        json = self.get(url, code)
        data = simplejson.loads(json)

        return data

    def post(self, url, data = {}, code = 200):
        response = self.client.post(API_ROOT + url, data)
        self.assertEqual(response.status_code, code)

        return response.content

    def postAndParse(self, url, data = {}, code = 200):
        json = self.post(url, data, code)
        data = simplejson.loads(json)

        return data

class AgoraTest(RootTestCase):
    def test_agora(self):
        # all
        data = self.getAndParse('agora/')
        agoras = data['objects']
        self.assertEqual(len(agoras), 2)

        # find
        data = self.getAndParse('agora/1/')
        self.assertEquals(data['name'], 'agoraone')

        data = self.getAndParse('agora/2/')
        self.assertEquals(data['name'], 'agoratwo')

        data = self.get('agora/200/', 404)

        # TODO set

class CastVoteTest(RootTestCase):
    # TODO
    pass

class ElectionTest(RootTestCase):
    # TODO
    pass

class FollowTest(RootTestCase):
    # TODO
    pass

class ActionTest(RootTestCase):
    # TODO
    pass

class UserTest(RootTestCase):

    def powerset(self, iterable):
        s = list(iterable)
        return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

    def merge(self, a, b): return dict(a.items() + b.items())

    def test_user(self):
        # all
        data = self.getAndParse('user/')
        users = data['objects']
        # 7 = 5 test users, + admin + anonymous
        self.assertEqual(len(users), 7)<commit_msg>Test: Added agora creation, deletion and update<commit_after>from django.utils import unittest
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.utils import simplejson
from itertools import *
import json


# FIXME better url treatment
# FIXME relying on ordering when doing api set calls
# FIXME should construct data through posts

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AgoraTest))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(UserTest))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(MiscTest))
    return suite

API_ROOT = '/api/v1/'

class RootTestCase(TestCase):
    fixtures = ['test_users.json', 'test_agoras.json']

    def login(self, user, passw):
        loggedIn = self.client.login(username=user, password=<PASSWORD>)
        self.assertTrue(loggedIn)

    def get(self, url, code = 200):
        response = self.client.get(API_ROOT + url)
        self.assertEqual(response.status_code, code)

        return response.content

    def getAndParse(self, url, code = 200):
        json = self.get(url, code)
        data = simplejson.loads(json)

        return data

    def post(self, url, data = {}, code = 200, **kwargs):
        response = self.client.post(API_ROOT + url, data, **kwargs)
        self.assertEqual(response.status_code, code)

        return response.content

    def postAndParse(self, url, data = {}, code = 200, **kwargs):
        json = self.post(url, data, code, **kwargs)
        data = simplejson.loads(json)

        return data

    def delete(self, url, data = {}, code = 200, **kwargs):
        response = self.client.delete(API_ROOT + url, data, **kwargs)
        self.assertEqual(response.status_code, code)

        return response.content

    def deleteAndParse(self, url, data = {}, code = 200, **kwargs):
        json = self.delete(url, data, code, **kwargs)
        data = simplejson.loads(json)

        return data

    def put(self, url, data = {}, code = 200, **kwargs):
        response = self.client.put(API_ROOT + url, data, **kwargs)
        self.assertEqual(response.status_code, code)

        return response.content

    def putAndParse(self, url, data = {}, code = 200, **kwargs):
        json = self.put(url, data, code, **kwargs)
        data = simplejson.loads(json)

        return data


class AgoraTest(RootTestCase):
    def test_agora(self):
        # all
        data = self.getAndParse('agora/')
        agoras = data['objects']
        self.assertEqual(len(agoras), 2)

    def test_agora_find(self):
        # find
        data = self.getAndParse('agora/1/')
        self.assertEquals(data['name'], 'agoraone')

        data = self.getAndParse('agora/2/')
        self.assertEquals(data['name'], 'agoratwo')

        data = self.get('agora/200/', 404)

    def test_agora_creation(self):
        # creating agora
        self.login('david', 'david')
        orig_data = {'pretty_name': 'created agora',
                     'short_description': 'created agora description',
                     'is_vote_secret': False}
        data = self.postAndParse('agora/', json.dumps(orig_data), 201,
                                 content_type='application/json')
        self.assertEqual(data['pretty_name'], 'created agora')

        data = self.getAndParse('agora/%s/' % data['id'])
        for k, v in orig_data.items():
            self.assertEquals(data[k], v)

    def test_agora_deletion(self):
        # TODO check permissions
        self.login('david', 'david')
        data = self.getAndParse('agora/')
        agoras = data['objects']
        self.assertEqual(len(agoras), 2)

        self.delete('agora/1/', {}, 204)

        data = self.getAndParse('agora/')
        agoras = data['objects']
        self.assertEqual(len(agoras), 1)

        self.delete('agora/1/', {}, 404)
        self.delete('agora/200/', {}, 404)

    def test_agora_update(self):
        # TODO check permissions
        self.login('david', 'david')
        orig_data = {'pretty_name': "updated name",
                     'short_description': "new desc",
                     'is_vote_secret': False,
                     'biography': "bio",
                     'membership_policy': 'ANYONE_CAN_JOIN',
                     'comments_policy': 'ANYONE_CAN_COMMENT'}
        data = self.putAndParse('agora/1/', json.dumps(orig_data), 202,
                                 content_type='application/json')

        data = self.getAndParse('agora/1/')
        for k, v in orig_data.items():
            self.assertEquals(data[k], v)


class CastVoteTest(RootTestCase):
    # TODO
    pass

class ElectionTest(RootTestCase):
    # TODO
    pass

class FollowTest(RootTestCase):
    # TODO
    pass

class ActionTest(RootTestCase):
    # TODO
    pass

class UserTest(RootTestCase):

    def powerset(self, iterable):
        s = list(iterable)
        return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

    def merge(self, a, b): return dict(a.items() + b.items())

    def test_user(self):
        # all
        data = self.getAndParse('user/')
        users = data['objects']
        # 7 = 5 test users, + admin + anonymous
        self.assertEqual(len(users), 7)
{'fraction_non_alphanumeric': 0.12484363272454341, 'fraction_numerical': 0.011008256192144109, 'mean_word_length': 2.6792452830188678, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3719867', 'n_tokens_mistral': 2508, 'n_tokens_neox': 2386, 'n_words': 560}