|
| 1 | +import os |
| 2 | + |
| 3 | + |
| 4 | +class TestSafeLiteralEval: |
| 5 | + |
| 6 | + def test_reads_dictionary_from_file(self, temp_dir): |
| 7 | + test_file = os.path.join(temp_dir, "ports.txt") |
| 8 | + with open(test_file, "w") as f: |
| 9 | + f.write("{'a': 1, 'b': 2}") |
| 10 | + |
| 11 | + from concoredocker import safe_literal_eval |
| 12 | + result = safe_literal_eval(test_file, {}) |
| 13 | + |
| 14 | + assert result == {'a': 1, 'b': 2} |
| 15 | + |
| 16 | + def test_reads_list_from_file(self, temp_dir): |
| 17 | + test_file = os.path.join(temp_dir, "data.txt") |
| 18 | + with open(test_file, "w") as f: |
| 19 | + f.write("[1, 2, 3]") |
| 20 | + |
| 21 | + from concoredocker import safe_literal_eval |
| 22 | + result = safe_literal_eval(test_file, []) |
| 23 | + |
| 24 | + assert result == [1, 2, 3] |
| 25 | + |
| 26 | + def test_returns_default_when_file_missing(self): |
| 27 | + from concoredocker import safe_literal_eval |
| 28 | + result = safe_literal_eval("/nonexistent.txt", {'default': True}) |
| 29 | + |
| 30 | + assert result == {'default': True} |
| 31 | + |
| 32 | + def test_returns_default_for_bad_syntax(self, temp_dir): |
| 33 | + test_file = os.path.join(temp_dir, "bad.txt") |
| 34 | + with open(test_file, "w") as f: |
| 35 | + f.write("not valid {{{") |
| 36 | + |
| 37 | + from concoredocker import safe_literal_eval |
| 38 | + result = safe_literal_eval(test_file, "fallback") |
| 39 | + |
| 40 | + assert result == "fallback" |
| 41 | + |
| 42 | + |
| 43 | +class TestUnchanged: |
| 44 | + |
| 45 | + def test_returns_true_when_unchanged(self): |
| 46 | + import concoredocker |
| 47 | + concoredocker.s = "abc" |
| 48 | + concoredocker.olds = "abc" |
| 49 | + |
| 50 | + assert concoredocker.unchanged() == True |
| 51 | + assert concoredocker.s == '' |
| 52 | + |
| 53 | + def test_returns_false_when_changed(self): |
| 54 | + import concoredocker |
| 55 | + concoredocker.s = "new" |
| 56 | + concoredocker.olds = "old" |
| 57 | + |
| 58 | + assert concoredocker.unchanged() == False |
| 59 | + assert concoredocker.olds == "new" |
| 60 | + |
| 61 | + |
| 62 | +class TestInitval: |
| 63 | + |
| 64 | + def test_parses_simtime_and_values(self): |
| 65 | + import concoredocker |
| 66 | + concoredocker.simtime = 0 |
| 67 | + result = concoredocker.initval("[5.0, 1.0, 2.0]") |
| 68 | + |
| 69 | + assert result == [1.0, 2.0] |
| 70 | + assert concoredocker.simtime == 5.0 |
| 71 | + |
| 72 | + def test_parses_single_value(self): |
| 73 | + import concoredocker |
| 74 | + concoredocker.simtime = 0 |
| 75 | + result = concoredocker.initval("[10.0, 99]") |
| 76 | + |
| 77 | + assert result == [99] |
| 78 | + assert concoredocker.simtime == 10.0 |
| 79 | + |
| 80 | + |
| 81 | +class TestWrite: |
| 82 | + |
| 83 | + def test_writes_list_with_simtime(self, temp_dir): |
| 84 | + import concoredocker |
| 85 | + old_outpath = concoredocker.outpath |
| 86 | + outdir = os.path.join(temp_dir, "1") |
| 87 | + os.makedirs(outdir) |
| 88 | + concoredocker.outpath = temp_dir |
| 89 | + concoredocker.simtime = 5.0 |
| 90 | + |
| 91 | + concoredocker.write(1, "testfile", [1.0, 2.0], delta=0) |
| 92 | + |
| 93 | + with open(os.path.join(outdir, "testfile")) as f: |
| 94 | + content = f.read() |
| 95 | + assert content == "[5.0, 1.0, 2.0]" |
| 96 | + concoredocker.outpath = old_outpath |
| 97 | + |
| 98 | + def test_writes_with_delta(self, temp_dir): |
| 99 | + import concoredocker |
| 100 | + old_outpath = concoredocker.outpath |
| 101 | + outdir = os.path.join(temp_dir, "1") |
| 102 | + os.makedirs(outdir) |
| 103 | + concoredocker.outpath = temp_dir |
| 104 | + concoredocker.simtime = 10.0 |
| 105 | + |
| 106 | + concoredocker.write(1, "testfile", [3.0], delta=2) |
| 107 | + |
| 108 | + with open(os.path.join(outdir, "testfile")) as f: |
| 109 | + content = f.read() |
| 110 | + assert content == "[12.0, 3.0]" |
| 111 | + assert concoredocker.simtime == 12.0 |
| 112 | + concoredocker.outpath = old_outpath |
| 113 | + |
| 114 | + |
| 115 | +class TestRead: |
| 116 | + |
| 117 | + def test_reads_and_parses_data(self, temp_dir): |
| 118 | + import concoredocker |
| 119 | + old_inpath = concoredocker.inpath |
| 120 | + old_delay = concoredocker.delay |
| 121 | + indir = os.path.join(temp_dir, "1") |
| 122 | + os.makedirs(indir) |
| 123 | + concoredocker.inpath = temp_dir |
| 124 | + concoredocker.delay = 0.001 |
| 125 | + |
| 126 | + with open(os.path.join(indir, "data"), 'w') as f: |
| 127 | + f.write("[7.0, 100, 200]") |
| 128 | + |
| 129 | + concoredocker.s = '' |
| 130 | + concoredocker.simtime = 0 |
| 131 | + result = concoredocker.read(1, "data", "[0, 0, 0]") |
| 132 | + |
| 133 | + assert result == [100, 200] |
| 134 | + assert concoredocker.simtime == 7.0 |
| 135 | + concoredocker.inpath = old_inpath |
| 136 | + concoredocker.delay = old_delay |
| 137 | + |
| 138 | + def test_returns_default_when_file_missing(self, temp_dir): |
| 139 | + import concoredocker |
| 140 | + old_inpath = concoredocker.inpath |
| 141 | + old_delay = concoredocker.delay |
| 142 | + indir = os.path.join(temp_dir, "1") |
| 143 | + os.makedirs(indir) |
| 144 | + concoredocker.inpath = temp_dir |
| 145 | + concoredocker.delay = 0.001 |
| 146 | + |
| 147 | + concoredocker.s = '' |
| 148 | + concoredocker.simtime = 0 |
| 149 | + result = concoredocker.read(1, "nofile", "[0, 5, 5]") |
| 150 | + |
| 151 | + assert result == [5, 5] |
| 152 | + concoredocker.inpath = old_inpath |
| 153 | + concoredocker.delay = old_delay |
0 commit comments