Skip to content

Commit

Permalink
Merge pull request #4 from AndyRids/testing/pytest-functions
Browse files Browse the repository at this point in the history
Testing/pytest functions
  • Loading branch information
andyrids authored May 16, 2024
2 parents 0400360 + 6449d8c commit 09b6cae
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 59 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/poetry-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
########################################################################
# Modified action from https://jacobian.org/til/github-actions-poetry/ #
########################################################################

# Run this job on pushes to `main`, and for pull requests. If you don't specify
# `branches: [main], then this actions runs _twice_ on pull requests, which is
# annoying.

name: poetry-test

run-name: ${{ github.actor }} is installing dependencies and testing with Poetry

on:
push:
branches: [main]
pull_request:

# Groups together all the jobs that run in the poetry-test workflow.
jobs:
test:
# Configures the job to run on the latest version of an Ubuntu Linux runner
runs-on: ubuntu-latest
# Groups together all the steps that run in the test job. Each item nested under this section
# is a separate action or shell script.
steps:
# The uses keyword specifies that this step will run v4 of the actions/checkout action. This
# is an action that checks out your repository onto the runner, allowing you to run scripts or
# other actions against your code (such as build and test tools). You should use the checkout
# action any time your workflow will use the repository's code.
- uses: actions/checkout@v4

# If you wanted to use multiple Python versions, you'd have specify a matrix in the job and
# reference the matrixe python version here.
- uses: actions/setup-python@v5
with:
python-version: 3.9

# Cache the installation of Poetry itself, e.g. the next step. This prevents the workflow
# from installing Poetry every time, which can be slow. Note the use of the Poetry version
# number in the cache key, and the "-0" suffix: this allows you to invalidate the cache
# manually if/when you want to upgrade Poetry, or if something goes wrong. This could be
# mildly cleaner by using an environment variable.
- name: cache poetry install
uses: actions/cache@v4
with:
path: ~/.local
key: poetry-1.8.2-0

# Install Poetry. You could do this manually, or there are several actions that do this.
# `snok/install-poetry` seems to be minimal yet complete, and really just calls out to
# Poetry's default install script, which feels correct. I pin the Poetry version here
# because Poetry does occasionally change APIs between versions and I don't want my
# actions to break if it does.
#
# The key configuration value here is `virtualenvs-in-project: true`: this creates the
# venv as a `.venv` in your testing directory, which allows the next step to easily
# cache it.
- uses: snok/install-poetry@v1
with:
version: 1.8.2
virtualenvs-create: true
virtualenvs-in-project: true

# Cache your dependencies (i.e. all the stuff in your `pyproject.toml`). Note the cache
# key: if you're using multiple Python versions, or multiple OSes, you'd need to include
# them in the cache key. I'm not, so it can be simple and just depend on the poetry.lock.
- name: cache deps
id: cache-deps
uses: actions/cache@v4
with:
path: .venv
key: pydeps-${{ hashFiles('**/poetry.lock') }}

# Install dependencies. `--no-root` means "install all dependencies but not the project
# itself", which is what you want to avoid caching _your_ code. The `if` statement
# ensures this only runs on a cache miss.
- run: poetry install --no-interaction --no-root
if: steps.cache-deps.outputs.cache-hit != 'true'

# Now install _your_ project. This isn't necessary for many types of projects -- particularly
# things like Django apps don't need this. But it's a good idea since it fully-exercises the
# pyproject.toml and makes that if you add things like console-scripts at some point that
# they'll be installed and working.
- run: poetry install --no-interaction

# And finally run tests. I'm using pytest and all my pytest config is in my `pyproject.toml`
# so this line is super-simple. But it could be as complex as you need.
- run: poetry run pytest --cov-report term-missing --cov=ipymediator tests/
12 changes: 2 additions & 10 deletions ipymediator/dialogs/custom_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@
from typing import Optional, Union

from ipymediator.enumerations import ButtonColour, Options, Value
from ipymediator.interface import Component, Mediator
from ipymediator.interface import Component, MediatorWithTraits
from ipymediator.utils.common_functions import (
deiconify_str,
directory_contents,
directory_paths,
singlenotifydispatch
)
from ipywidgets import widgets as w
from traitlets import Bool, HasTraits, Instance


class MediatorWithTraits(Mediator, HasTraits):
"""Abstract Mediator class for Mediator interface implimentation. Extends
the Mediator interface by allowing traits from the traitlets library,
through HasTraits inheritence.
"""
pass
from traitlets import Bool, Instance


class FileDialog(MediatorWithTraits):
Expand Down
10 changes: 5 additions & 5 deletions ipymediator/enumerations/custom_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
class ButtonColour(str, Enum):
"""StrEnum for button widget colours"""

BLUE: str = "#cce6f4"
GREEN: str = "#ebebeb"
RED: str = "#f8edeb"
BLUE: "ButtonColour.BLUE.value" = "#cce6f4"
GREEN: "ButtonColour.GREEN.value" = "#ebebeb"
RED: "ButtonColour.RED.value" = "#f8edeb"

def __str__(self) -> str:
"""Enables StrEnum prior to 3.11"""
Expand All @@ -16,8 +16,8 @@ def __str__(self) -> str:
class IconUnicode(str, Enum):
"""StrEnum for unicode icons 📁 & 📄"""

DIR: str = "\U0001F4C1"
FILE: str = "\U0001F4C4"
DIR = "\U0001F4C1"
FILE = "\U0001F4C4"

def __str__(self) -> str:
"""Enables StrEnum prior to 3.11"""
Expand Down
4 changes: 2 additions & 2 deletions ipymediator/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# from .custom_exceptions import *
# from .custom_exceptions import *

__all__ = ()
# __all__ = ()
4 changes: 2 additions & 2 deletions ipymediator/interface/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from .component import Component
from .mediator import DialogMediator, Mediator
from .mediator import Mediator, MediatorWithTraits
from .metaclass import ABCTraits, ABCTraitsMeta

__all__ = (
"ABCTraits",
"ABCTraitsMeta",
"Component",
"DialogMediator",
"Mediator",
"MediatorWithTraits"
)
18 changes: 13 additions & 5 deletions ipymediator/interface/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ipymediator.interface.metaclass import ABCTraits

from ipywidgets import widgets
from traitlets import Bool
from traitlets import Bool, HasTraits


class Component(ABCTraits):
Expand All @@ -17,11 +17,18 @@ def __new__(cls, **kwargs):
"""Add a bool value trait to any Button widgets and assigns an on_click
function to toggle the Button value."""
if isinstance(kwargs["widget"], widgets.Button):

def on_click(w) -> None:
w.value = not w.value
# ipywidgets overwrites HasTraits.add_traits and uses depreciated
# trait.get_metadata. The metadata of a trait type instance should
# be directly accessed via the metadata attribute.
# Issue: https://github.com/jupyter-widgets/ipywidgets/pull/3894

# pytest depreciation warning:
# kwargs["widget"].add_traits(value=Bool(False))

kwargs["widget"].add_traits(value=Bool(False)) # type: ignore
# NOTE: HasTraits.add_traits avoids depreciation warning.
HasTraits.add_traits(kwargs["widget"], value=Bool(False))
kwargs["widget"].on_click(on_click)
return super(Component, cls).__new__(cls)

Expand Down Expand Up @@ -55,6 +62,7 @@ def __init__(
AttributeError: Access trait name not held by widget property
"""
super(Component, self).__init__()
self.__mediator = mediator
self.widget = widget
try:
Expand Down Expand Up @@ -85,9 +93,9 @@ def __call__(self, trait: str, *args):
the call to the Component's DOMWidget properties"""
return itemgetter(trait, *args)(self)

def __contains__(self, item):
def __contains__(self, trait) -> bool:
""""""
return item in self.widget
return self.widget.has_trait(trait)

def __getitem__(self, trait: str):
"""Subscriptable interface of Component passed to DOMWidget"""
Expand Down
30 changes: 6 additions & 24 deletions ipymediator/interface/mediator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import pathlib
from abc import abstractmethod
from typing import Union

from ipymediator.interface.metaclass import ABCTraits
from ipymediator.enumerations import Value, Options

from traitlets import Bool, HasTraits, Instance
from traitlets import HasTraits


class Mediator(ABCTraits):
Expand All @@ -23,27 +21,11 @@ def notify(
change (Mapping): Trait changes from DOMWidget observe function
"""
pass


class DialogMediator(Mediator, HasTraits):
"""Abstract Mediator class for Mediator interface implimentation. A
DialogMediator extends the Mediator interface implimentation by adding
traitlet library traits.

Traits:
dialog_open (bool): Dialog open/closed flag

dialog_selection (pathlib.Path): Selected file path
Abstract methods:
notify: From Mediator class
_control: Holds a reference to a DialogMediator control Mediator
class MediatorWithTraits(Mediator, HasTraits):
"""Abstract Mediator class for Mediator interface implimentation. Extends
the Mediator interface by allowing traits from the traitlets library,
through HasTraits inheritence.
"""

dialog_open = Bool(default_value=True).tag(sync=True) # type: ignore
dialog_selection = Instance(
pathlib.Path,
allow_none=True,
default_value=None).tag(sync=True) # type: ignore
pass
87 changes: 86 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ipywidgets = "*"
pytest = "^8.0.6"
flake8 = "^7.0.0"
black = "^24.4.0"
pytest-cov = "^5.0.0"

[build-system]
requires = ["poetry-core"]
Expand Down
Loading

0 comments on commit 09b6cae

Please sign in to comment.