Update unit tests to work with per-calendar scrape intervals.
This commit is contained in:
parent
bc024ea82d
commit
ffc720cabe
2 changed files with 30 additions and 6 deletions
|
@ -83,16 +83,20 @@ def _scrape_calendar(name: str, config: CalendarConfig, start: datetime, end: da
|
||||||
|
|
||||||
|
|
||||||
def scrape_calendar(name: str, config: CalendarConfig):
|
def scrape_calendar(name: str, config: CalendarConfig):
|
||||||
|
# Get current time in configured timezone
|
||||||
tz = get_config().tz
|
tz = get_config().tz
|
||||||
now: datetime = datetime.now(tz)
|
now: datetime = datetime.now(tz)
|
||||||
|
# Reschedule calendar scraping
|
||||||
|
cron = Timer(config.interval.totimedelta(start=now).total_seconds(),
|
||||||
|
lambda: scrape_calendar(name, config))
|
||||||
|
cron.start()
|
||||||
|
# Compute interval for which to return events
|
||||||
start_delta: Duration = get_config().start_delta
|
start_delta: Duration = get_config().start_delta
|
||||||
end_delta: Duration = get_config().end_delta
|
end_delta: Duration = get_config().end_delta
|
||||||
start: datetime = now + start_delta
|
start: datetime = now + start_delta
|
||||||
end: datetime = now + end_delta
|
end: datetime = now + end_delta
|
||||||
|
# Scrape and parse the calendar
|
||||||
_scrape_calendar(name, config, start, end)
|
_scrape_calendar(name, config, start, end)
|
||||||
cron = Timer(config.interval.totimedelta(start=now).total_seconds(),
|
|
||||||
lambda: scrape_calendar(name, config))
|
|
||||||
cron.start()
|
|
||||||
|
|
||||||
|
|
||||||
def get_calendar(name: str):
|
def get_calendar(name: str):
|
||||||
|
|
|
@ -16,7 +16,6 @@ _CONFIG_VALID = """
|
||||||
"port": 8090,
|
"port": 8090,
|
||||||
"start_delta": "-PT3H",
|
"start_delta": "-PT3H",
|
||||||
"end_delta": "P30D",
|
"end_delta": "P30D",
|
||||||
"cache": "PT15M",
|
|
||||||
"tz": "Europe/Zurich",
|
"tz": "Europe/Zurich",
|
||||||
"calendars": {
|
"calendars": {
|
||||||
"private": {
|
"private": {
|
||||||
|
@ -28,10 +27,12 @@ _CONFIG_VALID = """
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"public": {
|
"public": {
|
||||||
"url": "https://example.cloud/dav/me/public.ics"
|
"url": "https://example.cloud/dav/me/public.ics",
|
||||||
|
"interval": "P1D"
|
||||||
},
|
},
|
||||||
"confidential": {
|
"confidential": {
|
||||||
"url": "https://example.cloud/dav/me/confidential.ics",
|
"url": "https://example.cloud/dav/me/confidential.ics",
|
||||||
|
"interval": "PT5M",
|
||||||
"ca": "/etc/ssl/ca.pem",
|
"ca": "/etc/ssl/ca.pem",
|
||||||
"auth": {
|
"auth": {
|
||||||
"type": "tls",
|
"type": "tls",
|
||||||
|
@ -124,5 +125,24 @@ class ConfigTest(unittest.TestCase):
|
||||||
self.assertEqual(config.port, 8090)
|
self.assertEqual(config.port, 8090)
|
||||||
self.assertEqual(config.start_delta, Duration(hours=-3))
|
self.assertEqual(config.start_delta, Duration(hours=-3))
|
||||||
self.assertEqual(config.end_delta, Duration(days=30))
|
self.assertEqual(config.end_delta, Duration(days=30))
|
||||||
self.assertEqual(config.cache, Duration(minutes=15))
|
|
||||||
self.assertEqual(config.tz, pytz.timezone('Europe/Zurich'))
|
self.assertEqual(config.tz, pytz.timezone('Europe/Zurich'))
|
||||||
|
|
||||||
|
def test_parse_calendars(self):
|
||||||
|
config = Config(json.loads(_CONFIG_VALID))
|
||||||
|
self.assertEqual({'public', 'private', 'confidential'}, config.calendars.keys())
|
||||||
|
|
||||||
|
self.assertEqual('https://example.cloud/dav/me/public.ics', config.calendars['public'].url)
|
||||||
|
self.assertEqual(Duration(days=1), config.calendars['public'].interval)
|
||||||
|
self.assertEqual('none', config.calendars['public']._authtype)
|
||||||
|
|
||||||
|
self.assertEqual('https://example.cloud/dav/me/private.ics', config.calendars['private'].url)
|
||||||
|
self.assertEqual(Duration(minutes=15), config.calendars['private'].interval)
|
||||||
|
self.assertEqual('basic', config.calendars['private']._authtype)
|
||||||
|
self.assertEqual('Basic bWU6bXlzdXBlcnNlY3VyZXBhc3N3b3Jk',
|
||||||
|
config.calendars['private']._request_headers['Authorization'])
|
||||||
|
|
||||||
|
self.assertEqual('https://example.cloud/dav/me/confidential.ics', config.calendars['confidential'].url)
|
||||||
|
self.assertEqual(Duration(minutes=5), config.calendars['confidential'].interval)
|
||||||
|
self.assertEqual('tls', config.calendars['confidential']._authtype)
|
||||||
|
self.assertEqual('/etc/ssl/client.pem', config.calendars['confidential']._tls_keyfile)
|
||||||
|
self.assertEqual('mysupersecurepassword', config.calendars['confidential']._tls_passphrase)
|
||||||
|
|
Loading…
Reference in a new issue