|
19 | 19 | import mock |
20 | 20 | import unittest |
21 | 21 |
|
22 | | -from pypuppetdbquery import parse, query_facts |
| 22 | +from pypuppetdbquery import parse, query_facts, query_fact_contents |
23 | 23 |
|
24 | 24 |
|
25 | 25 | class _FakeNode(object): |
@@ -168,3 +168,109 @@ def test_query_facts_in_raw_mode(self): |
168 | 168 | node_facts = self._query_facts(mock_pdb, 'foo=bar', raw=True) |
169 | 169 |
|
170 | 170 | self.assertEquals(node_facts, mock_pdb.facts.return_value) |
| 171 | + |
| 172 | + |
| 173 | +class TestFrontendQueryFactContents(unittest.TestCase): |
| 174 | + """ |
| 175 | + Test cases targetting :func:`pypuppetdbquery.query_fact_contents`. |
| 176 | + """ |
| 177 | + |
| 178 | + def _query_fact_contents(self, pdb, s, facts=None, raw=False): |
| 179 | + return query_fact_contents( |
| 180 | + pdb, s, facts, raw, |
| 181 | + lex_options={ |
| 182 | + 'debug': False, |
| 183 | + 'optimize': False, |
| 184 | + }, |
| 185 | + yacc_options={ |
| 186 | + 'debug': False, |
| 187 | + 'optimize': False, |
| 188 | + 'write_tables': False, |
| 189 | + }) |
| 190 | + |
| 191 | + def test_with_query_and_facts_list(self): |
| 192 | + mock_pdb = mock.NonCallableMock() |
| 193 | + mock_pdb.fact_contents = mock.Mock(return_value=[ |
| 194 | + { |
| 195 | + 'value': 14, |
| 196 | + 'certname': 'alpha', |
| 197 | + 'environment': 'production', |
| 198 | + 'path': ['system_uptime', 'days'], |
| 199 | + 'name': 'system_uptime', |
| 200 | + }, |
| 201 | + ]) |
| 202 | + |
| 203 | + out = self._query_fact_contents( |
| 204 | + mock_pdb, 'foo=bar', ['system_uptime.days']) |
| 205 | + |
| 206 | + mock_pdb.fact_contents.assert_called_once_with(query=json.dumps([ |
| 207 | + 'and', |
| 208 | + ['in', 'certname', |
| 209 | + ['extract', 'certname', |
| 210 | + ['select_fact_contents', |
| 211 | + ['and', |
| 212 | + ['=', 'path', ['foo']], |
| 213 | + ['=', 'value', 'bar']]]]], |
| 214 | + ['or', |
| 215 | + ['=', 'path', |
| 216 | + ['system_uptime', 'days']]]])) |
| 217 | + |
| 218 | + self.assertEquals(out, { |
| 219 | + 'alpha': {'system_uptime.days': 14}, |
| 220 | + }) |
| 221 | + |
| 222 | + def test_without_query(self): |
| 223 | + mock_pdb = mock.NonCallableMock() |
| 224 | + mock_pdb.fact_contents = mock.Mock(return_value=[ |
| 225 | + { |
| 226 | + 'value': 14, |
| 227 | + 'certname': 'alpha', |
| 228 | + 'environment': 'production', |
| 229 | + 'path': ['system_uptime', 'days'], |
| 230 | + 'name': 'system_uptime', |
| 231 | + }, |
| 232 | + ]) |
| 233 | + |
| 234 | + out = self._query_fact_contents(mock_pdb, '', ['system_uptime.days']) |
| 235 | + |
| 236 | + mock_pdb.fact_contents.assert_called_once_with(query=json.dumps([ |
| 237 | + 'or', |
| 238 | + ['=', 'path', |
| 239 | + ['system_uptime', 'days']]])) |
| 240 | + |
| 241 | + self.assertEquals(out, { |
| 242 | + 'alpha': {'system_uptime.days': 14}, |
| 243 | + }) |
| 244 | + |
| 245 | + def test_without_either(self): |
| 246 | + out = self._query_fact_contents(None, '') |
| 247 | + self.assertTrue(out is None) |
| 248 | + |
| 249 | + def test_raw_output(self): |
| 250 | + mock_pdb = mock.NonCallableMock() |
| 251 | + mock_pdb.fact_contents = mock.Mock(return_value=[ |
| 252 | + { |
| 253 | + 'value': 14, |
| 254 | + 'certname': 'alpha', |
| 255 | + 'environment': 'production', |
| 256 | + 'path': ['system_uptime', 'days'], |
| 257 | + 'name': 'system_uptime', |
| 258 | + }, |
| 259 | + ]) |
| 260 | + |
| 261 | + out = self._query_fact_contents( |
| 262 | + mock_pdb, 'foo=bar', ['system_uptime.days'], True) |
| 263 | + |
| 264 | + mock_pdb.fact_contents.assert_called_once_with(query=json.dumps([ |
| 265 | + 'and', |
| 266 | + ['in', 'certname', |
| 267 | + ['extract', 'certname', |
| 268 | + ['select_fact_contents', |
| 269 | + ['and', |
| 270 | + ['=', 'path', ['foo']], |
| 271 | + ['=', 'value', 'bar']]]]], |
| 272 | + ['or', |
| 273 | + ['=', 'path', |
| 274 | + ['system_uptime', 'days']]]])) |
| 275 | + |
| 276 | + self.assertEquals(out, mock_pdb.fact_contents.return_value) |
0 commit comments