You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by ot...@apache.org on 2020/09/14 08:39:50 UTC

[plc4x] branch feature/plc4py updated: add initial class and test

This is an automated email from the ASF dual-hosted git repository.

otluk pushed a commit to branch feature/plc4py
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/plc4py by this push:
     new a303ec5  add initial class and test
     new 37d73ce  Merge pull request #187 from ottobackwards/py-init-class-test
a303ec5 is described below

commit a303ec5a6e650010bc5183fb25f9a14e74036dcb
Author: Otto Fowler <ot...@gmail.com>
AuthorDate: Sun Sep 13 22:31:35 2020 -0400

    add initial class and test
---
 sandbox/plc4py/plc4py/org/__init__.py              |  0
 sandbox/plc4py/plc4py/org/apache/__init__.py       |  0
 .../plc4py/org/apache/plc4x/PlcDriverManager.py    | 43 +++++++++++
 sandbox/plc4py/plc4py/org/apache/plc4x/__init__.py |  0
 .../plc4py/org/apache/plc4x/api/PlcConnection.py   | 31 ++++++++
 .../plc4py/plc4py/org/apache/plc4x/api/__init__.py |  0
 .../org/apache/plc4x/api/exceptions/__init__.py    |  0
 .../org/apache/plc4x/api/exceptions/exceptions.py  | 19 +++++
 sandbox/plc4py/poetry.lock                         | 90 +++++++++++++---------
 sandbox/plc4py/pyproject.toml                      |  2 +
 sandbox/plc4py/tests/unit/__init__.py              |  0
 sandbox/plc4py/tests/unit/org/__init__.py          |  0
 sandbox/plc4py/tests/unit/org/apache/__init__.py   |  0
 .../plc4py/tests/unit/org/apache/plc4x/__init__.py |  0
 .../unit/org/apache/plc4x/test_PlcDriverManager.py | 42 ++++++++++
 15 files changed, 191 insertions(+), 36 deletions(-)

diff --git a/sandbox/plc4py/plc4py/org/__init__.py b/sandbox/plc4py/plc4py/org/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/plc4py/org/apache/__init__.py b/sandbox/plc4py/plc4py/org/apache/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/plc4py/org/apache/plc4x/PlcDriverManager.py b/sandbox/plc4py/plc4py/org/apache/plc4x/PlcDriverManager.py
new file mode 100644
index 0000000..1476d57
--- /dev/null
+++ b/sandbox/plc4py/plc4py/org/apache/plc4x/PlcDriverManager.py
@@ -0,0 +1,43 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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 contextlib import contextmanager
+
+from plc4py.org.apache.plc4x.api.PlcConnection import PlcConnection
+
+
+class PlcDriverManager:
+
+    def __init__(self):
+        pass
+
+    @contextmanager
+    def connection(self, url: str) -> PlcConnection:
+        """
+        Context manager to handle connection.
+        """
+        conn = None
+        try:
+            conn = self.get_connection(url)
+            yield conn
+        finally:
+            if conn is not None:
+                conn.close()
+
+    def get_connection(self, url: str) -> PlcConnection:
+        pass
diff --git a/sandbox/plc4py/plc4py/org/apache/plc4x/__init__.py b/sandbox/plc4py/plc4py/org/apache/plc4x/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/plc4py/org/apache/plc4x/api/PlcConnection.py b/sandbox/plc4py/plc4py/org/apache/plc4x/api/PlcConnection.py
new file mode 100644
index 0000000..eef77a8
--- /dev/null
+++ b/sandbox/plc4py/plc4py/org/apache/plc4x/api/PlcConnection.py
@@ -0,0 +1,31 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+#
+
+class PlcConnection:
+    def __init__(self):
+        print('init method called')
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *args):
+        pass
+
+    def close(self) -> None:
+        pass
diff --git a/sandbox/plc4py/plc4py/org/apache/plc4x/api/__init__.py b/sandbox/plc4py/plc4py/org/apache/plc4x/api/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/plc4py/org/apache/plc4x/api/exceptions/__init__.py b/sandbox/plc4py/plc4py/org/apache/plc4x/api/exceptions/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/plc4py/org/apache/plc4x/api/exceptions/exceptions.py b/sandbox/plc4py/plc4py/org/apache/plc4x/api/exceptions/exceptions.py
new file mode 100644
index 0000000..1c732fd
--- /dev/null
+++ b/sandbox/plc4py/plc4py/org/apache/plc4x/api/exceptions/exceptions.py
@@ -0,0 +1,19 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+class PlcException(Exception):
+    pass;
diff --git a/sandbox/plc4py/poetry.lock b/sandbox/plc4py/poetry.lock
index 0b92a6c..8cf7d2d 100644
--- a/sandbox/plc4py/poetry.lock
+++ b/sandbox/plc4py/poetry.lock
@@ -1,20 +1,3 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you 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.
 [[package]]
 category = "dev"
 description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"."
@@ -38,13 +21,12 @@ description = "Classes Without Boilerplate"
 name = "attrs"
 optional = false
 python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
-version = "19.3.0"
+version = "20.1.0"
 
 [package.extras]
-azure-pipelines = ["coverage", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface", "pytest-azurepipelines"]
-dev = ["coverage", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface", "sphinx", "pre-commit"]
-docs = ["sphinx", "zope.interface"]
-tests = ["coverage", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface"]
+dev = ["coverage (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface", "sphinx", "sphinx-rtd-theme", "pre-commit"]
+docs = ["sphinx", "sphinx-rtd-theme", "zope.interface"]
+tests = ["coverage (>=5.0.2)", "hypothesis", "pympler", "pytest (>=4.3.0)", "six", "zope.interface"]
 
 [[package]]
 category = "dev"
@@ -85,18 +67,31 @@ description = "File identification library for Python"
 name = "identify"
 optional = false
 python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.7"
-version = "1.4.28"
+version = "1.4.30"
 
 [package.extras]
 license = ["editdistance"]
 
 [[package]]
 category = "dev"
+description = "Rolling backport of unittest.mock for all Pythons"
+name = "mock"
+optional = false
+python-versions = ">=3.6"
+version = "4.0.2"
+
+[package.extras]
+build = ["twine", "wheel", "blurb"]
+docs = ["sphinx"]
+test = ["pytest", "pytest-cov"]
+
+[[package]]
+category = "dev"
 description = "More routines for operating on iterables, beyond itertools"
 name = "more-itertools"
 optional = false
 python-versions = ">=3.5"
-version = "8.4.0"
+version = "8.5.0"
 
 [[package]]
 category = "dev"
@@ -104,7 +99,7 @@ description = "Node.js virtual environment builder"
 name = "nodeenv"
 optional = false
 python-versions = "*"
-version = "1.4.0"
+version = "1.5.0"
 
 [[package]]
 category = "dev"
@@ -135,7 +130,7 @@ description = "A framework for managing and maintaining multi-language pre-commi
 name = "pre-commit"
 optional = false
 python-versions = ">=3.6.1"
-version = "2.6.0"
+version = "2.7.1"
 
 [package.dependencies]
 cfgv = ">=2.0.0"
@@ -185,6 +180,20 @@ testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xm
 
 [[package]]
 category = "dev"
+description = "Thin-wrapper around the mock package for easier use with pytest"
+name = "pytest-mock"
+optional = false
+python-versions = ">=3.5"
+version = "3.3.1"
+
+[package.dependencies]
+pytest = ">=5.0"
+
+[package.extras]
+dev = ["pre-commit", "tox", "pytest-asyncio"]
+
+[[package]]
+category = "dev"
 description = "YAML parser and emitter for Python"
 name = "pyyaml"
 optional = false
@@ -234,7 +243,7 @@ python-versions = "*"
 version = "0.2.5"
 
 [metadata]
-content-hash = "4d9f9cbd84c540716e435c6067b848d1f835906850d5a074fa32e610fef2f02d"
+content-hash = "18eba98546671b753191d1d4a0a2d1687c166b6a2d4501b2e487f77d97250f4a"
 python-versions = "^3.8"
 
 [metadata.files]
@@ -247,8 +256,8 @@ atomicwrites = [
     {file = "atomicwrites-1.4.0.tar.gz", hash = "sha256:ae70396ad1a434f9c7046fd2dd196fc04b12f9e91ffb859164193be8b6168a7a"},
 ]
 attrs = [
-    {file = "attrs-19.3.0-py2.py3-none-any.whl", hash = "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c"},
-    {file = "attrs-19.3.0.tar.gz", hash = "sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72"},
+    {file = "attrs-20.1.0-py2.py3-none-any.whl", hash = "sha256:2867b7b9f8326499ab5b0e2d12801fa5c98842d2cbd22b35112ae04bf85b4dff"},
+    {file = "attrs-20.1.0.tar.gz", hash = "sha256:0ef97238856430dcf9228e07f316aefc17e8939fc8507e18c6501b761ef1a42a"},
 ]
 cfgv = [
     {file = "cfgv-3.2.0-py2.py3-none-any.whl", hash = "sha256:32e43d604bbe7896fe7c248a9c2276447dbef840feb28fe20494f62af110211d"},
@@ -267,15 +276,20 @@ filelock = [
     {file = "filelock-3.0.12.tar.gz", hash = "sha256:18d82244ee114f543149c66a6e0c14e9c4f8a1044b5cdaadd0f82159d6a6ff59"},
 ]
 identify = [
-    {file = "identify-1.4.28-py2.py3-none-any.whl", hash = "sha256:69c4769f085badafd0e04b1763e847258cbbf6d898e8678ebffc91abdb86f6c6"},
-    {file = "identify-1.4.28.tar.gz", hash = "sha256:d6ae6daee50ba1b493e9ca4d36a5edd55905d2cf43548fdc20b2a14edef102e7"},
+    {file = "identify-1.4.30-py2.py3-none-any.whl", hash = "sha256:f9f84a4ff44e29b9cc23c4012c2c8954089860723f80ce63d760393e5f197108"},
+    {file = "identify-1.4.30.tar.gz", hash = "sha256:e105a62fd3a496c701fd1bc4e24eb695455b5efb97e722816d5bd988c3344311"},
+]
+mock = [
+    {file = "mock-4.0.2-py3-none-any.whl", hash = "sha256:3f9b2c0196c60d21838f307f5825a7b86b678cedc58ab9e50a8988187b4d81e0"},
+    {file = "mock-4.0.2.tar.gz", hash = "sha256:dd33eb70232b6118298d516bbcecd26704689c386594f0f3c4f13867b2c56f72"},
 ]
 more-itertools = [
-    {file = "more-itertools-8.4.0.tar.gz", hash = "sha256:68c70cc7167bdf5c7c9d8f6954a7837089c6a36bf565383919bb595efb8a17e5"},
-    {file = "more_itertools-8.4.0-py3-none-any.whl", hash = "sha256:b78134b2063dd214000685165d81c154522c3ee0a1c0d4d113c80361c234c5a2"},
+    {file = "more-itertools-8.5.0.tar.gz", hash = "sha256:6f83822ae94818eae2612063a5101a7311e68ae8002005b5e05f03fd74a86a20"},
+    {file = "more_itertools-8.5.0-py3-none-any.whl", hash = "sha256:9b30f12df9393f0d28af9210ff8efe48d10c94f73e5daf886f10c4b0b0b4f03c"},
 ]
 nodeenv = [
-    {file = "nodeenv-1.4.0-py2.py3-none-any.whl", hash = "sha256:4b0b77afa3ba9b54f4b6396e60b0c83f59eaeb2d63dc3cc7a70f7f4af96c82bc"},
+    {file = "nodeenv-1.5.0-py2.py3-none-any.whl", hash = "sha256:5304d424c529c997bc888453aeaa6362d242b6b4631e90f3d4bf1b290f1c84a9"},
+    {file = "nodeenv-1.5.0.tar.gz", hash = "sha256:ab45090ae383b716c4ef89e690c41ff8c2b257b85b309f01f3654df3d084bd7c"},
 ]
 packaging = [
     {file = "packaging-20.4-py2.py3-none-any.whl", hash = "sha256:998416ba6962ae7fbd6596850b80e17859a5753ba17c32284f67bfff33784181"},
@@ -286,8 +300,8 @@ pluggy = [
     {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"},
 ]
 pre-commit = [
-    {file = "pre_commit-2.6.0-py2.py3-none-any.whl", hash = "sha256:e8b1315c585052e729ab7e99dcca5698266bedce9067d21dc909c23e3ceed626"},
-    {file = "pre_commit-2.6.0.tar.gz", hash = "sha256:1657663fdd63a321a4a739915d7d03baedd555b25054449090f97bb0cb30a915"},
+    {file = "pre_commit-2.7.1-py2.py3-none-any.whl", hash = "sha256:810aef2a2ba4f31eed1941fc270e72696a1ad5590b9751839c90807d0fff6b9a"},
+    {file = "pre_commit-2.7.1.tar.gz", hash = "sha256:c54fd3e574565fe128ecc5e7d2f91279772ddb03f8729645fa812fe809084a70"},
 ]
 py = [
     {file = "py-1.9.0-py2.py3-none-any.whl", hash = "sha256:366389d1db726cd2fcfc79732e75410e5fe4d31db13692115529d34069a043c2"},
@@ -301,6 +315,10 @@ pytest = [
     {file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"},
     {file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"},
 ]
+pytest-mock = [
+    {file = "pytest-mock-3.3.1.tar.gz", hash = "sha256:a4d6d37329e4a893e77d9ffa89e838dd2b45d5dc099984cf03c703ac8411bb82"},
+    {file = "pytest_mock-3.3.1-py3-none-any.whl", hash = "sha256:024e405ad382646318c4281948aadf6fe1135632bea9cc67366ea0c4098ef5f2"},
+]
 pyyaml = [
     {file = "PyYAML-5.3.1-cp27-cp27m-win32.whl", hash = "sha256:74809a57b329d6cc0fdccee6318f44b9b8649961fa73144a98735b0aaf029f1f"},
     {file = "PyYAML-5.3.1-cp27-cp27m-win_amd64.whl", hash = "sha256:240097ff019d7c70a4922b6869d8a86407758333f02203e0fc6ff79c5dcede76"},
diff --git a/sandbox/plc4py/pyproject.toml b/sandbox/plc4py/pyproject.toml
index 82d5db9..9d9af87 100644
--- a/sandbox/plc4py/pyproject.toml
+++ b/sandbox/plc4py/pyproject.toml
@@ -27,6 +27,8 @@ python = "^3.8"
 [tool.poetry.dev-dependencies]
 pytest = "^5.2"
 pre-commit = "^2.6.0"
+pytest-mock = "^3.3.1"
+mock = "^4.0.2"
 
 [build-system]
 requires = ["poetry>=0.12"]
diff --git a/sandbox/plc4py/tests/unit/__init__.py b/sandbox/plc4py/tests/unit/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/tests/unit/org/__init__.py b/sandbox/plc4py/tests/unit/org/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/tests/unit/org/apache/__init__.py b/sandbox/plc4py/tests/unit/org/apache/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/tests/unit/org/apache/plc4x/__init__.py b/sandbox/plc4py/tests/unit/org/apache/plc4x/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/sandbox/plc4py/tests/unit/org/apache/plc4x/test_PlcDriverManager.py b/sandbox/plc4py/tests/unit/org/apache/plc4x/test_PlcDriverManager.py
new file mode 100644
index 0000000..689a5f3
--- /dev/null
+++ b/sandbox/plc4py/tests/unit/org/apache/plc4x/test_PlcDriverManager.py
@@ -0,0 +1,42 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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 typing import Union
+from unittest.mock import MagicMock
+
+from plc4py.org.apache.plc4x.PlcDriverManager import PlcDriverManager
+from plc4py.org.apache.plc4x.api.PlcConnection import PlcConnection
+
+
+def test_connection_context_manager_impl_close_called(mocker) -> None:
+    manager: PlcDriverManager = PlcDriverManager()
+
+    # getup a plain return value for get_connection
+    connection_mock: MagicMock = mocker.patch.object(manager,"get_connection")
+    connection_mock.return_value = PlcConnection()
+
+    close_mock: Union[MagicMock, None] = None
+
+    # the connection function is supposed to support context manager
+    # so using it in a with statement should result in close being called on the connection
+    with manager.connection("foo://bar") as conn:
+        close_mock = mocker.patch.object(conn, "close")
+        print(conn.__doc__)
+
+    # verify that close was called by the context manager
+    close_mock.assert_called()