1556
1556
def test_DumpJSON_normal(self):
1557
output = command.DumpJSON().output(self.clients.values())
1558
json_data = json.loads(output)
1557
with self.capture_stdout_to_buffer() as buffer:
1558
command.DumpJSON().run(self.clients)
1559
json_data = json.loads(buffer.getvalue())
1559
1560
self.assertDictEqual(self.expected_json, json_data)
1563
@contextlib.contextmanager
1564
def capture_stdout_to_buffer():
1565
capture_buffer = io.StringIO()
1566
old_stdout = sys.stdout
1567
sys.stdout = capture_buffer
1569
yield capture_buffer
1571
sys.stdout = old_stdout
1561
1573
def test_DumpJSON_one_client(self):
1562
output = command.DumpJSON().output(self.one_client.values())
1563
json_data = json.loads(output)
1574
with self.capture_stdout_to_buffer() as buffer:
1575
command.DumpJSON().run(self.one_client)
1576
json_data = json.loads(buffer.getvalue())
1564
1577
expected_json = {"foo": self.expected_json["foo"]}
1565
1578
self.assertDictEqual(expected_json, json_data)
1567
1580
def test_PrintTable_normal(self):
1568
output = command.PrintTable().output(self.clients.values())
1581
with self.capture_stdout_to_buffer() as buffer:
1582
command.PrintTable().run(self.clients)
1569
1583
expected_output = "\n".join((
1570
1584
"Name Enabled Timeout Last Successful Check",
1571
1585
"foo Yes 00:05:00 2019-02-03T00:00:00 ",
1572
1586
"barbar Yes 00:05:00 2019-02-04T00:00:00 ",
1574
self.assertEqual(expected_output, output)
1588
self.assertEqual(expected_output, buffer.getvalue())
1576
1590
def test_PrintTable_verbose(self):
1577
output = command.PrintTable(verbose=True).output(
1578
self.clients.values())
1591
with self.capture_stdout_to_buffer() as buffer:
1592
command.PrintTable(verbose=True).run(self.clients)
1665
1679
num_lines = max(len(rows) for rows in columns)
1666
expected_output = "\n".join("".join(rows[line]
1667
for rows in columns)
1668
for line in range(num_lines))
1669
self.assertEqual(expected_output, output)
1680
expected_output = ("\n".join("".join(rows[line]
1681
for rows in columns)
1682
for line in range(num_lines))
1684
self.assertEqual(expected_output, buffer.getvalue())
1671
1686
def test_PrintTable_one_client(self):
1672
output = command.PrintTable().output(self.one_client.values())
1687
with self.capture_stdout_to_buffer() as buffer:
1688
command.PrintTable().run(self.one_client)
1673
1689
expected_output = "\n".join((
1674
1690
"Name Enabled Timeout Last Successful Check",
1675
1691
"foo Yes 00:05:00 2019-02-03T00:00:00 ",
1677
self.assertEqual(expected_output, output)
1693
self.assertEqual(expected_output, buffer.getvalue())
1680
1696
class TestPropertyCmd(TestCommand):