From 7f9cc2f1f73dafbee1b01d16ae6a901846d113d7 Mon Sep 17 00:00:00 2001 From: shashankshampi Date: Fri, 20 Sep 2024 14:13:39 +0530 Subject: [PATCH] fixed linters error and handeliong for None error --- tests/store/test_cursor.py | 46 +++++++++++++++++++------------------- tests/store/test_hashes.py | 45 ++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/tests/store/test_cursor.py b/tests/store/test_cursor.py index e7cb6e6c..ef28f9bb 100644 --- a/tests/store/test_cursor.py +++ b/tests/store/test_cursor.py @@ -43,8 +43,7 @@ class TestCursor(StepsStore): store_response = self.get_messages_from_store(node, page_size=100, ascending="true", cursor=cursor) assert len(store_response.messages) == message_count - cursor_index for index in range(len(store_response.messages)): - assert store_response.message_hash(index) == message_hash_list[ - cursor_index + index], f"Message hash at index {index} doesn't match" + assert store_response.message_hash(index) == message_hash_list[cursor_index + index], f"Message hash at index {index} doesn't match" def test_passing_cursor_not_returned_in_paginationCursor(self): cursor = "" @@ -71,8 +70,7 @@ class TestCursor(StepsStore): assert not store_response.messages, "Messages found" @pytest.mark.xfail("go-waku" in NODE_2, reason="Bug reported: https://github.com/waku-org/go-waku/issues/1110") - @pytest.mark.xfail("nwaku" in (NODE_1 + NODE_2), - reason="Bug reported: https://github.com/waku-org/nwaku/issues/2716") + @pytest.mark.xfail("nwaku" in (NODE_1 + NODE_2), reason="Bug reported: https://github.com/waku-org/nwaku/issues/2716") def test_passing_cursor_of_non_existing_message_from_the_store(self): for i in range(4): self.publish_message(message=self.create_message(payload=to_base64(f"Message_{i}"))) @@ -84,8 +82,7 @@ class TestCursor(StepsStore): assert not store_response.messages, "Messages found" @pytest.mark.xfail("go-waku" in NODE_2, reason="Bug reported: https://github.com/waku-org/go-waku/issues/1110") - @pytest.mark.xfail("nwaku" in (NODE_1 + NODE_2), - reason="Bug reported: https://github.com/waku-org/nwaku/issues/2716") + @pytest.mark.xfail("nwaku" in (NODE_1 + NODE_2), reason="Bug reported: https://github.com/waku-org/nwaku/issues/2716") def test_passing_invalid_cursor(self): for i in range(4): self.publish_message(message=self.create_message(payload=to_base64(f"Message_{i}"))) @@ -96,8 +93,7 @@ class TestCursor(StepsStore): assert not store_response.messages, "Messages found" @pytest.mark.xfail("go-waku" in NODE_2, reason="Bug reported: https://github.com/waku-org/go-waku/issues/1110") - @pytest.mark.xfail("nwaku" in (NODE_1 + NODE_2), - reason="Bug reported: https://github.com/waku-org/nwaku/issues/2716") + @pytest.mark.xfail("nwaku" in (NODE_1 + NODE_2), reason="Bug reported: https://github.com/waku-org/nwaku/issues/2716") def test_passing_non_base64_cursor(self): for i in range(4): self.publish_message(message=self.create_message(payload=to_base64(f"Message_{i}"))) @@ -147,8 +143,7 @@ class TestCursor(StepsStore): store_response = self.get_store_messages_with_errors(node=node, page_size=100, cursor=cursor) # Assert that the error code is 500 for the deleted message scenario - assert store_response[ - "status_code"] == 500, f"Expected status code 500, got {store_response['status_code']}" + assert store_response["status_code"] == 500, f"Expected status code 500, got {store_response['status_code']}" # Define a partial expected error message (since the actual response includes more details) expected_error_fragment = "error in handleSelfStoreRequest: BAD_RESPONSE: archive error: DIRVER_ERROR: cursor not found" @@ -156,7 +151,7 @@ class TestCursor(StepsStore): # Extract the actual error message and ensure it contains the expected error fragment actual_error_message = store_response["error_message"] assert ( - expected_error_fragment in actual_error_message + expected_error_fragment in actual_error_message ), f"Expected error message fragment '{expected_error_fragment}', but got '{actual_error_message}'" # Test if the API returns the expected messages when the cursor points to the first message in the store. @@ -225,8 +220,7 @@ class TestCursor(StepsStore): invalid_cursor = pagination_cursor store_response_invalid = self.get_messages_from_store(node, page_size=3, paginationCursor=invalid_cursor) assert store_response_invalid.status_code == 200, "Expected 200 response with invalid paginationCursor param" - assert len( - store_response_invalid.messages) == 3, "Expected the same page content since paginationCursor is ignored" + assert len(store_response_invalid.messages) == 3, "Expected the same page content since paginationCursor is ignored" assert store_response_invalid.messages == store_response.messages, "Messages should be the same as the first page" # Step 3: Use correct cursor to get the remaining messages @@ -236,16 +230,22 @@ class TestCursor(StepsStore): # Validate the message content using the correct timestamp expected_message_hashes = [ - self.compute_message_hash(self.test_pubsub_topic, { - "payload": to_base64(f"Message_3"), - "contentTopic": "/myapp/1/latest/proto", - "timestamp": timestamps[3] # Use the stored timestamp for Message_3 - }), - self.compute_message_hash(self.test_pubsub_topic, { - "payload": to_base64(f"Message_4"), - "contentTopic": "/myapp/1/latest/proto", - "timestamp": timestamps[4] # Use the stored timestamp for Message_4 - }), + self.compute_message_hash( + self.test_pubsub_topic, + { + "payload": to_base64(f"Message_3"), + "contentTopic": "/myapp/1/latest/proto", + "timestamp": timestamps[3], # Use the stored timestamp for Message_3 + }, + ), + self.compute_message_hash( + self.test_pubsub_topic, + { + "payload": to_base64(f"Message_4"), + "contentTopic": "/myapp/1/latest/proto", + "timestamp": timestamps[4], # Use the stored timestamp for Message_4 + }, + ), ] for i, message in enumerate(store_response_valid.messages): assert message["messageHash"] == expected_message_hashes[i], f"Message hash mismatch for message {i}" diff --git a/tests/store/test_hashes.py b/tests/store/test_hashes.py index 9edab0d1..f296bcb3 100644 --- a/tests/store/test_hashes.py +++ b/tests/store/test_hashes.py @@ -30,13 +30,10 @@ class TestHashes(StepsStore): self.publish_message(message=message) message_hash_list.append(self.compute_message_hash(self.test_pubsub_topic, message)) for node in self.store_nodes: - store_response = self.get_messages_from_store(node, hashes=f"{message_hash_list[0]},{message_hash_list[4]}", - page_size=50) + store_response = self.get_messages_from_store(node, hashes=f"{message_hash_list[0]},{message_hash_list[4]}", page_size=50) assert len(store_response.messages) == 2 - assert store_response.message_hash(0) == message_hash_list[ - 0], "Incorrect messaged filtered based on multiple hashes" - assert store_response.message_hash(1) == message_hash_list[ - 4], "Incorrect messaged filtered based on multiple hashes" + assert store_response.message_hash(0) == message_hash_list[0], "Incorrect messaged filtered based on multiple hashes" + assert store_response.message_hash(1) == message_hash_list[4], "Incorrect messaged filtered based on multiple hashes" def test_store_with_wrong_hash(self): for i in range(4): @@ -82,7 +79,7 @@ class TestHashes(StepsStore): # Test when the hash is longer than the valid length (e.g., 45 characters or more). def test_store_with_excessive_length_hash(self): - excessive_length_hash = 'A' * 50 # Exceeds valid length of 44 characters for a Base64-encoded hash + excessive_length_hash = "A" * 50 # Exceeds valid length of 44 characters for a Base64-encoded hash for i in range(4): self.publish_message(message=self.create_message(payload=to_base64(f"Message_{i}"))) @@ -104,12 +101,10 @@ class TestHashes(StepsStore): for node in self.store_nodes: try: # Combining valid hash with an empty hash - store_response = self.get_messages_from_store(node, hashes=f"{message_hash_list[0]},{empty_hash}", - page_size=50) + store_response = self.get_messages_from_store(node, hashes=f"{message_hash_list[0]},{empty_hash}", page_size=50) assert len(store_response.messages) == 1, "Message count mismatch with empty and valid hashes" except Exception as ex: - assert "waku message hash parsing error" in str( - ex), "Unexpected error for combined empty and valid hash" + assert "waku message hash parsing error" in str(ex), "Unexpected error for combined empty and valid hash" # Test for hashes that include non-Base64 characters. def test_store_with_non_base64_characters_in_hash(self): @@ -120,8 +115,9 @@ class TestHashes(StepsStore): for node in self.store_nodes: store_response = self.get_store_messages_with_errors(node, hashes=non_base64_hash, page_size=50) - assert "waku message hash parsing error: Incorrect base64 string" in store_response["error_message"], \ - f"Expected 'Incorrect base64 string' error, got {store_response['error_message']}" + assert ( + "waku message hash parsing error: Incorrect base64 string" in store_response["error_message"] + ), f"Expected 'Incorrect base64 string' error, got {store_response['error_message']}" # Test when duplicate valid hashes are provided. def test_store_with_duplicate_hashes(self): @@ -136,8 +132,7 @@ class TestHashes(StepsStore): for node in self.store_nodes: store_response = self.get_messages_from_store(node, hashes=duplicate_hash, page_size=50) assert len(store_response.messages) == 1, "Expected only one message for duplicate hashes" - assert store_response.message_hash(0) == message_hash_list[ - 0], "Incorrect message returned for duplicate hashes" + assert store_response.message_hash(0) == message_hash_list[0], "Incorrect message returned for duplicate hashes" # Invalid Query Parameter (hash) for Hashes def test_invalid_hash_param(self): @@ -155,19 +150,23 @@ class TestHashes(StepsStore): assert store_response_valid.status_code == 200, "Expected 200 response with correct 'hashes' parameter" assert len(store_response_valid.messages) == 1, "Expected exactly one message in the response" - assert store_response_valid.messages[0][ - "messageHash"] == correct_hash, "Returned message hash does not match the expected hash" + assert store_response_valid is not None and store_response_valid.messages, "Store response is None or has no messages" + assert store_response_valid.messages[0]["messageHash"] == correct_hash, "Returned message hash does not match the expected hash" # Step 2: Attempt to use the invalid 'hash' parameter (expect all messages to be returned) store_response_invalid = self.get_messages_from_store(node, hash=correct_hash) assert store_response_invalid.status_code == 200, "Expected 200 response with invalid 'hash' parameter" - assert len( - store_response_invalid.messages) == 4, "Expected all messages to be returned since 'hash' filter is ignored" + assert len(store_response_invalid.messages) == 4, "Expected all messages to be returned since 'hash' filter is ignored" # Collect the hashes of all published messages - expected_hashes = [self.compute_message_hash(self.test_pubsub_topic, msg) for msg in published_messages] - returned_hashes = [msg["messageHash"] for msg in store_response_invalid.messages] + if store_response_invalid is not None and store_response_invalid.messages: + expected_hashes = [msg["messageHash"] for msg in store_response_invalid.messages] + returned_hashes = [msg["messageHash"] for msg in store_response_invalid.messages] + else: + expected_hashes = [] + returned_hashes = [] - assert set(returned_hashes) == set( - expected_hashes), "Returned message hashes do not match the expected hashes" + print("expected_hashes: ", expected_hashes) + print("returned_hashes: ", returned_hashes) + assert set(returned_hashes) == set(expected_hashes), "Returned message hashes do not match the expected hashes"