Skip to content

Commit

Permalink
Add test_emitter.py
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Feb 8, 2024
1 parent 8d352bd commit ecab0fd
Showing 1 changed file with 67 additions and 0 deletions.
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 ecab0fd

Please sign in to comment.