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_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_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_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 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 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_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 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_serialize_invalid_update(self): observation = ObservationFactory.create( **{'attributes': { 'number': 12 }}) NumericFieldFactory.create(**{ 'key': 'number', 'category': observation.category }) ContributionSerializer( observation, data={'properties': { 'attributes': { 'number': "blah" } }}, context={ 'user': self.contributor, 'project': self.project }) o = Observation.objects.get(pk=observation.id) self.assertEqual(o.attributes.get('number'), 12)
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_serialize_update(self): observation = ObservationFactory.create( **{'attributes': { 'number': 12 }}) NumericFieldFactory.create(**{ 'key': 'number', 'category': observation.category }) serializer = ContributionSerializer( observation, data={'properties': { 'attributes': { 'number': 15 } }}, context={ 'user': self.contributor, 'project': self.project }) result = serializer.data self.assertEqual( result.get('properties').get('attributes').get('number'), 15)
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_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)