From ba40084772a565f36af1290070e6c9bba14fb9e7 Mon Sep 17 00:00:00 2001 From: Justin Warren Date: Sat, 2 Nov 2024 09:23:08 +1100 Subject: [PATCH] Moved data files to pytest fixtures. (#69) Datafiles now load from relative path. Fixes: #27 --- tests/conftest.py | 33 +++++++++++ tests/fixtures/__init__.py | 0 tests/{ => fixtures}/data-mastodon.json | 0 tests/{ => fixtures}/data-noop-01.csv | 0 tests/{ => fixtures}/data-rapidblock.json | 0 tests/{ => fixtures}/data-silences-01.csv | 0 tests/{ => fixtures}/data-suspends-01.csv | 0 tests/test_merge_thresholds.py | 7 +-- tests/test_mergeplan.py | 68 ++++++++++++----------- tests/test_parser_json.py | 17 ++---- tests/test_parser_rapidblockjson.py | 68 +++++++++++------------ 11 files changed, 107 insertions(+), 86 deletions(-) create mode 100644 tests/fixtures/__init__.py rename tests/{ => fixtures}/data-mastodon.json (100%) rename tests/{ => fixtures}/data-noop-01.csv (100%) rename tests/{ => fixtures}/data-rapidblock.json (100%) rename tests/{ => fixtures}/data-silences-01.csv (100%) rename tests/{ => fixtures}/data-suspends-01.csv (100%) diff --git a/tests/conftest.py b/tests/conftest.py index f0de967..976af88 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,37 @@ import os import sys +import pytest + sys.path.append(os.path.join(os.path.dirname(__file__), "helpers")) + + +def load_data(datafile): + """Load test data from a fixture datafile""" + with open(os.path.join(os.path.dirname(__file__), "fixtures", datafile)) as fp: + return fp.read() + + +@pytest.fixture +def data_mastodon_json(): + return load_data("data-mastodon.json") + + +@pytest.fixture +def data_rapidblock_json(): + return load_data("data-rapidblock.json") + + +@pytest.fixture +def data_suspends_01(): + return load_data("data-suspends-01.csv") + + +@pytest.fixture +def data_silences_01(): + return load_data("data-silences-01.csv") + + +@pytest.fixture +def data_noop_01(): + return load_data("data-noop-01.csv") diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/data-mastodon.json b/tests/fixtures/data-mastodon.json similarity index 100% rename from tests/data-mastodon.json rename to tests/fixtures/data-mastodon.json diff --git a/tests/data-noop-01.csv b/tests/fixtures/data-noop-01.csv similarity index 100% rename from tests/data-noop-01.csv rename to tests/fixtures/data-noop-01.csv diff --git a/tests/data-rapidblock.json b/tests/fixtures/data-rapidblock.json similarity index 100% rename from tests/data-rapidblock.json rename to tests/fixtures/data-rapidblock.json diff --git a/tests/data-silences-01.csv b/tests/fixtures/data-silences-01.csv similarity index 100% rename from tests/data-silences-01.csv rename to tests/fixtures/data-silences-01.csv diff --git a/tests/data-suspends-01.csv b/tests/fixtures/data-suspends-01.csv similarity index 100% rename from tests/data-suspends-01.csv rename to tests/fixtures/data-suspends-01.csv diff --git a/tests/test_merge_thresholds.py b/tests/test_merge_thresholds.py index 4f6410f..c7fe3cf 100644 --- a/tests/test_merge_thresholds.py +++ b/tests/test_merge_thresholds.py @@ -5,10 +5,6 @@ from fediblockhole.blocklists import Blocklist, parse_blocklist from fediblockhole.const import DomainBlock -datafile01 = "data-suspends-01.csv" -datafile02 = "data-silences-01.csv" -datafile03 = "data-noop-01.csv" - import_fields = [ "domain", "severity", @@ -20,7 +16,8 @@ ] -def load_test_blocklist_data(datafiles): +# FIXME: Deprecated data loader. Now using fixtures. +def __load_test_blocklist_data(datafiles): blocklists = [] diff --git a/tests/test_mergeplan.py b/tests/test_mergeplan.py index 4afe32e..f054814 100644 --- a/tests/test_mergeplan.py +++ b/tests/test_mergeplan.py @@ -5,10 +5,6 @@ from fediblockhole.blocklists import parse_blocklist from fediblockhole.const import DomainBlock, SeverityLevel -datafile01 = "data-suspends-01.csv" -datafile02 = "data-silences-01.csv" -datafile03 = "data-noop-01.csv" - import_fields = [ "domain", "severity", @@ -24,18 +20,16 @@ def load_test_blocklist_data(datafiles): blocklists = [] - for df in datafiles: - with open(df) as fp: - data = fp.read() - bl = parse_blocklist(data, df, "csv", import_fields) - blocklists.append(bl) + for data in datafiles: + bl = parse_blocklist(data, "pytest", "csv", import_fields) + blocklists.append(bl) return blocklists -def test_mergeplan_max(): +def test_mergeplan_max(data_suspends_01, data_silences_01): """Test 'max' mergeplan""" - blocklists = load_test_blocklist_data([datafile01, datafile02]) + blocklists = load_test_blocklist_data([data_suspends_01, data_silences_01]) bl = merge_blocklists(blocklists, "max") assert len(bl) == 13 @@ -43,9 +37,9 @@ def test_mergeplan_max(): assert bl[key].severity.level == SeverityLevel.SUSPEND -def test_mergeplan_min(): +def test_mergeplan_min(data_suspends_01, data_silences_01): """Test 'max' mergeplan""" - blocklists = load_test_blocklist_data([datafile01, datafile02]) + blocklists = load_test_blocklist_data([data_suspends_01, data_silences_01]) bl = merge_blocklists(blocklists, "min") assert len(bl) == 13 @@ -54,9 +48,9 @@ def test_mergeplan_min(): assert bl[key].severity.level == SeverityLevel.SILENCE -def test_mergeplan_default(): +def test_mergeplan_default(data_suspends_01, data_silences_01): """Default mergeplan is max, so see if it's chosen""" - blocklists = load_test_blocklist_data([datafile01, datafile02]) + blocklists = load_test_blocklist_data([data_suspends_01, data_silences_01]) bl = merge_blocklists(blocklists) assert len(bl) == 13 @@ -65,9 +59,11 @@ def test_mergeplan_default(): assert bl[key].severity.level == SeverityLevel.SUSPEND -def test_mergeplan_3_max(): +def test_mergeplan_3_max(data_suspends_01, data_silences_01, data_noop_01): """3 datafiles and mergeplan of 'max'""" - blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03]) + blocklists = load_test_blocklist_data( + [data_suspends_01, data_silences_01, data_noop_01] + ) bl = merge_blocklists(blocklists, "max") assert len(bl) == 13 @@ -79,9 +75,11 @@ def test_mergeplan_3_max(): assert bl[key].obfuscate is True -def test_mergeplan_3_min(): +def test_mergeplan_3_min(data_suspends_01, data_silences_01, data_noop_01): """3 datafiles and mergeplan of 'min'""" - blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03]) + blocklists = load_test_blocklist_data( + [data_suspends_01, data_silences_01, data_noop_01] + ) bl = merge_blocklists(blocklists, "min") assert len(bl) == 13 @@ -93,9 +91,9 @@ def test_mergeplan_3_min(): assert bl[key].obfuscate is False -def test_mergeplan_noop_v_silence_max(): +def test_mergeplan_noop_v_silence_max(data_silences_01, data_noop_01): """Mergeplan of max should choose silence over noop""" - blocklists = load_test_blocklist_data([datafile02, datafile03]) + blocklists = load_test_blocklist_data([data_silences_01, data_noop_01]) bl = merge_blocklists(blocklists, "max") assert len(bl) == 13 @@ -104,9 +102,9 @@ def test_mergeplan_noop_v_silence_max(): assert bl[key].severity.level == SeverityLevel.SILENCE -def test_mergeplan_noop_v_silence_min(): +def test_mergeplan_noop_v_silence_min(data_silences_01, data_noop_01): """Mergeplan of min should choose noop over silence""" - blocklists = load_test_blocklist_data([datafile02, datafile03]) + blocklists = load_test_blocklist_data([data_silences_01, data_noop_01]) bl = merge_blocklists(blocklists, "min") assert len(bl) == 13 @@ -115,8 +113,10 @@ def test_mergeplan_noop_v_silence_min(): assert bl[key].severity.level == SeverityLevel.NONE -def test_merge_public_comment(): - blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03]) +def test_merge_public_comment(data_suspends_01, data_silences_01, data_noop_01): + blocklists = load_test_blocklist_data( + [data_suspends_01, data_silences_01, data_noop_01] + ) bl = merge_blocklists(blocklists, "min") assert len(bl) == 13 @@ -124,8 +124,10 @@ def test_merge_public_comment(): assert bl["public-comment.example.org"].public_comment == "This is a public comment" -def test_merge_private_comment(): - blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03]) +def test_merge_private_comment(data_suspends_01, data_silences_01, data_noop_01): + blocklists = load_test_blocklist_data( + [data_suspends_01, data_silences_01, data_noop_01] + ) bl = merge_blocklists(blocklists, "min") assert len(bl) == 13 @@ -135,8 +137,10 @@ def test_merge_private_comment(): ) -def test_merge_public_comments(): - blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03]) +def test_merge_public_comments(data_suspends_01, data_silences_01, data_noop_01): + blocklists = load_test_blocklist_data( + [data_suspends_01, data_silences_01, data_noop_01] + ) bl = merge_blocklists(blocklists, "min") assert len(bl) == 13 @@ -147,9 +151,11 @@ def test_merge_public_comments(): ) -def test_merge_duplicate_comments(): +def test_merge_duplicate_comments(data_suspends_01, data_silences_01, data_noop_01): """The same comment on multiple sources shouldn't get added""" - blocklists = load_test_blocklist_data([datafile01, datafile02, datafile03]) + blocklists = load_test_blocklist_data( + [data_suspends_01, data_silences_01, data_noop_01] + ) bl = merge_blocklists(blocklists, "min") assert len(bl) == 13 diff --git a/tests/test_parser_json.py b/tests/test_parser_json.py index e640643..a1cc693 100644 --- a/tests/test_parser_json.py +++ b/tests/test_parser_json.py @@ -4,19 +4,11 @@ from fediblockhole.blocklists import BlocklistParserJSON from fediblockhole.const import SeverityLevel -datafile = "data-mastodon.json" +def test_json_parser(data_mastodon_json): -def load_data(): - with open(datafile) as fp: - return fp.read() - - -def test_json_parser(): - - data = load_data() parser = BlocklistParserJSON() - bl = parser.parse_blocklist(data, "test_json") + bl = parser.parse_blocklist(data_mastodon_json, "test_json") assert len(bl) == 10 assert "example.org" in bl @@ -30,11 +22,10 @@ def test_json_parser(): assert bl["example4.org"].severity.level == SeverityLevel.NONE -def test_ignore_comments(): +def test_ignore_comments(data_mastodon_json): - data = load_data() parser = BlocklistParserJSON() - bl = parser.parse_blocklist(data, "test_json") + bl = parser.parse_blocklist(data_mastodon_json, "test_json") assert len(bl) == 10 assert "example.org" in bl diff --git a/tests/test_parser_rapidblockjson.py b/tests/test_parser_rapidblockjson.py index 141bfae..42ab481 100644 --- a/tests/test_parser_rapidblockjson.py +++ b/tests/test_parser_rapidblockjson.py @@ -4,40 +4,34 @@ from fediblockhole.blocklists import parse_blocklist from fediblockhole.const import SeverityLevel -rapidblockjson = "data-rapidblock.json" - - -def test_parse_rapidblock_json(): - with open(rapidblockjson) as fp: - data = fp.read() - bl = parse_blocklist(data, "pytest", "rapidblock.json") - - assert "101010.pl" in bl - assert bl["101010.pl"].severity.level == SeverityLevel.SUSPEND - assert bl["101010.pl"].public_comment == "" - - assert "berserker.town" in bl - assert bl["berserker.town"].severity.level == SeverityLevel.SUSPEND - assert bl["berserker.town"].public_comment == "" - assert bl["berserker.town"].private_comment == "" - - -def test_parse_with_comments(): - with open(rapidblockjson) as fp: - data = fp.read() - bl = parse_blocklist( - data, - "pytest", - "rapidblock.json", - ["domain", "severity", "public_comment", "private_comment"], - ) - - assert "101010.pl" in bl - assert bl["101010.pl"].severity.level == SeverityLevel.SUSPEND - assert ( - bl["101010.pl"].public_comment == "cryptomining javascript, white supremacy" - ) - - assert "berserker.town" in bl - assert bl["berserker.town"].severity.level == SeverityLevel.SUSPEND - assert bl["berserker.town"].public_comment == "freeze peach" + +def test_parse_rapidblock_json(data_rapidblock_json): + + bl = parse_blocklist(data_rapidblock_json, "pytest", "rapidblock.json") + + assert "101010.pl" in bl + assert bl["101010.pl"].severity.level == SeverityLevel.SUSPEND + assert bl["101010.pl"].public_comment == "" + + assert "berserker.town" in bl + assert bl["berserker.town"].severity.level == SeverityLevel.SUSPEND + assert bl["berserker.town"].public_comment == "" + assert bl["berserker.town"].private_comment == "" + + +def test_parse_with_comments(data_rapidblock_json): + + bl = parse_blocklist( + data_rapidblock_json, + "pytest", + "rapidblock.json", + ["domain", "severity", "public_comment", "private_comment"], + ) + + assert "101010.pl" in bl + assert bl["101010.pl"].severity.level == SeverityLevel.SUSPEND + assert bl["101010.pl"].public_comment == "cryptomining javascript, white supremacy" + + assert "berserker.town" in bl + assert bl["berserker.town"].severity.level == SeverityLevel.SUSPEND + assert bl["berserker.town"].public_comment == "freeze peach"