Преглед на файлове

Merge pull request #67 from Avaiga/feature/#66-added-unittests-for-taipy-repo

feature/#66 add pipenv env and unit tests
Toan Quach преди 2 години
родител
ревизия
4564acaac2
променени са 10 файла, в които са добавени 169 реда и са изтрити 15 реда
  1. 1 1
      .github/workflows/packaging.yml
  2. 35 0
      .github/workflows/tests.yml
  3. 11 0
      .gitignore
  4. 17 0
      Pipfile
  5. 6 2
      setup.py
  6. 8 7
      src/taipy/__init__.py
  7. 7 5
      src/taipy/_run.py
  8. 12 0
      tests/__init__.py
  9. 55 0
      tests/test_run.py
  10. 17 0
      tox.ini

+ 1 - 1
.github/workflows/packaging.yml

@@ -31,7 +31,7 @@ jobs:
       - name: Install Taipy without dependencies
         run: |
           pip install .
-          rm -r taipy
+          rm -rf src
 
           python -c "import taipy as tp; tp.Scenario"
           python -c "import taipy as tp; tp.gui"

+ 35 - 0
.github/workflows/tests.yml

@@ -0,0 +1,35 @@
+name: Python tests
+
+on:
+  push:
+    branches: [ develop ]
+  pull_request:
+    branches: [ develop ]
+
+jobs:
+  backend:
+    timeout-minutes: 20
+    strategy:
+      matrix:
+        python-versions: ['3.8', '3.9', '3.10']
+        os: [ubuntu-18.04, windows-latest, macos-latest]
+    runs-on: ${{ matrix.os }}
+
+    steps:
+      - uses: actions/checkout@v2
+      - uses: actions/setup-python@v2
+        with:
+          python-version: ${{ matrix.python-versions }}
+
+      - name: Tests
+        run: |
+          pip install tox
+          tox -e tests
+
+      - name: Notify user if failed
+        if: failure() && github.event_name == 'workflow_dispatch'
+        run: |
+          if [[ -n "${{ github.event.inputs.user-to-notify }}" ]]; then
+            curl "${{ secrets.notify_endpoint }}" -d '{"username": "${{ github.event.inputs.user-to-notify }}", "url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" }' -H "Content-Type: application/json"
+          fi
+        shell: bash

+ 11 - 0
.gitignore

@@ -1,3 +1,14 @@
 .idea
 .venv
 __pycache__
+
+# Distribution / packaging
+Pipfile.lock
+build/
+*.egg-info/
+
+# IDE settings
+.vscode/
+.idea/
+.idea/taipy.iml
+.DS_Store

+ 17 - 0
Pipfile

@@ -0,0 +1,17 @@
+[[source]]
+url = "https://pypi.org/simple"
+verify_ssl = true
+name = "pypi"
+
+[packages]
+taipy-gui = {git = "https://git@github.com/Avaiga/taipy-gui.git@develop"}
+taipy-rest = {git = "https://git@github.com/Avaiga/taipy-rest.git@develop"}
+
+[dev-packages]
+pytest = "*"
+
+[requires]
+python_version = "3.8"
+
+[pipenv]
+allow_prereleases = true

+ 6 - 2
setup.py

@@ -13,7 +13,7 @@
 
 """The setup script."""
 
-from setuptools import find_packages, setup
+from setuptools import find_packages, find_namespace_packages, setup
 
 with open("README.md") as readme_file:
     readme = readme_file.read()
@@ -23,6 +23,8 @@ requirements = [
     "taipy-rest@git+https://git@github.com/Avaiga/taipy-rest.git@develop",
 ]
 
+test_requirements = ["pytest>=3.8"]
+
 extras_require = {
     "ngrok": ["pyngrok>=5"],
     "image": ["python-magic;platform_system!='Windows'", "python-magic-bin;platform_system=='Windows'"],
@@ -51,7 +53,9 @@ setup(
     long_description_content_type="text/markdown",
     keywords="taipy",
     name="taipy",
-    packages=find_packages(include=['taipy']),
+    package_dir={"": "src"},
+    packages=find_namespace_packages(where="src") + find_packages(include=["taipy"]),
+    test_suite="tests",
     url="https://github.com/avaiga/taipy",
     version="1.2.0.dev",
     zip_safe=False,

+ 8 - 7
taipy/__init__.py → src/taipy/__init__.py

@@ -11,24 +11,25 @@
 
 from importlib.util import find_spec
 
-if find_spec('taipy'):
-    if find_spec('taipy.config'):
+if find_spec("taipy"):
+
+    if find_spec("taipy.config"):
         from taipy.config import *
         from taipy.logger import *
 
-    if find_spec('taipy.core'):
+    if find_spec("taipy.core"):
         from taipy.core import *
 
-    if find_spec('taipy.gui'):
+    if find_spec("taipy.gui"):
         from taipy.gui import Gui
 
-        if find_spec('taipy.enterprise') and find_spec('taipy.enterprise.gui'):
+        if find_spec("taipy.enterprise") and find_spec("taipy.enterprise.gui"):
             from taipy.enterprise.gui import _init_gui_enterprise
 
             _init_gui_enterprise(Gui)
 
-    if find_spec('taipy.rest'):
+    if find_spec("taipy.rest"):
         from taipy.rest import Rest
 
-    if find_spec('taipy._run'):
+    if find_spec("taipy._run"):
         from taipy._run import _run as run

+ 7 - 5
taipy/_run.py → src/taipy/_run.py

@@ -11,18 +11,18 @@
 
 import typing as t
 
-from .gui import Gui
-from .rest import Rest
-from .core import Core
+from . import Gui
+from . import Rest
+from . import Core
 
 
-def _run(*apps: t.List[t.Union[Gui, Rest]], **kwargs) -> t.Optional[t.Union[Gui, Rest]]:
+def _run(*apps: t.List[t.Union[Gui, Rest, Core]], **kwargs) -> t.Optional[t.Union[Gui, Rest, Core]]:
     """Run one or multiple Taipy services.
 
     A Taipy service is an instance of a class that runs code as a Web application.
 
     Parameters:
-        *args (List[Union[`Gui^`, `Rest^`]]): Services to run. If several services are provided, all the services run simultaneously. If this is empty or set to None, this method does nothing.
+        *args (List[Union[`Gui^`, `Rest^`, `Core^`]]): Services to run. If several services are provided, all the services run simultaneously. If this is empty or set to None, this method does nothing.
         **kwargs: Other parameters to provide to the services.
     """
     gui = __typing_get(apps, Gui)
@@ -30,6 +30,8 @@ def _run(*apps: t.List[t.Union[Gui, Rest]], **kwargs) -> t.Optional[t.Union[Gui,
     core = __typing_get(apps, Core)
 
     if rest or core:
+        if not core:
+            core = Core()
         core.run()
 
     if not rest and not gui:

+ 12 - 0
tests/__init__.py

@@ -0,0 +1,12 @@
+# Copyright 2022 Avaiga Private Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations under the License.
+
+"""Unit test package for taipy."""

+ 55 - 0
tests/test_run.py

@@ -0,0 +1,55 @@
+# Copyright 2022 Avaiga Private Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations under the License.
+
+from unittest import mock
+
+from src.taipy._run import _run
+from taipy.gui import Gui
+from taipy.rest import Rest
+from taipy.core import Core
+
+
+@mock.patch("taipy.gui.Gui.run")
+def test_run_pass_with_gui(gui_run):
+    _run(Gui())
+    gui_run.assert_called_once()
+
+
+@mock.patch("taipy.core.Core.run")
+def test_run_pass_with_core(core_run):
+    _run(Core())
+    core_run.assert_called_once()
+
+
+@mock.patch("taipy.rest.Rest.run")
+@mock.patch("taipy.core.Core.run")
+def test_run_pass_with_rest(rest_run, core_run):
+    _run(Rest())
+    rest_run.assert_called_once()
+    core_run.assert_called_once()
+
+
+@mock.patch("taipy.rest.Rest.run")
+@mock.patch("taipy.core.Core.run")
+def test_run_pass_with_core_and_rest(core_run, rest_run):
+    _run(Core(), Rest())
+    core_run.assert_called_once()
+    rest_run.assert_called_once()
+
+
+@mock.patch("taipy.gui.Gui.run")
+@mock.patch("taipy.rest.Rest.run")
+@mock.patch("taipy.core.Core.run")
+def test_run_pass_with_gui_and_rest(core_run, rest_run, gui_run):
+    _run(Gui(), Rest())
+    gui_run.assert_called_once()
+    core_run.assert_called_once()
+    rest_run.assert_not_called()

+ 17 - 0
tox.ini

@@ -0,0 +1,17 @@
+[tox]
+skipsdist = true
+isolated_build = true
+envlist = clean, lint, without-pyodbc
+
+[pytest]
+filterwarnings =
+    ignore::DeprecationWarning
+
+[testenv]
+allowlist_externals = pytest
+deps = pipenv
+
+[testenv:tests]
+commands =
+    pipenv install --dev --skip-lock
+    pytest tests