from unittest import TestCase

from mock import patch

from corehq.util.workbook_json.excel import get_workbook
from custom.icds.location_reassignment.const import (
    ARCHIVED_COLUMN,
    CASE_COUNT_COLUMN,
    MISSING_COLUMN,
    NEW_LOCATION_CODE_COLUMN,
    OLD_LOCATION_CODE_COLUMN,
    TRANSITION_COLUMN,
)
from custom.icds.location_reassignment.dumper import Dumper


class TestDumper(TestCase):
    domain = "test"

    @patch('custom.icds.location_reassignment.dumper.Dumper._old_location_ids_by_site_code')
    @patch('corehq.form_processor.interfaces.dbaccessors.CaseAccessors.get_case_ids_by_owners')
    @patch('corehq.apps.locations.models.SQLLocation.inactive_objects.filter')
    @patch('corehq.apps.locations.models.SQLLocation.active_objects.filter')
    def test_dump(self, mock_active_site_codes_search, mock_inactive_site_codes_search, mock_case_count,
                  mock_location_ids):
        mock_case_count.return_value = []
        transitions = {
            'awc': {
                'Move': {'112': '111'},  # new: old
                'Merge': {'115': ['113', '114']},  # new: old
                'Split': {'116': ['117', '118']},  # old: new
                'Extract': {'120': '119'}  # new: old
            },
            'supervisor': {
                'Move': {'13': '12'}
            },
            'state': {}
        }

        mock_location_ids.return_value = {
            '111': 'locationid111',
            '113': 'locationid113',
            '114': 'locationid114',
            '116': 'locationid116',
            '119': 'locationid119',
            '12': 'locationid12'
        }
        mock_active_site_codes_search.return_value.values_list.return_value = ['112', '115', '117']
        mock_inactive_site_codes_search.return_value.values_list.return_value = ['120']
        filestream = Dumper(self.domain).dump(transitions)
        workbook = get_workbook(filestream)
        awc_worksheet_rows = list(workbook.get_worksheet('awc'))
        supervisor_worksheet_rows = list(workbook.get_worksheet('supervisor'))
        self.assertEqual(
            supervisor_worksheet_rows,
            [
                {OLD_LOCATION_CODE_COLUMN: '12', TRANSITION_COLUMN: 'Move', NEW_LOCATION_CODE_COLUMN: '13',
                 MISSING_COLUMN: True, ARCHIVED_COLUMN: False, CASE_COUNT_COLUMN: 0}
            ]
        )
        self.assertEqual(
            awc_worksheet_rows,
            [
                {OLD_LOCATION_CODE_COLUMN: '111', TRANSITION_COLUMN: 'Move', NEW_LOCATION_CODE_COLUMN: '112',
                 MISSING_COLUMN: False, ARCHIVED_COLUMN: False, CASE_COUNT_COLUMN: 0},
                {OLD_LOCATION_CODE_COLUMN: '113', TRANSITION_COLUMN: 'Merge', NEW_LOCATION_CODE_COLUMN: '115',
                 MISSING_COLUMN: False, ARCHIVED_COLUMN: False, CASE_COUNT_COLUMN: 0},
                {OLD_LOCATION_CODE_COLUMN: '114', TRANSITION_COLUMN: 'Merge', NEW_LOCATION_CODE_COLUMN: '115',
                 MISSING_COLUMN: False, ARCHIVED_COLUMN: False, CASE_COUNT_COLUMN: 0},
                {OLD_LOCATION_CODE_COLUMN: '116', TRANSITION_COLUMN: 'Split', NEW_LOCATION_CODE_COLUMN: '117',
                 MISSING_COLUMN: False, ARCHIVED_COLUMN: False, CASE_COUNT_COLUMN: 0},
                {OLD_LOCATION_CODE_COLUMN: '116', TRANSITION_COLUMN: 'Split', NEW_LOCATION_CODE_COLUMN: '118',
                 MISSING_COLUMN: True, ARCHIVED_COLUMN: False, CASE_COUNT_COLUMN: 0},
                {OLD_LOCATION_CODE_COLUMN: '119', TRANSITION_COLUMN: 'Extract', NEW_LOCATION_CODE_COLUMN: '120',
                 MISSING_COLUMN: True, ARCHIVED_COLUMN: True, CASE_COUNT_COLUMN: 0}
            ]
        )

@@ -207,29 +207,93 @@
 MN,%0A    
-MISSING_COLUM
+EXTRACT_OPERATION,%0A    MERGE_OPERATION,%0A    MISSING_COLUMN,%0A    MOVE_OPERATIO
 N,%0A    N
@@ -343,24 +343,45 @@
 ODE_COLUMN,%0A
+    SPLIT_OPERATION,%0A
     TRANSITI
@@ -1091,30 +1091,38 @@
             
-'Move'
+MOVE_OPERATION
 : %7B'112': '1
@@ -1155,23 +1155,31 @@
         
-'Merge'
+MERGE_OPERATION
 : %7B'115'
@@ -1225,23 +1225,31 @@
         
-'Split'
+SPLIT_OPERATION
 : %7B'116'
@@ -1295,25 +1295,33 @@
         
-'Extract'
+EXTRACT_OPERATION
 : %7B'120'
@@ -1400,22 +1400,30 @@
         
-'Move'
+MOVE_OPERATION
 : %7B'13':
@@ -2319,38 +2319,46 @@
 NSITION_COLUMN: 
-'Move'
+MOVE_OPERATION
 , NEW_LOCATION_C
@@ -2629,14 +2629,22 @@
 MN: 
-'Move'
+MOVE_OPERATION
 , NE
@@ -2822,39 +2822,47 @@
 NSITION_COLUMN: 
-'Merge'
+MERGE_OPERATION
 , NEW_LOCATION_C
@@ -3040,15 +3040,23 @@
 MN: 
-'Merge'
+MERGE_OPERATION
 , NE
@@ -3234,39 +3234,47 @@
 NSITION_COLUMN: 
-'Split'
+SPLIT_OPERATION
 , NEW_LOCATION_C
@@ -3452,15 +3452,23 @@
 MN: 
-'Split'
+SPLIT_OPERATION
 , NE
@@ -3657,17 +3657,25 @@
 MN: 
-'Extract'
+EXTRACT_OPERATION
 , NE

Use other constants