Skip to content

Commit

Permalink
add tests (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
didil committed Jun 24, 2024
1 parent 3669a0a commit e9cffb8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/lib/redis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lib

import (
"testing"

"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
)

func TestInitRedisClient(t *testing.T) {
// Test case 1: Valid Redis URL
t.Run("Valid Redis URL", func(t *testing.T) {
conf := &AppConfig{
Redis: RedisConfig{
URL: "redis://localhostexample:6377",
},
}

client, err := InitRedisClient(conf)
assert.NoError(t, err)
assert.NotNil(t, client)
assert.IsType(t, &redis.Client{}, client)

// Clean up
err = client.Close()
assert.NoError(t, err)
})

// Test case 2: Invalid Redis URL
t.Run("Invalid Redis URL", func(t *testing.T) {
conf := &AppConfig{
Redis: RedisConfig{
URL: "invalid://url",
},
}

client, err := InitRedisClient(conf)
assert.Error(t, err)
assert.Nil(t, client)
assert.Contains(t, err.Error(), "failed to parse redis url")
})
}
50 changes: 50 additions & 0 deletions pkg/server/handlers/ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers_test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -121,3 +122,52 @@ func TestIngest_FlowNotFound(t *testing.T) {

assert.Equal(t, "unknown source slug my-source", jsonErr.Error)
}

func TestIngest_MessageBuildFailed(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

inhooksConfigSvc := mocks.NewMockInhooksConfigService(ctrl)
messageBuilder := mocks.NewMockMessageBuilder(ctrl)
messageEnqueuer := mocks.NewMockMessageEnqueuer(ctrl)
logger, err := zap.NewDevelopment()
assert.NoError(t, err)

app := handlers.NewApp(
handlers.WithLogger(logger),
handlers.WithInhooksConfigService(inhooksConfigSvc),
handlers.WithMessageBuilder(messageBuilder),
handlers.WithMessageEnqueuer(messageEnqueuer),
)
r := server.NewRouter(app)
s := httptest.NewServer(r)
defer s.Close()

flow := &models.Flow{
ID: "flow-id",
Source: &models.Source{
ID: "source-id",
},
}
inhooksConfigSvc.EXPECT().FindFlowForSource("my-source").Return(flow)

messageBuilder.EXPECT().FromHttp(flow, gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("failed to build message"))

buf := bytes.NewBufferString(`{"id": "abc"}`)

req, err := http.NewRequest(http.MethodPost, s.URL+"/api/v1/ingest/my-source", buf)
assert.NoError(t, err)

cl := &http.Client{}
resp, err := cl.Do(req)
assert.NoError(t, err)
defer resp.Body.Close()

assert.Equal(t, http.StatusBadRequest, resp.StatusCode)

jsonErr := &handlers.JSONErr{}
err = json.NewDecoder(resp.Body).Decode(jsonErr)
assert.NoError(t, err)

assert.Equal(t, "unable to read data", jsonErr.Error)
}
37 changes: 37 additions & 0 deletions pkg/server/handlers/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package handlers_test

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/didil/inhooks/pkg/server"
"github.com/didil/inhooks/pkg/server/handlers"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)

func TestHandleMetrics(t *testing.T) {
logger, err := zap.NewDevelopment()
assert.NoError(t, err)

app := handlers.NewApp(
handlers.WithLogger(logger),
)
r := server.NewRouter(app)
s := httptest.NewServer(r)
defer s.Close()

resp, err := http.Get(s.URL + "/api/v1/metrics")
assert.NoError(t, err)
defer resp.Body.Close()

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/plain; version=0.0.4; charset=utf-8; escaping=values", resp.Header.Get("Content-Type"))
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)

// test body content
assert.Contains(t, string(body), "go_goroutines")
}
11 changes: 11 additions & 0 deletions pkg/services/message_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ func TestMessageProcessor(t *testing.T) {
err := p.Process(ctx, sink, m)
assert.NoError(t, err)
}

func TestMessageProcessor_userAgent(t *testing.T) {
version.SetVersion("1.2.3")

p := &messageProcessor{}

expectedUserAgent := "Inhooks/1.2.3 (https://github.com/didil/inhooks)"
actualUserAgent := p.userAgent()

assert.Equal(t, expectedUserAgent, actualUserAgent)
}

0 comments on commit e9cffb8

Please sign in to comment.