<commit_before># coding=utf-8
from __future__ import unicode_literals

import json

import time

from datetime import timedelta
from django.db import connection, reset_queries
from django.test import override_settings
from django.utils import timezone
from mock import patch, PropertyMock

from temba_client.v2.types import Boundary as TembaBoundary
from temba_client.v2.types import Field as TembaField, ObjectRef, Contact as TembaContact
from temba_client.v2.types import Run as TembaRun

from dash.test import MockClientQuery

from dash.categories.models import Category
from ureport.backend.rapidpro import FieldSyncer, BoundarySyncer, ContactSyncer, RapidProBackend
from ureport.contacts.models import ContactField, Contact
from ureport.locations.models import Boundary
from ureport.polls.models import PollResult, Poll, PollQuestion
from ureport.tests import UreportTest
from ureport.utils import json_date_to_datetime, datetime_to_json_date


class FieldSyncerTest(UreportTest):
    def setUp(self):
        super(FieldSyncerTest, self).setUp()
        self.syncer = FieldSyncer()

    def test_local_kwargs(self):
        kwargs = self.syncer.local_kwargs(self.nigeria, TembaField.create(key='foo', label='Bar', value_type='text'))

        self.assertEqual(kwargs, {'org': self.nigeria, 'key': 'foo', 'label': 'Bar', 'value_type': 'T'})

    def test_update_required(self):
        local = ContactField.objects.create(org=self.nigeria, key='foo', label='Bar', value_type='T')

        remote = TembaField.create(key='foo', label='Bar', value_type='text')
        self.assertFalse(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaField.create(key='foo', label='Baz', value_type='text')

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaField.create(key='foo', label='Bar', value_type='numeric')
        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaField.create(key='foo', label='Baz', value_type='numeric')

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))
        

class BoundarySyncerTest(UreportTest):

    def setUp(self):
        super(BoundarySyncerTest, self).setUp()
        self.syncer = BoundarySyncer()

    def test_local_kwargs(self):
        country = TembaBoundary.create(osm_id='R12345', name='Nigeria', parent=None, level=Boundary.COUNTRY_LEVEL,
                                       geometry=None, aliases=None)

        kwargs = self.syncer.local_kwargs(self.nigeria, country)
        self.assertEqual(kwargs, {'org': self.nigeria,
                                  'geometry': json.dumps(dict()),
                                  'parent': None, 'level': 0,
                                  'name': 'Nigeria', 'osm_id': 'R12345'})

        geometry = TembaBoundary.Geometry.create(type='MultiPolygon', coordinates=['COORDINATES'])
        country = TembaBoundary.create(osm_id='R12345', name='Nigeria', parent=None, level=Boundary.COUNTRY_LEVEL,
                                       geometry=geometry, aliases=None)

        kwargs = self.syncer.local_kwargs(self.nigeria, country)
        self.assertEqual(kwargs, {'org': self.nigeria,
                                  'geometry': json.dumps(dict(type=geometry.type, coordinates=geometry.coordinates)),
                                  'parent': None, 'level': 0,
                                  'name': 'Nigeria', 'osm_id': 'R12345'})

        # try creating an object from the kwargs
        country_boundary = Boundary.objects.create(**kwargs)
        parent = TembaBoundary.BoundaryRef.create(osm_id=country.osm_id, name=country.name)
        state = TembaBoundary.create(osm_id='R23456', name='Lagos', parent=parent, level=Boundary.STATE_LEVEL,
                                     geometry=geometry, aliases=None)
        kwargs = self.syncer.local_kwargs(self.nigeria, state)
        self.assertEqual(kwargs, {'org': self.nigeria, 'osm_id': "R23456", 'name': "Lagos",
                                  'level': Boundary.STATE_LEVEL, 'parent': country_boundary,
                                  'geometry':json.dumps(dict(type='MultiPolygon', coordinates=['COORDINATES']))})

    def test_update_required(self):
        geometry = TembaBoundary.Geometry.create(type='MultiPolygon', coordinates=[[1, 2]])

        local = Boundary.objects.create(org=self.nigeria, osm_id='OLD123', name='OLD', parent=None,
                                        level=Boundary.COUNTRY_LEVEL,
                                        geometry='{"type":"MultiPolygon", "coordinates":[[1, 2]]}')

        remote = TembaBoundary.create(osm_id='OLD123', name='OLD', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=geometry)

        self.assertFalse(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaBoundary.create(osm_id='OLD123', name='NEW', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=geometry)

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaBoundary.create(osm_id='OLD123', name='NEW', parent=None, level=Boundary.STATE_LEVEL,
                                      geometry=geometry)

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        geometry = TembaBoundary.Geometry.create(type='MultiPolygon', coordinates=[[1, 3]])
        remote = TembaBoundary.create(osm_id='OLD123', name='OLD', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=geometry)

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        local = Boundary.objects.create(org=self.nigeria, osm_id='SOME123', name='Location', parent=None,
                                        level=Boundary.COUNTRY_LEVEL,
                                        geometry='{}')

        remote = TembaBoundary.create(osm_id='SOME123', name='Location', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=None)

        self.assertFalse(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

    def test_delete_local(self):

        local = Boundary.objects.create(org=self.nigeria, osm_id='OLD123', name='OLD', parent=None,
                                        level=Boundary.COUNTRY_LEVEL,
                                        geometry='{"type":"MultiPolygon", "coordinates":[[1, 2]]}')

        self.syncer.delete_local(local)
        self.assertFalse(Boundary.objects.filter(pk=local.pk))


class ContactSyncerTest(UreportTest):
    def setUp(self):
        super(ContactSyncerTest, self).setUp()
        self.syncer = ContactSyncer()
        self.nigeria.set_config('reporter_group', "Ureporters")
        self.nigeria.set_config('registration_label', "Registration Date")
        self.nigeria.set_config('state_label', "State")
        self.nigeria.set_config('district_label', "LGA")
        self.nigeria.set_config('ward_label', "Ward")
        self.nigeria.set_config('occupation_label', "Activité")
        self.nigeria.set_config('born_label', "Born")
        self.nigeria.set_config('gender_label', 'Gender')
        self.nigeria.set_config('female_label', "Female")
        self.nigeria.set_config('male_label', 'Male')

        # boundaries fetched
        self.country = Boundary.objects.create(org=self.nigeria, osm_id="R-NIGERIA", name="Nigeria",<commit_msg>More coverage<commit_after># coding=utf-8
from __future__ import unicode_literals

import json

import time

from datetime import timedelta
from django.db import connection, reset_queries
from django.test import override_settings
from django.utils import timezone
from mock import patch, PropertyMock

from temba_client.v2.types import Boundary as TembaBoundary
from temba_client.v2.types import Field as TembaField, ObjectRef, Contact as TembaContact
from temba_client.v2.types import Run as TembaRun

from dash.test import MockClientQuery

from dash.categories.models import Category
from ureport.backend.rapidpro import FieldSyncer, BoundarySyncer, ContactSyncer, RapidProBackend
from ureport.contacts.models import ContactField, Contact
from ureport.locations.models import Boundary
from ureport.polls.models import PollResult, Poll, PollQuestion
from ureport.tests import UreportTest
from ureport.utils import json_date_to_datetime, datetime_to_json_date


class FieldSyncerTest(UreportTest):
    def setUp(self):
        super(FieldSyncerTest, self).setUp()
        self.syncer = FieldSyncer()

    def test_local_kwargs(self):
        kwargs = self.syncer.local_kwargs(self.nigeria, TembaField.create(key='foo', label='Bar', value_type='text'))

        self.assertEqual(kwargs, {'org': self.nigeria, 'key': 'foo', 'label': 'Bar', 'value_type': 'T'})

    def test_update_required(self):
        local = ContactField.objects.create(org=self.nigeria, key='foo', label='Bar', value_type='T')

        remote = TembaField.create(key='foo', label='Bar', value_type='text')
        self.assertFalse(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaField.create(key='foo', label='Baz', value_type='text')

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaField.create(key='foo', label='Bar', value_type='numeric')
        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaField.create(key='foo', label='Baz', value_type='numeric')

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))
        

class BoundarySyncerTest(UreportTest):

    def setUp(self):
        super(BoundarySyncerTest, self).setUp()
        self.syncer = BoundarySyncer()

    def test_local_kwargs(self):
        country = TembaBoundary.create(osm_id='R12345', name='Nigeria', parent=None, level=Boundary.COUNTRY_LEVEL,
                                       geometry=None, aliases=None)

        kwargs = self.syncer.local_kwargs(self.nigeria, country)
        self.assertEqual(kwargs, {'org': self.nigeria,
                                  'geometry': json.dumps(dict()),
                                  'parent': None, 'level': 0,
                                  'name': 'Nigeria', 'osm_id': 'R12345'})

        geometry = TembaBoundary.Geometry.create(type='MultiPolygon', coordinates=['COORDINATES'])
        country = TembaBoundary.create(osm_id='R12345', name='Nigeria', parent=None, level=Boundary.COUNTRY_LEVEL,
                                       geometry=geometry, aliases=None)

        kwargs = self.syncer.local_kwargs(self.nigeria, country)
        self.assertEqual(kwargs, {'org': self.nigeria,
                                  'geometry': json.dumps(dict(type=geometry.type, coordinates=geometry.coordinates)),
                                  'parent': None, 'level': 0,
                                  'name': 'Nigeria', 'osm_id': 'R12345'})

        # try creating an object from the kwargs
        country_boundary = Boundary.objects.create(**kwargs)
        parent = TembaBoundary.BoundaryRef.create(osm_id=country.osm_id, name=country.name)
        state = TembaBoundary.create(osm_id='R23456', name='Lagos', parent=parent, level=Boundary.STATE_LEVEL,
                                     geometry=geometry, aliases=None)
        kwargs = self.syncer.local_kwargs(self.nigeria, state)
        self.assertEqual(kwargs, {'org': self.nigeria, 'osm_id': "R23456", 'name': "Lagos",
                                  'level': Boundary.STATE_LEVEL, 'parent': country_boundary,
                                  'geometry':json.dumps(dict(type='MultiPolygon', coordinates=['COORDINATES']))})

    def test_update_required(self):
        geometry = TembaBoundary.Geometry.create(type='MultiPolygon', coordinates=[[1, 2]])

        local = Boundary.objects.create(org=self.nigeria, osm_id='OLD123', name='OLD', parent=None,
                                        level=Boundary.COUNTRY_LEVEL,
                                        geometry='{"type":"MultiPolygon", "coordinates":[[1, 2]]}')

        remote = TembaBoundary.create(osm_id='OLD123', name='OLD', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=geometry)

        self.assertFalse(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaBoundary.create(osm_id='OLD123', name='NEW', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=geometry)

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        remote = TembaBoundary.create(osm_id='OLD123', name='NEW', parent=None, level=Boundary.STATE_LEVEL,
                                      geometry=geometry)

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        geometry = TembaBoundary.Geometry.create(type='MultiPolygon', coordinates=[[1, 3]])
        remote = TembaBoundary.create(osm_id='OLD123', name='OLD', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=geometry)

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        local = Boundary.objects.create(org=self.nigeria, osm_id='SOME123', name='Location', parent=None,
                                        level=Boundary.COUNTRY_LEVEL,
                                        geometry='{}')

        remote = TembaBoundary.create(osm_id='SOME123', name='Location', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=None)

        self.assertFalse(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

        local = Boundary.objects.create(org=self.nigeria, osm_id='SOME124', name='Location', parent=None,
                                        level=Boundary.STATE_LEVEL,
                                        geometry='{}')

        remote = TembaBoundary.create(osm_id='SOME124', name='Location', parent=None, level=Boundary.COUNTRY_LEVEL,
                                      geometry=None)

        self.assertTrue(self.syncer.update_required(local, remote, self.syncer.local_kwargs(self.nigeria, remote)))

    def test_delete_local(self):

        local = Boundary.objects.create(org=self.nigeria, osm_id='OLD123', name='OLD', parent=None,
                                        level=Boundary.COUNTRY_LEVEL,
                                        geometry='{"type":"MultiPolygon", "coordinates":[[1, 2]]}')

        self.syncer.delete_local(local)
        self.assertFalse(Boundary.objects.filter(pk=local.pk))


class ContactSyncerTest(UreportTest):
    def setUp(self):
        super(ContactSyncerTest, self).setUp()
        self.syncer = ContactSyncer()
        self.nigeria.set_config('reporter_group', "Ureporters")
        self.nigeria.set_config('registration_label', "Registration Date")
        self.nigeria.set_config('state_label', "State")
        self.nigeria.set_config('district_label', "LGA")
        self.nigeria.set_config('ward_label', "Ward")
        self.nigeria.set_config('occupation_label', "Activité")
        self.nigeria.set_config('born_label', "Born")
        self.nigeria.set_config('gender_label', 'Gender')
        self.nigeria.set_config('female_label', "Female")
        self.nigeria.set_config('male_label', 'Male')

        # boundaries fetched
        self.country = Boundary.objects.create(org=self.nigeria, osm_id="R-NIGERIA", name="Nigeria",
{'fraction_non_alphanumeric': 0.12208402945362291, 'fraction_numerical': 0.00878658498855269, 'mean_word_length': 2.932360097323601, 'pattern_counts': {'":': 8, '<': 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': 47, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2478270', 'n_tokens_mistral': 4914, 'n_tokens_neox': 4664, 'n_words': 767}