def test_tzaware_datetime_support(self): """Test that using timezone aware datetime instances works with the MinTimeUUID/MaxTimeUUID functions. """ pk = uuid4() midpoint_utc = datetime.utcnow().replace(tzinfo=TzOffset(0)) midpoint_helsinki = midpoint_utc.astimezone(TzOffset(3)) # Assert pre-condition that we have the same logical point in time assert midpoint_utc.utctimetuple() == midpoint_helsinki.utctimetuple() assert midpoint_utc.timetuple() != midpoint_helsinki.timetuple() TimeUUIDQueryModel.create( partition=pk, time=columns.TimeUUID.from_datetime(midpoint_utc - timedelta(minutes=1)), data='1') TimeUUIDQueryModel.create( partition=pk, time=columns.TimeUUID.from_datetime(midpoint_utc), data='2') TimeUUIDQueryModel.create( partition=pk, time=columns.TimeUUID.from_datetime(midpoint_utc + timedelta(minutes=1)), data='3') assert ['1', '2'] == [ o.data for o in TimeUUIDQueryModel.filter( TimeUUIDQueryModel.partition == pk, TimeUUIDQueryModel.time <= functions.MaxTimeUUID(midpoint_utc)) ] assert ['1', '2'] == [ o.data for o in TimeUUIDQueryModel.filter( TimeUUIDQueryModel.partition == pk, TimeUUIDQueryModel.time <= functions.MaxTimeUUID(midpoint_helsinki)) ] assert ['2', '3'] == [ o.data for o in TimeUUIDQueryModel.filter( TimeUUIDQueryModel.partition == pk, TimeUUIDQueryModel.time >= functions.MinTimeUUID(midpoint_utc)) ] assert ['2', '3'] == [ o.data for o in TimeUUIDQueryModel.filter( TimeUUIDQueryModel.partition == pk, TimeUUIDQueryModel.time >= functions.MinTimeUUID(midpoint_helsinki)) ]
def test_success_case(self): """ Test that the min and max time uuid functions work as expected """ pk = uuid4() TimeUUIDQueryModel.create(partition=pk, time=uuid1(), data='1') time.sleep(0.2) TimeUUIDQueryModel.create(partition=pk, time=uuid1(), data='2') time.sleep(0.2) midpoint = datetime.utcnow() time.sleep(0.2) TimeUUIDQueryModel.create(partition=pk, time=uuid1(), data='3') time.sleep(0.2) TimeUUIDQueryModel.create(partition=pk, time=uuid1(), data='4') time.sleep(0.2) # test kwarg filtering q = TimeUUIDQueryModel.filter( partition=pk, time__lte=functions.MaxTimeUUID(midpoint)) q = [d for d in q] assert len(q) == 2 datas = [d.data for d in q] assert '1' in datas assert '2' in datas q = TimeUUIDQueryModel.filter( partition=pk, time__gte=functions.MinTimeUUID(midpoint)) assert len(q) == 2 datas = [d.data for d in q] assert '3' in datas assert '4' in datas # test query expression filtering q = TimeUUIDQueryModel.filter( TimeUUIDQueryModel.partition == pk, TimeUUIDQueryModel.time <= functions.MaxTimeUUID(midpoint)) q = [d for d in q] assert len(q) == 2 datas = [d.data for d in q] assert '1' in datas assert '2' in datas q = TimeUUIDQueryModel.filter( TimeUUIDQueryModel.partition == pk, TimeUUIDQueryModel.time >= functions.MinTimeUUID(midpoint)) assert len(q) == 2 datas = [d.data for d in q] assert '3' in datas assert '4' in datas
def test_mintimeuuid_function(self): """ Tests that queries with helper functions are generated properly """ now = datetime.now() col = columns.DateTime() col.set_column_name('time') qry = query.EqualsOperator(col, functions.MinTimeUUID(now)) assert qry.cql == '"time" = MinTimeUUID(:{})'.format( qry.value.identifier)
def test_mintimeuuid_function(self): """ Tests that queries with helper functions are generated properly """ now = datetime.now() where = WhereClause('time', EqualsOperator(), functions.MinTimeUUID(now)) where.set_context_id(5) self.assertEqual(str(where), '"time" = MinTimeUUID(%(5)s)') ctx = {} where.update_context(ctx) self.assertEqual(ctx, {'5': DateTime().to_database(now)})