|
1 | 1 | import unittest |
2 | 2 | import json |
| 3 | +import pymysql |
3 | 4 | from slurmdb import SlurmDB |
4 | 5 | from slurm_schema import extract_schema, extract_schema_from_dump |
5 | 6 |
|
@@ -132,6 +133,60 @@ def fake_connect(): |
132 | 133 | self.assertTrue(conn.closed) |
133 | 134 | self.assertIsNone(db._conn) |
134 | 135 |
|
| 136 | + def test_get_tres_map_handles_missing_table(self): |
| 137 | + db = SlurmDB() |
| 138 | + db.connect = lambda: None |
| 139 | + |
| 140 | + class FakeCursor: |
| 141 | + def __enter__(self): |
| 142 | + return self |
| 143 | + |
| 144 | + def __exit__(self, exc_type, exc, tb): |
| 145 | + pass |
| 146 | + |
| 147 | + def execute(self, query): |
| 148 | + raise pymysql.err.ProgrammingError(1146, "Table 'slurm_acct_db.tres' doesn't exist") |
| 149 | + |
| 150 | + def fetchall(self): |
| 151 | + return [] |
| 152 | + |
| 153 | + class FakeConn: |
| 154 | + def cursor(self): |
| 155 | + return FakeCursor() |
| 156 | + |
| 157 | + db._conn = FakeConn() |
| 158 | + tmap = db._get_tres_map() |
| 159 | + self.assertEqual(tmap, {}) |
| 160 | + |
| 161 | + def test_get_tres_map_uses_tres_table_when_missing_tres(self): |
| 162 | + db = SlurmDB() |
| 163 | + db.connect = lambda: None |
| 164 | + |
| 165 | + class FakeCursor: |
| 166 | + def __enter__(self): |
| 167 | + return self |
| 168 | + |
| 169 | + def __exit__(self, exc_type, exc, tb): |
| 170 | + pass |
| 171 | + |
| 172 | + def execute(self, query): |
| 173 | + self.query = query |
| 174 | + if "tres_table" not in query: |
| 175 | + raise pymysql.err.ProgrammingError(1146, "Table 'slurm_acct_db.tres' doesn't exist") |
| 176 | + |
| 177 | + def fetchall(self): |
| 178 | + if "tres_table" in self.query: |
| 179 | + return [{"id": 1, "type": "cpu", "name": ""}] |
| 180 | + return [] |
| 181 | + |
| 182 | + class FakeConn: |
| 183 | + def cursor(self): |
| 184 | + return FakeCursor() |
| 185 | + |
| 186 | + db._conn = FakeConn() |
| 187 | + tmap = db._get_tres_map() |
| 188 | + self.assertEqual(tmap, {1: "cpu"}) |
| 189 | + |
135 | 190 | def test_extract_schema_with_context_manager_closes_connection(self): |
136 | 191 | class FakeCursor: |
137 | 192 | def __init__(self, dbname): |
|
0 commit comments