Unit test for storing book events

main
Bob Mottram 2023-12-31 15:08:13 +00:00
parent ae83d7f4fc
commit eb205f9222
2 changed files with 95 additions and 16 deletions

View File

@ -40,12 +40,16 @@ def get_book_link_from_content(content: str) -> str:
return book_url
def get_book_from_post(post_json_object: {}) -> {}:
def get_book_from_post(post_json_object: {}, debug: bool) -> {}:
""" Returns a book details from the given post
"""
if 'tag' not in post_json_object:
if debug:
print('DEBUG: get_book_from_post no tag in post')
return {}
if not isinstance(post_json_object['tag'], list):
if debug:
print('DEBUG: get_book_from_post tag is not a list')
return {}
for tag_dict in post_json_object['tag']:
if 'type' not in tag_dict:
@ -63,7 +67,7 @@ def get_book_from_post(post_json_object: {}) -> {}:
if not isinstance(tag_dict['name'], str):
continue
tag_dict['name'] = tag_dict['name'].replace('@', '')
return tag_dict
return tag_dict.copy()
return {}
@ -91,7 +95,8 @@ def _get_book_image_from_post(post_json_object: {}) -> str:
def get_reading_status(post_json_object: {},
system_language: str,
languages_understood: [],
translate: {}) -> {}:
translate: {},
debug: bool) -> {}:
"""Returns any reading status from the content of a post
"""
post_obj = post_json_object
@ -102,20 +107,32 @@ def get_reading_status(post_json_object: {},
languages_understood,
"content")
if not content:
if debug:
print('DEBUG: get_reading_status no content')
return {}
book_url = get_book_link_from_content(content)
if not book_url:
if debug:
print('DEBUG: get_reading_status no book url')
return {}
if not post_obj.get('id'):
if debug:
print('DEBUG: get_reading_status no id')
return {}
if not isinstance(post_obj['id'], str):
if debug:
print('DEBUG: get_reading_status id is not a string')
return {}
# get the published date
if not post_obj.get('published'):
if debug:
print('DEBUG: get_reading_status no published')
return {}
if not isinstance(post_obj['published'], str):
if debug:
print('DEBUG: get_reading_status published is not a string')
return {}
published = post_obj['published']
if post_obj.get('updated'):
@ -123,9 +140,13 @@ def get_reading_status(post_json_object: {},
published = post_obj['updated']
if not post_obj.get('attributedTo'):
if debug:
print('DEBUG: get_reading_status no attributedTo')
return {}
actor = get_attributed_to(post_obj['attributedTo'])
if not actor:
if debug:
print('DEBUG: get_reading_status no actor')
return {}
book_image_url = _get_book_image_from_post(post_obj)
@ -152,8 +173,11 @@ def get_reading_status(post_json_object: {},
return book_dict
# get the book details from a post tag
book_dict = get_book_from_post(post_json_object)
book_dict = get_book_from_post(post_json_object, debug)
if not book_dict:
if debug:
print('DEBUG: get_reading_status no book_dict ' +
str(post_json_object))
return {}
# want to read a book
@ -225,6 +249,7 @@ def _add_book_to_reader(reader_books_json: {}, book_dict: {}) -> bool:
post_days_since_epoch = days_diff.days
reader_books_json['timeline'][post_days_since_epoch] = book_url
return True
print('test8')
return False
@ -338,8 +363,10 @@ def store_book_events(base_dir: str,
book_dict = get_reading_status(post_json_object,
system_language,
languages_understood,
translate)
translate, debug)
if not book_dict:
if debug:
print('DEBUG: no book event')
return False
reading_path = base_dir + '/accounts/reading'
if not os.path.isdir(reading_path):
@ -356,6 +383,8 @@ def store_book_events(base_dir: str,
reader_books_filename = \
readers_path + '/' + actor.replace('/', '#') + '.json'
if debug:
print('reader_books_filename: ' + reader_books_filename)
reader_books_json = {}
# get the reader from cache if possible
@ -368,6 +397,8 @@ def store_book_events(base_dir: str,
reader_books_json = load_json(reader_books_filename)
if _add_book_to_reader(reader_books_json, book_dict):
if not save_json(reader_books_json, reader_books_filename):
if debug:
print('DEBUG: unable to save reader book event')
return False
# update the cache for this reader
@ -390,6 +421,8 @@ def store_book_events(base_dir: str,
book_json = load_json(book_filename)
_add_reader_to_book(book_json, book_dict)
if not save_json(book_json, book_filename):
if debug:
print('DEBUG: unable to save book reader')
return False
_update_recent_books_list(base_dir, book_id, debug)

View File

@ -217,6 +217,7 @@ from followerSync import get_followers_sync_hash
from reading import get_book_link_from_content
from reading import get_book_from_post
from reading import get_reading_status
from reading import store_book_events
TEST_SERVER_GROUP_RUNNING = False
@ -8229,8 +8230,17 @@ def _test_dateformat():
assert dtime.tzinfo
def _test_book_link():
def _test_book_link(base_dir: str):
print('book_link')
system_language = 'en'
books_cache = {}
max_recent_books = 1000
max_cached_readers = 10
reading_dir = base_dir + '/accounts/reading'
if os.path.isdir(reading_dir):
shutil.rmtree(reading_dir, ignore_errors=False, onerror=None)
content = 'Not a link'
result = get_book_link_from_content(content)
assert result is None
@ -8281,14 +8291,14 @@ def _test_book_link():
languages_understood = []
translate = {}
book_dict = get_book_from_post(post_json_object)
book_dict = get_book_from_post(post_json_object, True)
assert book_dict
assert book_dict['name'] == title
assert book_dict['href'] == book_url
result = get_reading_status(post_json_object, 'en',
result = get_reading_status(post_json_object, system_language,
languages_understood,
translate)
translate, True)
assert result.get('type')
assert result['actor'] == actor
assert result['published'] == published
@ -8297,6 +8307,15 @@ def _test_book_link():
assert result['name'] == title
assert result['id'] == id_str
assert store_book_events(base_dir,
post_json_object,
system_language,
languages_understood,
translate, True,
max_recent_books,
books_cache,
max_cached_readers)
title = 'The Rise of the Meritocracy'
image_url = 'https://bookwyrm.instance/images/previews/covers/6735.jpg'
book_url = 'https://bookwyrm.instance/book/7235'
@ -8322,14 +8341,14 @@ def _test_book_link():
'type': 'Edition'}],
'to': ['https://www.w3.org/ns/activitystreams#Public'],
'type': 'Note'}
book_dict = get_book_from_post(post_json_object)
book_dict = get_book_from_post(post_json_object, True)
assert book_dict
assert book_dict['name'] == title
assert book_dict['href'] == book_url
result = get_reading_status(post_json_object, 'en',
result = get_reading_status(post_json_object, system_language,
languages_understood,
translate)
translate, True)
assert result.get('type')
assert result['actor'] == actor
assert result['published'] == published
@ -8338,6 +8357,15 @@ def _test_book_link():
assert result['name'] == title
assert result['id'] == id_str
assert store_book_events(base_dir,
post_json_object,
system_language,
languages_understood,
translate, True,
max_recent_books,
books_cache,
max_cached_readers)
title = 'Pirate Enlightenment, or the Real Libertalia'
image_url = 'https://bookwyrm.instance/images/previews/covers/5283.jpg'
book_url = 'https://bookwyrm.instance/book/78252'
@ -8362,12 +8390,12 @@ def _test_book_link():
'sensitive': False,
'to': ['https://www.w3.org/ns/activitystreams#Public'],
'type': 'Note'}
book_dict = get_book_from_post(post_json_object)
book_dict = get_book_from_post(post_json_object, True)
assert not book_dict
result = get_reading_status(post_json_object, 'en',
result = get_reading_status(post_json_object, system_language,
languages_understood,
translate)
translate, True)
assert result.get('type')
assert result['actor'] == actor
assert result['published'] == published
@ -8376,6 +8404,24 @@ def _test_book_link():
assert result['rating'] == rating
assert result['id'] == id_str
assert store_book_events(base_dir,
post_json_object,
system_language,
languages_understood,
translate, True,
max_recent_books,
books_cache,
max_cached_readers)
assert books_cache
assert 'reader_list' in books_cache
if len(books_cache['reader_list']) != 3:
pprint(books_cache)
print('reader_list: ' + str(books_cache['reader_list']))
assert len(books_cache['reader_list']) == 3
assert books_cache['reader_list'][2] == actor
assert books_cache['readers'].get(actor)
def run_all_tests():
base_dir = os.getcwd()
@ -8394,7 +8440,7 @@ def run_all_tests():
_test_checkbox_names()
_test_thread_functions()
_test_functions()
_test_book_link()
_test_book_link(base_dir)
_test_dateformat()
_test_is_right_to_left()
_test_format_mixed_rtl()