aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/tests/test_cli.py
blob: e5a464cf294896d137af9f00cc28193f06c35a07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json
import tempfile
import unittest
from pathlib import Path

import numpy as np

from tests.helpers import globe_world
from tests.test_engine import small_setup
from worldhistory.cli import main
from worldhistory.engine import Engine
from worldhistory.preview import compare, preview


class PreviewTest(unittest.TestCase):
    def test_preview_and_compare(self):
        with tempfile.TemporaryDirectory() as d:
            a, b = Path(d) / "a", Path(d) / "b"
            w, h, races = small_setup(1)
            Engine(w, h, races).run(a)
            w2, h2, races2 = small_setup(2)
            Engine(w2, h2, races2).run(b)
            files = preview(a, world=w)
            names = {p.name for p in files}
            self.assertIn("race_y0060.png", names)
            self.assertIn("chart.png", names)
            self.assertIn("contact.png", names)
            same = compare(a, a, world=w)
            self.assertEqual(same["agreement"], 1.0)
            diff = compare(a, b, world=w, out=Path(d) / "cmp")
            self.assertLessEqual(diff["agreement"], 1.0)
            self.assertTrue((Path(d) / "cmp" / "compare.png").exists())


class CliTest(unittest.TestCase):
    def test_check_config_errors(self):
        with tempfile.TemporaryDirectory() as d:
            (Path(d) / "races").mkdir()
            (Path(d) / "history.toml").write_text("[run]\nyers = 5\n")
            (Path(d) / "races" / "a.toml").write_text('id = "a"\n[demography]\ndensity = 1\ngrowth = 0.1\n'
                                                      '[habitat]\nterms = [{ p = "land", w = 1 }]\n')
            self.assertEqual(main(["check-config", d]), 1)
            (Path(d) / "history.toml").write_text("[run]\nyears = 5\n")
            self.assertEqual(main(["check-config", d]), 0)