Skip to content

Commit 72f6b3b

Browse files
committed
chore: cleanup tracer tests
1 parent f11c419 commit 72f6b3b

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

tests/functional/test_tracing.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,120 +14,125 @@ def reset_tracing_config():
1414
yield
1515

1616

17+
@pytest.fixture
18+
def service_name():
19+
return "booking"
20+
21+
1722
def test_capture_lambda_handler(dummy_response):
18-
# GIVEN tracer is disabled, and decorator is used
19-
# WHEN a lambda handler is run
20-
# THEN tracer should not raise an Exception
23+
# GIVEN tracer lambda handler decorator is used
2124
tracer = Tracer(disabled=True)
2225

26+
# WHEN a lambda handler is run
2327
@tracer.capture_lambda_handler
2428
def handler(event, context):
2529
return dummy_response
2630

31+
# THEN tracer should not raise an Exception
2732
handler({}, {})
2833

2934

3035
def test_capture_method(dummy_response):
31-
# GIVEN tracer is disabled, and method decorator is used
32-
# WHEN a function is run
33-
# THEN tracer should not raise an Exception
34-
36+
# GIVEN tracer method decorator is used
3537
tracer = Tracer(disabled=True)
3638

39+
# WHEN a function is run
3740
@tracer.capture_method
3841
def greeting(name, message):
3942
return dummy_response
4043

44+
# THEN tracer should not raise an Exception
4145
greeting(name="Foo", message="Bar")
4246

4347

4448
def test_tracer_lambda_emulator(monkeypatch, dummy_response):
45-
# GIVEN tracer is run locally
46-
# WHEN a lambda function is run through SAM CLI
47-
# THEN tracer should not raise an Exception
49+
# GIVEN tracer runs locally
4850
monkeypatch.setenv("AWS_SAM_LOCAL", "true")
4951
tracer = Tracer()
5052

53+
# WHEN a lambda function is run through SAM CLI
5154
@tracer.capture_lambda_handler
5255
def handler(event, context):
5356
return dummy_response
5457

58+
# THEN tracer should run in disabled mode, and not raise an Exception
5559
handler({}, {})
56-
monkeypatch.delenv("AWS_SAM_LOCAL")
5760

5861

5962
def test_tracer_metadata_disabled(dummy_response):
6063
# GIVEN tracer is disabled, and annotations/metadata are used
61-
# WHEN a lambda handler is run
62-
# THEN tracer should not raise an Exception and simply ignore
6364
tracer = Tracer(disabled=True)
6465

66+
# WHEN a lambda handler is run
6567
@tracer.capture_lambda_handler
6668
def handler(event, context):
6769
tracer.put_annotation("PaymentStatus", "SUCCESS")
6870
tracer.put_metadata("PaymentMetadata", "Metadata")
6971
return dummy_response
7072

73+
# THEN tracer should not raise any Exception
7174
handler({}, {})
7275

7376

74-
def test_tracer_env_vars(monkeypatch):
75-
# GIVEN tracer disabled, is run without parameters
76-
# WHEN service is explicitly defined
77-
# THEN tracer should have use that service name
78-
service_name = "booking"
77+
def test_tracer_service_env_var(monkeypatch, service_name):
78+
# GIVEN tracer is run without parameters
79+
# WHEN service is implicitly defined via env var
7980
monkeypatch.setenv("POWERTOOLS_SERVICE_NAME", service_name)
80-
tracer_env_var = Tracer(disabled=True)
81+
tracer = Tracer(disabled=True)
82+
83+
# THEN tracer should have use that service name
84+
assert tracer.service == service_name
8185

82-
assert tracer_env_var.service == service_name
8386

87+
def test_tracer_explicit_service(monkeypatch, service_name):
88+
# GIVEN tracer is disabled
89+
# WHEN service is explicitly defined
8490
tracer_explicit = Tracer(disabled=True, service=service_name)
8591
assert tracer_explicit.service == service_name
8692

8793
monkeypatch.setenv("POWERTOOLS_TRACE_DISABLED", "true")
8894
tracer = Tracer()
8995

90-
assert bool(tracer.disabled) is True
96+
# THEN tracer should have use that service name
97+
assert tracer.service == service_name
9198

9299

93-
def test_tracer_with_exception(mocker):
94-
# GIVEN tracer is disabled, decorator is used
95-
# WHEN a lambda handler or method returns an Exception
96-
# THEN tracer should reraise the same Exception
100+
def test_tracer_propagate_exception(mocker):
101+
# GIVEN tracer decorator is used
97102
class CustomException(Exception):
98103
pass
99104

100105
tracer = Tracer(disabled=True)
101106

107+
# WHEN a lambda handler or method returns an Exception
102108
@tracer.capture_lambda_handler
103109
def handler(event, context):
104110
raise CustomException("test")
105111

106112
@tracer.capture_method
107-
def greeting(name, message):
113+
def greeting():
108114
raise CustomException("test")
109115

116+
# THEN tracer should reraise the same Exception
110117
with pytest.raises(CustomException):
111118
handler({}, {})
112119

113120
with pytest.raises(CustomException):
114-
greeting(name="Foo", message="Bar")
121+
greeting()
115122

116123

117-
def test_tracer_reuse():
118-
# GIVEN tracer A, B were initialized
119-
# WHEN tracer B explicitly reuses A config
120-
# THEN tracer B attributes should be equal to tracer A
121-
service_name = "booking"
124+
def test_tracer_reuse_configuration(service_name):
125+
# GIVEN tracer A is initialized
122126
tracer_a = Tracer(disabled=True, service=service_name)
127+
# WHEN tracer B is initialized afterwards
123128
tracer_b = Tracer()
124129

125-
assert id(tracer_a) != id(tracer_b)
130+
# THEN tracer B attributes should be equal to tracer A
126131
assert tracer_a.__dict__.items() == tracer_b.__dict__.items()
127132

128133

129134
def test_tracer_method_nested_sync(mocker):
130-
# GIVEN tracer is disabled, decorator is used
135+
# GIVEN tracer decorator is used
131136
# WHEN multiple sync functions are nested
132137
# THEN tracer should not raise a Runtime Error
133138
tracer = Tracer(disabled=True)

0 commit comments

Comments
 (0)