From a60965223246938132cd3241af3011202949b56d Mon Sep 17 00:00:00 2001 From: Ev-1 <33662061+Ev-1@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:17:01 +0200 Subject: [PATCH 1/4] Add tests for Dict Mapper --- musicbot/cogs/nodemanager.py | 4 +- .../utils/localisation/localizedcontext.py | 4 + .../settingsmanager/test_settingsmanager.py | 91 +++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 musicbot/utils/settingsmanager/test_settingsmanager.py diff --git a/musicbot/cogs/nodemanager.py b/musicbot/cogs/nodemanager.py index 5e4c501..5d7417d 100644 --- a/musicbot/cogs/nodemanager.py +++ b/musicbot/cogs/nodemanager.py @@ -7,6 +7,8 @@ import codecs import yaml +# Bot Utilities +from musicbot.utils.settingsmanager import Settings from ..utils.mixplayer import MixPlayer from ..utils.userinteraction import ClearOn, Scroller from .helpformatter import commandhelper @@ -15,7 +17,7 @@ from .helpformatter import commandhelper class NodeManager(commands.Cog): def __init__(self, bot): self.bot = bot - self.settings = self.bot.settings + self.settings: Settings = self.bot.settings self.logger = self.bot.main_logger.bot_logger.getChild("NodeManager") async def load_music_cogs(self): diff --git a/musicbot/utils/localisation/localizedcontext.py b/musicbot/utils/localisation/localizedcontext.py index 0071838..3a9f810 100644 --- a/musicbot/utils/localisation/localizedcontext.py +++ b/musicbot/utils/localisation/localizedcontext.py @@ -1,6 +1,8 @@ # Discord Packages from discord.ext import commands +from typing import Optional +from .localizer.localizerwrapper import LocalizerWrapper class LocalizedContext(commands.Context): def __init__(self, **kwargs): @@ -9,3 +11,5 @@ class LocalizedContext(commands.Context): self.locale = self.bot.settings.get(self.message.guild, 'locale', 'default_locale') else: self.locale = self.bot.settings.default_locale + + self.localizer: Optional[LocalizerWrapper] = None diff --git a/musicbot/utils/settingsmanager/test_settingsmanager.py b/musicbot/utils/settingsmanager/test_settingsmanager.py new file mode 100644 index 0000000..825be1e --- /dev/null +++ b/musicbot/utils/settingsmanager/test_settingsmanager.py @@ -0,0 +1,91 @@ +from .dictmapper import DictMapper + + +class TestDictMapper(): + def test_dict_mapper_basic_set(self): + d = {} + DictMapper.set(d, ["a", "b", "c"], 1) + assert d == {"a": {"b": {"c": 1}}} + + def test_dict_mapper_basic_get(self): + d = {"a": {"b": {"c": 1}}} + assert DictMapper.get(d, ["a", "b", "c"]) == 1 + + def test_get_variations(self): + d = {"a": {"b": {"c": 1}}} + + assert DictMapper.get(d, ["a", "b", "c"]) == DictMapper.get(d["a"], ["b", "c"]) + assert DictMapper.get(d, ["a", "b", "c"]) == DictMapper.get(d["a"]["b"], ["c"]) + assert DictMapper.get(d, ["a", "b", "c"]) == d["a"]["b"]["c"] + + def test_get_sub_dictionaries(self): + d = {"a": {"b": {"c": 1}}} + assert DictMapper.get(d, ["a", "b"]) == d["a"]["b"] + assert DictMapper.get(d["a"], ["b"]) == DictMapper.get(d["a"], ["b"]) + + def test_set_variation(self): + d = {"a": {"b": {"c": 1}}} + d1 = {} + d2 = {} + + DictMapper.set(d1, ["a", "b"], {"c": 1}) + assert d1 == d + + DictMapper.set(d2, ["a"], {"b": {"c": 1}}) + assert d2 == d + + def test_multiple_paths(self): + d = {} + + DictMapper.set(d, ["a", "b1", "c"], 1) + DictMapper.set(d, ["a", "b1", "d"], 2) + DictMapper.set(d, ["a", "b2", "c"], 3) + DictMapper.set(d, ["a", "b2", "d"], 4) + + expected = { + "a": { + "b1": { + "c": 1, + "d": 2, + }, + "b2": { + "c": 3, + "d": 4, + } + } + } + assert d == expected + + def test_overwrite(self): + d = {} + + DictMapper.set(d, ["a", "b1", "c"], 1) + DictMapper.set(d, ["a", "b1", "d"], 2) + DictMapper.set(d, ["a", "b2", "c"], 3) + DictMapper.set(d, ["a", "b2", "d"], 4) + + expected = { + "a": { + "b1": { + "c": 1, + "d": 2, + }, + "b2": { + "c": 3, + "d": 4, + } + } + } + assert d == expected + + DictMapper.set(d, ["a", "b2"], 4) + expected = { + "a": { + "b1": { + "c": 1, + "d": 2, + }, + "b2": 4, + } + } + assert d == expected From 3cc7e90a190ca7dee6bc4c1a213e424a1f55a8ed Mon Sep 17 00:00:00 2001 From: Ev-1 <33662061+Ev-1@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:32:00 +0200 Subject: [PATCH 2/4] rename test file --- .../utils/localisation/localizedcontext.py | 2 + .../utils/settingsmanager/test_dictmapper.py | 91 +++++++++++++++++++ .../settingsmanager/test_settingsmanager.py | 5 + 3 files changed, 98 insertions(+) create mode 100644 musicbot/utils/settingsmanager/test_dictmapper.py diff --git a/musicbot/utils/localisation/localizedcontext.py b/musicbot/utils/localisation/localizedcontext.py index 3a9f810..bcfebd5 100644 --- a/musicbot/utils/localisation/localizedcontext.py +++ b/musicbot/utils/localisation/localizedcontext.py @@ -2,8 +2,10 @@ from discord.ext import commands from typing import Optional + from .localizer.localizerwrapper import LocalizerWrapper + class LocalizedContext(commands.Context): def __init__(self, **kwargs): super().__init__(**kwargs) diff --git a/musicbot/utils/settingsmanager/test_dictmapper.py b/musicbot/utils/settingsmanager/test_dictmapper.py new file mode 100644 index 0000000..825be1e --- /dev/null +++ b/musicbot/utils/settingsmanager/test_dictmapper.py @@ -0,0 +1,91 @@ +from .dictmapper import DictMapper + + +class TestDictMapper(): + def test_dict_mapper_basic_set(self): + d = {} + DictMapper.set(d, ["a", "b", "c"], 1) + assert d == {"a": {"b": {"c": 1}}} + + def test_dict_mapper_basic_get(self): + d = {"a": {"b": {"c": 1}}} + assert DictMapper.get(d, ["a", "b", "c"]) == 1 + + def test_get_variations(self): + d = {"a": {"b": {"c": 1}}} + + assert DictMapper.get(d, ["a", "b", "c"]) == DictMapper.get(d["a"], ["b", "c"]) + assert DictMapper.get(d, ["a", "b", "c"]) == DictMapper.get(d["a"]["b"], ["c"]) + assert DictMapper.get(d, ["a", "b", "c"]) == d["a"]["b"]["c"] + + def test_get_sub_dictionaries(self): + d = {"a": {"b": {"c": 1}}} + assert DictMapper.get(d, ["a", "b"]) == d["a"]["b"] + assert DictMapper.get(d["a"], ["b"]) == DictMapper.get(d["a"], ["b"]) + + def test_set_variation(self): + d = {"a": {"b": {"c": 1}}} + d1 = {} + d2 = {} + + DictMapper.set(d1, ["a", "b"], {"c": 1}) + assert d1 == d + + DictMapper.set(d2, ["a"], {"b": {"c": 1}}) + assert d2 == d + + def test_multiple_paths(self): + d = {} + + DictMapper.set(d, ["a", "b1", "c"], 1) + DictMapper.set(d, ["a", "b1", "d"], 2) + DictMapper.set(d, ["a", "b2", "c"], 3) + DictMapper.set(d, ["a", "b2", "d"], 4) + + expected = { + "a": { + "b1": { + "c": 1, + "d": 2, + }, + "b2": { + "c": 3, + "d": 4, + } + } + } + assert d == expected + + def test_overwrite(self): + d = {} + + DictMapper.set(d, ["a", "b1", "c"], 1) + DictMapper.set(d, ["a", "b1", "d"], 2) + DictMapper.set(d, ["a", "b2", "c"], 3) + DictMapper.set(d, ["a", "b2", "d"], 4) + + expected = { + "a": { + "b1": { + "c": 1, + "d": 2, + }, + "b2": { + "c": 3, + "d": 4, + } + } + } + assert d == expected + + DictMapper.set(d, ["a", "b2"], 4) + expected = { + "a": { + "b1": { + "c": 1, + "d": 2, + }, + "b2": 4, + } + } + assert d == expected diff --git a/musicbot/utils/settingsmanager/test_settingsmanager.py b/musicbot/utils/settingsmanager/test_settingsmanager.py index 825be1e..d5c662b 100644 --- a/musicbot/utils/settingsmanager/test_settingsmanager.py +++ b/musicbot/utils/settingsmanager/test_settingsmanager.py @@ -89,3 +89,8 @@ class TestDictMapper(): } } assert d == expected + + +@patch +class TestSettingsManager: + def test_ From 0d2b85bd7e1c1a5a697e6ab89bd71f0ef6064a5b Mon Sep 17 00:00:00 2001 From: Ev-1 <33662061+Ev-1@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:48:50 +0200 Subject: [PATCH 3/4] Remove file that should be deleted, add tests as separate step --- .github/workflows/build.yml | 10 ++ .../settingsmanager/test_settingsmanager.py | 96 ------------------- 2 files changed, 10 insertions(+), 96 deletions(-) delete mode 100644 musicbot/utils/settingsmanager/test_settingsmanager.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3ab3efb..95a007a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -24,6 +24,16 @@ jobs: pip install isort isort --check --sp setup.cfg ${CheckFiles} + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Python 3.10 + uses: actions/setup-python@v4.2.0 + with: + python-version: 3.10.7 + - name: Run tests run: | pip install pytest diff --git a/musicbot/utils/settingsmanager/test_settingsmanager.py b/musicbot/utils/settingsmanager/test_settingsmanager.py deleted file mode 100644 index d5c662b..0000000 --- a/musicbot/utils/settingsmanager/test_settingsmanager.py +++ /dev/null @@ -1,96 +0,0 @@ -from .dictmapper import DictMapper - - -class TestDictMapper(): - def test_dict_mapper_basic_set(self): - d = {} - DictMapper.set(d, ["a", "b", "c"], 1) - assert d == {"a": {"b": {"c": 1}}} - - def test_dict_mapper_basic_get(self): - d = {"a": {"b": {"c": 1}}} - assert DictMapper.get(d, ["a", "b", "c"]) == 1 - - def test_get_variations(self): - d = {"a": {"b": {"c": 1}}} - - assert DictMapper.get(d, ["a", "b", "c"]) == DictMapper.get(d["a"], ["b", "c"]) - assert DictMapper.get(d, ["a", "b", "c"]) == DictMapper.get(d["a"]["b"], ["c"]) - assert DictMapper.get(d, ["a", "b", "c"]) == d["a"]["b"]["c"] - - def test_get_sub_dictionaries(self): - d = {"a": {"b": {"c": 1}}} - assert DictMapper.get(d, ["a", "b"]) == d["a"]["b"] - assert DictMapper.get(d["a"], ["b"]) == DictMapper.get(d["a"], ["b"]) - - def test_set_variation(self): - d = {"a": {"b": {"c": 1}}} - d1 = {} - d2 = {} - - DictMapper.set(d1, ["a", "b"], {"c": 1}) - assert d1 == d - - DictMapper.set(d2, ["a"], {"b": {"c": 1}}) - assert d2 == d - - def test_multiple_paths(self): - d = {} - - DictMapper.set(d, ["a", "b1", "c"], 1) - DictMapper.set(d, ["a", "b1", "d"], 2) - DictMapper.set(d, ["a", "b2", "c"], 3) - DictMapper.set(d, ["a", "b2", "d"], 4) - - expected = { - "a": { - "b1": { - "c": 1, - "d": 2, - }, - "b2": { - "c": 3, - "d": 4, - } - } - } - assert d == expected - - def test_overwrite(self): - d = {} - - DictMapper.set(d, ["a", "b1", "c"], 1) - DictMapper.set(d, ["a", "b1", "d"], 2) - DictMapper.set(d, ["a", "b2", "c"], 3) - DictMapper.set(d, ["a", "b2", "d"], 4) - - expected = { - "a": { - "b1": { - "c": 1, - "d": 2, - }, - "b2": { - "c": 3, - "d": 4, - } - } - } - assert d == expected - - DictMapper.set(d, ["a", "b2"], 4) - expected = { - "a": { - "b1": { - "c": 1, - "d": 2, - }, - "b2": 4, - } - } - assert d == expected - - -@patch -class TestSettingsManager: - def test_ From 7ac1d13496a5c0f7237a7c480ef321adfcb46155 Mon Sep 17 00:00:00 2001 From: Ev-1 <33662061+Ev-1@users.noreply.github.com> Date: Sat, 8 Oct 2022 21:07:17 +0200 Subject: [PATCH 4/4] Use separate job for pytest --- .github/workflows/build.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 95a007a..cb10b99 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,7 +2,7 @@ name: Test And Build BOT on: [push, pull_request] jobs: - flake8: + lint: runs-on: ubuntu-latest env: CheckFiles: "bot.py musicbot/" @@ -24,7 +24,7 @@ jobs: pip install isort isort --check --sp setup.cfg ${CheckFiles} - tests: + test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -34,14 +34,17 @@ jobs: with: python-version: 3.10.7 - - name: Run tests + - name: Install dependencies run: | pip install pytest pip install -r requirements.txt + + - name: Run pytest + run: | pytest build-and-push: - needs: flake8 + needs: [lint, test] runs-on: ubuntu-latest if: github.event_name != 'pull_request' && contains(github.ref, 'actions') != 'true' env: