Revision 2ebf1568 test/ganeti.cli_unittest.py

b/test/ganeti.cli_unittest.py
124 124
      cli._ToStream(buf, "foo %s %s", "a", "b")
125 125
      self.failUnlessEqual(buf.getvalue(), "foo a b\n")
126 126

  
127

  
128
class TestGenerateTable(unittest.TestCase):
129
  HEADERS = dict([("f%s" % i, "Field%s" % i) for i in range(5)])
130

  
131
  FIELDS1 = ["f1", "f2"]
132
  DATA1 = [
133
    ["abc", 1234],
134
    ["foobar", 56],
135
    ["b", -14],
136
    ]
137

  
138
  def _test(self, headers, fields, separator, data,
139
            numfields, unitfields, units, expected):
140
    table = cli.GenerateTable(headers, fields, separator, data,
141
                              numfields=numfields, unitfields=unitfields,
142
                              units=units)
143
    self.assertEqual(table, expected)
144

  
145
  def testPlain(self):
146
    exp = [
147
      "Field1 Field2",
148
      "abc    1234",
149
      "foobar 56",
150
      "b      -14",
151
      ]
152
    self._test(self.HEADERS, self.FIELDS1, None, self.DATA1,
153
               None, None, "m", exp)
154

  
155
  def testNoFields(self):
156
    self._test(self.HEADERS, [], None, [[], []],
157
               None, None, "m", ["", "", ""])
158
    self._test(None, [], None, [[], []],
159
               None, None, "m", ["", ""])
160

  
161
  def testSeparator(self):
162
    for sep in ["#", ":", ",", "^", "!", "%", "|", "###", "%%", "!!!", "||"]:
163
      exp = [
164
        "Field1%sField2" % sep,
165
        "abc%s1234" % sep,
166
        "foobar%s56" % sep,
167
        "b%s-14" % sep,
168
        ]
169
      self._test(self.HEADERS, self.FIELDS1, sep, self.DATA1,
170
                 None, None, "m", exp)
171

  
172
  def testNoHeader(self):
173
    exp = [
174
      "abc    1234",
175
      "foobar 56",
176
      "b      -14",
177
      ]
178
    self._test(None, self.FIELDS1, None, self.DATA1,
179
               None, None, "m", exp)
180

  
181
  def testUnknownField(self):
182
    headers = {
183
      "f1": "Field1",
184
      }
185
    exp = [
186
      "Field1 UNKNOWN",
187
      "abc    1234",
188
      "foobar 56",
189
      "b      -14",
190
      ]
191
    self._test(headers, ["f1", "UNKNOWN"], None, self.DATA1,
192
               None, None, "m", exp)
193

  
194
  def testNumfields(self):
195
    fields = ["f1", "f2", "f3"]
196
    data = [
197
      ["abc", 1234, 0],
198
      ["foobar", 56, 3],
199
      ["b", -14, "-"],
200
      ]
201
    exp = [
202
      "Field1 Field2 Field3",
203
      "abc      1234      0",
204
      "foobar     56      3",
205
      "b         -14      -",
206
      ]
207
    self._test(self.HEADERS, fields, None, data,
208
               ["f2", "f3"], None, "m", exp)
209

  
210
  def testUnitfields(self):
211
    expnosep = [
212
      "Field1 Field2 Field3",
213
      "abc      1234     0M",
214
      "foobar     56     3M",
215
      "b         -14      -",
216
      ]
217

  
218
    expsep = [
219
      "Field1:Field2:Field3",
220
      "abc:1234:0M",
221
      "foobar:56:3M",
222
      "b:-14:-",
223
      ]
224

  
225
    for sep, expected in [(None, expnosep), (":", expsep)]:
226
      fields = ["f1", "f2", "f3"]
227
      data = [
228
        ["abc", 1234, 0],
229
        ["foobar", 56, 3],
230
        ["b", -14, "-"],
231
        ]
232
      self._test(self.HEADERS, fields, sep, data,
233
                 ["f2", "f3"], ["f3"], "h", expected)
234

  
235
  def testUnusual(self):
236
    data = [
237
      ["%", "xyz"],
238
      ["%%", "abc"],
239
      ]
240
    exp = [
241
      "Field1 Field2",
242
      "%      xyz",
243
      "%%     abc",
244
      ]
245
    self._test(self.HEADERS, ["f1", "f2"], None, data,
246
               None, None, "m", exp)
247

  
248

  
127 249
if __name__ == '__main__':
128 250
  testutils.GanetiTestProgram()

Also available in: Unified diff