def test_get_update_projects(self): # Setup everything updated_project_1 = ProjectF.create() updated_project_2 = ProjectF.create() not_updated_project = ProjectF.create() for x in range(0, 2): for project in [ updated_project_1, updated_project_2, not_updated_project ]: ObservationFactory.create(project=project) updated = ObservationFactory.create(project=updated_project_2) yesterday = datetime.utcnow().replace(tzinfo=utc) ObservationFactory.create(project=updated_project_1) updated.update(attributes={'key': 'value'}, updator=UserF.create()) # the tests command = Command() updated_projects = command.get_updated_projects(yesterday) self.assertIn(updated_project_1, updated_projects) self.assertIn(updated_project_2, updated_projects) self.assertNotIn(not_updated_project, updated_projects)
def test_get_update_stats(self): moderator = UserF.create() contributor = UserF.create() some_dude = UserF.create() project = ProjectF.create(add_admins=[moderator], add_contributors=[contributor]) suspended = ObservationFactory.create( created_at=(datetime.utcnow() - timedelta(2)).replace(tzinfo=utc), project=project, creator=contributor, status='active') updated = ObservationFactory.create(project=project, creator=contributor) approved = ObservationFactory.create(project=project, creator=contributor, status='pending') yesterday = datetime.utcnow().replace(tzinfo=utc) suspended.update(attributes=None, status='pending', updator=UserF.create()) updated.update(attributes={'key': 'value'}, updator=UserF.create()) approved.update(attributes=None, status='active', updator=moderator) for user in [moderator, contributor]: ObservationFactory.create(project=project, creator=user, status='pending') command = Command() report = command.get_updated_items(project, moderator, yesterday) to_moderate = report.get('to_moderate') self.assertEqual(len(to_moderate.get('new')), 2) self.assertEqual(len(to_moderate.get('suspended')), 1) self.assertIsNone(report.get('yours')) report = command.get_updated_items(project, contributor, yesterday) yours = report.get('yours') self.assertEqual(len(yours.get('changed')), 3) self.assertEqual(len(yours.get('approved')), 1) self.assertEqual(len(yours.get('suspended')), 1) report = command.get_updated_items(project, some_dude, yesterday) self.assertEqual(report, None)
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_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_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_daily_digest(self): moderator = UserF.create() contributor = UserF.create() UserF.create() project = ProjectF.create( add_admins=[moderator], add_contributors=[contributor] ) suspended = ObservationFactory.create( created_at=(datetime.utcnow() - timedelta(2)).replace(tzinfo=utc), project=project, creator=contributor, status='active' ) updated = ObservationFactory.create( project=project, creator=contributor ) approved = ObservationFactory.create( project=project, creator=contributor, status='pending' ) yesterday = datetime.utcnow().replace(tzinfo=utc) suspended.update( attributes=None, status='pending', updator=UserF.create() ) updated.update(attributes={'key': 'value'}, updator=UserF.create()) approved.update(attributes=None, status='active', updator=moderator) for user in [moderator, contributor]: ObservationFactory.create( project=project, creator=user, status='pending' ) command = Command() command.send_daily_digest(yesterday) self.assertEquals(len(mail.outbox), 3)
def test_get_type_name(self): image_file = ImageFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), image=get_image()) self.assertEqual(image_file.type_name, 'ImageFile')
def test_delete_with_observation(self): category = CategoryFactory.create() observation = ObservationFactory.create(**{ 'category': category }) category.delete() Category.objects.get(pk=category.id) Observation.objects.get(pk=observation.id)
def test_delete_file(self): image_file = ImageFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), image=get_image()) image_file.delete() self.assertEquals(image_file.status, 'deleted')
def test_get_type_name(self): image_file = ImageFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), image=get_image() ) self.assertEqual(image_file.type_name, 'ImageFile')
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_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 test_get_type_name(self): image_file = VideoFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), video=get_image(), youtube_link='http://example.com/1122323', swf_link='http://example.com/1122323.swf') self.assertEqual(image_file.type_name, 'VideoFile')
def test_create_image(self): image_file = MediaFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), the_file=get_image()) self.assertIsNotNone(image_file.image) self.assertEqual(image_file.type_name, 'ImageFile')
def test_delete_file(self): image_file = ImageFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), image=get_image() ) image_file.delete() self.assertEquals(image_file.status, 'deleted')
def test_create_image(self): image_file = MediaFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), the_file=get_image() ) self.assertIsNotNone(image_file.image) self.assertEqual(image_file.type_name, 'ImageFile')
def test_get_type_name(self): image_file = VideoFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), video=get_image(), youtube_link='http://example.com/1122323', swf_link='http://example.com/1122323.swf' ) self.assertEqual(image_file.type_name, 'VideoFile')
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 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_create_not_supported(self): xyz_file = StringIO() xyz = Image.new('RGBA', size=(50, 50), color=(256, 0, 0)) xyz.save(xyz_file, 'png') xyz_file.seek(0) the_file = ContentFile(xyz_file.read(), 'test.xyz') the_file.content_type = 'chemical/x-xyz' MediaFile.objects.create(name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), the_file=the_file)
def test_daily_digest(self): moderator = UserF.create() contributor = UserF.create() UserF.create() project = ProjectF.create(add_admins=[moderator], add_contributors=[contributor]) suspended = ObservationFactory.create( created_at=(datetime.utcnow() - timedelta(2)).replace(tzinfo=utc), project=project, creator=contributor, status='active') updated = ObservationFactory.create(project=project, creator=contributor) approved = ObservationFactory.create(project=project, creator=contributor, status='pending') yesterday = datetime.utcnow().replace(tzinfo=utc) suspended.update(attributes=None, status='pending', updator=UserF.create()) updated.update(attributes={'key': 'value'}, updator=UserF.create()) approved.update(attributes=None, status='active', updator=moderator) for user in [moderator, contributor]: ObservationFactory.create(project=project, creator=user, status='pending') command = Command() command.send_daily_digest(yesterday) self.assertEquals(len(mail.outbox), 3)
def test_get_update_projects(self): # Setup everything updated_project_1 = ProjectF.create() updated_project_2 = ProjectF.create() not_updated_project = ProjectF.create() for x in range(0, 2): for project in [ updated_project_1, updated_project_2, not_updated_project]: ObservationFactory.create(project=project) updated = ObservationFactory.create(project=updated_project_2) yesterday = datetime.utcnow().replace(tzinfo=utc) ObservationFactory.create(project=updated_project_1) updated.update(attributes={'key': 'value'}, updator=UserF.create()) # the tests command = Command() updated_projects = command.get_updated_projects(yesterday) self.assertIn(updated_project_1, updated_projects) self.assertIn(updated_project_2, updated_projects) self.assertNotIn(not_updated_project, updated_projects)
def test_create_not_supported(self): xyz_file = StringIO() xyz = Image.new('RGBA', size=(50, 50), color=(256, 0, 0)) xyz.save(xyz_file, 'png') xyz_file.seek(0) the_file = ContentFile(xyz_file.read(), 'test.xyz') the_file.content_type = 'chemical/x-xyz' MediaFile.objects.create( name='Test name', description='Test Description', contribution=ObservationFactory.create(), creator=UserF.create(), the_file=the_file )
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_get_update_stats(self): moderator = UserF.create() contributor = UserF.create() some_dude = UserF.create() project = ProjectF.create( add_admins=[moderator], add_contributors=[contributor] ) suspended = ObservationFactory.create( created_at=(datetime.utcnow() - timedelta(2)).replace(tzinfo=utc), project=project, creator=contributor, status='active' ) updated = ObservationFactory.create( project=project, creator=contributor ) approved = ObservationFactory.create( project=project, creator=contributor, status='pending' ) yesterday = datetime.utcnow().replace(tzinfo=utc) suspended.update( attributes=None, status='pending', updator=UserF.create() ) updated.update(attributes={'key': 'value'}, updator=UserF.create()) approved.update(attributes=None, status='active', updator=moderator) for user in [moderator, contributor]: ObservationFactory.create( project=project, creator=user, status='pending' ) command = Command() report = command.get_updated_items(project, moderator, yesterday) to_moderate = report.get('to_moderate') self.assertEqual(len(to_moderate.get('new')), 2) self.assertEqual(len(to_moderate.get('suspended')), 1) self.assertIsNone(report.get('yours')) report = command.get_updated_items(project, contributor, yesterday) yours = report.get('yours') self.assertEqual(len(yours.get('changed')), 3) self.assertEqual(len(yours.get('approved')), 1) self.assertEqual(len(yours.get('suspended')), 1) report = command.get_updated_items(project, some_dude, yesterday) self.assertEqual(report, None)
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)