import datetime
from collections import namedtuple

import pytz
from django.test import SimpleTestCase
from mock import patch, MagicMock

from corehq.apps.export.filter_builders import ESFormExportFilterBuilder
from corehq.apps.export.filters import FormSubmittedByFilter
from corehq.apps.export.forms import FilterFormESExportDownloadForm, DashboardFeedFilterForm


DomainObject = namedtuple('DomainObject', ['uses_locations', 'name'])


@patch('corehq.apps.export.forms.Group', new=MagicMock(get_reporting_groups=lambda x: []))
class TestDashboardFeedFilterForm(SimpleTestCase):

    def test_good_data(self):
        data = {
            'type_or_group': 'group',
            'date_range': 'range',
            'start_date': '1992-01-30',
            'end_date': '2016-10-01',
        }
        form = DashboardFeedFilterForm(DomainObject([], 'my domain'), data=data)
        self.assertTrue(form.is_valid())

    def test_missing_fields(self):
        data = {
            'type_or_group': 'group',
            'date_range': 'range',
            'start_date': '1992-01-30',
        }
        form = DashboardFeedFilterForm(DomainObject([], 'my domain'), data=data)
        self.assertFalse(form.is_valid())

    def test_bad_dates(self):
        data = {
            'type_or_group': 'group',
            'date_range': 'range',
            'start_date': '1992-01-30',
            'end_date': 'banana',
        }
        form = DashboardFeedFilterForm(DomainObject([], 'my domain'), data=data)
        self.assertFalse(form.is_valid())


@patch('corehq.apps.reports.util.get_first_form_submission_received', lambda x: datetime.datetime(2015, 1, 1))
@patch('corehq.apps.export.forms.Group', new=MagicMock(
    get_reporting_groups=lambda x: [MagicMock(_id='some_group_id', name='my group')]
))
class TestFilterFormESExportDownloadForm(SimpleTestCase):

    def setUp(self):
        self.project = DomainObject(False, "foo")

    def test_get_datespan_filter(self):

        form_data = {'date_range': '2015-06-25 to 2016-02-19'}
        form = FilterFormESExportDownloadForm(self.project, pytz.utc, form_data)
        self.assertTrue(form.is_valid())

        filter_builder = ESFormExportFilterBuilder(
            self.project.name, pytz.utc, 'group', [], [], form.cleaned_data['date_range']
        )

        datespan_filter = filter_builder._get_datespan_filter()
        self.assertEqual(datespan_filter.lt, datetime.datetime(2016, 2, 20, tzinfo=pytz.utc))
        self.assertEqual(datespan_filter.gte, datetime.datetime(2015, 6, 25, tzinfo=pytz.utc))
        self.assertEqual(datespan_filter.lte, None)
        self.assertEqual(datespan_filter.gt, None)

    def test_get_group_filter(self):
        # Confirm that FilterFormESExportDownloadForm._get_group_filter() returns
        # a filter with the correct group_id and correct base_filter.
        form_data = {
            'type_or_group': 'group',
            'group': 'some_group_id',
            'date_range': '2015-06-25 to 2016-02-19',
        }
        form = FilterFormESExportDownloadForm(self.project, pytz.utc, form_data)

        self.assertTrue(form.is_valid(), "Form had the following errors: {}".format(form.errors))

        filter_builder = ESFormExportFilterBuilder(
            self.project.name,
            pytz.utc,
            form.cleaned_data['type_or_group'],
            form.cleaned_data['group'],
            form.cleaned_data['user_types'],
            form.cleaned_data['date_range']
        )

        group_filter = filter_builder._get_group_filter()
        self.assertEqual(group_filter.group_id, 'some_group_id')
        self.assertEqual(group_filter.base_filter, FormSubmittedByFilter)

@@ -357,17 +357,37 @@
 lterForm
-%0A
+, CreateExportTagForm
 %0A%0ADomain
@@ -448,24 +448,1087 @@
  'name'%5D)%0A%0A%0A
+class TestCreateExportTagForm(SimpleTestCase):%0A%0A    def test_required_fields(self):%0A        # Confirm that form cannot be submitted without case_type when model_type is case%0A        data = %7B%0A            'model_type': 'case',%0A            'app_type': %22application%22,%0A            'application': %22fdksajhfjkqwf%22,%0A        %7D%0A        form = CreateExportTagForm(True, True, data)%0A        # Missing case_type%0A        self.assertFalse(form.is_valid())%0A%0A    def test_static_model_type(self):%0A        # Confirm that model_type is cleaned according to export permissions%0A        data = %7B%0A            'model_type': 'form',  # This should be switch to case in the cleaned_data%0A            'app_type': 'application',%0A            'application': 'asdfjwkeghrk',%0A            %22case_type%22: %22my_case_type%22%0A        %7D%0A        form = CreateExportTagForm(%0A            has_form_export_permissions=False,%0A            has_case_export_permissions=True,%0A            data=data%0A        )%0A        self.assertTrue(form.is_valid())%0A        self.assertEqual(form.cleaned_data%5B'model_type'%5D, 'case')%0A%0A%0A
 @patch('core

Add test for CreateExportTagForm