<commit_before>from django.conf import settings
from django.core.urlresolvers import reverse
from mock import patch

from ab_testing_tool.pages.main_pages import STAGE_URL_TAG
from ab_testing_tool.controllers import (get_uninstalled_stages,
    stage_url)
from ab_testing_tool.models import Stage, StageUrl
from ab_testing_tool.tests.test_main_pages import (SessionTestCase,
    TEST_COURSE_ID, APIReturn, LIST_MODULES, LIST_ITEMS)


class test_stage_pages(SessionTestCase):

    def test_get_uninstalled_stages(self):
        """Tests method get_uninstalled_stages runs and returns no stages when database empty"""
        stages = get_uninstalled_stages(self.request)
        self.assertEqual(len(stages), 0)

    @patch(LIST_MODULES, return_value=APIReturn([{"id": 0}]))
    def test_get_uninstalled_stages_with_item(self, _mock1):
        """Tests method get_uninstalled_stages returns one when database has one item"""
        #TODO !! take into account course_id here!!
        Stage.objects.create(name="stage1")
        stages = get_uninstalled_stages(self.request)
        self.assertEqual(len(stages), 1)

    @patch(LIST_MODULES, return_value=APIReturn([{"id": 0}]))
    def test_get_uninstalled_stages_against_api(self, _mock1):
        """ Tests method get_uninstalled_stages returns zero when stage in database
            is also returned by the API, which means it is installed """
        stage = Stage.objects.create(name="stage1")
        mock_item = {"type": "ExternalTool",
                     "external_url": stage_url(self.request, stage.id)}
        with patch(LIST_ITEMS, return_value=APIReturn([mock_item])):
            stages = get_uninstalled_stages(self.request)
            self.assertEqual(len(stages), 0)

    def test_submit_create_stage(self):
        #TODO: change this test to several tests that Stage and StageUrls in DB,
        #covering the cases when no stage urls submited, with stage urls are submitted
        """Tests that create_stage creates a Stage object verified by DB count"""
        num_stages = Stage.objects.count()
        data = {"name": "stage", "notes": "hi"}
        response = self.client.post(reverse("submit_create_stage"), data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEquals(num_stages + 1, Stage.objects.count())

    def test_submit_create_stage_with_stageurls(self):
        """Tests that create_stage creates a Stage object and StageUrl objects
            verified by DB count"""
        num_stages = Stage.objects.count()
        num_stageurls = StageUrl.objects.count()
        data = {"name": "stage", STAGE_URL_TAG + "1": "http://example.com/page",
                STAGE_URL_TAG + "2": "http://example.com/otherpage", "notes": "hi"}
        response = self.client.post(reverse("submit_create_stage"), data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEquals(num_stages + 1, Stage.objects.count())
        self.assertEquals(num_stageurls + 2, Stage.objects.count())

    def test_submit_edit_stage(self):
        """ Tests that edit_stage does not change DB count but does change Stage
            attribute"""
        stage = Stage.objects.create(name="old_name",
                                             course_id=TEST_COURSE_ID)
        stage_id = stage.id
        num_stages = Stage.objects.count()
        data = {"name": "new_name", "url1": "http://example.com/page",
                "url2": "http://example.com/otherpage", "notes": "",
                "id": stage_id}
        response = self.client.post(reverse("submit_edit_stage"), data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEquals(num_stages, Stage.objects.count())
        stage = Stage.objects.get(id=stage_id)
        self.assertEquals(stage.name, "new_name")

    def test_update_nonexistent_stage(self):
        """ Tests that update_stage method raises error for non-existent Stage """
        data = {"name": "new_name", "url1": "http://example.com/page",
                "url2": "http://example.com/otherpage", "notes": "",
                "id": 10000987}
        self.assertRaises(Exception, self.client.post,<commit_msg>fixes test<commit_after>from django.conf import settings
from django.core.urlresolvers import reverse
from mock import patch

from ab_testing_tool.pages.main_pages import STAGE_URL_TAG
from ab_testing_tool.controllers import (get_uninstalled_stages,
    stage_url)
from ab_testing_tool.models import Stage, StageUrl
from ab_testing_tool.tests.test_main_pages import (SessionTestCase,
    TEST_COURSE_ID, APIReturn, LIST_MODULES, LIST_ITEMS)


class test_stage_pages(SessionTestCase):

    def test_get_uninstalled_stages(self):
        """Tests method get_uninstalled_stages runs and returns no stages when database empty"""
        stages = get_uninstalled_stages(self.request)
        self.assertEqual(len(stages), 0)

    @patch(LIST_MODULES, return_value=APIReturn([{"id": 0}]))
    def test_get_uninstalled_stages_with_item(self, _mock1):
        """Tests method get_uninstalled_stages returns one when database has one item"""
        #TODO !! take into account course_id here!!
        Stage.objects.create(name="stage1")
        stages = get_uninstalled_stages(self.request)
        self.assertEqual(len(stages), 1)

    @patch(LIST_MODULES, return_value=APIReturn([{"id": 0}]))
    def test_get_uninstalled_stages_against_api(self, _mock1):
        """ Tests method get_uninstalled_stages returns zero when stage in database
            is also returned by the API, which means it is installed """
        stage = Stage.objects.create(name="stage1")
        mock_item = {"type": "ExternalTool",
                     "external_url": stage_url(self.request, stage.id)}
        with patch(LIST_ITEMS, return_value=APIReturn([mock_item])):
            stages = get_uninstalled_stages(self.request)
            self.assertEqual(len(stages), 0)

    def test_submit_create_stage(self):
        #TODO: change this test to several tests that Stage and StageUrls in DB,
        #covering the cases when no stage urls submited, with stage urls are submitted
        """Tests that create_stage creates a Stage object verified by DB count"""
        num_stages = Stage.objects.count()
        data = {"name": "stage", "notes": "hi"}
        response = self.client.post(reverse("submit_create_stage"), data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEquals(num_stages + 1, Stage.objects.count())

    def test_submit_create_stage_with_stageurls(self):
        """Tests that create_stage creates a Stage object and StageUrl objects
            verified by DB count"""
        num_stages = Stage.objects.count()
        num_stageurls = StageUrl.objects.count()
        data = {"name": "stage", STAGE_URL_TAG + "1": "http://example.com/page",
                STAGE_URL_TAG + "2": "http://example.com/otherpage", "notes": "hi"}
        response = self.client.post(reverse("submit_create_stage"), data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEquals(num_stages + 1, Stage.objects.count())
        self.assertEquals(num_stageurls + 2, StageUrl.objects.count())

    def test_submit_edit_stage(self):
        """ Tests that edit_stage does not change DB count but does change Stage
            attribute"""
        stage = Stage.objects.create(name="old_name",
                                             course_id=TEST_COURSE_ID)
        stage_id = stage.id
        num_stages = Stage.objects.count()
        data = {"name": "new_name", "url1": "http://example.com/page",
                "url2": "http://example.com/otherpage", "notes": "",
                "id": stage_id}
        response = self.client.post(reverse("submit_edit_stage"), data,
                                    follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertEquals(num_stages, Stage.objects.count())
        stage = Stage.objects.get(id=stage_id)
        self.assertEquals(stage.name, "new_name")

    def test_update_nonexistent_stage(self):
        """ Tests that update_stage method raises error for non-existent Stage """
        data = {"name": "new_name", "url1": "http://example.com/page",
                "url2": "http://example.com/otherpage", "notes": "",
                "id": 10000987}
        self.assertRaises(Exception, self.client.post,
{'fraction_non_alphanumeric': 0.10909935004642525, 'fraction_numerical': 0.008124419684308264, 'mean_word_length': 2.909709618874773, 'pattern_counts': {'":': 40, '<': 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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6069849', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2455, 'n_words': 572}