1# -*- coding: utf-8 -*- 2"""Test CLI Helpers' tabular output preprocessors.""" 3 4from __future__ import unicode_literals 5from decimal import Decimal 6 7import pytest 8 9from cli_helpers.compat import HAS_PYGMENTS 10from cli_helpers.tabular_output.preprocessors import ( 11 align_decimals, 12 bytes_to_string, 13 convert_to_string, 14 quote_whitespaces, 15 override_missing_value, 16 override_tab_value, 17 style_output, 18 format_numbers, 19) 20 21if HAS_PYGMENTS: 22 from pygments.style import Style 23 from pygments.token import Token 24 25import inspect 26import cli_helpers.tabular_output.preprocessors 27import types 28 29 30def test_convert_to_string(): 31 """Test the convert_to_string() function.""" 32 data = [[1, "John"], [2, "Jill"]] 33 headers = [0, "name"] 34 expected = ([["1", "John"], ["2", "Jill"]], ["0", "name"]) 35 results = convert_to_string(data, headers) 36 37 assert expected == (list(results[0]), results[1]) 38 39 40def test_override_missing_values(): 41 """Test the override_missing_values() function.""" 42 data = [[1, None], [2, "Jill"]] 43 headers = [0, "name"] 44 expected = ([[1, "<EMPTY>"], [2, "Jill"]], [0, "name"]) 45 results = override_missing_value(data, headers, missing_value="<EMPTY>") 46 47 assert expected == (list(results[0]), results[1]) 48 49 50@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library") 51def test_override_missing_value_with_style(): 52 """Test that *override_missing_value()* styles output.""" 53 54 class NullStyle(Style): 55 styles = {Token.Output.Null: "#0f0"} 56 57 headers = ["h1", "h2"] 58 data = [[None, "2"], ["abc", None]] 59 60 expected_headers = ["h1", "h2"] 61 expected_data = [ 62 ["\x1b[38;5;10m<null>\x1b[39m", "2"], 63 ["abc", "\x1b[38;5;10m<null>\x1b[39m"], 64 ] 65 results = override_missing_value( 66 data, headers, style=NullStyle, missing_value="<null>" 67 ) 68 69 assert (expected_data, expected_headers) == (list(results[0]), results[1]) 70 71 72def test_override_tab_value(): 73 """Test the override_tab_value() function.""" 74 data = [[1, "\tJohn"], [2, "Jill"]] 75 headers = ["id", "name"] 76 expected = ([[1, " John"], [2, "Jill"]], ["id", "name"]) 77 results = override_tab_value(data, headers) 78 79 assert expected == (list(results[0]), results[1]) 80 81 82def test_bytes_to_string(): 83 """Test the bytes_to_string() function.""" 84 data = [[1, "John"], [2, b"Jill"]] 85 headers = [0, "name"] 86 expected = ([[1, "John"], [2, "Jill"]], [0, "name"]) 87 results = bytes_to_string(data, headers) 88 89 assert expected == (list(results[0]), results[1]) 90 91 92def test_align_decimals(): 93 """Test the align_decimals() function.""" 94 data = [[Decimal("200"), Decimal("1")], [Decimal("1.00002"), Decimal("1.0")]] 95 headers = ["num1", "num2"] 96 column_types = (float, float) 97 expected = ([["200", "1"], [" 1.00002", "1.0"]], ["num1", "num2"]) 98 results = align_decimals(data, headers, column_types=column_types) 99 100 assert expected == (list(results[0]), results[1]) 101 102 103def test_align_decimals_empty_result(): 104 """Test align_decimals() with no results.""" 105 data = [] 106 headers = ["num1", "num2"] 107 column_types = () 108 expected = ([], ["num1", "num2"]) 109 results = align_decimals(data, headers, column_types=column_types) 110 111 assert expected == (list(results[0]), results[1]) 112 113 114def test_align_decimals_non_decimals(): 115 """Test align_decimals() with non-decimals.""" 116 data = [[Decimal("200.000"), Decimal("1.000")], [None, None]] 117 headers = ["num1", "num2"] 118 column_types = (float, float) 119 expected = ([["200.000", "1.000"], [None, None]], ["num1", "num2"]) 120 results = align_decimals(data, headers, column_types=column_types) 121 122 assert expected == (list(results[0]), results[1]) 123 124 125def test_quote_whitespaces(): 126 """Test the quote_whitespaces() function.""" 127 data = [[" before", "after "], [" both ", "none"]] 128 headers = ["h1", "h2"] 129 expected = ([["' before'", "'after '"], ["' both '", "'none'"]], ["h1", "h2"]) 130 results = quote_whitespaces(data, headers) 131 132 assert expected == (list(results[0]), results[1]) 133 134 135def test_quote_whitespaces_empty_result(): 136 """Test the quote_whitespaces() function with no results.""" 137 data = [] 138 headers = ["h1", "h2"] 139 expected = ([], ["h1", "h2"]) 140 results = quote_whitespaces(data, headers) 141 142 assert expected == (list(results[0]), results[1]) 143 144 145def test_quote_whitespaces_non_spaces(): 146 """Test the quote_whitespaces() function with non-spaces.""" 147 data = [["\tbefore", "after \r"], ["\n both ", "none"]] 148 headers = ["h1", "h2"] 149 expected = ([["'\tbefore'", "'after \r'"], ["'\n both '", "'none'"]], ["h1", "h2"]) 150 results = quote_whitespaces(data, headers) 151 152 assert expected == (list(results[0]), results[1]) 153 154 155@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library") 156def test_style_output_no_styles(): 157 """Test that *style_output()* does not style without styles.""" 158 headers = ["h1", "h2"] 159 data = [["1", "2"], ["a", "b"]] 160 results = style_output(data, headers) 161 162 assert (data, headers) == (list(results[0]), results[1]) 163 164 165@pytest.mark.skipif(HAS_PYGMENTS, reason="requires the Pygments library be missing") 166def test_style_output_no_pygments(): 167 """Test that *style_output()* does not try to style without Pygments.""" 168 headers = ["h1", "h2"] 169 data = [["1", "2"], ["a", "b"]] 170 results = style_output(data, headers) 171 172 assert (data, headers) == (list(results[0]), results[1]) 173 174 175@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library") 176def test_style_output(): 177 """Test that *style_output()* styles output.""" 178 179 class CliStyle(Style): 180 default_style = "" 181 styles = { 182 Token.Output.Header: "bold ansibrightred", 183 Token.Output.OddRow: "bg:#eee #111", 184 Token.Output.EvenRow: "#0f0", 185 } 186 187 headers = ["h1", "h2"] 188 data = [["观音", "2"], ["Ποσειδῶν", "b"]] 189 190 expected_headers = ["\x1b[91;01mh1\x1b[39;00m", "\x1b[91;01mh2\x1b[39;00m"] 191 expected_data = [ 192 ["\x1b[38;5;233;48;5;7m观音\x1b[39;49m", "\x1b[38;5;233;48;5;7m2\x1b[39;49m"], 193 ["\x1b[38;5;10mΠοσειδῶν\x1b[39m", "\x1b[38;5;10mb\x1b[39m"], 194 ] 195 results = style_output(data, headers, style=CliStyle) 196 197 assert (expected_data, expected_headers) == (list(results[0]), results[1]) 198 199 200@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library") 201def test_style_output_with_newlines(): 202 """Test that *style_output()* styles output with newlines in it.""" 203 204 class CliStyle(Style): 205 default_style = "" 206 styles = { 207 Token.Output.Header: "bold ansibrightred", 208 Token.Output.OddRow: "bg:#eee #111", 209 Token.Output.EvenRow: "#0f0", 210 } 211 212 headers = ["h1", "h2"] 213 data = [["观音\nLine2", "Ποσειδῶν"]] 214 215 expected_headers = ["\x1b[91;01mh1\x1b[39;00m", "\x1b[91;01mh2\x1b[39;00m"] 216 expected_data = [ 217 [ 218 "\x1b[38;5;233;48;5;7m观音\x1b[39;49m\n\x1b[38;5;233;48;5;7m" 219 "Line2\x1b[39;49m", 220 "\x1b[38;5;233;48;5;7mΠοσειδῶν\x1b[39;49m", 221 ] 222 ] 223 results = style_output(data, headers, style=CliStyle) 224 225 assert (expected_data, expected_headers) == (list(results[0]), results[1]) 226 227 228@pytest.mark.skipif(not HAS_PYGMENTS, reason="requires the Pygments library") 229def test_style_output_custom_tokens(): 230 """Test that *style_output()* styles output with custom token names.""" 231 232 class CliStyle(Style): 233 default_style = "" 234 styles = { 235 Token.Results.Headers: "bold ansibrightred", 236 Token.Results.OddRows: "bg:#eee #111", 237 Token.Results.EvenRows: "#0f0", 238 } 239 240 headers = ["h1", "h2"] 241 data = [["1", "2"], ["a", "b"]] 242 243 expected_headers = ["\x1b[91;01mh1\x1b[39;00m", "\x1b[91;01mh2\x1b[39;00m"] 244 expected_data = [ 245 ["\x1b[38;5;233;48;5;7m1\x1b[39;49m", "\x1b[38;5;233;48;5;7m2\x1b[39;49m"], 246 ["\x1b[38;5;10ma\x1b[39m", "\x1b[38;5;10mb\x1b[39m"], 247 ] 248 249 output = style_output( 250 data, 251 headers, 252 style=CliStyle, 253 header_token="Token.Results.Headers", 254 odd_row_token="Token.Results.OddRows", 255 even_row_token="Token.Results.EvenRows", 256 ) 257 258 assert (expected_data, expected_headers) == (list(output[0]), output[1]) 259 260 261def test_format_integer(): 262 """Test formatting for an INTEGER datatype.""" 263 data = [[1], [1000], [1000000]] 264 headers = ["h1"] 265 result_data, result_headers = format_numbers( 266 data, headers, column_types=(int,), integer_format=",", float_format="," 267 ) 268 269 expected = [["1"], ["1,000"], ["1,000,000"]] 270 assert expected == list(result_data) 271 assert headers == result_headers 272 273 274def test_format_decimal(): 275 """Test formatting for a DECIMAL(12, 4) datatype.""" 276 data = [[Decimal("1.0000")], [Decimal("1000.0000")], [Decimal("1000000.0000")]] 277 headers = ["h1"] 278 result_data, result_headers = format_numbers( 279 data, headers, column_types=(float,), integer_format=",", float_format="," 280 ) 281 282 expected = [["1.0000"], ["1,000.0000"], ["1,000,000.0000"]] 283 assert expected == list(result_data) 284 assert headers == result_headers 285 286 287def test_format_float(): 288 """Test formatting for a REAL datatype.""" 289 data = [[1.0], [1000.0], [1000000.0]] 290 headers = ["h1"] 291 result_data, result_headers = format_numbers( 292 data, headers, column_types=(float,), integer_format=",", float_format="," 293 ) 294 expected = [["1.0"], ["1,000.0"], ["1,000,000.0"]] 295 assert expected == list(result_data) 296 assert headers == result_headers 297 298 299def test_format_integer_only(): 300 """Test that providing one format string works.""" 301 data = [[1, 1.0], [1000, 1000.0], [1000000, 1000000.0]] 302 headers = ["h1", "h2"] 303 result_data, result_headers = format_numbers( 304 data, headers, column_types=(int, float), integer_format="," 305 ) 306 307 expected = [["1", 1.0], ["1,000", 1000.0], ["1,000,000", 1000000.0]] 308 assert expected == list(result_data) 309 assert headers == result_headers 310 311 312def test_format_numbers_no_format_strings(): 313 """Test that numbers aren't formatted without format strings.""" 314 data = ((1), (1000), (1000000)) 315 headers = ("h1",) 316 result_data, result_headers = format_numbers(data, headers, column_types=(int,)) 317 assert list(data) == list(result_data) 318 assert headers == result_headers 319 320 321def test_format_numbers_no_column_types(): 322 """Test that numbers aren't formatted without column types.""" 323 data = ((1), (1000), (1000000)) 324 headers = ("h1",) 325 result_data, result_headers = format_numbers( 326 data, headers, integer_format=",", float_format="," 327 ) 328 assert list(data) == list(result_data) 329 assert headers == result_headers 330 331 332def test_enforce_iterable(): 333 preprocessors = inspect.getmembers( 334 cli_helpers.tabular_output.preprocessors, inspect.isfunction 335 ) 336 loremipsum = ( 337 "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod".split( 338 " " 339 ) 340 ) 341 for name, preprocessor in preprocessors: 342 preprocessed = preprocessor(zip(loremipsum), ["lorem"], column_types=(str,)) 343 try: 344 first = next(preprocessed[0]) 345 except StopIteration: 346 assert False, "{} gives no output with iterator data".format(name) 347 except TypeError: 348 assert False, "{} doesn't return iterable".format(name) 349 if isinstance(preprocessed[1], types.GeneratorType): 350 assert False, "{} returns headers as iterator".format(name) 351