From 098f97f4d9890e215e5eee52395417d6e2df551c Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 2 Jun 2020 13:53:12 -0700 Subject: [PATCH] Add test sources for the tt* fields --- inquisitor/importer.py | 1 + sources/dummy.py | 14 ++++++++++++++ sources/example.py | 17 ----------------- sources/ttddemogen.py | 17 +++++++++++++++++ sources/ttddemoupd.py | 12 ++++++++++++ sources/ttldemogen.py | 17 +++++++++++++++++ sources/ttldemoupd.py | 13 +++++++++++++ sources/ttsdemo.py | 16 ++++++++++++++++ 8 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 sources/dummy.py delete mode 100644 sources/example.py create mode 100644 sources/ttddemogen.py create mode 100644 sources/ttddemoupd.py create mode 100644 sources/ttldemogen.py create mode 100644 sources/ttldemoupd.py create mode 100644 sources/ttsdemo.py diff --git a/inquisitor/importer.py b/inquisitor/importer.py index da6684b..2f21fd4 100644 --- a/inquisitor/importer.py +++ b/inquisitor/importer.py @@ -145,6 +145,7 @@ def populate_new(source_name, item): if 'id' not in item: raise Exception(f'Source "{source_name}" returned an item with no id') # source is auto-populated with the source name if missing + # Note: this allows sources to create items in other cells! if 'source' not in item: item['source'] = source_name # active is forced to True for new items item['active'] = True diff --git a/sources/dummy.py b/sources/dummy.py new file mode 100644 index 0000000..12aca89 --- /dev/null +++ b/sources/dummy.py @@ -0,0 +1,14 @@ +""" +Generates a dummy item. +""" +# Standard library imports +from datetime import datetime +import random + +def fetch_new(state): + item = { + 'source': "dummy", + 'id': '{:x}'.format(random.getrandbits(16 * 4)), + 'title': str(datetime.now()), + } + return [item] diff --git a/sources/example.py b/sources/example.py deleted file mode 100644 index 43dc173..0000000 --- a/sources/example.py +++ /dev/null @@ -1,17 +0,0 @@ -""" -An example itemsource that produces an item with the current date. -Fetch new items with `python inquisitor update --sources example` -""" -# Standard library imports -from datetime import date -import time - - -def fetch_new(state): - now = date.today() - item = { - 'source': "example", - 'id': '{}-{}-{}'.format(now.year, now.month, now.day), - 'title': "Today is {}-{}-{}".format(now.year, now.month, now.day), - } - return [item] diff --git a/sources/ttddemogen.py b/sources/ttddemogen.py new file mode 100644 index 0000000..c1ff527 --- /dev/null +++ b/sources/ttddemogen.py @@ -0,0 +1,17 @@ +""" +Demonstrates the behavior of the time-to-die item field. +On update, this source creates a new item with a ttd of 30 in the cell +for the source ttddemoupd. +""" +# Standard library imports +from datetime import datetime +import random + +def fetch_new(state): + item = { + 'source': "ttddemoupd", + 'id': '{:x}'.format(random.getrandbits(16 * 4)), + 'title': f"This item was created at {datetime.now()}", + 'ttd': 30, + } + return [item] diff --git a/sources/ttddemoupd.py b/sources/ttddemoupd.py new file mode 100644 index 0000000..a391ed9 --- /dev/null +++ b/sources/ttddemoupd.py @@ -0,0 +1,12 @@ +""" +Demonstrates the behavior of the time-to-die item field. +This source does not generate items. It is solely for use with the +ttddemogen source, which creats new items for it. This source can be +updated to cause a ttd check on the item created by ttddemogen. +""" +# Standard library imports +from datetime import datetime +import random + +def fetch_new(state): + return [] diff --git a/sources/ttldemogen.py b/sources/ttldemogen.py new file mode 100644 index 0000000..a7d07dc --- /dev/null +++ b/sources/ttldemogen.py @@ -0,0 +1,17 @@ +""" +Demonstrates the behavior of the time-to-live item field. +On update, this source creates a new item with a ttl of 30 in the cell +for the source ttldemoupd. +""" +# Standard library imports +from datetime import datetime +import random + +def fetch_new(state): + item = { + 'source': "ttldemoupd", + 'id': '{:x}'.format(random.getrandbits(16 * 4)), + 'title': f"This item was created at {datetime.now()}", + 'ttl': 30, + } + return [item] diff --git a/sources/ttldemoupd.py b/sources/ttldemoupd.py new file mode 100644 index 0000000..4ae3a1b --- /dev/null +++ b/sources/ttldemoupd.py @@ -0,0 +1,13 @@ +""" +Demonstrates the behavior of the time-to-live item field. +This source does not generate items. It is solely for use with the +ttldemogen source, which creats new items for it. This source can be +updated to cause a removal check on inactive items created by +ttldemogen. +""" +# Standard library imports +from datetime import datetime +import random + +def fetch_new(state): + return [] diff --git a/sources/ttsdemo.py b/sources/ttsdemo.py new file mode 100644 index 0000000..34ec489 --- /dev/null +++ b/sources/ttsdemo.py @@ -0,0 +1,16 @@ +""" +Demonstrates the behavior of the time-to-show item field. +On update, this source returns a new item with a tts of 30 seconds. +""" +# Standard library imports +from datetime import datetime +import random + +def fetch_new(state): + item = { + 'source': "ttsdemo", + 'id': '{:x}'.format(random.getrandbits(16 * 4)), + 'title': f"This item was created at {datetime.now()}", + 'tts': 30, + } + return [item]