File: test_replace.py

package info (click to toggle)
python-semver 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 700 kB
  • sloc: python: 1,972; makefile: 28
file content (50 lines) | stat: -rw-r--r-- 1,789 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pytest

from semver import Version, replace


@pytest.mark.parametrize(
    "version,parts,expected",
    [
        ("3.4.5", dict(major=2), "2.4.5"),
        ("3.4.5", dict(major="2"), "2.4.5"),
        ("3.4.5", dict(major=2, minor=5), "2.5.5"),
        ("3.4.5", dict(minor=2), "3.2.5"),
        ("3.4.5", dict(major=2, minor=5, patch=10), "2.5.10"),
        ("3.4.5", dict(major=2, minor=5, patch=10, prerelease="rc1"), "2.5.10-rc1"),
        (
            "3.4.5",
            dict(major=2, minor=5, patch=10, prerelease="rc1", build="b1"),
            "2.5.10-rc1+b1",
        ),
        ("3.4.5-alpha.1.2", dict(major=2), "2.4.5-alpha.1.2"),
        ("3.4.5-alpha.1.2", dict(build="x1"), "3.4.5-alpha.1.2+x1"),
        ("3.4.5+build1", dict(major=2), "2.4.5+build1"),
    ],
)
def test_replace_method_replaces_requested_parts(version, parts, expected):
    assert replace(version, **parts) == expected


def test_replace_raises_TypeError_for_invalid_keyword_arg():
    with pytest.raises(TypeError, match=r"replace\(\).*unknown.*"):
        assert replace("1.2.3", unknown="should_raise")


@pytest.mark.parametrize(
    "version,parts,expected",
    [
        ("3.4.5", dict(major=2, minor=5), "2.5.5"),
        ("3.4.5", dict(major=2, minor=5, patch=10), "2.5.10"),
        ("3.4.5-alpha.1.2", dict(major=2), "2.4.5-alpha.1.2"),
        ("3.4.5-alpha.1.2", dict(build="x1"), "3.4.5-alpha.1.2+x1"),
        ("3.4.5+build1", dict(major=2), "2.4.5+build1"),
    ],
)
def test_should_return_versioninfo_with_replaced_parts(version, parts, expected):
    assert Version.parse(version).replace(**parts) == Version.parse(expected)


def test_replace_raises_ValueError_for_non_numeric_values():
    with pytest.raises(ValueError):
        Version.parse("1.2.3").replace(major="x")