File: test_cli.py

package info (click to toggle)
pykoplenti 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 372 kB
  • sloc: python: 2,215; makefile: 3
file content (151 lines) | stat: -rw-r--r-- 4,288 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from pathlib import Path
from click.testing import CliRunner
import pytest
from pykoplenti.cli import cli, SessionCache
import os
from conftest import only_smoketest


@pytest.fixture
def credentials(tmp_path: Path, smoketest_config: tuple[str, int, str]):
    _, _, password = smoketest_config
    credentials_path = tmp_path / "credentials"
    credentials_path.write_text(f"password={password}")
    return credentials_path


@pytest.fixture
def dummy_credentials(tmp_path: Path):
    credentials_path = tmp_path / "credentials"
    credentials_path.write_text("password=dummy")
    return credentials_path


@pytest.fixture
def session_cache(smoketest_config: tuple[str, int, str]):
    host, _, _ = smoketest_config
    session_cache = SessionCache(host, "user")
    session_cache.remove()
    yield session_cache
    session_cache.remove()


class TestInvalidGlobalOptions:
    """Test invalid global options."""

    def test_crendentials_and_password(self, dummy_credentials: Path):
        runner = CliRunner()
        result = runner.invoke(
            cli,
            [
                "--credentials",
                str(dummy_credentials),
                "--password",
                "topsecret",
                "all-processdata",
            ],
        )
        assert result.exit_code == 2
        assert "password cannot be used with credentials" in result.output

    @pytest.mark.filterwarnings(
        "ignore:--password-file is deprecated. Use --credentials instead."
    )
    def test_crendentials_and_password_file(self, dummy_credentials: Path):
        runner = CliRunner()
        result = runner.invoke(
            cli,
            [
                "--credentials",
                str(dummy_credentials),
                "--password-file",
                str(dummy_credentials),
                "all-processdata",
            ],
        )

        assert result.exit_code == 2
        assert "password-file cannot be used with credentials" in result.output

    def test_crendentials_and_service_code(
        self, dummy_credentials: Path, tmp_path: Path
    ):
        # As --password-file has a default value, this ensures
        # that no default password-file exists.
        os.chdir(tmp_path)

        runner = CliRunner()
        result = runner.invoke(
            cli,
            [
                "--credentials",
                str(dummy_credentials),
                "--service-code",
                "topsecret",
                "all-processdata",
            ],
        )
        assert result.exit_code == 2
        assert "service_code cannot be used with credentials" in result.output


@only_smoketest
def test_read_process_data(
    credentials: Path,
    session_cache: SessionCache,
    smoketest_config: tuple[str, int, str],
):
    # As --password-file has a default value, this ensures
    # that no default password-file exists.
    os.chdir(credentials.parent)

    host, port, _ = smoketest_config

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "--host",
            host,
            "--port",
            str(port),
            "--credentials",
            str(credentials),
            "all-processdata",
        ],
    )
    assert result.exit_code == 0
    # check any data which is most likely present on most inverter
    assert "devices:local/Inverter:State" in result.stdout.splitlines()
    assert session_cache.read_session_id() is not None


@only_smoketest
def test_read_settings_data(
    credentials: Path,
    session_cache: SessionCache,
    smoketest_config: tuple[str, int, str],
):
    # As --password-file has a default value, this ensures
    # that no default password-file exists.
    os.chdir(credentials.parent)

    host, port, _ = smoketest_config

    runner = CliRunner()
    result = runner.invoke(
        cli,
        [
            "--host",
            host,
            "--port",
            str(port),
            "--credentials",
            str(credentials),
            "all-settings",
        ],
    )
    assert result.exit_code == 0
    # check any data which is most likely present on most inverter
    assert "devices:local/Branding:ProductName1" in result.stdout.splitlines()
    assert session_cache.read_session_id() is not None