<commit_before>from __future__ import unicode_literals

from flask import url_for, session
from cla_public import app
from cla_public.apps.checker.forms import (set_form_session_key,
                                           unset_form_session_key,
                                           get_form_session_key)
import unittest


class TestMultiPageForm(unittest.TestCase):

    def setUp(self):
        self.app = app.create_app('FLASK_TEST')
        self._ctx = self.app.test_request_context()
        self._ctx.push()

    def tearDown(self):
        pass

    def test_session_set_and_unset(self):
        # Test that we can set, unset and recall session state.
        set_form_session_key('test_key', 'test_value')
        self.assertEqual(get_form_session_key('test_key'), 'test_value')
        unset_form_session_key('test_key')
        with self.assertRaises(KeyError):
            get_form_session_key('test_key')

    def test_session_single_page_valid_form(self):
        # Test that POSTing to a single page will store the form data
        # against the session.
        with self.app.test_client() as c:
            uri = url_for('checker.problem')
            choice = 'debt'
            resp = c.post(uri, data={'categories': choice})
            # We expect a 302 redirect as we are shuffled to the next
            # page.
            self.assertEquals(resp.status_code, 302)
            # The session store should now contain a field called
            # <FormName>_categories where FormName is ProblemForm.
            key = '{0}_{1}'.format('ProblemForm', 'categories')
            self.assertEquals(get_form_session_key(key), choice)

    def test_session_single_page_fail_validation(self):
        # Test that POSTing to a single page -- and fail the form
        # validation requirements
        with self.app.test_client() as c:
            # POST correct details
            uri = url_for('checker.problem')
            # Make an invalid choice
            choice = None
            resp = c.post(uri, data={'categories': choice})
            # Get a 200 OK because we are re-sending the original
            # Problem page.
            self.assertEquals(resp.status_code, 200)

            # Test that the "categories" field is not stored in the
            # session.
            with self.assertRaises(KeyError):
                key = '{0}_{1}'.format('ProblemForm', 'categories')
                get_form_session_key(key)

    def test_session_multi_page_valid_forms(self):
        # Test that Form fields are stored across several pages and
        # requests in the session object.

        def post_form_page(page_name, data):
            uri = url_for(page_name)
            resp = c.post(uri, data=data)
            # We expect a 302 redirect as we are shuffled to the next
            # page.
            self.assertEquals(resp.status_code, 302)

        with self.app.test_client() as c:
            problem_data = {'categories': 'debt'}
            post_form_page('checker.problem', problem_data)

            about_data = {
                'have_partner': '1',
                'in_dispute': '0',
                'on_benefits': '0',
                'have_children': '0',
                'num_children': 0,
                'have_dependants': '0',
                'num_dependants': 0,
                'have_savings': '0',
                'own_property': '0',
                'is_employed': '0',
                'is_self_employed': '0',
                'aged_60_or_over': '0',
                }

            post_form_page('checker.about', about_data)

            def make_key(form, field):
                return '{0}_{1}'.format(form, field)

            self.assertEqual(get_form_session_key(make_key('ProblemForm', 'categories')),
                             problem_data['categories'])

            for field, value in about_data.items():
                self.assertEqual(get_form_session_key(make_key('AboutYouForm', field)),
                                 value)

<commit_msg>Fix unit tests<commit_after>from __future__ import unicode_literals
import unittest

from flask import url_for, session
from cla_public import app


def make_key(form, field):
    return '{form}.{field}'.format(form=form, field=field)


class TestMultiPageForm(unittest.TestCase):

    def setUp(self):
        self.app = app.create_app('FLASK_TEST')
        self._ctx = self.app.test_request_context()
        self._ctx.push()

    def tearDown(self):
        pass

    def test_session_single_page_valid_form(self):
        # Test that POSTing to a single page will store the form data
        # against the session.
        with self.app.test_client() as c:
            uri = url_for('checker.problem')
            choice = 'debt'
            resp = c.post(uri, data={'categories': choice})
            # We expect a 302 redirect as we are shuffled to the next
            # page.
            self.assertEquals(resp.status_code, 302)
            # The session store should now contain a field called
            # <FormName>_categories where FormName is ProblemForm.
            key = make_key('ProblemForm', 'categories')
            self.assertEquals(session.get(key), choice)

    def test_session_single_page_fail_validation(self):
        # Test that POSTing to a single page -- and fail the form
        # validation requirements
        with self.app.test_client() as c:
            # POST correct details
            uri = url_for('checker.problem')
            # Make an invalid choice
            choice = None
            resp = c.post(uri, data={'categories': choice})
            # Get a 200 OK because we are re-sending the original
            # Problem page.
            self.assertEquals(resp.status_code, 200)

            # Test that the "categories" field is not stored in the
            # session.
            key = make_key('ProblemForm', 'categories')
            self.assertFalse(key in session)

    def test_session_multi_page_valid_forms(self):
        # Test that Form fields are stored across several pages and
        # requests in the session object.

        def post_form_page(page_name, data):
            uri = url_for(page_name)
            resp = c.post(uri, data=data)
            # We expect a 302 redirect as we are shuffled to the next
            # page.
            self.assertEquals(resp.status_code, 302)

        with self.app.test_client() as c:
            problem_data = {'categories': 'debt'}
            post_form_page('checker.problem', problem_data)

            about_data = {
                'have_partner': '1',
                'in_dispute': '0',
                'on_benefits': '0',
                'have_children': '0',
                'num_children': 0,
                'have_dependants': '0',
                'num_dependants': 0,
                'have_savings': '0',
                'own_property': '0',
                'is_employed': '0',
                'is_self_employed': '0',
                'aged_60_or_over': '0',
                }

            post_form_page('checker.about', about_data)

            self.assertEqual(
                session.get(make_key('ProblemForm', 'categories')),
                problem_data['categories'])

            for field, value in about_data.items():
                self.assertEqual(
                    session.get(make_key('AboutYouForm', field)),
                    value)
{'fraction_non_alphanumeric': 0.09408747300215983, 'fraction_numerical': 0.009449244060475162, 'mean_word_length': 1.8918813427010148, '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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '236407', 'n_tokens_mistral': 2103, 'n_tokens_neox': 1969, 'n_words': 526}