Skip to content

Commit

Permalink
fix: address code smells and other issues (#215)
Browse files Browse the repository at this point in the history
* fix: use randbytes to generate fake file contents

* fix: remove default LIMESURVEY_URL in integration tests

* chore: add .sonarcloud.properties

* fix: random.randbytes is available only in 3.9+

* fix: make LSRC url a test fixture
  • Loading branch information
edgarrmondragon committed Feb 9, 2022
1 parent 487359d commit 0bd71ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions .sonarcloud.properties
@@ -0,0 +1 @@
sonar.python.version=3.7, 3.8, 3.9, 3.10
21 changes: 15 additions & 6 deletions tests/test_client.py
Expand Up @@ -4,6 +4,8 @@

import base64
import io
import random
import sys
from pathlib import Path
from typing import Any, Generator

Expand All @@ -16,6 +18,16 @@
NEW_SURVEY_NAME = "New Survey"


def _get_random_bytes(n: int):
return random.getrandbits(n * 8).to_bytes(n, "little")


if sys.version_info >= (3, 9):
randbytes = random.randbytes
else:
randbytes = _get_random_bytes


class MockSession(Session):
"""Mock RPC session with some hardcoded methods for testing."""

Expand Down Expand Up @@ -300,8 +312,7 @@ def test_get_available_languages(client: MockClient):

def test_import_group(client: MockClient, tmp_path: Path):
"""Test import_group client method."""
# TODO: generate this truly randomly
random_bytes = b"1924m01'9280u '0', u'012"
random_bytes = randbytes(100)

filepath = Path(tmp_path) / "group.lsq"

Expand All @@ -314,8 +325,7 @@ def test_import_group(client: MockClient, tmp_path: Path):

def test_import_question(client: MockClient, tmp_path: Path):
"""Test import_question client method."""
# TODO: generate this truly randomly
random_bytes = b"1924m01'9280u '0', u'012"
random_bytes = randbytes(100)

filepath = Path(tmp_path) / "question.lsq"

Expand All @@ -328,8 +338,7 @@ def test_import_question(client: MockClient, tmp_path: Path):

def test_import_survey(client: MockClient, tmp_path: Path):
"""Test import_survey client method."""
# TODO: generate this truly randomly
random_bytes = b"1924m01'9280u '0', u'012"
random_bytes = randbytes(100)

filepath = Path(tmp_path) / "survey.lss"

Expand Down
11 changes: 8 additions & 3 deletions tests/test_integration.py
Expand Up @@ -14,7 +14,6 @@
from citric import enums
from citric.exceptions import LimeSurveyStatusError

LS_URL = os.getenv("LIMESURVEY_URL", "http://limesurvey/index.php/admin/remotecontrol")
LS_USER = "iamadmin"
LS_PW = "secret"

Expand All @@ -38,10 +37,16 @@ def enable_json_rpc():
conn.commit()


@pytest.fixture(scope="session")
def url() -> str:
"""Get LimeSurvey RC URL."""
return os.environ["LIMESURVEY_URL"]


@pytest.fixture(scope="module")
def client() -> Generator[citric.Client, None, None]:
def client(url: str) -> Generator[citric.Client, None, None]:
"""RemoteControl2 API client."""
client = citric.Client(LS_URL, LS_USER, LS_PW)
client = citric.Client(url, LS_USER, LS_PW)

yield client

Expand Down

0 comments on commit 0bd71ce

Please sign in to comment.