Revision 4d99964c test/py/ganeti.cli_unittest.py

b/test/py/ganeti.cli_unittest.py
1044 1044
                     ["zname", kind, "Ztitle", "zzz doc zzz"])
1045 1045

  
1046 1046

  
1047
class TestSerializeGenericInfo(unittest.TestCase):
1048
  """Test case for cli._SerializeGenericInfo"""
1049
  def _RunTest(self, data, expected):
1050
    buf = StringIO()
1051
    cli._SerializeGenericInfo(buf, data, 0)
1052
    self.assertEqual(buf.getvalue(), expected)
1053

  
1054
  def testSimple(self):
1055
    test_samples = [
1056
      ("abc", "abc\n"),
1057
      ([], "\n"),
1058
      ({}, "\n"),
1059
      (["1", "2", "3"], "- 1\n- 2\n- 3\n"),
1060
      ([("z", "26")], "z: 26\n"),
1061
      ({"z": "26"}, "z: 26\n"),
1062
      ([("z", "26"), ("a", "1")], "z: 26\na: 1\n"),
1063
      ({"z": "26", "a": "1"}, "a: 1\nz: 26\n"),
1064
      ]
1065
    for (data, expected) in test_samples:
1066
      self._RunTest(data, expected)
1067

  
1068
  def testLists(self):
1069
    adict = {
1070
      "aa": "11",
1071
      "bb": "22",
1072
      "cc": "33",
1073
      }
1074
    adict_exp = ("- aa: 11\n"
1075
                 "  bb: 22\n"
1076
                 "  cc: 33\n")
1077
    anobj = [
1078
      ("zz", "11"),
1079
      ("ww", "33"),
1080
      ("xx", "22"),
1081
      ]
1082
    anobj_exp = ("- zz: 11\n"
1083
                 "  ww: 33\n"
1084
                 "  xx: 22\n")
1085
    alist = ["aa", "cc", "bb"]
1086
    alist_exp = ("- - aa\n"
1087
                 "  - cc\n"
1088
                 "  - bb\n")
1089
    test_samples = [
1090
      (adict, adict_exp),
1091
      (anobj, anobj_exp),
1092
      (alist, alist_exp),
1093
      ]
1094
    for (base_data, base_expected) in test_samples:
1095
      for k in range(1, 4):
1096
        data = k * [base_data]
1097
        expected = k * base_expected
1098
        self._RunTest(data, expected)
1099

  
1100
  def testDictionaries(self):
1101
    data = [
1102
      ("aaa", ["x", "y"]),
1103
      ("bbb", {
1104
          "w": "1",
1105
          "z": "2",
1106
          }),
1107
      ("ccc", [
1108
          ("xyz", "123"),
1109
          ("efg", "456"),
1110
          ]),
1111
      ]
1112
    expected = ("aaa: \n"
1113
                "  - x\n"
1114
                "  - y\n"
1115
                "bbb: \n"
1116
                "  w: 1\n"
1117
                "  z: 2\n"
1118
                "ccc: \n"
1119
                "  xyz: 123\n"
1120
                "  efg: 456\n")
1121
    self._RunTest(data, expected)
1122
    self._RunTest(dict(data), expected)
1123

  
1124

  
1047 1125
if __name__ == "__main__":
1048 1126
  testutils.GanetiTestProgram()

Also available in: Unified diff