forked from retoor/devplacepy
248 lines
7.8 KiB
Python
248 lines
7.8 KiB
Python
# retoor <retoor@molodetz.nl>
|
|||
|
|
|
||
|
|
import pytest
|
||
|
|
import dataset
|
||
|
|
from pydantic import ValidationError
|
||
|
|
from devplacepy.database import visibility as visibility_module
|
||
|
|
|
||
|
|
from devplacepy.database import (
|
||
|
|
DEFAULT_VISIBILITY,
|
||
|
|
LISTED_VALUES,
|
||
|
|
OPEN_VALUES,
|
||
|
|
SOFT_DELETE_TABLES,
|
||
|
|
TARGET_TABLES,
|
||
|
|
VISIBILITY_CHOICES,
|
||
|
|
VISIBILITY_COLUMNS,
|
||
|
|
VISIBILITY_HINTS,
|
||
|
|
VISIBILITY_LABELS,
|
||
|
|
VISIBILITY_VALUES,
|
||
|
|
is_listed,
|
||
|
|
is_open,
|
||
|
|
item_visibility,
|
||
|
|
listed_clause,
|
||
|
|
listed_sql,
|
||
|
|
normalize_visibility,
|
||
|
|
visibility_label,
|
||
|
|
visibility_options,
|
||
|
|
)
|
||
|
|
from devplacepy.models import (
|
||
|
|
GistEditForm,
|
||
|
|
GistForm,
|
||
|
|
PostEditForm,
|
||
|
|
PostForm,
|
||
|
|
ProjectEditForm,
|
||
|
|
ProjectForm,
|
||
|
|
VisibilityForm,
|
||
|
|
)
|
||
|
|
from devplacepy.schemas.content import GistOut, PostOut, ProjectOut
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def scratch(monkeypatch):
|
||
|
|
memory = dataset.connect("sqlite:///:memory:")
|
||
|
|
monkeypatch.setattr(visibility_module, "db", memory)
|
||
|
|
monkeypatch.setattr(visibility_module, "get_table", lambda name: memory[name])
|
||
|
|
yield memory
|
||
|
|
memory.close()
|
||
|
|
|
||
|
|
|
||
|
|
def test_the_four_statuses_and_the_default():
|
||
|
|
assert VISIBILITY_VALUES == ("published", "hidden", "draft", "offline")
|
||
|
|
assert DEFAULT_VISIBILITY == "published"
|
||
|
|
assert VISIBILITY_CHOICES == ("published", "hidden", "offline")
|
||
|
|
assert set(VISIBILITY_CHOICES) <= set(VISIBILITY_VALUES)
|
||
|
|
|
||
|
|
|
||
|
|
def test_listed_is_a_subset_of_open():
|
||
|
|
assert set(LISTED_VALUES) <= set(OPEN_VALUES)
|
||
|
|
assert LISTED_VALUES == ("published",)
|
||
|
|
assert OPEN_VALUES == ("published", "hidden")
|
||
|
|
|
||
|
|
|
||
|
|
def test_labels_and_hints_cover_every_value():
|
||
|
|
for value in VISIBILITY_VALUES:
|
||
|
|
assert VISIBILITY_LABELS[value]
|
||
|
|
assert VISIBILITY_HINTS[value]
|
||
|
|
assert visibility_label(value) == VISIBILITY_LABELS[value]
|
||
|
|
|
||
|
|
|
||
|
|
def test_options_expose_only_user_choices_by_default():
|
||
|
|
assert [o["value"] for o in visibility_options()] == list(VISIBILITY_CHOICES)
|
||
|
|
assert [o["value"] for o in visibility_options(VISIBILITY_VALUES)] == list(
|
||
|
|
VISIBILITY_VALUES
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("raw", [None, "", " ", "nonsense", 0, "PUBLISHED "])
|
||
|
|
def test_normalize_falls_back_to_published_or_lowercases(raw):
|
||
|
|
expected = "published"
|
||
|
|
assert normalize_visibility(raw) == expected
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
|
||
|
|
def test_normalize_is_idempotent(value):
|
||
|
|
assert normalize_visibility(normalize_visibility(value)) == value
|
||
|
|
assert normalize_visibility(value.upper()) == value
|
||
|
|
|
||
|
|
|
||
|
|
def test_every_visibility_table_is_a_soft_delete_table_with_a_target():
|
||
|
|
for table in VISIBILITY_COLUMNS:
|
||
|
|
assert table in SOFT_DELETE_TABLES
|
||
|
|
assert set(TARGET_TABLES.values()) == set(VISIBILITY_COLUMNS)
|
||
|
|
|
||
|
|
|
||
|
|
def test_news_uses_its_existing_status_column():
|
||
|
|
assert VISIBILITY_COLUMNS["news"] == "status"
|
||
|
|
for table in ("posts", "gists", "projects"):
|
||
|
|
assert VISIBILITY_COLUMNS[table] == "visibility"
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("table", sorted(VISIBILITY_COLUMNS))
|
||
|
|
def test_null_and_unknown_rows_read_as_published(table):
|
||
|
|
column = VISIBILITY_COLUMNS[table]
|
||
|
|
assert item_visibility(table, {}) == "published"
|
||
|
|
assert item_visibility(table, {column: None}) == "published"
|
||
|
|
assert item_visibility(table, {column: "weird"}) == "published"
|
||
|
|
assert is_listed(table, {column: None})
|
||
|
|
assert is_open(table, {column: None})
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("table", sorted(VISIBILITY_COLUMNS))
|
||
|
|
@pytest.mark.parametrize(
|
||
|
|
"value,listed,open_",
|
||
|
|
[
|
||
|
|
("published", True, True),
|
||
|
|
("hidden", False, True),
|
||
|
|
("offline", False, False),
|
||
|
|
("draft", False, False),
|
||
|
|
],
|
||
|
|
)
|
||
|
|
def test_listed_and_open_truth_table(table, value, listed, open_):
|
||
|
|
row = {VISIBILITY_COLUMNS[table]: value}
|
||
|
|
assert is_listed(table, row) is listed
|
||
|
|
assert is_open(table, row) is open_
|
||
|
|
|
||
|
|
|
||
|
|
def test_untracked_tables_are_always_open_and_listed():
|
||
|
|
assert item_visibility("quizzes", {"visibility": "offline"}) == "published"
|
||
|
|
assert is_listed("quizzes", {"visibility": "offline"})
|
||
|
|
assert listed_sql("quizzes") == "1 = 1"
|
||
|
|
|
||
|
|
|
||
|
|
def test_listed_sql_uses_coalesce_so_null_rows_stay_visible(scratch):
|
||
|
|
scratch["posts"].insert({"uid": "x", "visibility": "published"})
|
||
|
|
scratch["news"].insert({"uid": "y", "status": "published"})
|
||
|
|
assert "COALESCE(p.visibility, 'published') = 'published'" == listed_sql(
|
||
|
|
"posts", "p"
|
||
|
|
)
|
||
|
|
assert listed_sql("news") == "COALESCE(status, 'published') = 'published'"
|
||
|
|
|
||
|
|
|
||
|
|
def test_listed_clause_is_none_for_tables_without_a_visibility_column(scratch):
|
||
|
|
scratch["sessions"].insert({"uid": "x"})
|
||
|
|
assert listed_clause(scratch["sessions"]) is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_listed_clause_filters_rows(scratch):
|
||
|
|
table = scratch["posts"]
|
||
|
|
table.insert(
|
||
|
|
{
|
||
|
|
"uid": "vis-unit-published",
|
||
|
|
"user_uid": "owner-1",
|
||
|
|
"visibility": "published",
|
||
|
|
"deleted_at": None,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
table.insert(
|
||
|
|
{
|
||
|
|
"uid": "vis-unit-legacy",
|
||
|
|
"user_uid": "owner-1",
|
||
|
|
"visibility": None,
|
||
|
|
"deleted_at": None,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
for value in ("hidden", "offline", "draft"):
|
||
|
|
table.insert(
|
||
|
|
{
|
||
|
|
"uid": f"vis-unit-{value}",
|
||
|
|
"user_uid": "owner-1",
|
||
|
|
"visibility": value,
|
||
|
|
"deleted_at": None,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
wanted = {"vis-unit-published", "vis-unit-legacy"}
|
||
|
|
clause = listed_clause(table)
|
||
|
|
found = {r["uid"] for r in table.find(clause) if r["uid"].startswith("vis-unit-")}
|
||
|
|
assert found == wanted
|
||
|
|
own = {
|
||
|
|
r["uid"]
|
||
|
|
for r in table.find(listed_clause(table, own_uid="owner-1"))
|
||
|
|
if r["uid"].startswith("vis-unit-")
|
||
|
|
}
|
||
|
|
assert own == {f"vis-unit-{v}" for v in VISIBILITY_VALUES} | {"vis-unit-legacy"}
|
||
|
|
stranger = {
|
||
|
|
r["uid"]
|
||
|
|
for r in table.find(listed_clause(table, own_uid="someone-else"))
|
||
|
|
if r["uid"].startswith("vis-unit-")
|
||
|
|
}
|
||
|
|
assert stranger == wanted
|
||
|
|
|
||
|
|
|
||
|
|
CREATE_FORMS = (
|
||
|
|
(PostForm, {"content": "x" * 12}),
|
||
|
|
(GistForm, {"title": "t", "source_code": "c"}),
|
||
|
|
(ProjectForm, {"title": "t", "description": "d"}),
|
||
|
|
)
|
||
|
|
EDIT_FORMS = (
|
||
|
|
(PostEditForm, {"content": "x" * 12}),
|
||
|
|
(GistEditForm, {"title": "t", "source_code": "c"}),
|
||
|
|
(ProjectEditForm, {"title": "t", "description": "d"}),
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("form,base", CREATE_FORMS)
|
||
|
|
def test_create_forms_default_to_published(form, base):
|
||
|
|
assert form(**base).visibility == "published"
|
||
|
|
assert form(**base, visibility="").visibility == "published"
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("form,base", CREATE_FORMS)
|
||
|
|
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
|
||
|
|
def test_create_forms_accept_every_status(form, base, value):
|
||
|
|
assert form(**base, visibility=value.upper()).visibility == value
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("form,base", EDIT_FORMS)
|
||
|
|
def test_edit_forms_leave_visibility_unset_by_default(form, base):
|
||
|
|
assert form(**base).visibility is None
|
||
|
|
assert form(**base, visibility="").visibility is None
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("form,base", EDIT_FORMS)
|
||
|
|
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
|
||
|
|
def test_edit_forms_accept_every_status(form, base, value):
|
||
|
|
assert form(**base, visibility=value).visibility == value
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("form,base", CREATE_FORMS + EDIT_FORMS)
|
||
|
|
def test_forms_reject_unknown_status(form, base):
|
||
|
|
with pytest.raises(ValidationError):
|
||
|
|
form(**base, visibility="secret")
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("value", VISIBILITY_VALUES)
|
||
|
|
def test_visibility_form_accepts_every_status(value):
|
||
|
|
assert VisibilityForm(value=value).value == value
|
||
|
|
|
||
|
|
|
||
|
|
def test_visibility_form_rejects_unknown_status():
|
||
|
|
with pytest.raises(ValidationError):
|
||
|
|
VisibilityForm(value="secret")
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("schema", [PostOut, GistOut, ProjectOut])
|
||
|
|
def test_output_schemas_always_report_a_status(schema):
|
||
|
|
assert schema(uid="1").visibility == "published"
|
||
|
|
assert schema(uid="1", visibility=None).visibility == "published"
|
||
|
|
assert schema(uid="1", visibility="offline").visibility == "offline"
|