Example #1
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))
Example #2
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)
Example #3
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)
Example #4
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)
Example #5
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)
Example #6
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)
Example #7
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)