Skip to content

Commit

Permalink
test: reuse examples in quickstart
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <[email protected]>
  • Loading branch information
sagikazarmark committed Sep 13, 2024
1 parent ff2b26a commit 6b081a7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 85 deletions.
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

0 comments on commit 6b081a7

Please sign in to comment.