Skip to content

Commit 1d2debe

Browse files
committed
moved stubber to propet location
1 parent 4d84e73 commit 1d2debe

20 files changed

+161
-218
lines changed

python/example_code/neptune/tests/analytics_tests/test_create_graph.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import pytest
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
24
from botocore.exceptions import ClientError, BotoCoreError
3-
from analytics.create_neptune_graph_example import execute_create_graph
4-
from neptune_graph_stubber import NeptuneGraphStubber
5+
from example_code.neptune.analytics.create_neptune_graph_example import execute_create_graph
6+
from test_tools.neptune_graph_stubber import NeptuneGraphStubber
57

68
class MockBotoCoreError(BotoCoreError):
79
def __init__(self, message="BotoCore error occurred"):
@@ -12,7 +14,6 @@ def __str__(self):
1214
return self.message
1315

1416
def test_execute_create_graph(capfd):
15-
# --- Success case ---
1617
stubber = NeptuneGraphStubber()
1718
client = stubber.get_client()
1819
stubber.activate()
@@ -26,9 +27,8 @@ def test_execute_create_graph(capfd):
2627
assert "Graph ARN: arn:aws:neptune-graph:us-east-1:123456789012:graph/test-graph" in out
2728
assert "Graph Endpoint: https://test-graph.cluster-neptune.amazonaws.com" in out
2829

29-
stubber.deactivate() # deactivate the stubber before mocking
30+
stubber.deactivate()
3031

31-
# --- ClientError case ---
3232
def raise_client_error(**kwargs):
3333
raise ClientError(
3434
{"Error": {"Message": "Client error occurred"}}, "CreateGraph"
@@ -38,15 +38,13 @@ def raise_client_error(**kwargs):
3838
out, _ = capfd.readouterr()
3939
assert "Failed to create graph: Client error occurred" in out
4040

41-
# --- BotoCoreError case ---
4241
def raise_boto_core_error(**kwargs):
4342
raise MockBotoCoreError()
4443
client.create_graph = raise_boto_core_error
4544
execute_create_graph(client, "test-graph")
4645
out, _ = capfd.readouterr()
4746
assert "Failed to create graph: BotoCore error occurred" in out
4847

49-
# --- Generic Exception case ---
5048
def raise_generic_error(**kwargs):
5149
raise Exception("Generic failure")
5250
client.create_graph = raise_generic_error

python/example_code/neptune/tests/analytics_tests/test_execute_gremlin_profile_query.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
import io
2-
import pytest
35
from botocore.response import StreamingBody
4-
from botocore.exceptions import ClientError
5-
from analytics.neptune_analytics_query_example import run_open_cypher_query
6-
from neptune_graph_stubber import NeptuneGraphStubber
6+
from example_code.neptune.analytics.neptune_analytics_query_example import run_open_cypher_query
7+
from test_tools.neptune_graph_stubber import NeptuneGraphStubber
78

89
GRAPH_ID = "test-graph-id"
910

1011
def test_execute_gremlin_profile_query(capfd):
1112
stubber = NeptuneGraphStubber()
1213
client = stubber.get_client()
1314

14-
# --- Success case with payload ---
1515
stubber.activate()
1616
payload_bytes = b'{"results": "some data"}'
1717
response_body = StreamingBody(io.BytesIO(payload_bytes), len(payload_bytes))
@@ -30,7 +30,6 @@ def test_execute_gremlin_profile_query(capfd):
3030
assert '{"results": "some data"}' in out
3131
stubber.deactivate()
3232

33-
# --- Success case with empty payload ---
3433
stubber.activate()
3534
empty_payload = StreamingBody(io.BytesIO(b''), 0)
3635

@@ -45,14 +44,13 @@ def test_execute_gremlin_profile_query(capfd):
4544
)
4645
run_open_cypher_query(client, GRAPH_ID)
4746
out, _ = capfd.readouterr()
48-
assert out.strip() == "" # Empty line
47+
assert out.strip() == ""
4948
stubber.deactivate()
5049

51-
# --- ClientError case ---
5250
stubber.activate()
5351
stubber.stubber.add_client_error(
5452
"execute_query",
55-
service_error_code="ValidationException", # <-- Updated to a valid exception code
53+
service_error_code="ValidationException",
5654
service_message="Client error occurred",
5755
http_status_code=400,
5856
expected_params={
@@ -66,8 +64,6 @@ def test_execute_gremlin_profile_query(capfd):
6664
assert "ClientError: Client error occurred" in out
6765
stubber.deactivate()
6866

69-
# --- Generic Exception case ---
70-
# Deactivate stubber to allow monkeypatching client.execute_query
7167
stubber.deactivate()
7268

7369
def raise_generic_error(**kwargs):
@@ -77,4 +73,3 @@ def raise_generic_error(**kwargs):
7773
run_open_cypher_query(client, GRAPH_ID)
7874
out, _ = capfd.readouterr()
7975
assert "Unexpected error: Generic failure" in out
80-

python/example_code/neptune/tests/database_tests/execute_gremlin_profile_query.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import types
5-
import pytest
65
from botocore.exceptions import ClientError, BotoCoreError
7-
from neptune_data_stubber import NeptuneDateStubber
8-
from database.neptune_execute_gremlin_profile_query import run_profile_query # Adjust import path as needed
6+
from test_tools.neptune_data_stubber import NeptuneDateStubber
7+
from example_code.neptune.database.neptune_execute_gremlin_profile_query import run_profile_query
98

109
def test_run_profile_query(capfd):
1110
stubber = NeptuneDateStubber()

python/example_code/neptune/tests/database_tests/test_execute_gremlin_query.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
21
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
# SPDX-License-Identifier: Apache-2.0
43

54
import types
65
from botocore.exceptions import ClientError, BotoCoreError
7-
from database.neptune_execute_gremlin_query import execute_gremlin_query
8-
from neptune_data_stubber import NeptuneDateStubber
6+
from test_tools.neptune_data_stubber import NeptuneDateStubber
7+
from example_code.neptune.database.neptune_execute_gremlin_query import execute_gremlin_query
98

109
def test_execute_gremlin_query(capfd):
1110
stubber = NeptuneDateStubber()

python/example_code/neptune/tests/database_tests/test_neptune_execute_gremlin_explain_query.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
import io
54
import types
65
from botocore.exceptions import ClientError, EndpointConnectionError
7-
from neptune_data_stubber import NeptuneDateStubber
8-
from database.neptune_execute_gremlin_explain_query import execute_gremlin_query
6+
from test_tools.neptune_data_stubber import NeptuneDateStubber
7+
from example_code.neptune.database.neptune_execute_gremlin_explain_query import execute_gremlin_query
98

109
def test_execute_gremlin_query(capfd):
1110
stubber = NeptuneDateStubber()
1211
client = stubber.get_client()
1312
stubber.activate()
1413

1514
try:
16-
# --- Success case with valid StreamingBody output ---
1715
response_payload = '{"explain": "details"}'
1816
stubber.add_execute_gremlin_explain_query_stub(
1917
gremlin_query="g.V().has('code', 'ANC')",
@@ -26,7 +24,6 @@ def test_execute_gremlin_query(capfd):
2624
assert "Full Response:" in out
2725
assert '{"explain": "details"}' in out
2826

29-
# --- ClientError case ---
3027
stubber.stubber.assert_no_pending_responses()
3128
stubber.stubber.add_client_error(
3229
method='execute_gremlin_explain_query',
@@ -38,7 +35,6 @@ def test_execute_gremlin_query(capfd):
3835
out, _ = capfd.readouterr()
3936
assert "Error calling Neptune: Invalid query" in out
4037

41-
# --- EndpointConnectionError (BotoCoreError subclass) case ---
4238
stubber.stubber.assert_no_pending_responses()
4339

4440
def raise_endpoint_connection_error(*args, **kwargs):

python/example_code/neptune/tests/database_tests/test_opencypher_explain_query.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import types
55
from botocore.exceptions import ClientError, BotoCoreError
66
from botocore.response import StreamingBody
7-
from database.neptune_execute_open_cypher_query import execute_open_cypher_explain_query
8-
from neptune_data_stubber import NeptuneDateStubber # adjust import path accordingly
7+
from test_tools.neptune_data_stubber import NeptuneDateStubber
8+
from example_code.neptune.database.neptune_execute_open_cypher_query import execute_open_cypher_explain_query
99
import io
1010

1111
def test_execute_opencypher_explain_query(capfd):
@@ -14,7 +14,6 @@ def test_execute_opencypher_explain_query(capfd):
1414
stubber.activate()
1515

1616
try:
17-
# --- Case 1: Successful result (StreamingBody with bytes) ---
1817
explain_payload = 'mocked byte explain output'
1918
stubber.add_execute_open_cypher_explain_query_stub(
2019
open_cypher_query="MATCH (n {code: 'ANC'}) RETURN n",
@@ -28,7 +27,6 @@ def test_execute_opencypher_explain_query(capfd):
2827

2928
stubber.stubber.assert_no_pending_responses()
3029

31-
# --- Case 2: No results (empty StreamingBody) ---
3230
empty_stream = StreamingBody(io.BytesIO(b""), 0)
3331
stubber.stubber.add_response(
3432
"execute_open_cypher_explain_query",
@@ -48,7 +46,6 @@ def test_execute_opencypher_explain_query(capfd):
4846
finally:
4947
stubber.deactivate()
5048

51-
# --- Case 3: ClientError ---
5249
def run_client_error_case():
5350
stubber = NeptuneDateStubber()
5451
client = stubber.get_client()
@@ -71,7 +68,6 @@ def run_client_error_case():
7168

7269
run_client_error_case()
7370

74-
# --- Case 4: BotoCoreError (monkeypatch) ---
7571
def raise_boto_core_error(*args, **kwargs):
7672
raise BotoCoreError()
7773

@@ -80,7 +76,6 @@ def raise_boto_core_error(*args, **kwargs):
8076
out, _ = capfd.readouterr()
8177
assert "BotoCore error:" in out
8278

83-
# --- Case 5: Generic Exception (monkeypatch) ---
8479
def raise_generic_exception(*args, **kwargs):
8580
raise Exception("Some generic error")
8681

python/example_code/neptune/tests/test_check_instance_status.py

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,18 @@
44
import pytest
55
from botocore.exceptions import ClientError
66
import boto3
7-
from neptune_stubber import Neptune
8-
from neptune_scenario import check_instance_status # your function to test
9-
10-
# Constants for polling & timeout - patch if needed
11-
TIMEOUT_SECONDS = 10
12-
POLL_INTERVAL_SECONDS = 1
13-
7+
from test_tools.neptune_stubber import Neptune
8+
from example_code.neptune.neptune_scenario import check_instance_status
149

1510
def test_check_instance_status_with_neptune_stubber(monkeypatch):
16-
# Create real boto3 client + wrap with Neptune stubber
1711
client = boto3.client("neptune", region_name="us-east-1")
1812
stubber = Neptune(client)
1913

2014
instance_id = "instance-1"
2115

22-
# Prepare stubbed responses for describe_db_instances paginator pages
23-
# Each call to paginate() will return these pages in order:
24-
# First call returns status 'starting', second returns 'available'
25-
# Because the paginator returns an iterator of pages, each page is a dict
26-
2716
stubbed_pages_starting = [{"DBInstances": [{"DBInstanceStatus": "starting"}]}]
2817
stubbed_pages_available = [{"DBInstances": [{"DBInstanceStatus": "available"}]}]
2918

30-
# We need to stub `describe_db_instances` for each paginator page request
31-
# So stub two responses in sequence to simulate status change on subsequent polls
3219
stubber.stubber.add_response(
3320
"describe_db_instances",
3421
stubbed_pages_starting[0],
@@ -40,17 +27,18 @@ def test_check_instance_status_with_neptune_stubber(monkeypatch):
4027
expected_params={"DBInstanceIdentifier": instance_id},
4128
)
4229

43-
# Patch time.time to simulate time passing quickly (simulate elapsed time)
4430
times = [0, 1, 2, 3, 4, 5]
45-
monkeypatch.setattr("neptune_scenario.time.time", lambda: times.pop(0) if times else 5)
46-
47-
# Patch time.sleep to avoid real wait during test
48-
monkeypatch.setattr("neptune_scenario.time.sleep", lambda s: None)
49-
50-
# Patch format_elapsed_time to just return seconds + 's' string
51-
monkeypatch.setattr("neptune_scenario.format_elapsed_time", lambda x: f"{x}s")
31+
monkeypatch.setattr(
32+
"example_code.neptune.neptune_scenario.time.time",
33+
lambda: times.pop(0) if times else 5,
34+
)
35+
monkeypatch.setattr(
36+
"example_code.neptune.neptune_scenario.time.sleep", lambda s: None
37+
)
38+
monkeypatch.setattr(
39+
"example_code.neptune.neptune_scenario.format_elapsed_time", lambda x: f"{x}s"
40+
)
5241

53-
# Run the check_instance_status function (should exit once status 'available' is found)
5442
check_instance_status(stubber.client, instance_id, "available")
5543

5644

@@ -60,50 +48,54 @@ def test_check_instance_status_timeout(monkeypatch):
6048

6149
instance_id = "instance-timeout"
6250

63-
# Always return status 'starting' to simulate never reaching 'available'
6451
stub_response = {"DBInstances": [{"DBInstanceStatus": "starting"}]}
6552

66-
# Stub multiple responses (enough for timeout loops)
6753
for _ in range(10):
6854
stubber.stubber.add_response(
6955
"describe_db_instances",
7056
stub_response,
7157
expected_params={"DBInstanceIdentifier": instance_id},
7258
)
7359

74-
# Patch time.time to simulate time passing beyond timeout (simulate elapsed time)
75-
times = list(range(15)) # simulate 15 seconds
76-
monkeypatch.setattr("neptune_scenario.time.time", lambda: times.pop(0) if times else 15)
77-
78-
monkeypatch.setattr("neptune_scenario.time.sleep", lambda s: None)
79-
monkeypatch.setattr("neptune_scenario.format_elapsed_time", lambda x: f"{x}s")
60+
times = list(range(15))
61+
monkeypatch.setattr(
62+
"example_code.neptune.neptune_scenario.time.time",
63+
lambda: times.pop(0) if times else 15,
64+
)
65+
monkeypatch.setattr(
66+
"example_code.neptune.neptune_scenario.time.sleep", lambda s: None
67+
)
68+
monkeypatch.setattr(
69+
"example_code.neptune.neptune_scenario.format_elapsed_time", lambda x: f"{x}s"
70+
)
8071

81-
# Patch timeout and poll interval inside your module (adjust as needed)
82-
monkeypatch.setattr("neptune_scenario.TIMEOUT_SECONDS", 5)
83-
monkeypatch.setattr("neptune_scenario.POLL_INTERVAL_SECONDS", 1)
72+
monkeypatch.setattr("example_code.neptune.neptune_scenario.TIMEOUT_SECONDS", 5)
73+
monkeypatch.setattr("example_code.neptune.neptune_scenario.POLL_INTERVAL_SECONDS", 1)
8474

85-
with pytest.raises(RuntimeError, match="Timeout waiting for 'instance-timeout'"):
75+
with pytest.raises(RuntimeError, match=f"Timeout waiting for '{instance_id}'"):
8676
check_instance_status(stubber.client, instance_id, "available")
8777

88-
8978
def test_check_instance_status_client_error(monkeypatch):
90-
client = boto3.client("neptune")
79+
client = boto3.client("neptune", region_name="us-east-1")
9180
stubber = Neptune(client)
9281

9382
instance_id = "not-there"
9483

95-
# Stub a ClientError for describe_db_instances
9684
stubber.stubber.add_client_error(
9785
"describe_db_instances",
9886
service_error_code="DBInstanceNotFound",
9987
service_message="Instance not found",
10088
expected_params={"DBInstanceIdentifier": instance_id},
10189
)
10290

103-
# Patch time.sleep and format_elapsed_time to avoid delays and keep output clean
104-
monkeypatch.setattr("neptune_scenario.time.sleep", lambda s: None)
105-
monkeypatch.setattr("neptune_scenario.format_elapsed_time", lambda x: f"{x}s")
91+
monkeypatch.setattr(
92+
"example_code.neptune.neptune_scenario.time.sleep", lambda s: None
93+
)
94+
monkeypatch.setattr(
95+
"example_code.neptune.neptune_scenario.format_elapsed_time", lambda x: f"{x}s"
96+
)
10697

10798
with pytest.raises(ClientError, match="Instance not found"):
10899
check_instance_status(stubber.client, instance_id, "available")
109100

101+

python/example_code/neptune/tests/test_create_db_cluster.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@
33

44
import pytest
55
import boto3
6-
from botocore.exceptions import ClientError
7-
from neptune_stubber import Neptune
8-
from neptune_scenario import create_db_cluster # Your actual function
9-
6+
from test_tools.neptune_stubber import Neptune
7+
from example_code.neptune.neptune_scenario import create_db_cluster
108

119
def test_create_db_cluster():
12-
# Create Boto3 Neptune client and attach Stubber wrapper
1310
boto_client = boto3.client("neptune", region_name="us-east-1")
1411
stubber = Neptune(boto_client)
1512

16-
# --- Success case ---
1713
stubber.stub_create_db_cluster(
1814
cluster_id="test-cluster"
19-
# engine, deletion_protection, backup_retention_period defaulted
2015
)
2116
cluster_id = create_db_cluster(stubber.client, "test-cluster")
2217
assert cluster_id == "test-cluster"
@@ -34,7 +29,6 @@ def test_create_db_cluster():
3429
with pytest.raises(RuntimeError, match="Cluster created but no ID returned"):
3530
create_db_cluster(stubber.client, "missing-id-cluster")
3631

37-
# --- ClientError is wrapped in RuntimeError ---
3832
stubber.stub_create_db_cluster(
3933
cluster_id="denied-cluster",
4034
error_code="AccessDenied"
@@ -43,7 +37,6 @@ def test_create_db_cluster():
4337
create_db_cluster(stubber.client, "denied-cluster")
4438
assert "AWS error [AccessDenied]" in str(exc_info.value)
4539

46-
# --- Unexpected exception raises RuntimeError ---
4740
def raise_generic_exception(**kwargs):
4841
raise Exception("Unexpected failure")
4942

0 commit comments

Comments
 (0)