A computational geometry learning and experimentation tool.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

import json
import os
import pytest
from clusterview.colors import Color
from clusterview.points import PointSet
from clusterview.point_manager import PointManager
@pytest.fixture(autouse=True)
def setup():
p = PointSet(8, 100, 100)
p.add_point(4, 4, Color.GREY)
p.add_point(9, 10, Color.GREY)
p.add_point(30, 40, Color.GREY)
PointManager.point_set = p
def test_load(tmpdir):
test = ("{\n"
"\"point_size\": 8,\n"
"\"viewport_height\": 100,\n"
"\"viewport_width\": 100,\n"
"\"points\":[\n"
"{\"x\": 8, \"y\": 8, \"color\": \"GREY\"},"
"{\"x\": 30, \"y\": 50, \"color\": \"GREY\"}"
"]\n"
"}")
p = tmpdir.mkdir("test_data").join("test.json")
p.write(test)
expected = PointSet(8, 100, 100)
expected.add_point(8, 8, Color.GREY)
expected.add_point(30, 50, Color.GREY)
PointManager.load(p)
# The fixture point_set inside the singleton PointManager should be
# overwritten.
assert PointManager.point_set == expected
def test_save(tmpdir):
d = tmpdir.mkdir("test_data").join("save.json")
PointManager.save(d)
expected_str = ("{"
"\"point_size\": 8,"
"\"viewport_height\": 100,"
"\"viewport_width\": 100,"
"\"points\":["
"{\"x\": 4, \"y\": 4, \"color\": \"GREY\"},"
"{\"x\": 9, \"y\": 10, \"color\": \"GREY\"},"
"{\"x\": 30, \"y\": 40, \"color\": \"GREY\"}"
"]"
"}")
with open(d) as f:
expected = json.loads(expected_str)
actual = json.load(f)
# Since the JSON module converts the `points` key to
# a list of dicts, we need to do manual comparison
# to get around the problem of list equality when we
# really want a set of Points.
assert actual["point_size"] == expected["point_size"]
assert actual["viewport_width"] == expected["viewport_width"]
assert actual["viewport_height"] == expected["viewport_height"]
assert len(actual["points"]) == len(expected["points"])
for p in actual["points"]:
assert p in expected["points"]