|
13 | 13 | import re |
14 | 14 | import sys |
15 | 15 | import types |
| 16 | +from collections import namedtuple |
16 | 17 |
|
17 | 18 | # 3rd party |
18 | 19 | import pytest |
19 | 20 |
|
20 | 21 | # this package |
21 | 22 | from domdf_python_tools import utils |
22 | 23 | from domdf_python_tools.testing import testing_boolean_values |
| 24 | +from domdf_python_tools.utils import HasHead, head |
23 | 25 |
|
24 | 26 |
|
25 | 27 | def test_pyversion(): |
@@ -433,3 +435,74 @@ def test_convert_indents(): |
433 | 435 | assert utils.convert_indents("hello world", tab_width=2, from_=" ") == "hello world" |
434 | 436 | assert utils.convert_indents(" hello world", tab_width=2, from_=" ") == " hello world" |
435 | 437 | assert utils.convert_indents(" hello world", tab_width=2, from_=" ") == " hello world" |
| 438 | + |
| 439 | + |
| 440 | +class TestHead: |
| 441 | + |
| 442 | + def test_protocol(self): |
| 443 | + assert not isinstance(str, HasHead) |
| 444 | + assert not isinstance(int, HasHead) |
| 445 | + assert not isinstance(float, HasHead) |
| 446 | + assert not isinstance(tuple, HasHead) |
| 447 | + assert not isinstance(list, HasHead) |
| 448 | + |
| 449 | + def test_protocol_pandas(self): |
| 450 | + pandas = pytest.importorskip("pandas") |
| 451 | + |
| 452 | + assert isinstance(pandas.DataFrame, HasHead) |
| 453 | + assert isinstance(pandas.Series, HasHead) |
| 454 | + |
| 455 | + def test_namedtuple(self): |
| 456 | + foo = namedtuple("foo", "a, b, c, d, e, f, g, h, i, j, k, l, m") |
| 457 | + assert head( |
| 458 | + foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) |
| 459 | + ) == "foo(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, ...)" |
| 460 | + assert head( |
| 461 | + foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), 13 |
| 462 | + ) == "foo(a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12, m=13)" |
| 463 | + |
| 464 | + def test_tuple(self): |
| 465 | + assert head((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) == "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...)" |
| 466 | + assert head((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), |
| 467 | + 13) == "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)" |
| 468 | + |
| 469 | + def test_list(self): |
| 470 | + assert head([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]" |
| 471 | + assert head([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], |
| 472 | + 13) == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]" |
| 473 | + |
| 474 | + def test_data_frame(self): |
| 475 | + pandas = pytest.importorskip("pandas") |
| 476 | + |
| 477 | + df = pandas.DataFrame( |
| 478 | + data=[["Bob", 20, "Apprentice"], ["Alice", 23, "Secretary"], ["Mario", 39, "Plumber"]], |
| 479 | + columns=["Name", "Age", "Occupation"], |
| 480 | + ) |
| 481 | + assert head( |
| 482 | + df |
| 483 | + ) == """ Name Age Occupation |
| 484 | +0 Bob 20 Apprentice |
| 485 | +1 Alice 23 Secretary |
| 486 | +2 Mario 39 Plumber\ |
| 487 | +""" |
| 488 | + assert head(df, 1) == " Name Age Occupation\n0 Bob 20 Apprentice" |
| 489 | + |
| 490 | + def test_series(self): |
| 491 | + pandas = pytest.importorskip("pandas") |
| 492 | + |
| 493 | + df = pandas.DataFrame( |
| 494 | + data=[["Bob", 20, "Apprentice"], ["Alice", 23, "Secretary"], ["Mario", 39, "Plumber"]], |
| 495 | + columns=["Name", "Age", "Occupation"], |
| 496 | + ) |
| 497 | + ser = df.iloc[0] |
| 498 | + assert head(ser) == """\ |
| 499 | +Name Bob |
| 500 | +Age 20 |
| 501 | +Occupation Apprentice\ |
| 502 | +""" |
| 503 | + assert head(ser, 1) == "Name Bob" |
| 504 | + |
| 505 | + def test_str(self): |
| 506 | + assert head("Hello World") == "Hello Worl..." |
| 507 | + assert head("Hello World", 11) == "Hello World" |
| 508 | + assert head("Hello World", 5) == "Hello..." |
0 commit comments