def test_build_date_time_range_query__time_across_midnight(self):
        start_time = time(23).isoformat()
        end_time = time(1).isoformat()

        self.create_meal(
            entry_datetime=datetime.combine(date.today(), time(2)).isoformat())

        self.create_meal(entry_datetime=datetime.combine(
            date.today(), time(1, 1)).isoformat())
        self.create_meal(
            entry_datetime=datetime.combine(date.today() - timedelta(
                days=3), time(1)).isoformat())
        self.create_meal(
            entry_datetime=datetime.combine(date.today() - timedelta(
                days=3), time(0, 59)).isoformat())
        self.create_meal(entry_datetime=datetime.combine(
            date.today(), time(23, 1)).isoformat())
        self.create_meal(entry_datetime=datetime.combine(
            date.today(), time(23)).isoformat())
        self.create_meal(
            entry_datetime=datetime.combine(date.today() + timedelta(
                days=3), time(22, 59)).isoformat())
        self.create_meal(entry_datetime=datetime.combine(
            date.today(), time(0, 0)).isoformat())

        query = Meal.build_date_time_range_query(self.user.id,
                                                 start_time=start_time,
                                                 end_time=end_time)

        eq_(len(query.all()), 5)
        eq_(len(Meal.query.all()), 8)
Esempio n. 2
0
    def get(self):

        current_user = g.user

        page = int(request.args.get('p', 1))

        keys = [
            'start_datetime',
            'end_datetime',
            'start_time',
            'end_time',
        ]

        query_options = {key: request.args.get(key, None) for key in keys}

        query = Meal.build_date_time_range_query(owner_user_id=current_user.id,
                                                 **query_options)

        result = query.paginate(page, per_page=10, error_out=False)
        return {'meals': result.items}
    def test_meal_query_by_datetime_and_time_range(self):
        start_datetime = parse('2018-11-25T09:00:00')
        end_datetime = parse('2018-11-27T18:00:00')

        start_time = time(11)
        end_time = time(15)

        test_date = parse('2018-11-24T09:00:00').date()
        for d in range(4):
            datetimes = [
                datetime.combine(test_date + relativedelta(days=d), time(0,
                                                                         0)),
                datetime.combine(test_date + relativedelta(days=d),
                                 time(10, 59)),
                datetime.combine(test_date + relativedelta(days=d),
                                 time(11, 0)),
                datetime.combine(test_date + relativedelta(days=d),
                                 time(12, 59)),
                datetime.combine(test_date + relativedelta(days=d),
                                 time(14, 59)),
                datetime.combine(test_date + relativedelta(days=d),
                                 time(15, 0)),
                datetime.combine(test_date + relativedelta(days=d),
                                 time(15, 59)),
                datetime.combine(test_date + relativedelta(days=d),
                                 time(23, 59))
            ]

            for idx, data_datetime in enumerate(datetimes):
                self.create_meal(text="Test Meal {}-{}".format(d + 1, idx + 1),
                                 entry_datetime=data_datetime.isoformat())

        result = Meal.build_date_time_range_query(
            self.user.id,
            start_datetime=start_datetime.isoformat(),
            end_datetime=end_datetime.isoformat(),
            start_time=start_time.isoformat(),
            end_time=end_time.isoformat())

        eq_(len(result.all()), 12)
        eq_(len(Meal.query.all()), 32)
    def test_build_date_time_range_query__datetime(self):
        start_datetime = parse('2018-11-26T23:01:52')
        end_datetime = parse('2018-11-27T23:01:52')

        self.create_meal(
            entry_datetime=parse('2018-11-26T23:59:59').isoformat())
        self.create_meal(
            entry_datetime=parse('2018-11-26T00:00:00').isoformat())
        self.create_meal(
            entry_datetime=parse('2018-11-27T00:00:00').isoformat())
        self.create_meal(
            entry_datetime=parse('2018-11-27T23:01:52').isoformat())
        self.create_meal(
            entry_datetime=parse('2018-11-27T23:59:59').isoformat())
        self.create_meal(
            entry_datetime=parse('2018-11-28T00:59:59').isoformat())

        query = Meal.build_date_time_range_query(
            self.user.id,
            start_datetime=start_datetime.isoformat(),
            end_datetime=end_datetime.isoformat())

        eq_(len(query.all()), 3)
        eq_(len(Meal.query.all()), 6)