diff options
Diffstat (limited to 'archaeological_operations/tests.py')
| -rw-r--r-- | archaeological_operations/tests.py | 120 | 
1 files changed, 119 insertions, 1 deletions
| diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 0d6908374..67b89ce11 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -23,6 +23,7 @@ import StringIO  import zipfile  from django.conf import settings +from django.contrib.contenttypes.models import ContentType  from django.core.files.uploadedfile import SimpleUploadedFile  from django.core.urlresolvers import reverse  from django.db.models import Q @@ -37,7 +38,8 @@ from archaeological_operations import views  from ishtar_common.models import OrganizationType, Organization, ItemKey, \      ImporterType, IshtarUser, TargetKey, ImporterModel, IshtarSiteProfile, \      Town, ImporterColumn, Person, Author, SourceType, AuthorType, \ -    DocumentTemplate, PersonType, TargetKeyGroup +    DocumentTemplate, PersonType, TargetKeyGroup, JsonDataField, \ +    JsonDataSection, ImportTarget, FormaterType  from archaeological_files.models import File, FileType  from archaeological_context_records.models import Unit @@ -453,6 +455,24 @@ class ImportOperationTest(ImportTest, TestCase):          impt.delete()          self.assertEqual(parcel_count - 3, models.Parcel.objects.count()) +    def test_json_fields(self): +        importer, form = self.init_ope_import("operations-with-json-fields.csv") +        col = ImporterColumn.objects.create(importer_type=importer, +                                            col_number=11) +        formater_type = FormaterType.objects.get( +            formater_type='IntegerFormater') +        ImportTarget.objects.create( +            column=col, target='data__autre_refs__arbitraire', +            formater_type=formater_type) +        impt = form.save(self.ishtar_user) +        impt.initialize() +        self.init_ope_targetkey(imp=impt) +        impt.importation() +        ope1 = models.Operation.objects.get(code_patriarche='4200') +        self.assertEqual(ope1.data, {u'autre_refs': {u'arbitraire': 789}}) +        ope2 = models.Operation.objects.get(code_patriarche='4201') +        self.assertEqual(ope2.data, {u'autre_refs': {u'arbitraire': 456}}) +  class ParcelTest(ImportTest, TestCase):      fixtures = OPERATION_TOWNS_FIXTURES @@ -895,6 +915,21 @@ class OperationTest(TestCase, OperationInitTest):          self.assertEqual(ope_id, 'OP2011-1')          self.assertEqual(town, self.towns[0].name) +    def test_search_vector_update(self): +        operation = self.operations[0] +        town = self.create_towns({'numero_insee': '12346', 'name': 'Daisy'})[-1] +        operation.towns.add(town) +        town = self.create_towns( +            {'numero_insee': '12347', 'name': 'Dirty old'})[-1] +        operation.towns.add(town) +        operation = models.Operation.objects.get(pk=operation.pk) +        operation.comment = u"Zardoz" +        operation.code_patriarche = u"HUIAAA5" +        operation.save() +        for key in ('old', 'op2010', 'dirty', 'daisy', "'2010'", "zardoz", +                    "huiaaa5"): +            self.assertIn(key, operation.search_vector) +      def test_cache_bulk_update(self):          if settings.USE_SPATIALITE_FOR_TESTS:              # using views - can only be tested with postgresql @@ -1015,6 +1050,53 @@ class OperationTest(TestCase, OperationInitTest):          z = zipfile.ZipFile(f)          self.assertIsNone(z.testzip()) +    def test_json(self): +        operation = self.operations[0] +        operation.data = {"groundhog": {"number": 53444, +                                        "awake_state": u"réveillée", +                                        "with_feather": "Oui"}, +                          "frog_number": 32303} +        operation.save() + +        content_type = ContentType.objects.get_for_model(operation) +        groundhog_section = JsonDataSection.objects.create( +            name="Marmotte", content_type=content_type) +        JsonDataField.objects.create(name=u"État d'éveil", +                                     key='groundhog__awake_state', +                                     content_type=content_type, +                                     section=groundhog_section) +        JsonDataField.objects.create(name=u"Avec plume", +                                     key='groundhog__with_feather', +                                     content_type=content_type, +                                     section=groundhog_section) +        JsonDataField.objects.create(name=u"Zzzzzzzz", +                                     key='groundhog__zzz', +                                     content_type=content_type, +                                     section=groundhog_section) +        JsonDataField.objects.create(name=u"Grenouille", +                                     key='frog_number', +                                     content_type=content_type) + +        c = Client() +        c.login(username=self.username, password=self.password) +        response = c.get(reverse('show-operation', kwargs={'pk': operation.pk})) +        self.assertEqual(response.status_code, 200) +        self.assertIn('class="sheet"', response.content) +        self.assertIn(u"Marmotte".encode('utf-8'), response.content) +        self.assertIn(u"État d'éveil".encode('utf-8'), response.content) +        self.assertIn(u"réveillée".encode('utf-8'), response.content) +        self.assertIn(u"Grenouille".encode('utf-8'), response.content) +        self.assertIn(u"32303".encode('utf-8'), response.content) +        self.assertNotIn(u"53444".encode('utf-8'), response.content) +        self.assertNotIn(u"Zzzzzzzz".encode('utf-8'), response.content) + +        operation.data = {} +        operation.save() +        response = c.get(reverse('show-operation', kwargs={'pk': operation.pk})) +        self.assertEqual(response.status_code, 200) +        self.assertIn('class="sheet"', response.content) +        self.assertNotIn(u"Marmotte".encode('utf-8'), response.content) +  class OperationSearchTest(TestCase, OperationInitTest):      fixtures = FILE_FIXTURES @@ -1104,6 +1186,42 @@ class OperationSearchTest(TestCase, OperationInitTest):          self.assertEqual(response.status_code, 200)          self.assertEqual(json.loads(response.content)['total'], 1) +    def test_town_search(self): +        c = Client() +        c.login(username=self.username, password=self.password) + +        data = {'numero_insee': '98989', 'name': 'base_town'} +        base_town = self.create_towns(datas=data)[-1] + +        data = {'numero_insee': '56789', 'name': 'parent_town'} +        parent_town = self.create_towns(datas=data)[-1] +        parent_town.children.add(base_town) + +        data = {'numero_insee': '01234', 'name': 'child_town'} +        child_town = self.create_towns(datas=data)[-1] +        base_town.children.add(child_town) + +        ope = self.operations[1] +        ope.towns.add(base_town) + +        # simple search +        search = {'towns': base_town.pk} +        response = c.get(reverse('get-operation'), search) +        self.assertEqual(response.status_code, 200) +        self.assertEqual(json.loads(response.content)['total'], 1) + +        # parent search +        search = {'towns': parent_town.pk} +        response = c.get(reverse('get-operation'), search) +        self.assertEqual(response.status_code, 200) +        self.assertEqual(json.loads(response.content)['total'], 1) + +        # child search +        search = {'towns': child_town.pk} +        response = c.get(reverse('get-operation'), search) +        self.assertEqual(response.status_code, 200) +        self.assertEqual(json.loads(response.content)['total'], 1) +      def testOwnSearch(self):          c = Client()          response = c.get(reverse('get-operation'), {'year': '2010'}) | 
