Skip to content

Commit

Permalink
Add blobl example tests back in (#30)
Browse files Browse the repository at this point in the history
* Add blobl example tests back in

The example tests used to be performed in a single place, which was
the same package responsible for generating the docs. However, that
package now exists within the connect repository and so it makes
sense to add the tests in here so that we can catch example issues
earlier.

* Use format directive

Signed-off-by: Jem Davies <jemsot@gmail.com>
  • Loading branch information
jem-davies committed Jun 23, 2024
1 parent 1d22b5d commit a213b81
Show file tree
Hide file tree
Showing 3 changed files with 494 additions and 0 deletions.
165 changes: 165 additions & 0 deletions internal/impl/io/bloblang_examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package io_test

import (
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace/noop"

"github.com/warpstreamlabs/bento/public/bloblang"
"github.com/warpstreamlabs/bento/public/service"

_ "github.com/warpstreamlabs/bento/internal/impl/io"
)

func TestFunctionExamples(t *testing.T) {
tmpJSONFile, err := os.CreateTemp("", "bento_bloblang_functions_test")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(tmpJSONFile.Name())
})

_, err = tmpJSONFile.WriteString(`{"foo":"bar"}`)
require.NoError(t, err)

key := "BENTO_TEST_BLOBLANG_FILE"
t.Setenv(key, tmpJSONFile.Name())

env := bloblang.GlobalEnvironment()
env.WalkFunctions(func(name string, view *bloblang.FunctionView) {
t.Run(name, func(t *testing.T) {
t.Parallel()

spec := view.TemplateData()
for i, e := range spec.Examples {
if e.SkipTesting {
continue
}

m, err := env.Parse(e.Mapping)
require.NoError(t, err)

for j, io := range e.Results {
msg := service.NewMessage([]byte(io[0]))
textMap := propagation.MapCarrier{
"traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
}
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}))

textProp := otel.GetTextMapPropagator()
otelCtx := textProp.Extract(msg.Context(), textMap)
pCtx, _ := noop.NewTracerProvider().Tracer("blobby").Start(otelCtx, "test")
msg = msg.WithContext(pCtx)

p, err := msg.BloblangQuery(m)
exp := io[1]
if strings.HasPrefix(exp, "Error(") {
exp = exp[7 : len(exp)-2]
require.EqualError(t, err, exp, fmt.Sprintf("%v-%v", i, j))
} else {
require.NoError(t, err)

pBytes, err := p.AsBytes()
require.NoError(t, err)

assert.Equal(t, exp, string(pBytes), fmt.Sprintf("%v-%v", i, j))
}
}
}
})
})
}

func TestMethodExamples(t *testing.T) {
tmpJSONFile, err := os.CreateTemp("", "bento_bloblang_methods_test")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(tmpJSONFile.Name())
})

_, err = tmpJSONFile.WriteString(`
{
"type":"object",
"properties":{
"foo":{
"type":"string"
}
}
}`)
require.NoError(t, err)

key := "BENTO_TEST_BLOBLANG_SCHEMA_FILE"
t.Setenv(key, tmpJSONFile.Name())

env := bloblang.GlobalEnvironment()
env.WalkMethods(func(name string, view *bloblang.MethodView) {
spec := view.TemplateData()
t.Run(spec.Name, func(t *testing.T) {
t.Parallel()
for i, e := range spec.Examples {
if e.SkipTesting {
continue
}

m, err := env.Parse(e.Mapping)
require.NoError(t, err)

for j, io := range e.Results {
msg := service.NewMessage([]byte(io[0]))
p, err := msg.BloblangQuery(m)
exp := io[1]
if strings.HasPrefix(exp, "Error(") {
exp = exp[7 : len(exp)-2]
require.EqualError(t, err, exp, fmt.Sprintf("%v-%v", i, j))
} else if exp == "<Message deleted>" {
require.NoError(t, err)
require.Nil(t, p)
} else {
require.NoError(t, err)

pBytes, err := p.AsBytes()
require.NoError(t, err)

assert.Equal(t, exp, string(pBytes), fmt.Sprintf("%v-%v", i, j))
}
}
}
for _, target := range spec.Categories {
for i, e := range target.Examples {
if e.SkipTesting {
continue
}

m, err := env.Parse(e.Mapping)
require.NoError(t, err)

for j, io := range e.Results {
msg := service.NewMessage([]byte(io[0]))
p, err := msg.BloblangQuery(m)
exp := io[1]
if strings.HasPrefix(exp, "Error(") {
exp = exp[7 : len(exp)-2]
require.EqualError(t, err, exp, fmt.Sprintf("%v-%v", i, j))
} else if exp == "<Message deleted>" {
require.NoError(t, err)
require.Nil(t, p)
} else {
require.NoError(t, err)

pBytes, err := p.AsBytes()
require.NoError(t, err)

assert.Equal(t, exp, string(pBytes), fmt.Sprintf("%v-%v", i, j))
}
}
}
}
})
})
}
165 changes: 165 additions & 0 deletions internal/impl/pure/bloblang_examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package pure_test

import (
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace/noop"

"github.com/warpstreamlabs/bento/public/bloblang"
"github.com/warpstreamlabs/bento/public/service"

_ "github.com/warpstreamlabs/bento/internal/impl/pure"
)

func TestFunctionExamples(t *testing.T) {
tmpJSONFile, err := os.CreateTemp("", "bento_bloblang_functions_test")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(tmpJSONFile.Name())
})

_, err = tmpJSONFile.WriteString(`{"foo":"bar"}`)
require.NoError(t, err)

key := "BENTO_TEST_BLOBLANG_FILE"
t.Setenv(key, tmpJSONFile.Name())

env := bloblang.GlobalEnvironment()
env.WalkFunctions(func(name string, view *bloblang.FunctionView) {
t.Run(name, func(t *testing.T) {
t.Parallel()

spec := view.TemplateData()
for i, e := range spec.Examples {
if e.SkipTesting {
continue
}

m, err := env.Parse(e.Mapping)
require.NoError(t, err)

for j, io := range e.Results {
msg := service.NewMessage([]byte(io[0]))
textMap := propagation.MapCarrier{
"traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
}
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}))

textProp := otel.GetTextMapPropagator()
otelCtx := textProp.Extract(msg.Context(), textMap)
pCtx, _ := noop.NewTracerProvider().Tracer("blobby").Start(otelCtx, "test")
msg = msg.WithContext(pCtx)

p, err := msg.BloblangQuery(m)
exp := io[1]
if strings.HasPrefix(exp, "Error(") {
exp = exp[7 : len(exp)-2]
require.EqualError(t, err, exp, fmt.Sprintf("%v-%v", i, j))
} else {
require.NoError(t, err)

pBytes, err := p.AsBytes()
require.NoError(t, err)

assert.Equal(t, exp, string(pBytes), fmt.Sprintf("%v-%v", i, j))
}
}
}
})
})
}

func TestMethodExamples(t *testing.T) {
tmpJSONFile, err := os.CreateTemp("", "bento_bloblang_methods_test")
require.NoError(t, err)
t.Cleanup(func() {
os.Remove(tmpJSONFile.Name())
})

_, err = tmpJSONFile.WriteString(`
{
"type":"object",
"properties":{
"foo":{
"type":"string"
}
}
}`)
require.NoError(t, err)

key := "BENTO_TEST_BLOBLANG_SCHEMA_FILE"
t.Setenv(key, tmpJSONFile.Name())

env := bloblang.GlobalEnvironment()
env.WalkMethods(func(name string, view *bloblang.MethodView) {
spec := view.TemplateData()
t.Run(spec.Name, func(t *testing.T) {
t.Parallel()
for i, e := range spec.Examples {
if e.SkipTesting {
continue
}

m, err := env.Parse(e.Mapping)
require.NoError(t, err)

for j, io := range e.Results {
msg := service.NewMessage([]byte(io[0]))
p, err := msg.BloblangQuery(m)
exp := io[1]
if strings.HasPrefix(exp, "Error(") {
exp = exp[7 : len(exp)-2]
require.EqualError(t, err, exp, fmt.Sprintf("%v-%v", i, j))
} else if exp == "<Message deleted>" {
require.NoError(t, err)
require.Nil(t, p)
} else {
require.NoError(t, err)

pBytes, err := p.AsBytes()
require.NoError(t, err)

assert.Equal(t, exp, string(pBytes), fmt.Sprintf("%v-%v", i, j))
}
}
}
for _, target := range spec.Categories {
for i, e := range target.Examples {
if e.SkipTesting {
continue
}

m, err := env.Parse(e.Mapping)
require.NoError(t, err)

for j, io := range e.Results {
msg := service.NewMessage([]byte(io[0]))
p, err := msg.BloblangQuery(m)
exp := io[1]
if strings.HasPrefix(exp, "Error(") {
exp = exp[7 : len(exp)-2]
require.EqualError(t, err, exp, fmt.Sprintf("%v-%v", i, j))
} else if exp == "<Message deleted>" {
require.NoError(t, err)
require.Nil(t, p)
} else {
require.NoError(t, err)

pBytes, err := p.AsBytes()
require.NoError(t, err)

assert.Equal(t, exp, string(pBytes), fmt.Sprintf("%v-%v", i, j))
}
}
}
}
})
})
}
Loading

0 comments on commit a213b81

Please sign in to comment.