# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function

import os

from django.conf import settings
from django.shortcuts import reverse
from django.core.management import call_command

import test
from dataops import pandas_db
from workflow.models import Workflow


class EmailActionTracking(test.OntaskTestCase):
    fixtures = ['simple_email_action']
    filename = os.path.join(
        settings.BASE_DIR(),
        'action',
        'fixtures',
        'simple_email_action_df.sql'
    )

    trck_tokens = [
        "eyJhY3Rpb24iOjIsInRvIjoic3R1ZGVudDFAYm9ndXMuY29tIiwiY29sdW1uX2RzdCI6IkVtYWlsUmVhZF8xIiwic2VuZGVyIjoiaWRlc2lnbmVyMUBib2d1cy5jb20iLCJjb2x1bW5fdG8iOiJlbWFpbCJ9:1eBtw5:MwH1axNDQq9HpgcP6jRvp7cAFmI",
        "eyJhY3Rpb24iOjIsInRvIjoic3R1ZGVudDJAYm9ndXMuY29tIiwiY29sdW1uX2RzdCI6IkVtYWlsUmVhZF8xIiwic2VuZGVyIjoiaWRlc2lnbmVyMUBib2d1cy5jb20iLCJjb2x1bW5fdG8iOiJlbWFpbCJ9:1eBtw5:FFS1EXjdgJjc37ZVOcW22aIegR4",
        "eyJhY3Rpb24iOjIsInRvIjoic3R1ZGVudDNAYm9ndXMuY29tIiwiY29sdW1uX2RzdCI6IkVtYWlsUmVhZF8xIiwic2VuZGVyIjoiaWRlc2lnbmVyMUBib2d1cy5jb20iLCJjb2x1bW5fdG8iOiJlbWFpbCJ9:1eBtw5:V0KhNWbcY3YPTfJXRagPaeJae4M"
    ]

    wflow_name = 'wflow1'
    wflow_desc = 'description text for workflow 1'
    wflow_empty = 'The workflow does not have data'

    @classmethod
    def setUpClass(cls):
        super(EmailActionTracking, cls).setUpClass()
        pandas_db.pg_restore_table(cls.filename)

    def tearDown(self):
        pandas_db.delete_all_tables()
        super(EmailActionTracking, self).tearDown()

    # Test that tracking hits are properly stored.
    def test_tracking(self):
        # Repeat the checks two times to test if they are accumulating
        for idx in range(1, 3):
            # Iterate over the tracking items
            for trck in self.trck_tokens:
                self.client.get(reverse('trck') + '?v=' + trck)

            # Get the workflow and the data frame
            workflow = Workflow.objects.get(name=self.wflow_name)
            df = pandas_db.load_from_db(workflow.id)

            # Check that the results have been updated in the DB (to 1)
            for uemail in [x[1] for x in test.user_info
                           if x[1].startswith('student')]:
                self.assertEqual(
                    int(df.loc[df['email'] == uemail, 'EmailRead_1'].values[0]),
                    idx
                )

[((18, 8, 18, 27), 'django.conf.settings.BASE_DIR', 'settings.BASE_DIR', ({}, {}), '()', False, 'from django.conf import settings\n'), ((37, 8, 37, 48), 'dataops.pandas_db.pg_restore_table', 'pandas_db.pg_restore_table', ({(37, 35, 37, 47): 'cls.filename'}, {}), '(cls.filename)', False, 'from dataops import pandas_db\n'), ((40, 8, 40, 37), 'dataops.pandas_db.delete_all_tables', 'pandas_db.delete_all_tables', ({}, {}), '()', False, 'from dataops import pandas_db\n'), ((52, 23, 52, 65), 'workflow.models.Workflow.objects.get', 'Workflow.objects.get', (), '', False, 'from workflow.models import Workflow\n'), ((53, 17, 53, 52), 'dataops.pandas_db.load_from_db', 'pandas_db.load_from_db', ({(53, 40, 53, 51): 'workflow.id'}, {}), '(workflow.id)', False, 'from dataops import pandas_db\n'), ((49, 32, 49, 47), 'django.shortcuts.reverse', 'reverse', ({(49, 40, 49, 46): '"""trck"""'}, {}), "('trck')", False, 'from django.shortcuts import reverse\n')]