Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
UTC.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
)
self.assertIsInstance(EWSDateTime.from_string('2000-01-02T03:04:05+01:00'), EWSDateTime)
self.assertIsInstance(EWSDateTime.from_string('2000-01-02T03:04:05Z'), EWSDateTime)
# Test addition, subtraction, summertime etc
self.assertIsInstance(dt + datetime.timedelta(days=1), EWSDateTime)
self.assertIsInstance(dt - datetime.timedelta(days=1), EWSDateTime)
self.assertIsInstance(dt - EWSDateTime.now(tz=tz), datetime.timedelta)
self.assertIsInstance(EWSDateTime.now(tz=tz), EWSDateTime)
self.assertEqual(dt, EWSDateTime.from_datetime(tz.localize(datetime.datetime(2000, 1, 2, 3, 4, 5))))
self.assertEqual(dt.ewsformat(), '2000-01-02T03:04:05+01:00')
utc_tz = EWSTimeZone.timezone('UTC')
self.assertEqual(dt.astimezone(utc_tz).ewsformat(), '2000-01-02T02:04:05Z')
# Test summertime
dt = tz.localize(EWSDateTime(2000, 8, 2, 3, 4, 5))
self.assertEqual(dt.astimezone(utc_tz).ewsformat(), '2000-08-02T01:04:05Z')
# Test normalize, for completeness
self.assertEqual(tz.normalize(dt).ewsformat(), '2000-08-02T03:04:05+02:00')
self.assertEqual(utc_tz.normalize(dt, is_dst=True).ewsformat(), '2000-08-02T01:04:05Z')
# Test in-place add and subtract
dt = tz.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
dt += datetime.timedelta(days=1)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 3, 3, 4, 5)))
dt = tz.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
dt -= datetime.timedelta(days=1)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 1, 3, 4, 5)))
# Test ewsformat() failure
state=OofSettings.SCHEDULED,
start=UTC.localize(EWSDateTime(2100, 12, 1)),
end=UTC.localize(EWSDateTime(2100, 11, 1)),
).clean(version=None)
with self.assertRaises(ValueError):
# End must be in the future
OofSettings(
state=OofSettings.SCHEDULED,
start=UTC.localize(EWSDateTime(2000, 11, 1)),
end=UTC.localize(EWSDateTime(2000, 12, 1)),
).clean(version=None)
with self.assertRaises(ValueError):
# Must have an internal and external reply
OofSettings(
state=OofSettings.SCHEDULED,
start=UTC.localize(EWSDateTime(2100, 11, 1)),
end=UTC.localize(EWSDateTime(2100, 12, 1)),
).clean(version=None)
# Test summertime
dt = tz.localize(EWSDateTime(2000, 8, 2, 3, 4, 5))
self.assertEqual(dt.astimezone(utc_tz).ewsformat(), '2000-08-02T01:04:05Z')
# Test normalize, for completeness
self.assertEqual(tz.normalize(dt).ewsformat(), '2000-08-02T03:04:05+02:00')
self.assertEqual(utc_tz.normalize(dt, is_dst=True).ewsformat(), '2000-08-02T01:04:05Z')
# Test in-place add and subtract
dt = tz.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
dt += datetime.timedelta(days=1)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 3, 3, 4, 5)))
dt = tz.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
dt -= datetime.timedelta(days=1)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 1, 3, 4, 5)))
# Test ewsformat() failure
dt = EWSDateTime(2000, 1, 2, 3, 4, 5)
with self.assertRaises(ValueError):
dt.ewsformat()
# Test wrong tzinfo type
with self.assertRaises(ValueError):
EWSDateTime(2000, 1, 2, 3, 4, 5, tzinfo=pytz.utc)
with self.assertRaises(ValueError):
EWSDateTime.from_datetime(EWSDateTime(2000, 1, 2, 3, 4, 5))
# Needs a start and end
OofSettings(
state=OofSettings.SCHEDULED,
).clean(version=None)
with self.assertRaises(ValueError):
# Start must be before end
OofSettings(
state=OofSettings.SCHEDULED,
start=UTC.localize(EWSDateTime(2100, 12, 1)),
end=UTC.localize(EWSDateTime(2100, 11, 1)),
).clean(version=None)
with self.assertRaises(ValueError):
# End must be in the future
OofSettings(
state=OofSettings.SCHEDULED,
start=UTC.localize(EWSDateTime(2000, 11, 1)),
end=UTC.localize(EWSDateTime(2000, 12, 1)),
).clean(version=None)
with self.assertRaises(ValueError):
# Must have an internal and external reply
OofSettings(
state=OofSettings.SCHEDULED,
start=UTC.localize(EWSDateTime(2100, 11, 1)),
end=UTC.localize(EWSDateTime(2100, 12, 1)),
).clean(version=None)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 3, 3, 4, 5)))
dt = tz.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
dt -= datetime.timedelta(days=1)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 1, 3, 4, 5)))
# Test ewsformat() failure
dt = EWSDateTime(2000, 1, 2, 3, 4, 5)
with self.assertRaises(ValueError):
dt.ewsformat()
# Test wrong tzinfo type
with self.assertRaises(ValueError):
EWSDateTime(2000, 1, 2, 3, 4, 5, tzinfo=pytz.utc)
with self.assertRaises(ValueError):
EWSDateTime.from_datetime(EWSDateTime(2000, 1, 2, 3, 4, 5))
self.assertEqual(str(dt), '2000-01-02 03:04:05+01:00')
self.assertEqual(
repr(dt),
"EWSDateTime(2000, 1, 2, 3, 4, 5, tzinfo=)"
)
# Test from_string
with self.assertRaises(NaiveDateTimeNotAllowed):
EWSDateTime.from_string('2000-01-02T03:04:05')
self.assertEqual(
EWSDateTime.from_string('2000-01-02T03:04:05+01:00'),
UTC.localize(EWSDateTime(2000, 1, 2, 2, 4, 5))
)
self.assertEqual(
EWSDateTime.from_string('2000-01-02T03:04:05Z'),
UTC.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
)
self.assertIsInstance(EWSDateTime.from_string('2000-01-02T03:04:05+01:00'), EWSDateTime)
self.assertIsInstance(EWSDateTime.from_string('2000-01-02T03:04:05Z'), EWSDateTime)
# Test addition, subtraction, summertime etc
self.assertIsInstance(dt + datetime.timedelta(days=1), EWSDateTime)
self.assertIsInstance(dt - datetime.timedelta(days=1), EWSDateTime)
self.assertIsInstance(dt - EWSDateTime.now(tz=tz), datetime.timedelta)
self.assertIsInstance(EWSDateTime.now(tz=tz), EWSDateTime)
self.assertEqual(dt, EWSDateTime.from_datetime(tz.localize(datetime.datetime(2000, 1, 2, 3, 4, 5))))
self.assertEqual(dt.ewsformat(), '2000-01-02T03:04:05+01:00')
utc_tz = EWSTimeZone.timezone('UTC')
self.assertEqual(dt.astimezone(utc_tz).ewsformat(), '2000-01-02T02:04:05Z')
# Test summertime
dt = tz.localize(EWSDateTime(2000, 8, 2, 3, 4, 5))
self.assertEqual(dt.astimezone(utc_tz).ewsformat(), '2000-08-02T01:04:05Z')
def test_sessionpool(self):
# First, empty the calendar
start = self.account.default_timezone.localize(EWSDateTime(2011, 10, 12, 8))
end = self.account.default_timezone.localize(EWSDateTime(2011, 10, 12, 10))
self.account.calendar.filter(start__lt=end, end__gt=start, categories__contains=self.categories).delete()
items = []
for i in range(75):
subject = 'Test Subject %s' % i
item = CalendarItem(
start=start,
end=end,
subject=subject,
categories=self.categories,
)
items.append(item)
return_ids = self.account.calendar.bulk_create(items=items)
self.assertEqual(len(return_ids), len(items))
ids = self.account.calendar.filter(start__lt=end, end__gt=start, categories__contains=self.categories) \
.values_list('id', 'changekey')
self.assertEqual(dt.ewsformat(), '2000-01-02T03:04:05+01:00')
utc_tz = EWSTimeZone.timezone('UTC')
self.assertEqual(dt.astimezone(utc_tz).ewsformat(), '2000-01-02T02:04:05Z')
# Test summertime
dt = tz.localize(EWSDateTime(2000, 8, 2, 3, 4, 5))
self.assertEqual(dt.astimezone(utc_tz).ewsformat(), '2000-08-02T01:04:05Z')
# Test normalize, for completeness
self.assertEqual(tz.normalize(dt).ewsformat(), '2000-08-02T03:04:05+02:00')
self.assertEqual(utc_tz.normalize(dt, is_dst=True).ewsformat(), '2000-08-02T01:04:05Z')
# Test in-place add and subtract
dt = tz.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
dt += datetime.timedelta(days=1)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 3, 3, 4, 5)))
dt = tz.localize(EWSDateTime(2000, 1, 2, 3, 4, 5))
dt -= datetime.timedelta(days=1)
self.assertIsInstance(dt, EWSDateTime)
self.assertEqual(dt, tz.localize(EWSDateTime(2000, 1, 1, 3, 4, 5)))
# Test ewsformat() failure
dt = EWSDateTime(2000, 1, 2, 3, 4, 5)
with self.assertRaises(ValueError):
dt.ewsformat()
# Test wrong tzinfo type
with self.assertRaises(ValueError):
EWSDateTime(2000, 1, 2, 3, 4, 5, tzinfo=pytz.utc)
with self.assertRaises(ValueError):
EWSDateTime.from_datetime(EWSDateTime(2000, 1, 2, 3, 4, 5))
def generate_items(count):
start = tz.localize(EWSDateTime(2000, 3, 1, 8, 30, 0))
end = tz.localize(EWSDateTime(2000, 3, 1, 9, 15, 0))
tpl_item = CalendarItem(
start=start,
end=end,
body='This is a performance optimization test of server %s intended to find the optimal batch size and '
'concurrent connection pool size of this server.' % config.protocol.server,
location="It's safe to delete this",
categories=categories,
)
for j in range(count):
item = copy.copy(tpl_item)
item.subject = 'Performance optimization test %s by exchangelib' % j,
yield item
t2 = datetime.now()
services.CreateItem.CHUNKSIZE = cbs
services.DeleteItem.CHUNKSIZE = dbs
config.protocol.poolsize = ps
print(('Config: batch %s/%s pool %s' % (cbs, dbs, ps)))
ids = cal.bulk_create(items=calitems)
t3 = datetime.now()
delta = t3 - t2
avg_create = avg(delta, n)
print(('Time to create %s items: %s (%s / sec)' % (len(ids), delta, avg_create)))
start = tz.localize(EWSDateTime(year, month, day, 0, 0, 0))
end = tz.localize(EWSDateTime(year, month, day, 23, 59, 59))
ids = cal.filter(start__le=end, end__gt=start, categories__contains=categories)
t4 = datetime.now()
delta = t4 - t3
avg_fetch = avg(delta, n)
print(('Time to fetch %s items: %s (%s / sec)' % (len(ids), delta, avg_fetch)))
result = account.bulk_delete(ids)
for stat, msg in result:
if not stat:
print(('ERROR: %s' % msg))
t5 = datetime.now()
delta = t5 - t4
avg_delete = avg(delta, n)
print(('Time to delete %s items: %s (%s / sec)' % (len(result), delta, avg_delete)))
total = t5 - t0
print(('Total time: %s' % total))