From b14c3e82b468eef265a900e9c0693427759a8f38 Mon Sep 17 00:00:00 2001 From: 283375 Date: Thu, 9 Nov 2023 16:33:51 +0800 Subject: [PATCH] test: add tests --- .sourcery.yaml | 2 + requirements.dev.txt | 2 + tests/calculate/test_world_step.py | 22 ++++++ tests/db/__init__.py | 0 tests/db/db.py | 5 ++ tests/db/models/__init__.py | 0 tests/db/models/test_songs.py | 118 +++++++++++++++++++++++++++++ tox.ini | 16 ++++ 8 files changed, 165 insertions(+) create mode 100644 .sourcery.yaml create mode 100644 tests/calculate/test_world_step.py create mode 100644 tests/db/__init__.py create mode 100644 tests/db/db.py create mode 100644 tests/db/models/__init__.py create mode 100644 tests/db/models/test_songs.py create mode 100644 tox.ini diff --git a/.sourcery.yaml b/.sourcery.yaml new file mode 100644 index 0000000..14fa3df --- /dev/null +++ b/.sourcery.yaml @@ -0,0 +1,2 @@ +rule_settings: + python_version: '3.8' diff --git a/requirements.dev.txt b/requirements.dev.txt index ce848f8..7cda2cf 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -2,3 +2,5 @@ black==23.3.0 isort==5.12.0 pre-commit==3.3.1 pylint==3.0.2 +pytest==7.4.3 +tox==4.11.3 diff --git a/tests/calculate/test_world_step.py b/tests/calculate/test_world_step.py new file mode 100644 index 0000000..714e9a0 --- /dev/null +++ b/tests/calculate/test_world_step.py @@ -0,0 +1,22 @@ +from decimal import Decimal + +from arcaea_offline.calculate.world_step import ( + AwakenedAyuPartnerBonus, + LegacyMapStepBooster, + PlayResult, + calculate_step_original, +) + + +def test_world_step(): + # the result was copied from https://arcaea.fandom.com/wiki/World_Mode_Mechanics#Calculation + # CC BY-SA 3.0 + + booster = LegacyMapStepBooster(6, 250) + partner_bonus = AwakenedAyuPartnerBonus("+3.6") + play_result = PlayResult(play_rating=Decimal("11.299"), partner_step=92) + result = calculate_step_original( + play_result, partner_bonus=partner_bonus, step_booster=booster + ) + + assert result.quantize(Decimal("0.000")) == Decimal("175.149") diff --git a/tests/db/__init__.py b/tests/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/db/db.py b/tests/db/db.py new file mode 100644 index 0000000..3947df8 --- /dev/null +++ b/tests/db/db.py @@ -0,0 +1,5 @@ +from sqlalchemy import Engine, create_engine, inspect + + +def create_engine_in_memory(): + return create_engine("sqlite:///:memory:") diff --git a/tests/db/models/__init__.py b/tests/db/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/db/models/test_songs.py b/tests/db/models/test_songs.py new file mode 100644 index 0000000..670f7b8 --- /dev/null +++ b/tests/db/models/test_songs.py @@ -0,0 +1,118 @@ +from sqlalchemy import Engine +from sqlalchemy.orm import Session + +from arcaea_offline.models.songs import ( + Chart, + ChartInfo, + Difficulty, + Pack, + Song, + SongsBase, + SongsViewBase, +) + +from ..db import create_engine_in_memory + + +def _song(**kw): + defaults = {"artist": "test"} + defaults.update(kw) + return Song(**defaults) + + +def _difficulty(**kw): + defaults = {"rating_plus": False, "audio_override": False, "jacket_override": False} + defaults.update(kw) + return Difficulty(**defaults) + + +class Test_Chart: + def init_db(self, engine: Engine): + SongsBase.metadata.create_all(engine) + SongsViewBase.metadata.create_all(engine) + + def db(self): + db = create_engine_in_memory() + self.init_db(db) + return db + + def test_chart_info(self): + pre_entites = [ + Pack(id="test", name="Test Pack"), + _song(idx=0, id="song0", set="test", title="Full Chart Info"), + _song(idx=1, id="song1", set="test", title="Partial Chart Info"), + _song(idx=2, id="song2", set="test", title="No Chart Info"), + _difficulty(song_id="song0", rating_class=2, rating=9), + _difficulty(song_id="song1", rating_class=2, rating=9), + _difficulty(song_id="song2", rating_class=2, rating=9), + ChartInfo(song_id="song0", rating_class=2, constant=90, notes=1234), + ChartInfo(song_id="song1", rating_class=2, constant=90), + ] + + db = self.db() + with Session(db) as session: + session.add_all(pre_entites) + session.commit() + + chart_song0_ratingclass2 = ( + session.query(Chart) + .where((Chart.song_id == "song0") & (Chart.rating_class == 2)) + .one() + ) + + assert chart_song0_ratingclass2.constant == 90 + assert chart_song0_ratingclass2.notes == 1234 + + chart_song1_ratingclass2 = ( + session.query(Chart) + .where((Chart.song_id == "song1") & (Chart.rating_class == 2)) + .one() + ) + + assert chart_song1_ratingclass2.constant == 90 + assert chart_song1_ratingclass2.notes is None + + chart_song2_ratingclass2 = ( + session.query(Chart) + .where((Chart.song_id == "song2") & (Chart.rating_class == 2)) + .first() + ) + + assert chart_song2_ratingclass2 is None + + def test_difficulty_title_override(self): + pre_entites = [ + Pack(id="test", name="Test Pack"), + _song(idx=0, id="test", set="test", title="Test"), + _difficulty(song_id="test", rating_class=0, rating=2), + _difficulty(song_id="test", rating_class=1, rating=5), + _difficulty(song_id="test", rating_class=2, rating=8), + _difficulty( + song_id="test", rating_class=3, rating=10, title="TEST ~REVIVE~" + ), + ChartInfo(song_id="test", rating_class=0, constant=10), + ChartInfo(song_id="test", rating_class=1, constant=10), + ChartInfo(song_id="test", rating_class=2, constant=10), + ChartInfo(song_id="test", rating_class=3, constant=10), + ] + + db = self.db() + with Session(db) as session: + session.add_all(pre_entites) + session.commit() + + charts_original_title = ( + session.query(Chart) + .where((Chart.song_id == "test") & (Chart.rating_class in [0, 1, 2])) + .all() + ) + + assert all(chart.title == "Test" for chart in charts_original_title) + + chart_overrided_title = ( + session.query(Chart) + .where((Chart.song_id == "test") & (Chart.rating_class == 3)) + .one() + ) + + assert chart_overrided_title.title == "TEST ~REVIVE~" diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..3b48811 --- /dev/null +++ b/tox.ini @@ -0,0 +1,16 @@ +[tox] +env_list = + py311 + py310 + py39 + py38 +minversion = 4.11.3 + +[testenv] +description = run the tests with pytest +package = wheel +wheel_build_env = .pkg +deps = + pytest==7.4.3 +commands = + pytest {tty:--color=yes} {posargs}