forked from Jason-Vaughan/TangleBrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_settings.py
More file actions
102 lines (76 loc) · 4.19 KB
/
Copy pathtest_settings.py
File metadata and controls
102 lines (76 loc) · 4.19 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Tests for the global settings loader (tanglebrain/settings.py) — the paid-API billing gate."""
from __future__ import annotations
import os
import tempfile
import unittest
from tanglebrain.settings import (
Settings,
SettingsError,
default_settings_path,
load_settings,
)
def write_yaml(text: str, test: unittest.TestCase) -> str:
"""Write YAML to a temp file and return its path, registering cleanup on the test."""
handle = tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False)
handle.write(text)
handle.close()
test.addCleanup(os.unlink, handle.name)
return handle.name
class PackagedSettingsTest(unittest.TestCase):
"""The settings shipped with the package parse and keep billing OFF by default."""
def test_default_path_points_at_packaged_yaml(self):
self.assertTrue(default_settings_path().exists())
self.assertEqual(default_settings_path().name, "settings.yaml")
def test_packaged_settings_ship_billing_disabled(self):
# The safety contract: the shipped gate must be off.
self.assertFalse(load_settings().api_billing_enabled)
class LoadSettingsTest(unittest.TestCase):
"""Loading defaults safely on absence, but hard-fails on a malformed gate."""
def test_missing_file_yields_safe_defaults(self):
settings = load_settings("/no/such/settings.yaml")
self.assertEqual(settings, Settings())
self.assertFalse(settings.api_billing_enabled)
def test_empty_file_yields_defaults(self):
self.assertFalse(load_settings(write_yaml("", self)).api_billing_enabled)
def test_explicit_true_enables(self):
self.assertTrue(load_settings(write_yaml("api_billing_enabled: true\n", self)).api_billing_enabled)
def test_explicit_false_disables(self):
self.assertFalse(load_settings(write_yaml("api_billing_enabled: false\n", self)).api_billing_enabled)
def test_absent_key_defaults_off(self):
self.assertFalse(load_settings(write_yaml("something_else: 1\n", self)).api_billing_enabled)
def test_non_mapping_rejected(self):
with self.assertRaises(SettingsError):
load_settings(write_yaml("- just\n- a\n- list\n", self))
def test_non_bool_gate_rejected(self):
# A stray non-bool must NOT be coerced into "billing enabled". (Bare yes/no/on/off ARE
# YAML booleans in PyYAML, so they are legitimately accepted; these are the non-bools.)
for bad in ("1", "'true'", "1.5"):
with self.assertRaises(SettingsError):
load_settings(write_yaml(f"api_billing_enabled: {bad}\n", self))
def test_invalid_yaml_rejected(self):
with self.assertRaises(SettingsError):
load_settings(write_yaml("api_billing_enabled: : :\n", self))
def test_classifier_gate_defaults_off(self):
self.assertFalse(load_settings(write_yaml("", self)).classifier_gate_enabled)
self.assertFalse(load_settings("/no/such.yaml").classifier_gate_enabled)
def test_classifier_gate_parses_and_validates(self):
self.assertTrue(load_settings(write_yaml("classifier_gate_enabled: true\n", self)).classifier_gate_enabled)
with self.assertRaises(SettingsError):
load_settings(write_yaml("classifier_gate_enabled: 1\n", self))
def test_packaged_settings_ship_classifier_gate_off(self):
self.assertFalse(load_settings().classifier_gate_enabled)
def test_delegate_max_concurrency_defaults_none(self):
self.assertIsNone(load_settings(write_yaml("", self)).delegate_max_concurrency)
self.assertIsNone(load_settings("/no/such.yaml").delegate_max_concurrency)
self.assertIsNone(load_settings().delegate_max_concurrency) # packaged ships it unset
def test_delegate_max_concurrency_parses_positive_int(self):
self.assertEqual(
load_settings(write_yaml("delegate_max_concurrency: 6\n", self)).delegate_max_concurrency,
6,
)
def test_delegate_max_concurrency_rejects_bad_values(self):
for bad in ("0", "-2", "true", "2.5", "'lots'"):
with self.assertRaises(SettingsError):
load_settings(write_yaml(f"delegate_max_concurrency: {bad}\n", self))
if __name__ == "__main__":
unittest.main()