Skip to content

Commit

Permalink
test: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibault Ballier committed Oct 31, 2023
1 parent fc40da8 commit 14bd003
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.4.3
62 changes: 62 additions & 0 deletions utils/test_data_decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import base64
from unittest import mock

from .data_decoder import decode_data


class __UtilArgsData:
args: dict[str, str]

def __init__(self) -> None:
self.args = {}


def test_decode_data_flask_data() -> None:
data = base64.b64encode(json.dumps({
"username": "flask_username",
"password": "flask_password",
"team_id": "flask_team_id"
}).encode("utf-8")).decode("utf-8")

util_args = __UtilArgsData()
util_args.args = {"data": data}

with mock.patch("flask.request", util_args):
username, password, team_id = decode_data()

assert username == "flask_username"
assert password == "flask_password"
assert team_id == "flask_team_id"


def test_decode_data_flask_args() -> None:
util_args = __UtilArgsData()
util_args.args = {
"username": "flask_args_username",
"password": "flask_args_password",
"team_id": "flask_args_team_id",
}

with mock.patch("flask.request", util_args):
username, password, team_id = decode_data()

assert username == "flask_args_username"
assert password == "flask_args_password"
assert team_id == "flask_args_team_id"


def test_decode_data_env() -> None:
util_args = __UtilArgsData()
env = {
"username": "env_username",
"password": "env_password",
"team_id": "env_team_id",
}
with (mock.patch("flask.request", util_args),
mock.patch("utils.data_decoder.env", env)):
username, password, team_id = decode_data()

assert username == "env_username"
assert password == "env_password"
assert team_id == "env_team_id"
18 changes: 18 additions & 0 deletions utils/test_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from unittest import mock

from .env import load_env_data


def test_env() -> None:
env = {
"username": "u",
"password": "p",
"team_id": "ti",
}

with mock.patch("utils.env.env", env):
username, password, team_id = load_env_data()

assert username == "u"
assert password == "p"
assert team_id == "ti"
12 changes: 12 additions & 0 deletions utils/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest

from .utils import normalize


@pytest.mark.parametrize("test_input,expected", [
("J'aimerais ça. Du pâté !", "J'aimerais ca. Du pate !"),
("phrase normale", "phrase normale"),
])
def test_normalize(test_input, expected) -> None:
result = normalize(test_input)
assert expected == result
5 changes: 4 additions & 1 deletion utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@


def normalize(any_str: str) -> str:
return unicodedata.normalize("NFD", any_str)
normalized = unicodedata.normalize("NFKD", any_str)
# Remove accent by removing combining character after each character
# Example: "é" => "e" + "'́"
return "".join([c for c in normalized if not unicodedata.combining(c)])

0 comments on commit 14bd003

Please sign in to comment.