From 217c4e32f83dab4362d519f058a2fba248b909e9 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Wed, 3 Jul 2024 23:01:01 +0100 Subject: [PATCH] ok, ok. It's time to start testing this thing;git push --- app/functions/generate_profile.py | 24 +++++++++++++++---- app/functions/get_score_list.py | 2 +- requirements.txt | 4 +++- tests/__init__.py | 0 tests/test_generate_profile.py | 30 ++++++++++++++++++++++++ tests/test_get_score_list.py | 24 +++++++++++++++++++ tests/test_roll_dices.py | 38 +++++++++++++++++++++++++++++++ 7 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_generate_profile.py create mode 100644 tests/test_get_score_list.py create mode 100644 tests/test_roll_dices.py diff --git a/app/functions/generate_profile.py b/app/functions/generate_profile.py index ded0957..f0d564f 100644 --- a/app/functions/generate_profile.py +++ b/app/functions/generate_profile.py @@ -2,15 +2,31 @@ import random from app.functions.get_character_age import get_age +def get_sex(): + return random.choice(['male', 'female']) + + +def get_hair(): + return random.choice(['black', 'brown', 'auburn', 'blonde', 'copper', 'brass', 'blue', 'bald']) + + +def get_eyes(): + return random.choice(['blue', 'brown', 'green', 'hazel', 'gray']) + + +def get_skin_tone(): + return random.choice(['dark', 'olive', 'medium', 'light', 'pale']) + + def generate_profile(chartype): profile = { 'name': "Anon", - 'sex': random.choice(['male', 'female', 'male', 'female', 'male', 'female']), + 'sex': get_sex(), 'age': get_age(chartype) } if chartype == 'human': - profile['hair'] = random.choice(['black', 'brown', 'auburn', 'blonde', 'copper', 'brass', 'blue', 'bald']) - profile['eyes'] = random.choice(['blue', 'brown', 'green', 'hazel', 'gray']) - profile['skintone'] = random.choice(['dark', 'olive', 'medium', 'light', 'pale']) + profile['hair'] = get_hair() + profile['eyes'] = get_eyes() + profile['skintone'] = get_skin_tone() return profile diff --git a/app/functions/get_score_list.py b/app/functions/get_score_list.py index 7c8c9fa..e711660 100644 --- a/app/functions/get_score_list.py +++ b/app/functions/get_score_list.py @@ -11,4 +11,4 @@ def get_score_list(count, modifier): roll = 100 tmp_scores.append(roll) - return tmp_scores \ No newline at end of file + return tmp_scores diff --git a/requirements.txt b/requirements.txt index 2004e98..2dc5668 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,6 @@ flask-restx~=1.3.0 marshmallow~=3.21.3 pandas~=2.2.2 flask-cors~=4.0.1 -numpy~=2.0.0 \ No newline at end of file +numpy~=2.0.0 +pytest~=8.2.2 +coverage~=7.5.4 \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_generate_profile.py b/tests/test_generate_profile.py new file mode 100644 index 0000000..adda481 --- /dev/null +++ b/tests/test_generate_profile.py @@ -0,0 +1,30 @@ +import pytest +from unittest.mock import patch +from app.functions.generate_profile import generate_profile + + +@patch('app.functions.generate_profile.get_sex', return_value='male') +@patch('app.functions.generate_profile.get_age', return_value=30) +@patch('app.functions.generate_profile.get_hair', return_value='black') +@patch('app.functions.generate_profile.get_eyes', return_value='blue') +@patch('app.functions.generate_profile.get_skin_tone', return_value='dark') +@pytest.mark.parametrize("chartype", ["human", "humanoid", "mutant", "cyborg"]) +def test_generate_profile( + mock_get_skin_tone, mock_get_eyes, mock_get_hair, mock_get_age, mock_get_sex, chartype): + profile = generate_profile(chartype) + + # Assertions common to all character types + assert profile["name"] == 'Anon' + assert profile["sex"] == 'male' + assert profile["age"] == 30 + + # Additional assertions for 'human' character type + if chartype == 'human': + assert profile["hair"] == 'black' + assert profile["eyes"] == 'blue' + assert profile["skintone"] == 'dark' + else: + # 'hair', 'eyes' and 'skintone' should not exist for non-human types + assert "hair" not in profile + assert "eyes" not in profile + assert "skintone" not in profile diff --git a/tests/test_get_score_list.py b/tests/test_get_score_list.py new file mode 100644 index 0000000..105546c --- /dev/null +++ b/tests/test_get_score_list.py @@ -0,0 +1,24 @@ +import pytest +from unittest.mock import patch, MagicMock +from app.functions.get_score_list import get_score_list + + +@patch('app.functions.get_score_list.roll_dices') +def test_get_score_list(mock_roll_dices): + # Setup the mock to return a specific value + mock_roll_dices.return_value = {'result': 50} + + # Test that the modifier is applied correctly when the roll value is less than 100 after applying modifier + scores = get_score_list(4, 10) + assert scores == [60, 60, 60, 60] + + # Test that the score is capped at 100 + scores = get_score_list(4, 60) + assert scores == [100, 100, 100, 100] + + # Test that the duplicates are handled You may need to setup some specific behavior on your mock object. Here we + # are making it return 50 on the first call, and 60 on any subsequent calls. This simulates a scenario where + # get_score_list may returns duplicate value + mock_roll_dices.side_effect = [{'result': 50}, {'result': 60}] + scores = get_score_list(2, 10) + assert scores == [60, 70] diff --git a/tests/test_roll_dices.py b/tests/test_roll_dices.py new file mode 100644 index 0000000..cd84295 --- /dev/null +++ b/tests/test_roll_dices.py @@ -0,0 +1,38 @@ +import pytest +from app.functions.roll_dices import roll_dices +from unittest.mock import patch + + +@patch('random.randint') +def test_roll_dices_no_discard(mock_randint): + # mocks + mock_randint.return_value = 4 + + # Test with discard_lowest = False + result = roll_dices(4, 6, False) + assert 'dice-set' in result + assert 'rolls' in result + assert 'result' in result + assert len(result['rolls']) == 4 + assert 'discarded' not in result + + mnemonic = result['dice-set']['mnemonic'] + assert mnemonic == '4d6' + + +@patch('random.randint') +def test_roll_dices_with_discard(mock_randint): + # mocks + mock_randint.return_value = 4 + + # Test with discard_lowest = True + result = roll_dices(4, 6, True) + assert 'dice-set' in result + assert 'rolls' in result + assert 'result' in result + assert len(result['rolls']) == 3 + assert 'discarded' in result + assert len(result['discarded']) == 1 + + mnemonic = result['dice-set']['mnemonic'] + assert mnemonic == '4(-1)d6'