Exemplo n.º 1
0
    def test_create_search_index_null_multiplelookup(self):
        category = CategoryFactory.create()
        TextFieldFactory.create(**{
            'key': 'text_1',
            'category': category,
            'order': 0
        })
        MultipleLookupFieldFactory.create(**{
            'category': category,
            'key': 'lookup',
            'order': 2
        })

        o = ObservationFactory.create(
            **{
                'properties': {
                    'text_1': 'blah, abc',
                    'lookup': None
                },
                'category': category
            })
        o.create_search_index()
        o.save()

        reference = Observation.objects.get(pk=o.id)
        self.assertEqual(
            reference.search_index.split(',').sort(),
            'blah,abc'.split(',').sort())
Exemplo n.º 2
0
    def test_get_data_multiple_lookup_filter(self):
        user = UserF.create()
        project = ProjectF.create(add_admins=[user])
        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.create(**{
                'project': project,
                'category': category_1,
                'properties': {'lookup': [lookup_1.id, lookup_3.id]}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_1,
                'properties': {'lookup': [lookup_2.id, lookup_3.id]}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_2,
                'properties': {'bla': [lookup_4.id]}
            })

        view = GroupingFactory(**{'project': project})
        RuleFactory(**{
            'grouping': view,
            'category': category_1,
            'constraints': {'lookup': [lookup_1.id, lookup_2.id]}
        })

        self.assertEqual(view.data(user).count(), 10)
Exemplo n.º 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)
    def test_log_create(self):
        """Test when multiple lookup field gets created."""
        log_count_init = LoggerHistory.objects.count()
        field = MultipleLookupFieldFactory.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': 'MultipleLookupField'})
        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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)
 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.multiplelookupfield = MultipleLookupFieldFactory.create(**{
         'category': self.category})
Exemplo n.º 8
0
    def test_get_multiplelookup_filter(self):
        field = MultipleLookupFieldFactory.create(**{'name': 'Field'})
        val_1 = MultipleLookupValueFactory.create(**{'field': field})
        val_2 = MultipleLookupValueFactory.create(**{'field': field})
        MultipleLookupValueFactory.create(**{'field': field})

        self.assertEqual(
            get_multiplelookup_filter(field, [val_1.id, val_2.id]),
            '<li>Field matches at least one of %s, %s</li>' %
            (val_1.name, val_2.name))
Exemplo n.º 9
0
    def test_get_multiplelookup_filter(self):
        field = MultipleLookupFieldFactory.create(**{'name': 'Field'})
        val_1 = MultipleLookupValueFactory.create(**{'field': field})
        val_2 = MultipleLookupValueFactory.create(**{'field': field})
        MultipleLookupValueFactory.create(**{'field': field})

        self.assertEqual(
            get_multiplelookup_filter(field, [val_1.id, val_2.id]),
            '<li>Field matches at least one of %s, %s</li>' % (
                val_1.name, val_2.name)
        )
Exemplo n.º 10
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)
Exemplo n.º 11
0
    def test_pre_save(self):
        o_type = CategoryFactory.create()
        TextFieldFactory.create(**{
            'key': 'key',
            'category': o_type,
            'order': 0
        })

        lookup = LookupFieldFactory.create(**{
            'category': o_type,
            'key': 'lookup',
            'order': 1
        })
        kermit = LookupValueFactory.create(**{
            'field': lookup,
            'name': 'Kermit'
        })
        LookupValueFactory.create(**{'field': lookup, 'name': 'Gonzo'})

        m_lookup = MultipleLookupFieldFactory.create(**{
            'category': o_type,
            'key': 'm_lookup',
            'order': 2
        })
        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(
            **{
                'properties': {
                    'key': 'blah',
                    'lookup': kermit.id,
                    'm_lookup': [m_kermit.id, m_piggy.id]
                },
                'category': o_type
            })

        pre_save_update(Observation, instance=o)
        self.assertIn('Ms Piggy', o.search_matches)
        self.assertIn('Kermit', o.search_matches)
        self.assertIn('blah', o.search_matches)
Exemplo n.º 12
0
    def test_create_search_index_null_multiplelookup(self):
        category = CategoryFactory.create()
        TextFieldFactory.create(
            **{'key': 'text_1', 'category': category, 'order': 0}
        )
        MultipleLookupFieldFactory.create(
            **{'category': category, 'key': 'lookup', 'order': 2}
        )

        o = ObservationFactory.create(**{
            'properties': {
                'text_1': 'blah, abc',
                'lookup': None
            },
            'category': category
        })
        o.create_search_index()
        o.save()

        reference = Observation.objects.get(pk=o.id)
        self.assertEqual(
            reference.search_index.split(',').sort(),
            'blah,abc'.split(',').sort()
        )
Exemplo n.º 13
0
    def test_multiple_lookup(self):
        project = ProjectFactory.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,
                'properties': {
                    'lookup': [piggy.id, kermit.id]
                }
            })
        ObservationFactory.create_batch(
            3, **{
                'project': project,
                'category': category,
                'properties': {
                    'lookup': [gonzo.id]
                }
            })

        result = project.observations.all().search('kermit')
        self.assertEqual(len(result), 3)

        for o in result:
            self.assertIn(kermit.id, o.properties.get('lookup'))

        result = project.observations.all().search('piggy')
        self.assertEqual(len(result), 3)

        for o in result:
            self.assertIn(kermit.id, o.properties.get('lookup'))
Exemplo n.º 14
0
    def test_pre_observation_save(self):
        o_type = CategoryFactory.create()
        TextFieldFactory.create(
            **{'key': 'key', 'category': o_type, 'order': 0}
        )

        lookup = LookupFieldFactory.create(
            **{'category': o_type, 'key': 'lookup', 'order': 1}
        )
        kermit = LookupValueFactory.create(**{
            'field': lookup,
            'name': 'Kermit'
        })
        LookupValueFactory.create(**{
            'field': lookup,
            'name': 'Gonzo'
        })

        m_lookup = MultipleLookupFieldFactory.create(
            **{'category': o_type, 'key': 'm_lookup', 'order': 2}
        )
        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(**{
            'properties': {
                'key': 'blah',
                'lookup': kermit.id,
                'm_lookup': [m_kermit.id, m_piggy.id]
            },
            'category': o_type
        })

        pre_save_observation_update(Observation, instance=o)
        self.assertIn('Ms Piggy', o.search_matches)
        self.assertIn('Kermit', o.search_matches)
        self.assertIn('blah', o.search_matches)
Exemplo n.º 15
0
    def test_create_search_index_multiplelookup(self):
        category = CategoryFactory.create()
        TextFieldFactory.create(**{
            'key': 'text_1',
            'category': category,
            'order': 0
        })
        lookup = MultipleLookupFieldFactory.create(**{
            'category': category,
            'key': 'lookup',
            'order': 2
        })
        gonzo = MultipleLookupValueFactory.create(**{
            'field': lookup,
            'name': 'Gonzo'
        })
        MultipleLookupValueFactory.create(**{
            'field': lookup,
            'name': 'Ms Piggy'
        })
        kermit = MultipleLookupValueFactory.create(**{
            'field': lookup,
            'name': 'Kermit'
        })

        o = ObservationFactory.create(
            **{
                'properties': {
                    'text_1': 'blah, abc',
                    'lookup': [kermit.id, gonzo.id]
                },
                'category': category
            })
        o.create_search_index()
        o.save()

        reference = Observation.objects.get(pk=o.id)
        self.assertEqual(
            reference.search_index.split(',').sort(),
            'blah,abc,gonzo,kermit'.split(',').sort())
Exemplo n.º 16
0
    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,
            'properties': {'lookup': [piggy.id, kermit.id]}
        })
        ObservationFactory.create_batch(3, **{
            'project': project,
            'category': category,
            'properties': {'lookup': [gonzo.id]}
        })

        result = project.observations.all().search('kermit')
        self.assertEqual(len(result), 3)

        for o in result:
            self.assertIn(kermit.id, o.properties.get('lookup'))

        result = project.observations.all().search('piggy')
        self.assertEqual(len(result), 3)

        for o in result:
            self.assertIn(kermit.id, o.properties.get('lookup'))
Exemplo n.º 17
0
    def test_create_search_index_multiplelookup(self):
        category = CategoryFactory.create()
        TextFieldFactory.create(
            **{'key': 'text_1', 'category': category, 'order': 0}
        )
        lookup = MultipleLookupFieldFactory.create(
            **{'category': category, 'key': 'lookup', 'order': 2}
        )
        gonzo = MultipleLookupValueFactory.create(**{
            'field': lookup,
            'name': 'Gonzo'
        })
        MultipleLookupValueFactory.create(**{
            'field': lookup,
            'name': 'Ms Piggy'
        })
        kermit = MultipleLookupValueFactory.create(**{
            'field': lookup,
            'name': 'Kermit'
        })

        o = ObservationFactory.create(**{
            'properties': {
                'text_1': 'blah, abc',
                'lookup': [kermit.id, gonzo.id]
            },
            'category': category
        })
        o.create_search_index()
        o.save()

        reference = Observation.objects.get(pk=o.id)
        self.assertEqual(
            reference.search_index.split(',').sort(),
            'blah,abc,gonzo,kermit'.split(',').sort()
        )
Exemplo n.º 18
0
    def test_get_data_multiple_lookup_filter(self):
        user = UserF.create()
        project = ProjectF.create()
        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
        })

        UserGroupF.create(
            add_users=[user],
            **{
                'project': project,
                'filters': {
                    category_1.id: {
                        'lookup': [str(lookup_1.id), str(lookup_2.id)]}
                }
            }
        )

        for x in range(0, 5):
            ObservationFactory.create(**{
                'project': project,
                'category': category_1,
                'properties': {'lookup': [lookup_1.id, lookup_3.id]}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_1,
                'properties': {'lookup': [lookup_2.id, lookup_3.id]}
            })

            ObservationFactory.create(**{
                'project': project,
                'category': category_2,
                'properties': {'bla': [lookup_4.id]}
            })

        self.assertEqual(project.get_all_contributions(user).count(), 10)