Skip to content

Commit

Permalink
Merge pull request #5 from simonsobs/dev
Browse files Browse the repository at this point in the history
Add unit tests for Emitter
  • Loading branch information
TaiSakuma authored Feb 8, 2024
2 parents a276674 + ecab0fd commit 3fad07b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tests = [
"pytest-cov>=4.1",
"pytest-timeout>=2.2",
"pytest>=7.4",
"pytest-httpx>=0.29",
"respx>=0.20",
]

[project.urls]
Expand All @@ -46,6 +46,14 @@ source = "regex_commit"
path = "src/nextline_alert/__about__.py"
tag_sign = false

[tool.pytest.ini_options]
asyncio_mode = "auto"
timeout = 60
addopts = "--doctest-modules"
# doctest_optionflags = ["ELLIPSIS", "NORMALIZE_WHITESPACE",]
doctest_optionflags = ["ELLIPSIS"]
norecursedirs = "build"

[tool.black]
skip-string-normalization = true
target_version = ['py310', 'py311', 'py312']
Expand Down
67 changes: 67 additions & 0 deletions tests/test_emitter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import asyncio
import json
import time

import respx
from nextline import Nextline

from nextline_alert.emitter import Emitter


def func_success():
time.sleep(0.001)


def func_rase():
time.sleep(0.001)
raise ValueError('test')


@respx.mock
async def test_emit_no_alert() -> None:
nextline = Nextline(func_success)

url = 'http://localhost:5000/alerts'
platform = 'pytest'
emitter = Emitter(url=url, platform=platform)
assert nextline.register(emitter)

# Mock the HTTP POST request
route = respx.post(url).respond(status_code=200)

# Run a script that does not raise an exception
async with nextline:
event = asyncio.Event()
await nextline.run_continue_and_wait(event)

# Assert the HTTP POST request
assert not route.called


@respx.mock
async def test_emit_alert() -> None:
nextline = Nextline(func_rase)

url = 'http://localhost:5000/alerts'
platform = 'pytest'
emitter = Emitter(url=url, platform=platform)
assert nextline.register(emitter)

# Mock the HTTP POST request
route = respx.post(url).respond(status_code=200)

# Run a script that raises an exception
async with nextline:
event = asyncio.Event()
await nextline.run_continue_and_wait(event)

# Assert the HTTP POST request
assert route.called
assert len(route.calls) == 1
call = route.calls[0]
assert url == call.request.url
data = json.loads(call.request.content)
labels = data['alerts'][0]['labels']
description = data['alerts'][0]['annotations']['description']
assert platform == labels['platform']
assert 'ValueError' in description

0 comments on commit 3fad07b

Please sign in to comment.