Skip to content

Commit

Permalink
Add event_payload test
Browse files Browse the repository at this point in the history
  • Loading branch information
amh4r committed Oct 22, 2023
1 parent bc45e6c commit a5e9184
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
15 changes: 11 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ make utest

# Integration tests
make itest
```

When running `make itest`, there are some optional env vars:

```sh
# Disable (when you want to use an already-running Dev Server)
DEV_SERVER_ENABLED=0

# Integration tests (don't start Dev Server)
(export DEV_SERVER_ENABLED=0 && make itest)
# Specify port (uses a random available port by default)
DEV_SERVER_PORT=9000

# Integration tests (start Dev Server on a specific port)
(export DEV_SERVER_PORT=9123 && make itest)
# Show Dev Server stdout and stderr
DEV_SERVER_VERBOSE=1
```
48 changes: 48 additions & 0 deletions tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,53 @@ class Case:
state: _BaseState


def _event_payload(client: inngest.Inngest, framework: str) -> Case:
name = "event_payload"
event_name = f"{framework}/{name}"

class State(_BaseState):
event: inngest.Event | None = None

def is_done(self) -> bool:
return self.event is not None

state = State()

@inngest.create_function(
inngest.FunctionOpts(id=name),
inngest.TriggerEvent(event=event_name),
)
def fn(*, event: inngest.Event, **_kwargs: object) -> None:
state.event = event

def run_test(_self: object) -> None:
client.send(
inngest.Event(
data={"foo": {"bar": "baz"}},
name=event_name,
user={"a": {"b": "c"}},
)
)

def assertion() -> None:
assert state.event is not None
assert state.event.id != ""
assert state.event.name == event_name
assert state.event.data == {"foo": {"bar": "baz"}}
assert state.event.ts > 0
assert state.event.user == {"a": {"b": "c"}}

wait_for(assertion)

return Case(
event_name=event_name,
fn=fn,
run_test=run_test,
state=state,
name=name,
)


def _no_steps(client: inngest.Inngest, framework: str) -> Case:
name = "no_steps"
event_name = f"{framework}/{name}"
Expand Down Expand Up @@ -110,6 +157,7 @@ def assertion() -> None:

def create_cases(client: inngest.Inngest, framework: str) -> list[Case]:
return [
_event_payload(client, framework),
_no_steps(client, framework),
_two_steps(client, framework),
]
26 changes: 23 additions & 3 deletions tests/dev_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@ class _DevServer:
_process: subprocess.Popen[bytes] | None = None
_thread: threading.Thread | None = None

def __init__(self) -> None:
self._enabled = os.getenv("DEV_SERVER_ENABLED") != "0"
def __init__(
self,
*,
enabled: bool,
port: int,
verbose: bool,
) -> None:
self._enabled = enabled
self.port = port
self._verbose = verbose

def start(self) -> None:
if not self._enabled:
return

stderr: int | None = subprocess.DEVNULL
stdout: int | None = subprocess.DEVNULL
if self._verbose:
stderr = None
stdout = None

def _run() -> None:
self._process = subprocess.Popen( # pylint: disable=consider-using-with
[
Expand All @@ -41,6 +55,8 @@ def _run() -> None:
"--port",
f"{DEV_SERVER_PORT}",
],
stderr=stderr,
stdout=stdout,
)
self._process.communicate()

Expand All @@ -65,4 +81,8 @@ def stop(self) -> None:
os.kill(self._process.pid, signal.SIGKILL)


dev_server = _DevServer()
dev_server = _DevServer(
enabled=_enabled,
port=DEV_SERVER_PORT,
verbose=os.getenv("DEV_SERVER_VERBOSE") == "1",
)

0 comments on commit a5e9184

Please sign in to comment.