Skip to content

Commit

Permalink
Proposed tests for tagweights PR
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed Feb 10, 2022
1 parent 63aa420 commit 8637745
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions tests/test_tagweights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from collections import Counter

import pytest

from lektor.context import Context

from lektor_tags import TagWeight


@pytest.fixture
def tags_plugin(env):
return env.plugins['tags']


@pytest.fixture
def lektor_context(pad):
with Context(pad=pad) as ctx:
yield ctx


@pytest.mark.usefixtures("lektor_context")
def test_tagcount(tags_plugin):
assert tags_plugin.tagcount() == Counter({'tag1': 2, 'tag2': 1, 'tag3': 1})


@pytest.mark.usefixtures("lektor_context")
def test_tagweights(tags_plugin):
assert tags_plugin.tagweights() == {
'tag1': TagWeight(2, 1, 2),
'tag2': TagWeight(1, 1, 2),
'tag3': TagWeight(1, 1, 2),
}


@pytest.mark.usefixtures("lektor_context")
def test_tagweights_no_tags(pad, tags_plugin):
config = tags_plugin.get_config()
config["tags_field"] = "test_no_tags"
assert tags_plugin.tagweights() == {}


@pytest.fixture
def tagweight(count, mincount, maxcount):
return TagWeight(count, mincount, maxcount)


@pytest.mark.parametrize(
"count, mincount, maxcount, lower, upper, expected",
[
(1, 1, 1, 1, 2, 1),
(1, 1, 3, 1, 2, 1),
(2, 1, 3, 1, 2, 1.5),
(3, 1, 3, 1, 2, 2),
]
)
def test_TagWeight_linear(tagweight, lower, upper, expected):
assert tagweight.linear(lower, upper) == expected


@pytest.mark.parametrize(
"count, mincount, maxcount, groups, expected",
[
(1, 1, 4, ('a', 'b'), 'a'),
(2, 1, 4, ('a', 'b'), 'a'),
(3, 1, 4, ('a', 'b'), 'b'),
(4, 1, 4, ('a', 'b'), 'b'),
]
)
def test_TagWeight_lineargroup(tagweight, groups, expected):
assert tagweight.lineargroup(groups) == expected


@pytest.mark.parametrize(
"count, mincount, maxcount, lower, upper, expected",
[
(1, 1, 1, 1, 3, 1),
(1, 1, 4, 1, 3, 1),
(2, 1, 4, 1, 3, 2),
(4, 1, 4, 1, 3, 3),
]
)
def test_TagWeight_log(tagweight, lower, upper, expected):
assert tagweight.log(lower, upper) == expected


@pytest.mark.parametrize(
"count, mincount, maxcount, groups, expected",
[
(4, 4, 8, ('a', 'b'), 'a'),
(5, 4, 8, ('a', 'b'), 'a'),
(6, 4, 8, ('a', 'b'), 'b'),
(8, 4, 8, ('a', 'b'), 'b'),
]
)
def test_TagWeight_loggroup(tagweight, groups, expected):
assert tagweight.loggroup(groups) == expected

0 comments on commit 8637745

Please sign in to comment.