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.
79 lines
2.1 KiB
79 lines
2.1 KiB
6 years ago
|
import json
|
||
|
import os
|
||
|
|
||
|
import pytest
|
||
|
|
||
|
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)
|
||
|
p.add_point(9, 10)
|
||
|
p.add_point(30, 40)
|
||
|
|
||
|
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},"
|
||
|
"{\"x\": 30, \"y\": 50}"
|
||
|
"]\n"
|
||
|
"}")
|
||
|
|
||
|
p = tmpdir.mkdir("test_data").join("test.json")
|
||
|
p.write(test)
|
||
|
|
||
|
expected = PointSet(8, 100, 100)
|
||
|
expected.add_point(8, 8)
|
||
|
expected.add_point(30, 50)
|
||
|
|
||
|
PointManager.load(p)
|
||
|
|
||
|
print(PointManager.point_set)
|
||
|
|
||
|
# 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},"
|
||
|
"{\"x\": 9, \"y\": 10},"
|
||
|
"{\"x\": 30, \"y\": 40}"
|
||
|
"]"
|
||
|
"}")
|
||
|
|
||
|
|
||
|
with open(d) as f:
|
||
|
expected = json.loads(expected_str)
|
||
|
actual = json.load(d)
|
||
|
|
||
|
# 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"]
|