Files
SocialPub/tools/pasture/town/tests/test_town.py
T

79 lines
2.9 KiB
Python
Raw Normal View History

"""The town's own logic, with no pasture: python3 -m unittest discover -s tools/pasture/town/tests"""
import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import check # noqa: E402
import gen # noqa: E402
from core.rng import Rng # noqa: E402
class RngTests(unittest.TestCase):
def test_the_same_seed_and_name_give_the_same_stream(self):
a, b = Rng(7, "x"), Rng(7, "x")
self.assertEqual([a.next64() for _ in range(5)], [b.next64() for _ in range(5)])
def test_substreams_do_not_disturb_each_other(self):
root = Rng(7)
before = Rng(7).sub("graph").next64()
root.sub("content").next64()
self.assertEqual(before, root.sub("graph").next64())
def test_below_stays_in_range(self):
r = Rng(1)
self.assertTrue(all(0 <= r.below(3) < 3 for _ in range(1000)))
class PlanTests(unittest.TestCase):
def test_a_spec_always_plans_the_same(self):
spec = gen.load("village")
self.assertEqual(gen.digest(gen.plan(spec)), gen.digest(gen.plan(spec)))
def test_every_post_names_its_marker_and_every_reply_a_parent_planned_before_it(self):
planner = gen.Planner(gen.load("village"))
steps = planner.plan()
made = set()
for s in steps:
if s["verb"] != "post":
continue
self.assertIn(f"({s['ref']})", s["args"]["text"])
if s["args"].get("reply_to"):
self.assertIn(s["args"]["reply_to"], made)
made.add(s["ref"])
def test_lemmy_accounts_never_follow_people_and_platforms_get_only_what_they_can_do(self):
steps = gen.plan(gen.load("village"))
for s in steps:
platform = s["actor"].split("/")[0]
if s["verb"] == "follow":
self.assertNotEqual("lemmy", platform)
if s["verb"] in ("react", "edit", "vote", "boost"):
self.assertIn({"react": "react", "edit": "edit", "vote": "vote", "boost": "boost"}[s["verb"]], gen.CAPS[platform])
def test_a_stored_plan_rebuilds_the_same_world(self):
planner = gen.Planner(gen.load("village"))
planner.plan()
stored = gen.Stored(gen.snapshot(planner))
self.assertEqual(planner.objects.keys(), stored.objects.keys())
self.assertEqual(planner.objects["p1"]["mentions"], stored.objects["p1"]["mentions"])
class GapTests(unittest.TestCase):
def test_a_known_gap_turns_failures_into_xfail_and_passes_into_xpass(self):
gaps = [{"id": "G-9", "match": {"feature": r"count\.like\..*", "observer": "privapub"}, "status": "open"}]
checks = check.classify([
{"feature": "count.like.followers", "observer": "privapub", "ok": False},
{"feature": "count.like.public", "observer": "privapub", "ok": True},
{"feature": "count.like.public", "observer": "gts", "ok": False},
], gaps)
self.assertEqual(["xfail", "xpass", "fail"], [c["status"] for c in checks])
def test_a_closed_gap_matches_nothing(self):
gaps = [{"id": "G-9", "match": {"feature": ".*"}, "status": "closed"}]
self.assertEqual("fail", check.classify([{"feature": "x", "ok": False}], gaps)[0]["status"])
if __name__ == "__main__":
unittest.main()