Skip to content

Commit

Permalink
Support testing extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Wh1isper committed Dec 11, 2023
1 parent d498a84 commit 0c92137
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 14 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Testing example extensions

on:
push:
branches: ["master", "main"]
pull_request:
branches: ["master", "main"]

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e .[test]
- name: Install all packages in example/extension
run: |
python -m pip install -e example/extension/dummydataconnector[test]
python -m pip install -e example/extension/dummydataprocessor[test]
python -m pip install -e example/extension/dummymodel[test]
- name: Test with pytest
run: |
pytest -vv example/extension
5 changes: 3 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e .[test]
- name: Test with pytest
run: |
pytest -vv --cov=sdgx
pytest -vv --cov=sdgx tests
- name: Install dependencies for building
run: |
pip install build twine hatch
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from sdgx.data_connectors.base import DataConnector


class MyOwnDataConnector(DataConnector):
...


from sdgx.data_connectors.extension import hookimpl


@hookimpl
def register(manager):
manager.register("DummyDataConnector", MyOwnDataConnector)
27 changes: 27 additions & 0 deletions example/extension/dummydataconnector/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "dummydataconnector"
dependencies = ["sdgx"]
dynamic = ["version"]
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
]
[project.optional-dependencies]
test = ["pytest"]

[tool.check-manifest]
ignore = [".*"]

[tool.hatch.version]
path = "dummydataconnector/__init__.py"

[project.entry-points."sdgx.data_connector"]
dummydataconnector = "dummydataconnector.dataconnector"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from sdgx.data_connectors.manager import DataConnectorManager


@pytest.fixture
def manager():
yield DataConnectorManager()


def test_registed_data_connector(manager: DataConnectorManager):
assert manager._normalize_name("DummyDataConnector") in manager.registed_data_connectors
manager.init_data_connector("DummyDataConnector")


if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])
15 changes: 13 additions & 2 deletions example/extension/dummydataprocessor/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "sdgx-dummymdataprocessor"
name = "dummydataprocessor"

dependencies = ["sdgx"]
dynamic = ["version"]
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
]

[project.optional-dependencies]
test = ["pytest"]

# This is the entry point for the Manager to find the extension.
[project.entry-points."sdgx.data_processor"]
dummydataprocessor = "dummydataprocessor.model"
dummydataprocessor = "dummydataprocessor.dataprocessor"

[tool.hatch.version]
path = "dummydataprocessor/__init__.py"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from sdgx.data_processors.manager import DataProcessorManager


@pytest.fixture
def manager():
yield DataProcessorManager()


def test_registed_data_processor(manager: DataProcessorManager):
assert manager._normalize_name("DummyDataProcessor") in manager.registed_data_processors
manager.init_data_processor("DummyDataProcessor")


if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])
13 changes: 12 additions & 1 deletion example/extension/dummymodel/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "sdgx-dummymodel"
name = "dummymodel"

dependencies = ["sdgx"]
dynamic = ["version"]
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
]

[project.optional-dependencies]
test = ["pytest"]

# This is the entry point for the Manager to find the extension.
[project.entry-points."sdgx.model"]
Expand Down
17 changes: 17 additions & 0 deletions example/extension/dummymodel/tests/test_registed_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from sdgx.models.manager import ModelManager


@pytest.fixture
def manager():
yield ModelManager()


def test_registed_model(manager: ModelManager):
assert manager._normalize_name("DummyModel") in manager.registed_models
manager.init_model("DummyModel")


if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "hatchling.build"
[project]
name = "sdgx"
description = "synthetic-data-generator"
keywords = ["sdg", "hitsz-ids"]
keywords = ["synthetic data", "hitsz-ids"]
requires-python = ">=3.8"

dependencies = [
Expand Down
8 changes: 3 additions & 5 deletions sdgx/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def load_all_local_model(self):

@property
def registed_cls(self):
# Lazy load when query registed_models
# Lazy load when access
if self._registed_cls:
return self._registed_cls
for f in self.pm.hook.register(manager=self):
Expand Down Expand Up @@ -73,14 +73,12 @@ def register(self, cls_name, cls: type):

def init(self, cls_name, **kwargs: dict[str, Any]):
cls_name = self._normalize_name(cls_name)
if not cls_name in self.registed_models:
if not cls_name in self.registed_cls:
raise NotFoundError
try:
instance = self.registed_cls[cls_name](**kwargs)
if not isinstance(instance, self.register_type):
raise InitializationError(
f"{cls_name} is not a subclass of {self.registed_models}."
)
raise InitializationError(f"{cls_name} is not a subclass of {self.register_type}.")
return instance
except Exception as e:
raise InitializationError(e)
2 changes: 1 addition & 1 deletion tests/manager/test_data_connector_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def manager():
[],
)
def test_manager(supported_data_connector, manager: DataConnectorManager):
assert supported_data_connector in manager.registed_data_connectors
assert manager._normalize_name(supported_data_connector) in manager.registed_data_connectors
manager.init_data_connector(supported_data_connector)


Expand Down
2 changes: 1 addition & 1 deletion tests/manager/test_data_processor_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def manager():
[],
)
def test_manager(supported_data_processor, manager: DataProcessorManager):
assert supported_data_processor in manager.registed_data_processors
assert manager._normalize_name(supported_data_processor) in manager.registed_data_processors
manager.init_data_processor(supported_data_processor)


Expand Down
2 changes: 1 addition & 1 deletion tests/manager/test_model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def manager():
],
)
def test_manager(supported_model, manager: ModelManager):
assert supported_model in manager.registed_models
assert manager._normalize_name(supported_model) in manager.registed_models
manager.init_model(supported_model, epochs=1)


Expand Down

0 comments on commit 0c92137

Please sign in to comment.