Skip to content

Commit

Permalink
Moved data files to pytest fixtures.
Browse files Browse the repository at this point in the history
Datafiles now load from relative path. Fixes: #27
  • Loading branch information
jpwarren committed Nov 1, 2024
1 parent bf47791 commit 9e9d6b2
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 86 deletions.
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
Empty file added tests/fixtures/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 2 additions & 5 deletions tests/test_merge_thresholds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -20,7 +16,8 @@
]


def load_test_blocklist_data(datafiles):
# FIXME: Deprecated data loader. Now using fixtures.
def __load_test_blocklist_data(datafiles):

blocklists = []

Expand Down
68 changes: 37 additions & 31 deletions tests/test_mergeplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,28 +20,26 @@ 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

for key in bl:
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -115,17 +113,21 @@ 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

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
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 4 additions & 13 deletions tests/test_parser_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
68 changes: 31 additions & 37 deletions tests/test_parser_rapidblockjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 9e9d6b2

Please sign in to comment.