Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
khiron committed Apr 1, 2024
1 parent d24c629 commit 17d4c1f
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions tests/test_app/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import json
import sys
import time

from importlib.metadata import EntryPoint
from inspect import getsourcelines, isclass
from os import path
from shutil import rmtree
from subprocess import check_call
from tempfile import mkdtemp
from unittest.mock import Mock, patch

import pkg_resources
from unittest.mock import patch

import pytest

from stevedore import extension
from stevedore.extension import ExtensionManager

import cogent3

from cogent3.app import (
_make_apphelp_docstring,
app_help,
apps,
available_apps,
get_app,
)
from cogent3.app import typing as c3types
from cogent3.app import _make_apphelp_docstring, app_help, get_app
from cogent3.app.composable import define_app


Expand All @@ -43,35 +26,44 @@ def _factory(extensions):

return _factory


@pytest.fixture
def mock_extension_manager(extension_manager_factory, monkeypatch):
"""Fixture to mock the ExtensionManager with the given extensions."""

def _mock_extension_manager(extensions):
# Create a mocked ExtensionManager with the specified mock extensions
mocked_manager = extension_manager_factory(extensions)
# Patch the __apps variable in cogent3.apps module to use the mocked_manager
monkeypatch.setattr(cogent3.app, "__apps", mocked_manager)
return mocked_manager

return _mock_extension_manager


def create_extension(plugin: object, name: str = None, module_name:str = "module1") -> extension.Extension:
def create_extension(
plugin: object, name: str = None, module_name: str = "module1"
) -> extension.Extension:
if name is None:
name = plugin.__name__
return extension.Extension(
name=name,
entry_point=EntryPoint(name=name, value=f"{module_name}:{name}", group="TESTING"),
entry_point=EntryPoint(
name=name, value=f"{module_name}:{name}", group="TESTING"
),
plugin=plugin,
obj=None,
)


def test_Install_app(mock_extension_manager):
@define_app
class uppercase:
"""Convert a string to uppercase."""
"""Test app that converts a string to uppercase, and has a class docstring."""

def main(self, data: str) -> str:
return data.upper()

mock_extension_manager([create_extension(uppercase)])

appercase = get_app("uppercase")
Expand All @@ -84,13 +76,14 @@ def main(self, data: str) -> str:
def test_app_docs(mock_extension_manager, app_doc, init_doc):
@define_app
class documented_app:
"""This is a test app that has a __init__"""
"""This is a test app that has a __init__, and a docstring"""

def __init__(self):
self.constant = 2

def main(self, val: int) -> int:
return val + self.constant

mock_extension_manager([create_extension(documented_app)])

assert cogent3.app.apps().names() == ["documented_app"]
Expand All @@ -107,12 +100,19 @@ def test_namespace_collision(mock_extension_manager):
class app1:
def main(self, data: str) -> str:
return data.upper()

@define_app
class app2:
def main(self, data: str) -> str:
return data.lower()

# create two apps with the same name and different modules
mock_extension_manager([create_extension(app1, module_name="module1"), create_extension(app2, name="app1",module_name="module2")])
mock_extension_manager(
[
create_extension(app1, module_name="module1"),
create_extension(app2, name="app1", module_name="module2"),
]
)

assert cogent3.app.apps().names() == ["app1", "app1"]

Expand Down

0 comments on commit 17d4c1f

Please sign in to comment.