<commit_before>"""
Run me with:
    python manage.py test
"""
import json
import unittest
from datetime import datetime
import logging
import urlparse

from django.contrib.auth.models import User
from django.test.client import Client
import requests
from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import User
from models import Organization, Course, Problem, Essay, EssayGrade, UserProfile
from django.core.urlresolvers import reverse

log = logging.getLogger(__name__)

def run_setup():
    if(User.objects.filter(username='test').count() == 0):
        user = User.objects.create_user('test', '<EMAIL>', 'test')
        user.save()

def delete_all():
    """
    Teardown function to delete everything in DB
    """
    Organization.objects.all().delete()
    Course.objects.all().delete()
    Problem.objects.all().delete()

def get_urls(resource_name):
    endpoint = reverse("api_dispatch_list", kwargs={'api_name': 'v1','resource_name': resource_name})
    schema = reverse("api_get_schema", kwargs={'api_name': 'v1','resource_name': resource_name})
    return endpoint,schema

class GenericTest(object):
    type = "generic"

    def generic_setup(self):
        run_setup()
        self.c = Client()
        self.c.login(username='test', password='<PASSWORD>')
        self.endpoint, self.schema = get_urls(self.type)

    def test_schema(self):
        result = self.c.get(self.schema,
                            data={'format' : 'json'}
        )

        self.assertEqual(result.status_code,200)

    def test_endpoint(self):
        result = self.c.get(self.endpoint,
                            data={'format' : 'json'}
        )

        self.assertEqual(result.status_code,200)


class OrganizationTest(unittest.TestCase, GenericTest):
    type="organization"
    def setUp(self):
        self.generic_setup()

class CourseTest(unittest.TestCase, GenericTest):
    type="course"
    def setUp(self):
        self.generic_setup()

class ProblemTest(unittest.TestCase, GenericTest):
    type="problem"
    def setUp(self):
        self.generic_setup()

class EssayTest(unittest.TestCase, GenericTest):
    type="essay"
    def setUp(self):
        self.generic_setup()

class EssayGradeTest(unittest.TestCase, GenericTest):
    type="essaygrade"
    def setUp(self):
        self.generic_setup()

<commit_msg>More tests, start working on object creation<commit_after>"""
Run me with:
    python manage.py test
"""
import json
import unittest
from datetime import datetime
import logging
import urlparse

from django.contrib.auth.models import User
from django.test.client import Client
import requests
from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import User
from models import Organization, Course, Problem, Essay, EssayGrade, UserProfile
from django.core.urlresolvers import reverse

log = logging.getLogger(__name__)

def run_setup():
    if(User.objects.filter(username='test').count() == 0):
        user = User.objects.create_user('test', '<EMAIL>', 'test')
        user.save()

def delete_all():
    """
    Teardown function to delete everything in DB
    """
    Organization.objects.all().delete()
    Course.objects.all().delete()
    Problem.objects.all().delete()

def get_urls(resource_name):
    endpoint = reverse("api_dispatch_list", kwargs={'api_name': 'v1','resource_name': resource_name})
    schema = reverse("api_get_schema", kwargs={'api_name': 'v1','resource_name': resource_name})
    return endpoint,schema

def get_first_resource_uri(type):
    c = login()
    endpoint, schema = get_urls(type)
    data = c.get(endpoint, data={'format' : 'json'})
    log.debug(data.content)
    object = json.loads(data.content)['objects'][0]
    resource_uri = [object['resource_uri']]
    return resource_uri

def login():
    c = Client()
    c.login(username='test', password='<PASSWORD>')
    return c

class GenericTest(object):
    type = "generic"
    object = {'hello' : 'world'}

    def generic_setup(self):
        run_setup()
        self.c = login()
        self.endpoint, self.schema = get_urls(self.type)

    def test_schema(self):
        result = self.c.get(self.schema,
                            data={'format' : 'json'}
        )

        self.assertEqual(result.status_code,200)

    def test_endpoint(self):
        result = self.c.get(self.endpoint,
                            data={'format' : 'json'}
        )

        self.assertEqual(result.status_code,200)

    def test_create(self):
        result = self.c.post(self.endpoint, json.dumps(self.object), "application/json")
        self.assertEqual(result.status_code,201)


class OrganizationTest(unittest.TestCase, GenericTest):
    type="organization"
    object = {"name" : "edX"}

    def setUp(self):
        self.generic_setup()

class CourseTest(unittest.TestCase, GenericTest):
    type="course"
    object = {'course_name' : "edx_test"}
    def setUp(self):
        self.generic_setup()

class ProblemTest(unittest.TestCase, GenericTest):
    type="problem"

    def setUp(self):
        self.generic_setup()
        self.create_object()

    def create_object(self):
        course_resource_uri = get_first_resource_uri("course")
        self.object = {'courses' : course_resource_uri}

class EssayTest(unittest.TestCase, GenericTest):
    type="essay"
    def setUp(self):
        self.generic_setup()
        self.create_object()

    def create_object(self):
        problem_resource_uri = get_first_resource_uri("problem")
        self.object = {'problem' : problem_resource_uri}

class EssayGradeTest(unittest.TestCase, GenericTest):
    type="essaygrade"
    def setUp(self):
        self.generic_setup()


{'fraction_non_alphanumeric': 0.12410667596304689, 'fraction_numerical': 0.0038347568415548197, 'mean_word_length': 3.420647149460709, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4189526', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1691, 'n_words': 370}