Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: reuse examples in quickstart #1507

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 3 additions & 51 deletions quickstart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,19 @@ docker compose up -d
Ingest usage events in [CloudEvents](https://cloudevents.io/) format:

```sh
curl -X POST http://localhost:8888/api/v1/events \
-H 'Content-Type: application/cloudevents+json' \
--data-raw '
{
"specversion" : "1.0",
"type": "request",
"id": "00001",
"time": "2023-01-01T00:00:00.001Z",
"source": "service-0",
"subject": "customer-1",
"data": {
"method": "GET",
"route": "/hello",
"duration_ms": 10
}
}
'
curl -X POST http://localhost:8888/api/v1/events -H 'Content-Type: application/cloudevents+json' -d @examples/first.json
```

Note how ID is different:

```sh
curl -X POST http://localhost:8888/api/v1/events \
-H 'Content-Type: application/cloudevents+json' \
--data-raw '
{
"specversion" : "1.0",
"type": "request",
"id": "00002",
"time": "2023-01-01T00:00:00.001Z",
"source": "service-0",
"subject": "customer-1",
"data": {
"method": "GET",
"route": "/hello",
"duration_ms": 20
}
}
'
curl -X POST http://localhost:8888/api/v1/events -H 'Content-Type: application/cloudevents+json' -d @examples/second.json
```

Note how ID and time are different:

```sh
curl -X POST http://localhost:8888/api/v1/events \
-H 'Content-Type: application/cloudevents+json' \
--data-raw '
{
"specversion" : "1.0",
"type": "request",
"id": "00003",
"time": "2023-01-02T00:00:00.001Z",
"source": "service-0",
"subject": "customer-1",
"data": {
"method": "GET",
"route": "/hello",
"duration_ms": 30
}
}
'
curl -X POST http://localhost:8888/api/v1/events -H 'Content-Type: application/cloudevents+json' -d @examples/third.json
```

## 3. Query Usage
Expand Down
13 changes: 13 additions & 0 deletions quickstart/examples/first.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"specversion": "1.0",
"type": "request",
"id": "00001",
"time": "2023-01-01T00:00:00.001Z",
"source": "service-0",
"subject": "customer-1",
"data": {
"method": "GET",
"route": "/hello",
"duration_ms": 10
}
}
13 changes: 13 additions & 0 deletions quickstart/examples/second.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"specversion": "1.0",
"type": "request",
"id": "00002",
"time": "2023-01-01T00:00:00.001Z",
"source": "service-0",
"subject": "customer-1",
"data": {
"method": "GET",
"route": "/hello",
"duration_ms": 20
}
}
13 changes: 13 additions & 0 deletions quickstart/examples/third.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"specversion": "1.0",
"type": "request",
"id": "00003",
"time": "2023-01-02T00:00:00.001Z",
"source": "service-0",
"subject": "customer-1",
"data": {
"method": "GET",
"route": "/hello",
"duration_ms": 30
}
}
59 changes: 25 additions & 34 deletions quickstart/quickstart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package quickstart

import (
"context"
_ "embed"
"encoding/json"
"net/http"
"os"
"testing"
Expand All @@ -15,6 +17,17 @@ import (
"github.com/openmeterio/openmeter/pkg/models"
)

var (
//go:embed examples/first.json
firstExample []byte

//go:embed examples/second.json
secondExample []byte

//go:embed examples/third.json
thirdExample []byte
)

func initClient(t *testing.T) *api.ClientWithResponses {
t.Helper()

Expand All @@ -32,19 +45,11 @@ func initClient(t *testing.T) *api.ClientWithResponses {
func TestQuickstart(t *testing.T) {
client := initClient(t)

// TODO: read these from JSON files to make it easier to keep things in sync
{
ev := cloudevents.New()
ev.SetID("00001")
ev.SetSource("service-0")
ev.SetType("request")
ev.SetSubject("customer-1")
ev.SetTime(time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC))
_ = ev.SetData("application/json", map[string]string{
"method": "GET",
"route": "/hello",
"duration_ms": "40",
})
var ev cloudevents.Event

err := json.Unmarshal(firstExample, &ev)
require.NoError(t, err)

require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err := client.IngestEventWithResponse(context.Background(), ev)
Expand All @@ -54,17 +59,10 @@ func TestQuickstart(t *testing.T) {
}

{
ev := cloudevents.New()
ev.SetID("00002")
ev.SetSource("service-0")
ev.SetType("request")
ev.SetSubject("customer-1")
ev.SetTime(time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC))
_ = ev.SetData("application/json", map[string]string{
"method": "GET",
"route": "/hello",
"duration_ms": "40",
})
var ev cloudevents.Event

err := json.Unmarshal(secondExample, &ev)
require.NoError(t, err)

require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err := client.IngestEventWithResponse(context.Background(), ev)
Expand All @@ -74,17 +72,10 @@ func TestQuickstart(t *testing.T) {
}

{
ev := cloudevents.New()
ev.SetID("00003")
ev.SetSource("service-0")
ev.SetType("request")
ev.SetSubject("customer-1")
ev.SetTime(time.Date(2023, time.January, 2, 0, 0, 0, 0, time.UTC))
_ = ev.SetData("application/json", map[string]string{
"method": "GET",
"route": "/hello",
"duration_ms": "40",
})
var ev cloudevents.Event

err := json.Unmarshal(thirdExample, &ev)
require.NoError(t, err)

require.EventuallyWithT(t, func(t *assert.CollectT) {
resp, err := client.IngestEventWithResponse(context.Background(), ev)
Expand Down
Loading