diff --git a/changedetectionio/tests/test_history_consistency.py b/changedetectionio/tests/test_history_consistency.py index 7f171c44b..f9b72048f 100644 --- a/changedetectionio/tests/test_history_consistency.py +++ b/changedetectionio/tests/test_history_consistency.py @@ -8,11 +8,19 @@ from flask import url_for from .util import live_server_setup, wait_for_all_checks from urllib.parse import urlparse, parse_qs +from ..model.Watch import WATCH_DB_JSON_FILENAME + + def test_consistent_history(client, live_server, measure_memory_usage): live_server_setup(live_server) + import glob r = range(1, 30) + # incase some exist from a previous test + for f in glob.glob(f"{live_server.app.config['DATASTORE'].datastore_path}/*/{WATCH_DB_JSON_FILENAME}", recursive=True): + os.unlink(f) + for one in r: test_url = url_for('test_endpoint', content_type="text/html", content=str(one), _external=True) res = client.post( @@ -44,11 +52,17 @@ def test_consistent_history(client, live_server, measure_memory_usage): with open(json_db_file, 'r') as f: json_obj = json.load(f) + + found_db_jsons = glob.glob(f"{live_server.app.config['DATASTORE'].datastore_path}/*/{WATCH_DB_JSON_FILENAME}", recursive=True) # assert the right amount of watches was found in the JSON - assert len(json_obj['watching']) == len(r), "Correct number of watches was found in the JSON" + assert len(found_db_jsons) == len(r), "Correct number of watches was found in the JSON" # each one should have a history.txt containing just one line - for w in json_obj['watching'].keys(): + for json_db_file in found_db_jsons: + + directory_path = os.path.dirname(json_db_file) + w = os.path.basename(directory_path) + history_txt_index_file = os.path.join(live_server.app.config['DATASTORE'].datastore_path, w, 'history.txt') assert os.path.isfile(history_txt_index_file), f"History.txt should exist where I expect it at {history_txt_index_file}" @@ -58,22 +72,21 @@ def test_consistent_history(client, live_server, measure_memory_usage): assert len(tmp_history) == 1, "History.txt should contain 1 line" # Should be two files,. the history.txt , and the snapshot.txt - files_in_watch_dir = os.listdir(os.path.join(live_server.app.config['DATASTORE'].datastore_path, - w)) + files_in_watch_dir = os.listdir(os.path.join(live_server.app.config['DATASTORE'].datastore_path,w)) # Find the snapshot one - for fname in files_in_watch_dir: - if fname != 'history.txt' and 'html' not in fname: - # contents should match what we requested as content returned from the test url - with open(os.path.join(live_server.app.config['DATASTORE'].datastore_path, w, fname), 'r') as snapshot_f: - contents = snapshot_f.read() - watch_url = json_obj['watching'][w]['url'] - u = urlparse(watch_url) - q = parse_qs(u[4]) - assert q['content'][0] == contents.strip(), f"Snapshot file {fname} should contain {q['content'][0]}" +# for fname in files_in_watch_dir: +# if fname != 'history.txt' and 'html' not in fname and fname != WATCH_DB_JSON_FILENAME: +# # contents should match what we requested as content returned from the test url +# with open(os.path.join(live_server.app.config['DATASTORE'].datastore_path, w, fname), 'r') as snapshot_f: +# contents = snapshot_f.read() +# watch_url = json_obj['watching'][w]['url'] +# u = urlparse(watch_url) +# q = parse_qs(u[4]) +# assert q['content'][0] == contents.strip(), f"Snapshot file {fname} should contain {q['content'][0]}" - assert len(files_in_watch_dir) == 3, "Should be just three files in the dir, html.br snapshot, history.txt and the extracted text snapshot" + assert len(files_in_watch_dir) == 4, "Should be just four files in the dir, html.br snapshot, history.txt, watch.json and the extracted text snapshot" json_db_file = os.path.join(live_server.app.config['DATASTORE'].datastore_path, 'url-watches.json')