Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor some tests to use pytest.mark.parametrize #108

Merged
merged 7 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.sw[po]
.coverage
.eggs
.testmondata
.tox
build
dist
Expand Down
77 changes: 28 additions & 49 deletions tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,34 @@

"""Tests for filesize humanizing."""

import pytest
from humanize import filesize

from .base import HumanizeTestCase


class FilesizeTestCase(HumanizeTestCase):
def test_naturalsize(self):
tests = (
300,
3000,
3000000,
3000000000,
3000000000000,
(300, True),
(3000, True),
(3000000, True),
(300, False, True),
(3000, False, True),
(3000000, False, True),
(1024, False, True),
(10 ** 26 * 30, False, True),
(10 ** 26 * 30, True),
10 ** 26 * 30,
(1, False, False),
(3141592, False, False, "%.2f"),
(3000, False, True, "%.3f"),
(3000000000, False, True, "%.0f"),
(10 ** 26 * 30, True, False, "%.3f"),
)
results = (
"300 Bytes",
"3.0 kB",
"3.0 MB",
"3.0 GB",
"3.0 TB",
"300 Bytes",
"2.9 KiB",
"2.9 MiB",
"300B",
"2.9K",
"2.9M",
"1.0K",
"2481.5Y",
"2481.5 YiB",
"3000.0 YB",
"1 Byte",
"3.14 MB",
"2.930K",
"3G",
"2481.542 YiB",
)
self.assertManyResults(filesize.naturalsize, tests, results)
@pytest.mark.parametrize(
"test_args, expected",
[
([300], "300 Bytes"),
([3000], "3.0 kB"),
([3000000], "3.0 MB"),
([3000000000], "3.0 GB"),
([3000000000000], "3.0 TB"),
([300, True], "300 Bytes"),
([3000, True], "2.9 KiB"),
([3000000, True], "2.9 MiB"),
([300, False, True], "300B"),
([3000, False, True], "2.9K"),
([3000000, False, True], "2.9M"),
([1024, False, True], "1.0K"),
([10 ** 26 * 30, False, True], "2481.5Y"),
([10 ** 26 * 30, True], "2481.5 YiB"),
([10 ** 26 * 30], "3000.0 YB"),
([1, False, False], "1 Byte"),
([3141592, False, False, "%.2f"], "3.14 MB"),
([3000, False, True, "%.3f"], "2.930K"),
([3000000000, False, True, "%.0f"], "3G"),
([10 ** 26 * 30, True, False, "%.3f"], "2481.542 YiB"),
],
)
def test_naturaltime_minimum_unit_default(test_args, expected):
assert filesize.naturalsize(*test_args) == expected
208 changes: 99 additions & 109 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,110 @@

"""Number tests."""

import pytest
from humanize import number

from .base import HumanizeTestCase

@pytest.mark.parametrize(
"test_input, expected",
[
("1", "1st"),
("2", "2nd"),
("3", "3rd"),
("4", "4th"),
("11", "11th"),
("12", "12th"),
("13", "13th"),
("101", "101st"),
("102", "102nd"),
("103", "103rd"),
("111", "111th"),
("something else", "something else"),
(None, None),
],
)
def test_ordinal(test_input, expected):
assert number.ordinal(test_input) == expected

class NumberTestCase(HumanizeTestCase):
def test_ordinal(self):
test_list = (
"1",
"2",
"3",
"4",
"11",
"12",
"13",
"101",
"102",
"103",
"111",
"something else",
None,
)
result_list = (
"1st",
"2nd",
"3rd",
"4th",
"11th",
"12th",
"13th",
"101st",
"102nd",
"103rd",
"111th",
"something else",
None,
)
self.assertManyResults(number.ordinal, test_list, result_list)

def test_intcomma(self):
test_list = (
100,
1000,
10123,
10311,
1000000,
1234567.25,
"100",
"1000",
"10123",
"10311",
"1000000",
"1234567.1234567",
None,
)
result_list = (
"100",
"1,000",
"10,123",
"10,311",
"1,000,000",
"1,234,567.25",
"100",
"1,000",
"10,123",
"10,311",
"1,000,000",
"1,234,567.1234567",
None,
)
self.assertManyResults(number.intcomma, test_list, result_list)
@pytest.mark.parametrize(
"test_input, expected",
[
(100, "100"),
(1000, "1,000"),
(10123, "10,123"),
(10311, "10,311"),
(1000000, "1,000,000"),
(1234567.25, "1,234,567.25"),
("100", "100"),
("1000", "1,000"),
("10123", "10,123"),
("10311", "10,311"),
("1000000", "1,000,000"),
("1234567.1234567", "1,234,567.1234567"),
(None, None),
],
)
def test_intcomma(test_input, expected):
assert number.intcomma(test_input) == expected

def test_intword(self):
# make sure that powers & human_powers have the same number of items
self.assertEqual(len(number.powers), len(number.human_powers))
# test the result of intword
test_list = (
"100",
"1000000",
"1200000",
"1290000",
"1000000000",
"2000000000",
"6000000000000",
"1300000000000000",
"3500000000000000000000",
"8100000000000000000000000000000000",
None,
("1230000", "%0.2f"),
10 ** 101,
)
result_list = (
"100",
"1.0 million",
"1.2 million",
"1.3 million",
"1.0 billion",
"2.0 billion",
"6.0 trillion",
"1.3 quadrillion",
"3.5 sextillion",
"8.1 decillion",
None,
"1.23 million",
"1" + "0" * 101,
)
self.assertManyResults(number.intword, test_list, result_list)

def test_apnumber(self):
test_list = (1, 2, 4, 5, 9, 10, "7", None)
result_list = ("one", "two", "four", "five", "nine", "10", "seven", None)
self.assertManyResults(number.apnumber, test_list, result_list)
def test_intword_powers():
# make sure that powers & human_powers have the same number of items
assert len(number.powers) == len(number.human_powers)

def test_fractional(self):
test_list = (1, 2.0, (4.0 / 3.0), (5.0 / 6.0), "7", "8.9", "ten", None)
result_list = ("1", "2", "1 1/3", "5/6", "7", "8 9/10", "ten", None)
self.assertManyResults(number.fractional, test_list, result_list)

@pytest.mark.parametrize(
"test_args, expected",
[
(["100"], "100"),
(["1000000"], "1.0 million"),
(["1200000"], "1.2 million"),
(["1290000"], "1.3 million"),
(["1000000000"], "1.0 billion"),
(["2000000000"], "2.0 billion"),
(["6000000000000"], "6.0 trillion"),
(["1300000000000000"], "1.3 quadrillion"),
(["3500000000000000000000"], "3.5 sextillion"),
(["8100000000000000000000000000000000"], "8.1 decillion"),
([None], None),
(["1230000", "%0.2f"], "1.23 million"),
([10 ** 101], "1" + "0" * 101),
],
)
def test_intword(test_args, expected):
assert number.intword(*test_args) == expected


@pytest.mark.parametrize(
"test_input, expected",
[
(1, "one"),
(2, "two"),
(4, "four"),
(5, "five"),
(9, "nine"),
(10, "10"),
("7", "seven"),
(None, None),
],
)
def test_apnumber(test_input, expected):
assert number.apnumber(test_input) == expected


@pytest.mark.parametrize(
"test_input, expected",
[
(1, "1"),
(2.0, "2"),
(4.0 / 3.0, "1 1/3"),
(5.0 / 6.0, "5/6"),
("7", "7"),
("8.9", "8 9/10"),
("ten", "ten"),
(None, None),
],
)
def test_fractional(test_input, expected):
assert number.fractional(test_input) == expected
Loading