Example #1
0
    def test_category_serialization(self):
        user = UserF.create()

        project = ProjectF.create(
            add_admins=[user]
        )
        CategoryFactory.create(**{'project': project})
        CategoryFactory.create(
            **{'project': project, 'status': 'inactive'}
        )
        o1 = CategoryFactory.create(**{'project': project})
        TextFieldFactory.create(**{'category': o1})
        o2 = CategoryFactory.create(**{'project': project})
        TextFieldFactory.create(**{'category': o2})

        request = self.factory.get(
            '/api/projects/%s/' % project.id)
        force_authenticate(request, user=user)
        view = SingleProject.as_view()
        response = view(request, project_id=project.id).render()

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            2,
            len(json.loads(response.content).get('categories'))
        )
Example #2
0
    def test_serialize_instance(self):
        observation = ObservationFactory.create(
            **{'attributes': {
                'key': 'value'
            }})
        TextFieldFactory.create(**{
            'key': 'key',
            'category': observation.category
        })
        serializer = ContributionSerializer(observation,
                                            context={
                                                'user': self.contributor,
                                                'project': self.project
                                            })
        result = serializer.data

        self.assertEqual(result.get('type'), 'Feature')
        self.assertEqual(result.get('geometry'),
                         json.loads(observation.location.geometry.geojson))
        self.assertEqual(
            result.get('properties').get('attributes').get('key'), 'value')
        self.assertEqual(
            result.get('category').get('id'), observation.category.id)
        self.assertEqual(
            result.get('properties').get('location').get('name'),
            observation.location.name)
        self.assertEqual(
            result.get('properties').get('location').get('description'),
            observation.location.description)
Example #3
0
    def setUp(self):
        self.admin = UserF.create()

        self.project = ProjectF(add_admins=[self.admin])
        self.category = CategoryFactory(**{
            'status': 'active',
            'project': self.project
        })

        TextFieldFactory.create(**{'key': 'key_1', 'category': self.category})
        NumericFieldFactory.create(**{
            'key': 'key_2',
            'category': self.category
        })

        self.data = {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [-0.134046077728271, 51.52439200896907]
            },
            "properties": {
                "attributes": {
                    "key_1": "value 1",
                    "key_2": 12
                },
                "category": self.category.id,
                "location": {
                    "name": "UCL",
                    "description": "UCL's main quad",
                    "private": True
                },
            }
        }
Example #4
0
    def test_update_observation_with_inactive_field(self):
        category = CategoryFactory()
        TextFieldFactory(**{'key': 'text', 'category': category})
        TextFieldFactory(
            **{
                'key': 'inactive_text',
                'category': category,
                'status': 'inactive',
                'required': True
            })
        NumericFieldFactory(**{'key': 'number', 'category': category})

        observation = ObservationFactory.create(
            **{
                'attributes': {
                    'text': 'Text',
                    'number': 12
                },
                'category': category,
                'project': category.project
            })

        updater = UserF()
        update = {'text': 'Udpated Text', 'number': 13}
        observation.update(attributes=update, updator=updater)

        self.assertEqual(observation.attributes, {
            'text': 'Udpated Text',
            'number': '13'
        })
        self.assertEqual(observation.version, 2)
Example #5
0
    def setUp(self):
        o_type = CategoryFactory.create()
        TextFieldFactory.create(**{'key': 'key', 'category': o_type})

        ObservationFactory.create_batch(5, **{
            'attributes': {'key': 'blah'},
            'category': o_type
        })
        ObservationFactory.create_batch(5, **{
            'attributes': {'key': 'blub'},
            'category': o_type
        })
Example #6
0
    def test_update_observation(self):
        category = CategoryFactory()
        TextFieldFactory(**{'key': 'text', 'category': category})
        NumericFieldFactory(**{'key': 'number', 'category': category})

        observation = ObservationFactory.create(
            **{
                'attributes': {
                    'text': 'Text',
                    'number': 12
                },
                'category': category,
                'project': category.project
            })

        updater = UserF()
        update = {'text': 'Udpated Text', 'number': 13}
        observation.update(attributes=update, updator=updater)

        # ref_observation = Observation.objects.get(pk=observation.id)
        self.assertEqual(observation.attributes, {
            'text': 'Udpated Text',
            'number': '13'
        })
        self.assertEqual(observation.version, 2)
Example #7
0
    def test_pre_save(self):
        o_type = CategoryFactory.create()
        TextFieldFactory.create(**{'key': 'key', 'category': o_type})

        lookup = LookupFieldFactory.create(**{
            'category': o_type,
            'key': 'lookup'
        })
        kermit = LookupValueFactory.create(**{
            'field': lookup,
            'name': 'Kermit'
        })
        LookupValueFactory.create(**{'field': lookup, 'name': 'Gonzo'})

        m_lookup = MultipleLookupFieldFactory.create(**{
            'category': o_type,
            'key': 'm_lookup'
        })
        m_kermit = MultipleLookupValueFactory.create(**{
            'field': m_lookup,
            'name': 'Kermit'
        })
        MultipleLookupValueFactory.create(**{
            'field': m_lookup,
            'name': 'Gonzo'
        })
        m_piggy = MultipleLookupValueFactory.create(**{
            'field': m_lookup,
            'name': 'Ms Piggy'
        })

        o = ObservationFactory.create(
            **{
                'attributes': {
                    'key': 'blah',
                    'lookup': kermit.id,
                    'm_lookup': [m_kermit.id, m_piggy.id]
                },
                'category': o_type
            })

        update_search_matches(Observation, instance=o)
        self.assertIn('Ms Piggy', o.search_matches)
        self.assertIn('Kermit', o.search_matches)
        self.assertIn('blah', o.search_matches)
Example #8
0
    def test_get_data_text_filter(self):
        project = ProjectF()
        category_1 = CategoryFactory(**{'project': project})
        TextFieldFactory(**{'key': 'text', 'category': category_1})
        category_2 = CategoryFactory(**{'project': project})
        TextFieldFactory(**{'key': 'bla', 'category': category_2})

        for x in range(0, 5):
            ObservationFactory(
                **{
                    'project': project,
                    'category': category_1,
                    'attributes': {
                        'text': 'yes ' + str(x)
                    }
                })

            ObservationFactory(
                **{
                    'project': project,
                    'category': category_1,
                    'attributes': {
                        'text': 'no ' + str(x)
                    }
                })

            ObservationFactory(
                **{
                    'project': project,
                    'category': category_2,
                    'attributes': {
                        'bla': 'yes ' + str(x)
                    }
                })

        view = GroupingFactory(**{'project': project})
        RuleFactory(**{
            'grouping': view,
            'category': category_1,
            'filters': {
                'text': 'yes'
            }
        })
        self.assertEqual(len(view.data), 5)
Example #9
0
    def setUp(self):
        self.admin = UserF.create()
        self.contributor = UserF.create()
        self.view_member = UserF.create()
        self.non_member = UserF.create()

        self.project = ProjectF(add_admins=[self.admin],
                                add_contributors=[self.contributor],
                                add_viewers=[self.view_member])
        self.category = CategoryFactory(**{
            'status': 'active',
            'project': self.project
        })

        TextFieldFactory.create(**{'key': 'key_1', 'category': self.category})
        NumericFieldFactory.create(**{
            'key': 'key_2',
            'category': self.category
        })
Example #10
0
    def test_update_invalid_observation(self):
        creator = UserF()
        location = LocationFactory()
        category = CategoryFactory()
        TextFieldFactory.create(**{'key': 'text', 'category': category})
        NumericFieldFactory.create(**{'key': 'number', 'category': category})
        data = {'text': 'Text', 'number': 12}
        observation = Observation.create(attributes=data,
                                         creator=creator,
                                         location=location,
                                         category=category,
                                         project=category.project)

        updater = UserF()
        update = {'text': 'Udpated Text', 'number': 'abc', 'version': 1}
        observation.update(attributes=update, updator=updater)

        self.assertEqual(observation.attributes, data)
        self.assertEqual(observation.version, 1)
Example #11
0
 def test_create_observation_with_inactive_field(self):
     creator = UserF()
     location = LocationFactory()
     category = CategoryFactory()
     TextFieldFactory(**{'key': 'text', 'category': category})
     TextFieldFactory(
         **{
             'key': 'inactive_text',
             'category': category,
             'status': 'inactive',
             'required': True
         })
     NumericFieldFactory(**{'key': 'number', 'category': category})
     data = {'text': 'Text', 'number': 12}
     observation = Observation.create(attributes=data,
                                      creator=creator,
                                      location=location,
                                      category=category,
                                      project=category.project)
     self.assertEqual(observation.attributes, data)
Example #12
0
 def test_create_invalid_observation(self):
     creator = UserF()
     location = LocationFactory()
     category = CategoryFactory()
     TextFieldFactory(**{'key': 'text', 'category': category})
     NumericFieldFactory(**{'key': 'number', 'category': category})
     data = {'text': 'Text', 'number': 'abc'}
     Observation.create(attributes=data,
                        creator=creator,
                        location=location,
                        category=category,
                        project=category.project)
Example #13
0
 def test_create_observation_active_default(self):
     creator = UserF()
     location = LocationFactory()
     category = CategoryFactory(**{'default_status': 'active'})
     TextFieldFactory(**{'key': 'text', 'category': category})
     NumericFieldFactory(**{'key': 'number', 'category': category})
     data = {'text': 'Text', 'number': 12}
     observation = Observation.create(attributes=data,
                                      creator=creator,
                                      location=location,
                                      category=category,
                                      project=category.project)
     self.assertEqual(observation.attributes, data)
     self.assertEqual(observation.status, 'active')
Example #14
0
    def test_update_draft_observation(self):
        creator = UserF()
        location = LocationFactory()
        category = CategoryFactory()
        TextFieldFactory.create(**{
            'key': 'text',
            'category': category,
            'required': True
        })
        NumericFieldFactory.create(**{'key': 'number', 'category': category})
        data = {'number': 12}
        observation = Observation.create(attributes=data,
                                         creator=creator,
                                         location=location,
                                         category=category,
                                         project=category.project,
                                         status='draft')

        updater = UserF()
        update = {'number': 13}
        observation.update(attributes=update, updator=updater)

        self.assertEqual(observation.attributes.get('number'), '13')
        self.assertEqual(observation.version, 1)
Example #15
0
 def test_create_invalid_observation_with_empty_number(self):
     creator = UserF()
     location = LocationFactory()
     category = CategoryFactory()
     TextFieldFactory(**{'key': 'text', 'category': category})
     NumericFieldFactory(**{
         'key': 'number',
         'required': True,
         'category': category
     })
     data = {'text': 'bla'}
     Observation.create(attributes=data,
                        creator=creator,
                        location=location,
                        category=category,
                        project=category.project)
Example #16
0
    def test_get_updated_data(self):
        project = ProjectF()
        category_1 = CategoryFactory(**{'project': project})
        TextFieldFactory(**{'key': 'text', 'category': category_1})

        observation = ObservationFactory(
            **{
                'project': project,
                'category': category_1,
                'attributes': {
                    'text': 'yes to update'
                }
            })

        for x in range(0, 5):
            ObservationFactory(
                **{
                    'project': project,
                    'category': category_1,
                    'attributes': {
                        'text': 'yes ' + str(x)
                    }
                })

            ObservationFactory(
                **{
                    'project': project,
                    'category': category_1,
                    'attributes': {
                        'text': 'no ' + str(x)
                    }
                })

        view = GroupingFactory(**{'project': project})
        RuleFactory(**{
            'grouping': view,
            'category': category_1,
            'filters': {
                'text': 'yes'
            }
        })

        updater = UserF()
        update = {'text': 'yes, this has been updated', 'version': 1}
        observation.update(attributes=update, updator=updater)
        self.assertEqual(len(view.data), 6)
Example #17
0
    def test_serialize_bulk_search(self):
        number = 20

        o_type = CategoryFactory.create()
        TextFieldFactory.create(**{
            'category': o_type,
            'key': 'field-1',
            'order': 0
        })
        TextFieldFactory.create(**{
            'category': o_type,
            'key': 'field-2',
            'order': 1
        })
        TextFieldFactory.create(**{
            'category': o_type,
            'key': 'field-3',
            'order': 2
        })

        ObservationFactory.create_batch(
            number, **{
                'attributes': {
                    'field-1': 'blah',
                    'field-2': 'blabla',
                    'field-3': 'sddsdsfdsf'
                },
                'category': o_type
            })
        observations = Observation.objects.prefetch_related(
            'location', 'category', 'creator', 'updator')

        serializer = ContributionSerializer(observations,
                                            many=True,
                                            context={
                                                'user': self.contributor,
                                                'project': self.project,
                                                'search': 'bla'
                                            })
        result = serializer.data

        self.assertEqual(result.get('type'), 'FeatureCollection')
        self.assertEqual(len(result.get('features')), number)

        for f in result.get('features'):
            self.assertIsNotNone(f.get('search_matches'))
            self.assertIsNone(f.get('search_matches').get('field-3'))