Revision 3b877f08 test/ganeti.query_unittest.py
b/test/ganeti.query_unittest.py | ||
---|---|---|
1317 | 1317 |
{ "name": "node2", "other": ["x", "y", "bar"], }, |
1318 | 1318 |
{ "name": "node3", "other": "Hello", }, |
1319 | 1319 |
{ "name": "node1", "other": ["a", "b", "foo"], }, |
1320 |
{ "name": "empty", "other": []}, |
|
1320 | 1321 |
] |
1321 | 1322 |
|
1322 | 1323 |
q = query.Query(fielddefs, ["name", "other"], namefield="name", |
... | ... | |
1343 | 1344 |
["node2", ["x", "y", "bar"]], |
1344 | 1345 |
]) |
1345 | 1346 |
|
1347 |
# Boolean test |
|
1348 |
q = query.Query(fielddefs, ["name", "other"], namefield="name", |
|
1349 |
filter_=["?", "other"]) |
|
1350 |
self.assertEqual(q.OldStyleQuery(data), [ |
|
1351 |
["node1", ["a", "b", "foo"]], |
|
1352 |
["node2", ["x", "y", "bar"]], |
|
1353 |
["node3", "Hello"], |
|
1354 |
]) |
|
1355 |
|
|
1356 |
q = query.Query(fielddefs, ["name", "other"], namefield="name", |
|
1357 |
filter_=["!", ["?", "other"]]) |
|
1358 |
self.assertEqual(q.OldStyleQuery(data), [ |
|
1359 |
["empty", []], |
|
1360 |
]) |
|
1361 |
|
|
1346 | 1362 |
def testFilterHostname(self): |
1347 | 1363 |
fielddefs = query._PrepareFieldList([ |
1348 | 1364 |
(query._MakeField("name", "Name", constants.QFT_TEXT, "Name"), |
... | ... | |
1402 | 1418 |
["node2.example.net"], |
1403 | 1419 |
]) |
1404 | 1420 |
|
1421 |
def testFilterBoolean(self): |
|
1422 |
fielddefs = query._PrepareFieldList([ |
|
1423 |
(query._MakeField("name", "Name", constants.QFT_TEXT, "Name"), |
|
1424 |
None, query.QFF_HOSTNAME, lambda ctx, item: item["name"]), |
|
1425 |
(query._MakeField("value", "Value", constants.QFT_BOOL, "Value"), |
|
1426 |
None, 0, lambda ctx, item: item["value"]), |
|
1427 |
], []) |
|
1428 |
|
|
1429 |
data = [ |
|
1430 |
{ "name": "node1", "value": False, }, |
|
1431 |
{ "name": "node2", "value": True, }, |
|
1432 |
{ "name": "node3", "value": True, }, |
|
1433 |
] |
|
1434 |
|
|
1435 |
q = query.Query(fielddefs, ["name", "value"], |
|
1436 |
filter_=["|", ["=", "value", False], |
|
1437 |
["=", "value", True]]) |
|
1438 |
self.assertTrue(q.RequestedNames() is None) |
|
1439 |
self.assertEqual(q.Query(data), [ |
|
1440 |
[(constants.RS_NORMAL, "node1"), (constants.RS_NORMAL, False)], |
|
1441 |
[(constants.RS_NORMAL, "node2"), (constants.RS_NORMAL, True)], |
|
1442 |
[(constants.RS_NORMAL, "node3"), (constants.RS_NORMAL, True)], |
|
1443 |
]) |
|
1444 |
|
|
1445 |
q = query.Query(fielddefs, ["name", "value"], |
|
1446 |
filter_=["|", ["=", "value", False], |
|
1447 |
["!", ["=", "value", False]]]) |
|
1448 |
self.assertTrue(q.RequestedNames() is None) |
|
1449 |
self.assertEqual(q.Query(data), [ |
|
1450 |
[(constants.RS_NORMAL, "node1"), (constants.RS_NORMAL, False)], |
|
1451 |
[(constants.RS_NORMAL, "node2"), (constants.RS_NORMAL, True)], |
|
1452 |
[(constants.RS_NORMAL, "node3"), (constants.RS_NORMAL, True)], |
|
1453 |
]) |
|
1454 |
|
|
1455 |
# Comparing bool with string |
|
1456 |
for i in ["False", "True", "0", "1", "no", "yes", "N", "Y"]: |
|
1457 |
self.assertRaises(errors.ParameterError, query.Query, |
|
1458 |
fielddefs, ["name", "value"], |
|
1459 |
filter_=["=", "value", i]) |
|
1460 |
|
|
1461 |
# Truth filter |
|
1462 |
q = query.Query(fielddefs, ["name", "value"], filter_=["?", "value"]) |
|
1463 |
self.assertTrue(q.RequestedNames() is None) |
|
1464 |
self.assertEqual(q.Query(data), [ |
|
1465 |
[(constants.RS_NORMAL, "node2"), (constants.RS_NORMAL, True)], |
|
1466 |
[(constants.RS_NORMAL, "node3"), (constants.RS_NORMAL, True)], |
|
1467 |
]) |
|
1468 |
|
|
1469 |
# Negative bool filter |
|
1470 |
q = query.Query(fielddefs, ["name", "value"], filter_=["!", ["?", "value"]]) |
|
1471 |
self.assertTrue(q.RequestedNames() is None) |
|
1472 |
self.assertEqual(q.Query(data), [ |
|
1473 |
[(constants.RS_NORMAL, "node1"), (constants.RS_NORMAL, False)], |
|
1474 |
]) |
|
1475 |
|
|
1476 |
# Complex truth filter |
|
1477 |
q = query.Query(fielddefs, ["name", "value"], |
|
1478 |
filter_=["|", ["&", ["=", "name", "node1"], |
|
1479 |
["!", ["?", "value"]]], |
|
1480 |
["?", "value"]]) |
|
1481 |
self.assertTrue(q.RequestedNames() is None) |
|
1482 |
self.assertEqual(q.Query(data), [ |
|
1483 |
[(constants.RS_NORMAL, "node1"), (constants.RS_NORMAL, False)], |
|
1484 |
[(constants.RS_NORMAL, "node2"), (constants.RS_NORMAL, True)], |
|
1485 |
[(constants.RS_NORMAL, "node3"), (constants.RS_NORMAL, True)], |
|
1486 |
]) |
|
1487 |
|
|
1405 | 1488 |
|
1406 | 1489 |
if __name__ == "__main__": |
1407 | 1490 |
testutils.GanetiTestProgram() |
Also available in: Unified diff