Skip to content

Commit

Permalink
Use GraphQL in itests
Browse files Browse the repository at this point in the history
  • Loading branch information
amh4r committed Oct 25, 2023
1 parent 3ff321e commit 1a23c9f
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 133 deletions.
14 changes: 7 additions & 7 deletions inngest/_internal/frameworks/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from flask import Flask, Response, make_response, request

from ..client import Inngest
from ..comm import CommHandler, CommResponse
from ..const import HeaderKey
from ..errors import MissingParam
from ..execution import Call
from ..function import Function
from ..net import RequestSignature
from inngest._internal.client import Inngest
from inngest._internal.comm import CommHandler, CommResponse
from inngest._internal.const import HeaderKey
from inngest._internal.errors import MissingParam
from inngest._internal.execution import Call
from inngest._internal.function import Function
from inngest._internal.net import RequestSignature


def serve(
Expand Down
14 changes: 7 additions & 7 deletions inngest/_internal/frameworks/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from tornado.web import Application, RequestHandler

from ..client import Inngest
from ..comm import CommHandler
from ..const import HeaderKey
from ..errors import MissingParam
from ..execution import Call
from ..function import Function
from ..net import RequestSignature
from inngest._internal.client import Inngest
from inngest._internal.comm import CommHandler
from inngest._internal.const import HeaderKey
from inngest._internal.errors import MissingParam
from inngest._internal.execution import Call
from inngest._internal.function import Function
from inngest._internal.net import RequestSignature


def serve(
Expand Down
47 changes: 47 additions & 0 deletions inngest/_internal/result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from __future__ import annotations

from typing import Generic, Literal, TypeAlias, TypeGuard, TypeVar

E = TypeVar("E")
T = TypeVar("T")


class Ok(Generic[T]):
def __init__(self, value: T):
self._value = value

@property
def value(self) -> T:
return self._value

def is_ok(self) -> Literal[True]:
return True

def is_err(self) -> Literal[False]:
return False


class Err(Generic[E]):
def __init__(self, value: E):
self._value = value

@property
def value(self) -> E:
return self._value

def is_err(self) -> bool:
return True

def is_ok(self) -> bool:
return False


Result: TypeAlias = Ok[T] | Err[E]


def is_err(result: Result[T, E]) -> TypeGuard[Err[E]]:
return result.is_err()


def is_ok(result: Result[T, E]) -> TypeGuard[Ok[T]]:
return result.is_ok()
11 changes: 9 additions & 2 deletions tests/cases/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@


class BaseState:
def is_done(self) -> bool:
raise NotImplementedError()
run_id: str | None = None

def wait_for_run_id(self) -> str:
def assertion() -> None:
assert self.run_id is not None

wait_for(assertion)
assert self.run_id is not None
return self.run_id


@dataclass
Expand Down
28 changes: 13 additions & 15 deletions tests/cases/event_payload.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import inngest
from tests import helper

from .base import BaseState, Case, wait_for
from .base import BaseState, Case

_TEST_NAME = "event_payload"


class _State(BaseState):
event: inngest.Event | None = None

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


def create(client: inngest.Inngest, framework: str) -> Case:
event_name = f"{framework}/{_TEST_NAME}"
Expand All @@ -20,8 +18,9 @@ def create(client: inngest.Inngest, framework: str) -> Case:
inngest.FunctionOpts(id=_TEST_NAME),
inngest.TriggerEvent(event=event_name),
)
def fn(*, event: inngest.Event, **_kwargs: object) -> None:
def fn(*, event: inngest.Event, run_id: str, **_kwargs: object) -> None:
state.event = event
state.run_id = run_id

def run_test(_self: object) -> None:
client.send(
Expand All @@ -31,16 +30,15 @@ def run_test(_self: object) -> None:
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)
run_id = state.wait_for_run_id()
helper.client.wait_for_run_status(run_id, helper.RunStatus.COMPLETED)

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"}}

return Case(
event_name=event_name,
Expand Down
22 changes: 8 additions & 14 deletions tests/cases/function_args.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inngest
from tests import helper

from .base import BaseState, Case, wait_for
from .base import BaseState, Case

_TEST_NAME = "function_args"

Expand All @@ -9,12 +10,8 @@ class _State(BaseState):
attempt: int | None = None
event: inngest.Event | None = None
events: list[inngest.Event] | None = None
run_id: str | None = None
step: inngest.Step | None = None

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


def create(client: inngest.Inngest, framework: str) -> Case:
event_name = f"{framework}/{_TEST_NAME}"
Expand All @@ -40,16 +37,13 @@ def fn(

def run_test(_self: object) -> None:
client.send(inngest.Event(name=event_name))
run_id = state.wait_for_run_id()
helper.client.wait_for_run_status(run_id, helper.RunStatus.COMPLETED)

def assertion() -> None:
assert state.is_done()
assert state.attempt == 0
assert isinstance(state.event, inngest.Event)
assert isinstance(state.events, list) and len(state.events) == 1
assert state.run_id != ""
assert isinstance(state.step, inngest.Step)

wait_for(assertion)
assert state.attempt == 0
assert isinstance(state.event, inngest.Event)
assert isinstance(state.events, list) and len(state.events) == 1
assert isinstance(state.step, inngest.Step)

return Case(
event_name=event_name,
Expand Down
23 changes: 7 additions & 16 deletions tests/cases/no_steps.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
import inngest
from tests import helper

from .base import BaseState, Case, wait_for
from .base import BaseState, Case

_TEST_NAME = "no_steps"


class _State(BaseState):
counter = 0

def is_done(self) -> bool:
return self.counter == 1


def create(client: inngest.Inngest, framework: str) -> Case:
event_name = f"{framework}/{_TEST_NAME}"
state = _State()
state = BaseState()

@inngest.create_function(
inngest.FunctionOpts(id=_TEST_NAME),
inngest.TriggerEvent(event=event_name),
)
def fn(**_kwargs: object) -> None:
state.counter += 1
def fn(*, run_id: str, **_kwargs: object) -> None:
state.run_id = run_id

def run_test(_self: object) -> None:
client.send(inngest.Event(name=event_name))

def assertion() -> None:
assert state.is_done()

wait_for(assertion)
run_id = state.wait_for_run_id()
helper.client.wait_for_run_status(run_id, helper.RunStatus.COMPLETED)

return Case(
event_name=event_name,
Expand Down
21 changes: 9 additions & 12 deletions tests/cases/sleep_until.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import datetime, timedelta

import inngest
from tests import helper

from .base import BaseState, Case, wait_for
from .base import BaseState, Case

_TEST_NAME = "sleep_until"

Expand All @@ -23,7 +24,9 @@ def create(client: inngest.Inngest, framework: str) -> Case:
inngest.FunctionOpts(id=_TEST_NAME),
inngest.TriggerEvent(event=event_name),
)
def fn(*, step: inngest.Step, **_kwargs: object) -> None:
def fn(*, run_id: str, step: inngest.Step, **_kwargs: object) -> None:
state.run_id = run_id

if state.before_sleep is None:
state.before_sleep = datetime.now()

Expand All @@ -34,17 +37,11 @@ def fn(*, step: inngest.Step, **_kwargs: object) -> None:

def run_test(_self: object) -> None:
client.send(inngest.Event(name=event_name))
run_id = state.wait_for_run_id()
helper.client.wait_for_run_status(run_id, helper.RunStatus.COMPLETED)

def assertion() -> None:
assert state.is_done()
assert (
state.after_sleep is not None and state.before_sleep is not None
)
assert state.after_sleep - state.before_sleep >= timedelta(
seconds=2
)

wait_for(assertion)
assert state.after_sleep is not None and state.before_sleep is not None
assert state.after_sleep - state.before_sleep >= timedelta(seconds=2)

return Case(
event_name=event_name,
Expand Down
24 changes: 9 additions & 15 deletions tests/cases/two_steps.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import inngest
from tests import helper

from .base import BaseState, Case, wait_for
from .base import BaseState, Case

_TEST_NAME = "two_steps"


class _State(BaseState):
step_1_counter = 0
step_2_counter = 0
end_counter = 0

def is_done(self) -> bool:
return (
self.step_1_counter == 1
and self.step_2_counter == 1
and self.end_counter == 1
)


def create(client: inngest.Inngest, framework: str) -> Case:
Expand All @@ -26,7 +19,9 @@ def create(client: inngest.Inngest, framework: str) -> Case:
inngest.FunctionOpts(id=_TEST_NAME),
inngest.TriggerEvent(event=event_name),
)
def fn(*, step: inngest.Step, **_kwargs: object) -> None:
def fn(*, run_id: str, step: inngest.Step, **_kwargs: object) -> None:
state.run_id = run_id

def step_1() -> str:
state.step_1_counter += 1
return "hi"
Expand All @@ -37,15 +32,14 @@ def step_2() -> None:
state.step_2_counter += 1

step.run("step_2", step_2)
state.end_counter += 1

def run_test(_self: object) -> None:
client.send(inngest.Event(name=event_name))
run_id = state.wait_for_run_id()
helper.client.wait_for_run_status(run_id, helper.RunStatus.COMPLETED)

def assertion() -> None:
assert state.is_done()

wait_for(assertion)
assert state.step_1_counter == 1
assert state.step_2_counter == 1

return Case(
event_name=event_name,
Expand Down
Loading

0 comments on commit 1a23c9f

Please sign in to comment.