Skip to content

Commit

Permalink
separate tests cases
Browse files Browse the repository at this point in the history
Handle different formats in different test cases.
  • Loading branch information
mauriciovasquezbernal committed Oct 31, 2019
1 parent 244a4f9 commit 30de890
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,19 @@ def test_span_on_error(self):
scope.span.unwrap().events[0].attributes["error.kind"], Exception
)

def test_inject(self):
"""Test `inject()` method."""
def test_inject_http_headers(self):
"""Test `inject()` method for Format.HTTP_HEADERS."""

otel_context = trace.SpanContext(trace_id=1220, span_id=7478)
context = opentracingshim.SpanContextShim(otel_context)

headers = {}
self.shim.inject(context, opentracing.Format.HTTP_HEADERS, headers)
self.assertEqual(headers[MockHTTPTextFormat.TRACE_ID_KEY], str(1220))
self.assertEqual(headers[MockHTTPTextFormat.SPAN_ID_KEY], str(7478))

def test_inject_text_map(self):
"""Test `inject()` method for Format.TEXT_MAP."""

otel_context = trace.SpanContext(trace_id=1220, span_id=7478)
context = opentracingshim.SpanContextShim(otel_context)
Expand All @@ -456,39 +467,42 @@ def test_inject(self):
self.assertEqual(text_map[MockHTTPTextFormat.TRACE_ID_KEY], str(1220))
self.assertEqual(text_map[MockHTTPTextFormat.SPAN_ID_KEY], str(7478))

# Verify Format.HTTP_HEADERS
http_headers = {}
self.shim.inject(
context, opentracing.Format.HTTP_HEADERS, http_headers
)
self.assertEqual(
http_headers[MockHTTPTextFormat.TRACE_ID_KEY], str(1220)
)
self.assertEqual(
http_headers[MockHTTPTextFormat.SPAN_ID_KEY], str(7478)
)
def test_inject_binary(self):
"""Test `inject()` method for Format.BINARY."""

otel_context = trace.SpanContext(trace_id=1220, span_id=7478)
context = opentracingshim.SpanContextShim(otel_context)

# Verify exception for non supported binary format.
with self.assertRaises(opentracing.UnsupportedFormatException):
self.shim.extract(opentracing.Format.BINARY, bytearray())
self.shim.inject(context, opentracing.Format.BINARY, bytearray())

def test_extract(self):
"""Test `extract()` method."""
def test_extract_http_headers(self):
"""Test `extract()` method for Format.HTTP_HEADERS."""

headers = {
carrier = {
MockHTTPTextFormat.TRACE_ID_KEY: 1220,
MockHTTPTextFormat.SPAN_ID_KEY: 7478,
}

# Verify Format.TEXT_MAP
ctx_text_map = self.shim.extract(opentracing.Format.TEXT_MAP, headers)
self.assertEqual(ctx_text_map.unwrap().trace_id, 1220)
self.assertEqual(ctx_text_map.unwrap().span_id, 7478)

# Verify Format.HTTP_HEADERS
ctx_http = self.shim.extract(opentracing.Format.HTTP_HEADERS, headers)
self.assertEqual(ctx_http.unwrap().trace_id, 1220)
self.assertEqual(ctx_http.unwrap().span_id, 7478)
ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier)
self.assertEqual(ctx.unwrap().trace_id, 1220)
self.assertEqual(ctx.unwrap().span_id, 7478)

def test_extract_text_map(self):
"""Test `extract()` method for Format.TEXT_MAP."""

carrier = {
MockHTTPTextFormat.TRACE_ID_KEY: 1220,
MockHTTPTextFormat.SPAN_ID_KEY: 7478,
}

ctx = self.shim.extract(opentracing.Format.TEXT_MAP, carrier)
self.assertEqual(ctx.unwrap().trace_id, 1220)
self.assertEqual(ctx.unwrap().span_id, 7478)

def test_extract_binary(self):
"""Test `extract()` method for Format.BINARY."""

# Verify exception for non supported binary format.
with self.assertRaises(opentracing.UnsupportedFormatException):
Expand Down

0 comments on commit 30de890

Please sign in to comment.