예제 #1
0
    def test_filters(self):
        category = CategoryFactory.create()
        TextFieldFactory.create(**{'category': category, 'key': 'text'})
        NumericFieldFactory.create(**{'category': category, 'key': 'number'})
        DateFieldFactory.create(**{'category': category, 'key': 'date'})
        DateTimeFieldFactory.create(
            **{'category': category, 'key': 'datetime'})
        TimeFieldFactory.create(**{'category': category, 'key': 'time'})
        lookup = LookupFieldFactory.create(
            **{'category': category, 'key': 'lookup'})
        val_1 = LookupValueFactory.create(**{'field': lookup})
        multiple = MultipleLookupFieldFactory.create(
            **{'category': category, 'key': 'multiple'})
        mul_1 = MultipleLookupValueFactory.create(**{'field': multiple})

        rule = RuleFactory.create(**{
            'category': category,
            'min_date': datetime.datetime(2007, 12, 5, 12, 00),
            'max_date': datetime.datetime(2012, 12, 5, 15, 00),
            'constraints': {
                'text': 'blah',
                'number': {'minval': 0, 'maxval': 10},
                'date': {'minval': 0, 'maxval': 10},
                'datetime': {'minval': 0, 'maxval': 10},
                'time': {'minval': 0, 'maxval': 10},
                'lookup': [val_1.id],
                'multiple': [mul_1.id]
            }
        })

        result = filters(rule)
        self.assertEqual(result.count('<li>'), 8)
예제 #2
0
    def test_filters(self):
        category = CategoryFactory.create()
        TextFieldFactory.create(**{'category': category, 'key': 'text'})
        NumericFieldFactory.create(**{'category': category, 'key': 'number'})
        DateFieldFactory.create(**{'category': category, 'key': 'date'})
        DateTimeFieldFactory.create(**{
            'category': category,
            'key': 'datetime'
        })
        TimeFieldFactory.create(**{'category': category, 'key': 'time'})
        lookup = LookupFieldFactory.create(**{
            'category': category,
            'key': 'lookup'
        })
        val_1 = LookupValueFactory.create(**{'field': lookup})
        multiple = MultipleLookupFieldFactory.create(**{
            'category': category,
            'key': 'multiple'
        })
        mul_1 = MultipleLookupValueFactory.create(**{'field': multiple})

        rule = RuleFactory.create(
            **{
                'category': category,
                'min_date': datetime.datetime(2007, 12, 5, 12, 00),
                'max_date': datetime.datetime(2012, 12, 5, 15, 00),
                'constraints': {
                    'text': 'blah',
                    'number': {
                        'minval': 0,
                        'maxval': 10
                    },
                    'date': {
                        'minval': 0,
                        'maxval': 10
                    },
                    'datetime': {
                        'minval': 0,
                        'maxval': 10
                    },
                    'time': {
                        'minval': 0,
                        'maxval': 10
                    },
                    'lookup': [val_1.id],
                    'multiple': [mul_1.id]
                }
            })

        result = filters(rule)
        self.assertEqual(result.count('<li>'), 8)
예제 #3
0
    def test_update_expiry_field(self):
        category = CategoryFactory()
        field = DateTimeFieldFactory(**{
            'key': 'expires_at',
            'category': category
        })
        category.expiry_field = field
        category.save()

        observation = ObservationFactory(
            **{
                'project': category.project,
                'category': category,
                'expiry_field': None,
                'properties': {
                    'expires_at': '2016-09-19T15:51:32.804Z'
                }
            })

        observation.update_expiry_field()
        observation.save()

        ref = Observation.objects.get(pk=observation.id)
        self.assertEqual(
            ref.expiry_field,
            datetime.datetime(2016, 9, 19, 15, 51, 32, 804000,
                              tzinfo=pytz.utc))
예제 #4
0
    def test_except_fields(self):
        category = CategoryFactory()
        TextFieldFactory(category=category)
        NumericFieldFactory(category=category)
        DateTimeFieldFactory(category=category)
        DateFieldFactory(category=category)
        TimeFieldFactory(category=category)
        LookupFieldFactory(category=category)
        MultipleLookupFieldFactory(category=category)

        all_fields = category.fields.all().select_subclasses()

        type_names = [
            'Text', 'Numeric', 'Date and Time', 'Date', 'Time', 'Select box',
            'Multiple select'
        ]

        for type_name in type_names:
            remaining_fields = filter_fields.except_fields(
                all_fields, type_name)
            self.assertEqual(len(remaining_fields), len(type_names) - 1)
            for field in remaining_fields:
                self.assertNotEqual(field.type_name, type_name)

        date_fields = filter_fields.except_fields(
            fields=all_fields, type_names=', '.join(type_names))
        self.assertEqual(len(date_fields), 0)
예제 #5
0
    def test_only_fields(self):
        category = CategoryFactory()
        TextFieldFactory(category=category)
        NumericFieldFactory(category=category)
        DateTimeFieldFactory(category=category)
        DateFieldFactory(category=category)
        TimeFieldFactory(category=category)
        LookupFieldFactory(category=category)
        MultipleLookupFieldFactory(category=category)

        all_fields = category.fields.all()

        type_names = [
            'Text', 'Numeric', 'Date and Time', 'Date', 'Time', 'Select box',
            'Multiple select'
        ]

        for type_name in type_names:
            date_fields = filter_fields.only_fields(all_fields, type_name)
            self.assertEqual(len(date_fields), 1)
            for field in date_fields:
                self.assertEqual(field.type_name, type_name)

        date_fields = filter_fields.only_fields(all_fields,
                                                (', ').join(type_names))
        self.assertEqual(len(date_fields), len(type_names))
        for field in date_fields:
            self.assertTrue(field.type_name in type_names)
예제 #6
0
    def test_log_create(self):
        """Test when date & time field gets created."""
        log_count_init = LoggerHistory.objects.count()
        field = DateTimeFieldFactory.create(**{
            'category': self.category})

        log = LoggerHistory.objects.last()
        log_count = LoggerHistory.objects.count()

        self.assertNotEqual(log.user, {
            'id': str(self.user.id),
            'display_name': self.user.display_name})
        self.assertEqual(log.project, {
            'id': str(self.project.id),
            'name': self.project.name})
        self.assertEqual(log.usergroup, None)
        self.assertEqual(log.category, {
            'id': str(self.category.id),
            'name': self.category.name})
        self.assertEqual(log.field, {
            'id': str(field.id),
            'name': field.name,
            'type': 'DateTimeField'})
        self.assertEqual(log.location, None)
        self.assertEqual(log.observation, None)
        self.assertEqual(log.comment, None)
        self.assertEqual(log.subset, None)
        self.assertEqual(log.action, {
            'id': 'created',
            'class': 'Field'})
        self.assertEqual(log_count, log_count_init + 1)
        self.assertEqual(log.historical, None)
예제 #7
0
    def test_get_data_min_max_datetime_filter(self):
        user = UserF.create()
        project = ProjectF.create()
        category_1 = CategoryFactory(**{'project': project})
        DateTimeFieldFactory(**{
            'key': 'date',
            'category': category_1
        })
        category_2 = CategoryFactory(**{'project': project})
        DateTimeFieldFactory(**{
            'key': 'bla',
            'category': category_2
        })

        UserGroupF.create(
            add_users=[user],
            **{
                'project': project,
                'filters': {
                    category_1.id: {'date': {
                        'minval': '2014-01-01', 'maxval': '2014-06-09 00:00'}
                    }
                }
            }
        )

        for x in range(0, 5):
            ObservationFactory.create(**{
                'project': project,
                'category': category_1,
                'properties': {'date': '2014-04-09'}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_1,
                'properties': {'date': '2013-04-09'}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_2,
                'properties': {'bla': '2014-04-09'}
            })

        self.assertEqual(project.get_all_contributions(user).count(), 5)
예제 #8
0
    def test_get_data_min_max_datetime_filter(self):
        user = UserF.create()
        project = ProjectF.create(add_admins=[user])
        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.create(**{
                'project': project,
                'category': category_1,
                'properties': {'date': '2014-04-09'}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_1,
                'properties': {'date': '2013-04-09'}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_2,
                'properties': {'bla': '2014-04-09'}
            })

        view = GroupingFactory(**{'project': project})
        RuleFactory(**{
            'grouping': view,
            'category': category_1,
            'constraints': {'date': {
                'minval': '2014-01-01', 'maxval': '2014-06-09 00:00'}
            }
        })

        self.assertEqual(view.data(user).count(), 5)
예제 #9
0
 def setUp(self):
     """Set up test."""
     self.user = UserFactory.create()
     self.project = ProjectFactory.create(**{
         'creator': self.user})
     self.category = CategoryFactory.create(**{
         'creator': self.user,
         'project': self.project})
     self.datetimefield = DateTimeFieldFactory.create(**{
         'category': self.category})
예제 #10
0
    def test_update_expiry_field_no_expiry_field(self):
        category = CategoryFactory()
        DateTimeFieldFactory(**{'key': 'expires_at', 'category': category})
        observation = ObservationFactory(
            **{
                'project': category.project,
                'category': category,
                'expiry_field': None,
                'properties': {}
            })

        observation.update_expiry_field()
        observation.save()

        ref = Observation.objects.get(pk=observation.id)
        self.assertIsNone(ref.expiry_field)
예제 #11
0
    def test_update_expiry_field_empty_properties(self):
        category = CategoryFactory()
        field = DateTimeFieldFactory(**{
            'key': 'expires_at',
            'category': category
        })
        category.expiry_field = field
        category.save()

        observation = ObservationFactory(**{
            'project': category.project,
            'category': category,
            'expiry_field': None,
            'properties': None
        })

        observation.update_expiry_field()
        observation.save()

        ref = Observation.objects.get(pk=observation.id)
        self.assertEqual(ref.expiry_field, None)