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())
    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)
    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})
Exemple #5
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))
    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)
        )
Exemple #7
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)
Exemple #8
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)
Exemple #9
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()
        )
Exemple #10
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)
Exemple #11
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'))
    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())
Exemple #13
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'))
Exemple #14
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()
        )