diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3ab3efb..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,14 +24,27 @@ jobs: pip install isort isort --check --sp setup.cfg ${CheckFiles} - - name: Run tests + test: + 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: 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: diff --git a/musicbot/cogs/nodemanager.py b/musicbot/cogs/nodemanager.py index c0699ff..45bb94c 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..bcfebd5 100644 --- a/musicbot/utils/localisation/localizedcontext.py +++ b/musicbot/utils/localisation/localizedcontext.py @@ -1,6 +1,10 @@ # 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 +13,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_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