def test_get_data(self): project = ProjectF() category_1 = CategoryFactory(**{'project': project}) category_2 = CategoryFactory(**{'project': project}) view = GroupingFactory(**{'project': project}) for x in range(0, 5): ObservationFactory(**{'project': project, 'category': category_1}) ObservationFactory(**{'project': project, 'category': category_2}) RuleFactory(**{'grouping': view, 'category': category_1}) self.assertEqual(len(view.data), 5) for observation in view.data: self.assertEqual(observation.category, category_1)
def test_get_data_min_max_date_filter(self): project = ProjectF() category_1 = CategoryFactory(**{'project': project}) DateTimeFieldFactory(**{'key': 'date', 'category': category_1}) category_2 = CategoryFactory(**{'project': project}) DateTimeFieldFactory(**{'key': 'bla', 'category': category_2}) for x in range(0, 5): ObservationFactory( **{ 'project': project, 'category': category_1, 'attributes': { 'date': '2014-04-09' } }) ObservationFactory( **{ 'project': project, 'category': category_1, 'attributes': { 'date': '2013-04-09' } }) ObservationFactory( **{ 'project': project, 'category': category_2, 'attributes': { 'bla': '2014-04-09' } }) view = GroupingFactory(**{'project': project}) RuleFactory( **{ 'grouping': view, 'category': category_1, 'filters': { 'date': { 'minval': '2014-01-01', 'maxval': '2014-06-09 00:00' } } }) self.assertEqual(len(view.data), 5)
def test_get_data_min_max_number_filter(self): project = ProjectF() category_1 = CategoryFactory(**{'project': project}) NumericFieldFactory(**{'key': 'rating', 'category': category_1}) category_2 = CategoryFactory(**{'project': project}) NumericFieldFactory(**{'key': 'bla', 'category': category_2}) for x in range(5, 11): ObservationFactory( **{ 'project': project, 'category': category_1, 'attributes': { 'rating': x } }) ObservationFactory( **{ 'project': project, 'category': category_1, 'attributes': { 'rating': x } }) ObservationFactory( **{ 'project': project, 'category': category_2, 'attributes': { 'rating': x } }) view = GroupingFactory(**{'project': project}) RuleFactory( **{ 'grouping': view, 'category': category_1, 'filters': { 'rating': { 'minval': 8, 'maxval': 10 } } }) self.assertEqual(len(view.data), 6)
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)
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 }, } }
def test_single_lookup(self): project = ProjectF.create() category = CategoryFactory.create(**{'project': project}) lookup = LookupFieldFactory.create( **{'category': category, 'key': 'lookup'} ) kermit = LookupValueFactory.create(**{ 'field': lookup, 'name': 'Kermit' }) gonzo = LookupValueFactory.create(**{ 'field': lookup, 'name': 'Gonzo' }) ObservationFactory.create_batch(3, **{ 'project': project, 'category': category, 'attributes': {'lookup': kermit.id} }) ObservationFactory.create_batch(3, **{ 'project': project, 'category': category, 'attributes': {'lookup': gonzo.id} }) result = project.observations.all().search('kermit') self.assertEqual(len(result), 3) for o in result: self.assertEqual(o.attributes.get('lookup'), str(kermit.id))
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)
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)
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)
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 })
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')) )
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')
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)
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'))
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)
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)
def test_get_created_before_and_after(self): project = ProjectF() category_1 = CategoryFactory(**{'project': project}) obs = ObservationFactory.create_batch( 5, **{ 'project': project, 'category': category_1 }) for o in obs: o.created_at = datetime(2014, 7, 23, 10, 34, 1, tzinfo=pytz.utc) o.save() obs = ObservationFactory.create_batch( 5, **{ 'project': project, 'category': category_1 }) for o in obs: o.created_at = datetime(2013, 7, 23, 10, 34, 1, tzinfo=pytz.utc) o.save() obs = ObservationFactory.create_batch( 5, **{ 'project': project, 'category': category_1 }) for o in obs: o.created_at = datetime(2012, 7, 23, 10, 34, 1, tzinfo=pytz.utc) o.save() view = GroupingFactory(**{'project': project}) RuleFactory( **{ 'grouping': view, 'category': category_1, 'min_date': datetime(2013, 1, 1, 0, 0, 0, tzinfo=pytz.utc), 'max_date': datetime(2013, 10, 1, 0, 0, 0, tzinfo=pytz.utc) }) self.assertEqual(len(view.data), 5)
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 })
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)
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)
def test_multiple_lookup(self): project = ProjectF.create() category = CategoryFactory.create(**{'project': project}) lookup = MultipleLookupFieldFactory.create( **{'category': category, 'key': 'lookup'} ) kermit = MultipleLookupValueFactory.create(**{ 'field': lookup, 'name': 'Kermit' }) gonzo = MultipleLookupValueFactory.create(**{ 'field': lookup, 'name': 'Gonzo' }) piggy = MultipleLookupValueFactory.create(**{ 'field': lookup, 'name': 'Ms Piggy' }) ObservationFactory.create_batch(3, **{ 'project': project, 'category': category, 'attributes': {'lookup': [piggy.id, kermit.id]} }) ObservationFactory.create_batch(3, **{ 'project': project, 'category': category, 'attributes': {'lookup': [gonzo.id]} }) result = project.observations.all().search('kermit') self.assertEqual(len(result), 3) for o in result: self.assertIn(kermit.id, json.loads(o.attributes.get('lookup'))) result = project.observations.all().search('piggy') self.assertEqual(len(result), 3) for o in result: self.assertIn(kermit.id, json.loads(o.attributes.get('lookup')))
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)
def test_get_data_multiple_lookup_filter(self): project = ProjectF() category_1 = CategoryFactory(**{'project': project}) lookup_field = MultipleLookupFieldFactory(**{ 'key': 'lookup', 'category': category_1 }) lookup_1 = MultipleLookupValueFactory(**{ 'name': 'Ms. Piggy', 'field': lookup_field }) lookup_2 = MultipleLookupValueFactory(**{ 'name': 'Kermit', 'field': lookup_field }) lookup_3 = MultipleLookupValueFactory(**{ 'name': 'Gonzo', 'field': lookup_field }) category_2 = CategoryFactory(**{'project': project}) lookup_field_2 = MultipleLookupFieldFactory(**{ 'key': 'bla', 'category': category_2 }) lookup_4 = MultipleLookupValueFactory(**{ 'name': 'Gonzo', 'field': lookup_field_2 }) for x in range(0, 5): ObservationFactory( **{ 'project': project, 'category': category_1, 'attributes': { 'lookup': [lookup_1.id, lookup_3.id] } }) ObservationFactory( **{ 'project': project, 'category': category_1, 'attributes': { 'lookup': [lookup_2.id, lookup_3.id] } }) ObservationFactory( **{ 'project': project, 'category': category_2, 'attributes': { 'bla': [lookup_4.id] } }) view = GroupingFactory(**{'project': project}) RuleFactory( **{ 'grouping': view, 'category': category_1, 'filters': { 'lookup': [lookup_1.id, lookup_2.id] } }) self.assertEqual(len(view.data), 10)