You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by bo...@apache.org on 2023/05/07 18:30:32 UTC

[streampipes] 06/06: feature(1259): adapt tests

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

bossenti pushed a commit to branch 1259-verify-authentication-on-startup-of-python-client
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit ff4f3adc7d99534bd8df93a669a80aef93a42d13
Author: bossenti <bo...@posteo.de>
AuthorDate: Sun May 7 19:52:38 2023 +0200

    feature(1259): adapt tests
    
    Signed-off-by: bossenti <bo...@posteo.de>
---
 .../streampipes/client/client.py                   |  7 +-
 .../tests/client/test_client.py                    | 18 +++--
 .../tests/client/test_data_lake_series.py          | 18 ++++-
 .../tests/client/test_endpoint.py                  | 78 ++++++++++++++++------
 .../tests/functions/test_function_handler.py       | 29 ++++++--
 .../tests/functions/test_river_function.py         |  8 +++
 6 files changed, 122 insertions(+), 36 deletions(-)

diff --git a/streampipes-client-python/streampipes/client/client.py b/streampipes-client-python/streampipes/client/client.py
index afb8c7888..00f15b0ba 100644
--- a/streampipes-client-python/streampipes/client/client.py
+++ b/streampipes-client-python/streampipes/client/client.py
@@ -274,10 +274,13 @@ class StreamPipesClient:
             }
         )
 
+        # ensure deterministic order
+        ordered_available_endpoints = sorted(available_endpoints)
+
         # collect the number of available resources per endpoint
         endpoint_stats = {
             (all_items := self.__getattribute__(endpoint_name).all()).__class__.__name__: len(all_items)
-            for endpoint_name in available_endpoints
+            for endpoint_name in ordered_available_endpoints
         }
 
         # sort the endpoints descending based on the number of resources
@@ -295,4 +298,4 @@ class StreamPipesClient:
 
         endpoint_stats_message = "\n".join(f"{count}x {name}" for name, count in sorted_endpoint_stats.items())
 
-        print(base_message + endpoint_stats_message)
+        logger.info(base_message + endpoint_stats_message)
diff --git a/streampipes-client-python/tests/client/test_client.py b/streampipes-client-python/tests/client/test_client.py
index a08904438..19a898833 100644
--- a/streampipes-client-python/tests/client/test_client.py
+++ b/streampipes-client-python/tests/client/test_client.py
@@ -28,7 +28,11 @@ from streampipes.endpoint.api import DataLakeMeasureEndpoint
 
 
 class TestStreamPipesClient(TestCase):
-    def test_client_init(self):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_client_init(self, server_version: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         result = StreamPipesClient(
             client_config=StreamPipesClientConfig(
                 credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
@@ -50,7 +54,11 @@ class TestStreamPipesClient(TestCase):
         self.assertTrue(isinstance(result.dataLakeMeasureApi, DataLakeMeasureEndpoint))
         self.assertEqual(result.base_api_path, "http://localhost:80/streampipes-backend/")
 
-    def test_client_create(self):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_client_create(self, server_version: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         result = StreamPipesClient.create(
             client_config=StreamPipesClientConfig(
                 credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
@@ -73,9 +81,9 @@ class TestStreamPipesClient(TestCase):
         self.assertTrue(isinstance(result.dataLakeMeasureApi, DataLakeMeasureEndpoint))
         self.assertEqual(result.base_api_path, "https://localhost:443/streampipes-backend/")
 
-    @patch("builtins.print")
+    @patch("streampipes.client.client.logger", autospec=True)
     @patch("streampipes.endpoint.endpoint.APIEndpoint._make_request", autospec=True)
-    def test_client_describe(self, make_request: MagicMock, mocked_print: MagicMock):
+    def test_client_describe(self, make_request: MagicMock, mocked_logger: MagicMock):
 
         class MockResponse:
             def __init__(self, text):
@@ -120,7 +128,7 @@ class TestStreamPipesClient(TestCase):
             )
         ).describe()
 
-        mocked_print.assert_has_calls(
+        mocked_logger.info.assert_has_calls(
             calls=[
                 call(
                     "\nHi there!\nYou are connected to a StreamPipes instance running at https://localhost:443 with version SP-dev.\n"
diff --git a/streampipes-client-python/tests/client/test_data_lake_series.py b/streampipes-client-python/tests/client/test_data_lake_series.py
index d634a1262..f6ebf41ee 100644
--- a/streampipes-client-python/tests/client/test_data_lake_series.py
+++ b/streampipes-client-python/tests/client/test_data_lake_series.py
@@ -94,7 +94,11 @@ class TestDataLakeSeries(TestCase):
         return result.to_pandas()
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_to_pandas(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_to_pandas(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         query_result = {
             "total": 1,
             "headers": self.headers,
@@ -114,7 +118,11 @@ class TestDataLakeSeries(TestCase):
         self.assertEqual(73.37740325927734, result_pd["level"][0])
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_group_by_to_pandas(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_group_by_to_pandas(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         query_result = {
             "total": 2,
             "headers": self.headers,
@@ -135,7 +143,11 @@ class TestDataLakeSeries(TestCase):
         self.assertEqual(70.03279876708984, result_pd["level"][3])
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_different_headers_exception(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_different_headers_exception(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         query_result = {
             "total": 1,
             "headers": ['one'],
diff --git a/streampipes-client-python/tests/client/test_endpoint.py b/streampipes-client-python/tests/client/test_endpoint.py
index 9b67df64b..c610339ee 100644
--- a/streampipes-client-python/tests/client/test_endpoint.py
+++ b/streampipes-client-python/tests/client/test_endpoint.py
@@ -115,7 +115,7 @@ class TestStreamPipesEndpoints(TestCase):
                             "topicDefinition": {
                                 "@class": "org.apache.streampipes.model.grounding.SimpleTopicDefinition",
                                 "actualTopicName": "org.apache.streampipes.connect."
-                                "fc22b8f6-698a-4127-aa71-e11854dc57c5",
+                                                   "fc22b8f6-698a-4127-aa71-e11854dc57c5",
                             },
                             "port": 4222,
                         }
@@ -170,7 +170,7 @@ class TestStreamPipesEndpoints(TestCase):
                 "measurementObject": None,
                 "index": 0,
                 "correspondingAdapterId": "urn:streampipes.apache.org:spi:org.apache.streampipes.connect."
-                "iiot.adapters.simulator.machine:11934d37-135b-4ef6-b5f1-4f520cc81a43",
+                                          "iiot.adapters.simulator.machine:11934d37-135b-4ef6-b5f1-4f520cc81a43",
                 "category": None,
                 "uri": "urn:streampipes.apache.org:eventstream:uPDKLI",
                 "dom": None,
@@ -188,11 +188,14 @@ class TestStreamPipesEndpoints(TestCase):
         self.data_lake_measure_all_json_validation = json.dumps(self.dlm_all_manipulated)
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_endpoint_get(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_endpoint_get(self, server_version: MagicMock, http_session: MagicMock):
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.json.return_value = self.data_stream_get
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         client = StreamPipesClient(
             client_config=StreamPipesClientConfig(
                 credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
@@ -206,7 +209,7 @@ class TestStreamPipesEndpoints(TestCase):
             calls=[
                 call().get(
                     url="https://localhost:80/streampipes-backend/api/v2/streams/urn:streampipes."
-                    "apache.org:eventstream:uPDKLI"
+                        "apache.org:eventstream:uPDKLI"
                 )
             ],
             any_order=True,
@@ -215,10 +218,13 @@ class TestStreamPipesEndpoints(TestCase):
         self.assertEqual(result.to_dict(use_source_names=True), self.data_stream_get)
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_endpoint_post(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_endpoint_post(self, server_version: MagicMock, http_session: MagicMock):
         http_session_mock = MagicMock()
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         client = StreamPipesClient(
             client_config=StreamPipesClientConfig(
                 credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
@@ -235,7 +241,11 @@ class TestStreamPipesEndpoints(TestCase):
         )
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_endpoint_data_stream_happy_path(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_endpoint_data_stream_happy_path(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.text = self.data_stream_all_json
         http_session.return_value = http_session_mock
@@ -301,7 +311,11 @@ class TestStreamPipesEndpoints(TestCase):
         )
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_endpoint_data_lake_measure_happy_path(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_endpoint_data_lake_measure_happy_path(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.text = self.data_lake_measure_all_json
         http_session.return_value = http_session_mock
@@ -350,7 +364,11 @@ class TestStreamPipesEndpoints(TestCase):
         self.assertEqual(2, result_pd["num_event_properties"][0])
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_endpoint_data_lake_measure_bad_return_code(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_endpoint_data_lake_measure_bad_return_code(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         response_mock = MagicMock()
         response_mock.status_code = 405
         response_mock.text = "Test error"
@@ -376,7 +394,11 @@ class TestStreamPipesEndpoints(TestCase):
         )
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_endpoint_data_lake_measure_json_error(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_endpoint_data_lake_measure_json_error(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.text = self.data_lake_measure_all_json_error
         http_session.return_value = http_session_mock
@@ -392,7 +414,11 @@ class TestStreamPipesEndpoints(TestCase):
             client.dataLakeMeasureApi.all()
 
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_endpoint_data_lake_measure_validation_error(self, http_session: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_endpoint_data_lake_measure_validation_error(self, server_version: MagicMock, http_session: MagicMock):
+
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.text = self.data_lake_measure_all_json_validation
         http_session.return_value = http_session_mock
@@ -411,24 +437,34 @@ class TestStreamPipesEndpoints(TestCase):
 
 
 class TestMessagingEndpoint(TestCase):
-    client = StreamPipesClient(
-        client_config=StreamPipesClientConfig(
-            credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
-            host_address="localhost",
+
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_messaging_endpoint_happy_path(self, _: MagicMock):
+
+        client = StreamPipesClient(
+            client_config=StreamPipesClientConfig(
+                credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
+                host_address="localhost",
+            )
         )
-    )
 
-    @patch("streampipes.client.client.Session", autospec=True)
-    def test_messaging_endpoint_happy_path(self):
-        demo_endpoint = MessagingEndpoint(parent_client=self.client)
+
+        demo_endpoint = MessagingEndpoint(parent_client=client)
 
         demo_endpoint.configure(broker=NatsConsumer())
 
         self.assertTrue(isinstance(demo_endpoint.broker, NatsConsumer))
 
-    @patch("streampipes.client.client.Session", autospec=True)
-    def test_messaging_endpoint_missing_configure(self):
-        demo_endpoint = MessagingEndpoint(parent_client=self.client)
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_messaging_endpoint_missing_configure(self, _:MagicMock):
+        client = StreamPipesClient(
+            client_config=StreamPipesClientConfig(
+                credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
+                host_address="localhost",
+            )
+        )
+
+        demo_endpoint = MessagingEndpoint(parent_client=client)
 
         with self.assertRaises(MessagingEndpointNotConfiguredError):
             demo_endpoint.broker
diff --git a/streampipes-client-python/tests/functions/test_function_handler.py b/streampipes-client-python/tests/functions/test_function_handler.py
index 8ff325299..fdb27ea46 100644
--- a/streampipes-client-python/tests/functions/test_function_handler.py
+++ b/streampipes-client-python/tests/functions/test_function_handler.py
@@ -174,11 +174,14 @@ class TestFunctionHandler(TestCase):
     @patch("streampipes.functions.broker.nats.nats_consumer.connect", autospec=True)
     @patch("streampipes.functions.broker.NatsConsumer.get_message", autospec=True)
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_function_handler_nats(self, http_session: MagicMock, get_messages: MagicMock, connection: AsyncMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_function_handler_nats(self, server_version: MagicMock, http_session: MagicMock, get_messages: MagicMock, connection: AsyncMock):
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.json.return_value = self.data_stream_nats
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": '0.x.y'}
+
         get_messages.return_value = TestMessageIterator(self.test_stream_data1)
 
         client = StreamPipesClient(
@@ -206,11 +209,14 @@ class TestFunctionHandler(TestCase):
 
     @patch("streampipes.functions.broker.kafka.kafka_consumer.KafkaConnection", autospec=True)
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_function_handler_kafka(self, http_session: MagicMock, connection: MagicMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_function_handler_kafka(self, server_version: MagicMock, http_session: MagicMock, connection: MagicMock):
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.json.return_value = self.data_stream_kafka
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": '0.x.y'}
+
         connection_mock = MagicMock()
         connection_mock.poll.side_effect = TestKafkaMessageContainer(self.test_stream_data1).get_data
         connection.return_value = connection_mock
@@ -240,11 +246,14 @@ class TestFunctionHandler(TestCase):
 
     @patch("streampipes.functions.broker.nats.nats_consumer.connect", autospec=True)
     @patch("streampipes.client.client.Session", autospec=True)
-    def test_function_handler_unsupported_broker(self, http_session: MagicMock, connection: AsyncMock):
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_function_handler_unsupported_broker(self, server_version: MagicMock, http_session: MagicMock, connection: AsyncMock):
         http_session_mock = MagicMock()
         http_session_mock.get.return_value.json.return_value = self.data_stream_unsupported_broker
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         client = StreamPipesClient(
             client_config=StreamPipesClientConfig(
                 credential_provider=StreamPipesApiKeyCredentials(username="user", api_key="key"),
@@ -261,8 +270,9 @@ class TestFunctionHandler(TestCase):
 
     @patch("streampipes.functions.broker.nats.nats_consumer.connect", autospec=True)
     @patch("streampipes.functions.broker.NatsConsumer.get_message", autospec=True)
-    @patch("streampipes.endpoint.endpoint.APIEndpoint.get", autospec=True)
-    def test_two_streams_nats(self, endpoint: MagicMock, nats_broker: MagicMock, *args: Tuple[AsyncMock]):
+    @patch("streampipes.endpoint.api.DataStreamEndpoint.get", autospec=True)
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
+    def test_two_streams_nats(self, server_version: MagicMock, endpoint: MagicMock, nats_broker: MagicMock, *args: Tuple[AsyncMock]):
         def get_stream(endpoint, stream_id):
             if stream_id == "urn:streampipes.apache.org:eventstream:uPDKLI":
                 return DataStream(**self.data_stream_nats)
@@ -272,6 +282,7 @@ class TestFunctionHandler(TestCase):
                 data_stream.event_grounding.transport_protocols[0].topic_definition.actual_topic_name = "test2"
                 return data_stream
 
+        server_version.return_value = {"backendVersion": "0.x.y"}
         endpoint.side_effect = get_stream
 
         def get_message(broker):
@@ -318,8 +329,10 @@ class TestFunctionHandler(TestCase):
     @patch("streampipes.functions.broker.NatsConsumer.get_message", autospec=True)
     @patch("streampipes.functions.broker.NatsPublisher.publish_event", autospec=True)
     @patch("streampipes.client.client.Session", autospec=True)
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
     def test_function_output_stream_nats(
         self,
+            server_version: MagicMock,
         http_session: MagicMock,
         pulish_event: MagicMock,
         get_message: MagicMock,
@@ -330,6 +343,8 @@ class TestFunctionHandler(TestCase):
         http_session_mock.get.return_value.json.return_value = self.data_stream_nats
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         output_events = []
 
         def save_event(self, event: Dict[str, Any]):
@@ -370,8 +385,10 @@ class TestFunctionHandler(TestCase):
     @patch("streampipes.functions.broker.kafka.kafka_consumer.KafkaConnection", autospec=True)
     @patch("streampipes.functions.broker.KafkaPublisher.publish_event", autospec=True)
     @patch("streampipes.client.client.Session", autospec=True)
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
     def test_function_output_stream_kafka(
         self,
+            server_version: MagicMock,
         http_session: MagicMock,
         pulish_event: MagicMock,
         connection: MagicMock,
@@ -384,6 +401,8 @@ class TestFunctionHandler(TestCase):
         http_session_mock.get.return_value.json.return_value = self.data_stream_kafka
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": "0.x.y"}
+
         output_events = []
 
         def save_event(self, event: Dict[str, Any]):
diff --git a/streampipes-client-python/tests/functions/test_river_function.py b/streampipes-client-python/tests/functions/test_river_function.py
index a850501ef..985a1359a 100644
--- a/streampipes-client-python/tests/functions/test_river_function.py
+++ b/streampipes-client-python/tests/functions/test_river_function.py
@@ -76,8 +76,10 @@ class TestRiverFunction(TestCase):
     @patch("streampipes.functions.broker.NatsConsumer.get_message", autospec=True)
     @patch("streampipes.functions.broker.NatsPublisher.publish_event", autospec=True)
     @patch("streampipes.client.client.Session", autospec=True)
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
     def test_river_function_unsupervised(
         self,
+        server_version: MagicMock,
         http_session: MagicMock,
         pulish_event: MagicMock,
         get_message: MagicMock,
@@ -88,6 +90,8 @@ class TestRiverFunction(TestCase):
         http_session_mock.get.return_value.json.return_value = self.data_stream
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": '0.x.y'}
+
         output_events = []
 
         def save_event(self, event: Dict[str, Any]):
@@ -141,8 +145,10 @@ class TestRiverFunction(TestCase):
     @patch("streampipes.functions.broker.NatsConsumer.get_message", autospec=True)
     @patch("streampipes.functions.broker.NatsPublisher.publish_event", autospec=True)
     @patch("streampipes.client.client.Session", autospec=True)
+    @patch("streampipes.client.client.StreamPipesClient._get_server_version", autospec=True)
     def test_river_function_supervised(
         self,
+        server_version: MagicMock,
         http_session: MagicMock,
         pulish_event: MagicMock,
         get_message: MagicMock,
@@ -153,6 +159,8 @@ class TestRiverFunction(TestCase):
         http_session_mock.get.return_value.json.return_value = self.data_stream
         http_session.return_value = http_session_mock
 
+        server_version.return_value = {"backendVersion": '0.x.y'}
+
         output_events = []
 
         def save_event(self, event: Dict[str, Any]):