Esempio n. 1
0
    def test_query_data_sort(self):
        self._publish_transportation_form()
        self._make_submissions()
        latest = Instance.objects.filter(xform=self.xform).latest('pk').pk
        oldest = Instance.objects.filter(xform=self.xform).first().pk

        data = [i.get('_id') for i in ParsedInstance.query_data(
            self.xform, sort='-pk')]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)

        # sort with a json field
        data = [i.get('_id') for i in ParsedInstance.query_data(
            self.xform, sort='-_id')]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)

        # mongo sort
        data = [i.get('_id') for i in ParsedInstance.query_data(
            self.xform, sort='{"pk": "-1"}')]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)

        # sort with a json field
        data = [i.get('_id') for i in ParsedInstance.query_data(
            self.xform, sort='{"_id": -1}')]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)
Esempio n. 2
0
    def test_query_filter_by_integer(self):
        self._publish_transportation_form()
        self._make_submissions()
        oldest = Instance.objects.filter(xform=self.xform).first().pk

        data = [
            i.get('_id') for i in ParsedInstance.query_data(
                self.xform, query='[{"_id": %s}]' % (oldest))
        ]
        self.assertEqual(len(data), 1)
        self.assertEqual(data, [oldest])

        # with fields
        data = [
            i.get('_id') for i in ParsedInstance.query_data(
                self.xform, query='{"_id": %s}' % (oldest), fields='["_id"]')
        ]
        self.assertEqual(len(data), 1)
        self.assertEqual(data, [oldest])

        # mongo $gt
        data = [
            i.get('_id')
            for i in ParsedInstance.query_data(self.xform,
                                               query='{"_id": {"$gt": %s}}' %
                                               (oldest),
                                               fields='["_id"]')
        ]
        self.assertEqual(self.xform.instances.count(), 4)
        self.assertEqual(len(data), 3)
Esempio n. 3
0
    def test_query_filter_by_datetime_field(self, mock_time):
        self._publish_transportation_form()
        now = datetime(2014, 01, 01, tzinfo=utc)
        times = [
            now, now + timedelta(seconds=1), now + timedelta(seconds=2),
            now + timedelta(seconds=3)
        ]
        mock_time.side_effect = times
        self._make_submissions()

        atime = None

        for i in self.xform.instances.all().order_by('-pk'):
            i.date_created = times.pop()
            i.save()
            if atime is None:
                atime = i.date_created.strftime(MONGO_STRFTIME)

        # mongo $gt
        data = [
            i.get('_submission_time') for i in ParsedInstance.query_data(
                self.xform,
                query='{"_submission_time": {"$lt": "%s"}}' % (atime),
                fields='["_submission_time"]')
        ]
        self.assertEqual(self.xform.instances.count(), 4)
        self.assertEqual(len(data), 3)
        self.assertNotIn(atime, data)
Esempio n. 4
0
    def test_query_filter_by_integer(self):
        self._publish_transportation_form()
        self._make_submissions()
        oldest = Instance.objects.filter(xform=self.xform).first().pk

        data = [i.get('_id') for i in ParsedInstance.query_data(
            self.xform, query='[{"_id": %s}]' % (oldest))]
        self.assertEqual(len(data), 1)
        self.assertEqual(data, [oldest])

        # with fields
        data = [i.get('_id') for i in ParsedInstance.query_data(
            self.xform, query='{"_id": %s}' % (oldest), fields='["_id"]')]
        self.assertEqual(len(data), 1)
        self.assertEqual(data, [oldest])

        # mongo $gt
        data = [i.get('_id') for i in ParsedInstance.query_data(
            self.xform, query='{"_id": {"$gt": %s}}' % (oldest),
            fields='["_id"]')]
        self.assertEqual(self.xform.instances.count(), 4)
        self.assertEqual(len(data), 3)
Esempio n. 5
0
    def test_query_data_sort(self):
        self._publish_transportation_form()
        self._make_submissions()
        latest = Instance.objects.filter(xform=self.xform).latest('pk').pk
        oldest = Instance.objects.filter(xform=self.xform).first().pk

        data = [
            i.get('_id')
            for i in ParsedInstance.query_data(self.xform, sort='-pk')
        ]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)

        # sort with a json field
        data = [
            i.get('_id')
            for i in ParsedInstance.query_data(self.xform, sort='-_id')
        ]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)

        # mongo sort
        data = [
            i.get('_id')
            for i in ParsedInstance.query_data(self.xform, sort='{"pk": "-1"}')
        ]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)

        # sort with a json field
        data = [
            i.get('_id')
            for i in ParsedInstance.query_data(self.xform, sort='{"_id": -1}')
        ]
        self.assertEqual(data[0], latest)
        self.assertEqual(data[len(data) - 1], oldest)
Esempio n. 6
0
    def test_query_filter_by_datetime_field(self, mock_time):
        self._publish_transportation_form()
        now = datetime(2014, 01, 01, tzinfo=utc)
        times = [now, now + timedelta(seconds=1), now + timedelta(seconds=2),
                 now + timedelta(seconds=3)]
        mock_time.side_effect = times
        self._make_submissions()

        atime = None

        for i in self.xform.instances.all().order_by('-pk'):
            i.date_created = times.pop()
            i.save()
            if atime is None:
                atime = i.date_created.strftime(MONGO_STRFTIME)

        # mongo $gt
        data = [i.get('_submission_time') for i in ParsedInstance.query_data(
            self.xform, query='{"_submission_time": {"$lt": "%s"}}' % (atime),
            fields='["_submission_time"]')]
        self.assertEqual(self.xform.instances.count(), 4)
        self.assertEqual(len(data), 3)
        self.assertNotIn(atime, data)