<commit_before>from django.test import TestCase

from datasource.forms import DataSourceForm
from datasource import client

import httpretty

import os


class NewTestCase(TestCase):
    def test_new(self):
        response = self.client.get("/datasource/")
        self.assertTemplateUsed(response, "datasource/new.html")
        self.assertIsInstance(response.context['form'], DataSourceForm)

    def test_new_invalid_post(self):
        response = self.client.post("/datasource/", {})
        self.assertFalse(response.context['form'].is_valid())

    def test_new_post(self):
        data = {
            "url": "someurl",
            "name": "name",
            "method": "GET",
        }
        response = self.client.post("/datasource/", data)
        self.assertEquals(response.context['form'].errors, {})
        self.assertTrue(response.context['form'].is_valid())


class DataSourceTestCase(TestCase):
    def test_required_fields(self):
        fields = {
            "url": True,
            "method": True,
            "name": True,
            "body": False,
            "headers": False,
        }

        form = DataSourceForm()

        for field, required in fields.items():
            self.assertEqual(form.fields[field].required, required)


class ClientTestCase(TestCase):
    def test_new(self):
        os.environ["AUTOSCALE_HOST"] = "http://autoscalehost.com"
        httpretty.enable()
        httpretty.register_uri(
            httpretty.POST,
            "http://autoscalehost.com/datasource",
        )

        client.new({})

        httpretty.disable()
        httpretty.reset()

    def test_list(self):
        os.environ["AUTOSCALE_HOST"] = "http://autoscalehost.com"
        httpretty.enable()
        httpretty.register_uri(
            httpretty.GET,
            "http://autoscalehost.com/datasource",
        )

        client.list()

        httpretty.disable()
        httpretty.reset()<commit_msg>datasource/client: refactored tests.<commit_after>from django.test import TestCase

from datasource.forms import DataSourceForm
from datasource import client

import httpretty

import os


class NewTestCase(TestCase):
    def test_new(self):
        response = self.client.get("/datasource/")
        self.assertTemplateUsed(response, "datasource/new.html")
        self.assertIsInstance(response.context['form'], DataSourceForm)

    def test_new_invalid_post(self):
        response = self.client.post("/datasource/", {})
        self.assertFalse(response.context['form'].is_valid())

    def test_new_post(self):
        data = {
            "url": "someurl",
            "name": "name",
            "method": "GET",
        }
        response = self.client.post("/datasource/", data)
        self.assertEquals(response.context['form'].errors, {})
        self.assertTrue(response.context['form'].is_valid())


class DataSourceTestCase(TestCase):
    def test_required_fields(self):
        fields = {
            "url": True,
            "method": True,
            "name": True,
            "body": False,
            "headers": False,
        }

        form = DataSourceForm()

        for field, required in fields.items():
            self.assertEqual(form.fields[field].required, required)


class ClientTestCase(TestCase):
    def setUp(self):
        httpretty.enable()

    def tearDown(self):
        httpretty.disable()
        httpretty.reset()

    def test_new(self):
        os.environ["AUTOSCALE_HOST"] = "http://autoscalehost.com"
        httpretty.register_uri(
            httpretty.POST,
            "http://autoscalehost.com/datasource",
        )

        client.new({})

    def test_list(self):
        os.environ["AUTOSCALE_HOST"] = "http://autoscalehost.com"
        httpretty.register_uri(
            httpretty.GET,
            "http://autoscalehost.com/datasource",
        )

        client.list()
{'fraction_non_alphanumeric': 0.12903225806451613, 'fraction_numerical': 0.0, 'mean_word_length': 2.32133676092545, 'pattern_counts': {'":': 16, '<': 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': '1504861', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1110, 'n_words': 188}