From 6b081a77d5330b7f3f8e332938be9e0c5f237588 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 13 Sep 2024 13:20:21 +0200 Subject: [PATCH] test: reuse examples in quickstart Signed-off-by: Mark Sagi-Kazar --- quickstart/README.md | 54 ++---------------------------- quickstart/examples/first.json | 13 ++++++++ quickstart/examples/second.json | 13 ++++++++ quickstart/examples/third.json | 13 ++++++++ quickstart/quickstart_test.go | 59 ++++++++++++++------------------- 5 files changed, 67 insertions(+), 85 deletions(-) create mode 100644 quickstart/examples/first.json create mode 100644 quickstart/examples/second.json create mode 100644 quickstart/examples/third.json diff --git a/quickstart/README.md b/quickstart/README.md index ae5cd4b2a..0366aa885 100644 --- a/quickstart/README.md +++ b/quickstart/README.md @@ -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 diff --git a/quickstart/examples/first.json b/quickstart/examples/first.json new file mode 100644 index 000000000..43bb46689 --- /dev/null +++ b/quickstart/examples/first.json @@ -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 + } +} \ No newline at end of file diff --git a/quickstart/examples/second.json b/quickstart/examples/second.json new file mode 100644 index 000000000..24345af80 --- /dev/null +++ b/quickstart/examples/second.json @@ -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 + } +} \ No newline at end of file diff --git a/quickstart/examples/third.json b/quickstart/examples/third.json new file mode 100644 index 000000000..7cd3420fb --- /dev/null +++ b/quickstart/examples/third.json @@ -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 + } +} \ No newline at end of file diff --git a/quickstart/quickstart_test.go b/quickstart/quickstart_test.go index 4cd0b2cd9..9b45496b6 100644 --- a/quickstart/quickstart_test.go +++ b/quickstart/quickstart_test.go @@ -2,6 +2,8 @@ package quickstart import ( "context" + _ "embed" + "encoding/json" "net/http" "os" "testing" @@ -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() @@ -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) @@ -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) @@ -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)