Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 11, 2024
1 parent 5830c2a commit e697827
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion node/migrations/dal/000003_measures.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CREATE TABLE IF NOT EXISTS websocket_connections (
api_key TEXT NOT NULL,
timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
connection_end TIMESTAMPTZ,
duration INTERVAL
duration INT
);

CREATE TABLE IF NOT EXISTS websocket_subscriptions (
Expand Down
102 changes: 102 additions & 0 deletions node/pkg/dal/tests/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//nolint:all
package test

import (
"context"
"testing"
"time"

"bisonai.com/orakl/node/pkg/dal/utils/stats"
"bisonai.com/orakl/node/pkg/db"

"github.com/stretchr/testify/assert"
)

type RestCall struct {
ID int32 `db:"id"`
ApiKey string `db:"api_key"`
Endpoint string `db:"endpoint"`
Timestamp time.Time `db:"timestamp"`
StatusCode int `db:"status_code"`
ResponseTime int `db:"response_time"`
}

type WebsocketConnection struct {
ID int32 `db:"id"`
ApiKey string `db:"api_key"`
Timestamp time.Time `db:"timestamp"`
ConnectionEnd *time.Time `db:"connection_end"`
Duration *int `db:"duration"`
}

type WebsocketSubscription struct {
ID int32 `db:"id"`
ConnectionId int32 `db:"connection_id"`
Topic string `db:"topic"`
Timestamp time.Time `db:"timestamp"`
}

func TestInsertRestCall(t *testing.T) {
ctx := context.Background()
err := stats.InsertRestCall(ctx, "test", "test", 200, 10*time.Millisecond)
assert.NoError(t, err)

result, err := db.QueryRows[RestCall](ctx, "SELECT * FROM rest_calls", nil)
assert.NoError(t, err)
assert.Greater(t, len(result), 0)

assert.Equal(t, "test", result[0].ApiKey)
err = db.QueryWithoutResult(ctx, "DELETE FROM rest_calls", nil)
assert.NoError(t, err)
}

func TestInsertWebsocketConnection(t *testing.T) {
ctx := context.Background()
id, err := stats.InsertWebsocketConnection(ctx, "test")
assert.NoError(t, err)
assert.Greater(t, id, int32(0))

result, err := db.QueryRows[WebsocketConnection](ctx, "SELECT * FROM websocket_connections", nil)
assert.NoError(t, err)
assert.Greater(t, len(result), 0)
assert.Equal(t, "test", result[0].ApiKey)
err = db.QueryWithoutResult(ctx, "DELETE FROM websocket_connections", nil)
assert.NoError(t, err)
}

func TestUpdateWebsocketConnection(t *testing.T) {
ctx := context.Background()
id, err := stats.InsertWebsocketConnection(ctx, "test")
assert.NoError(t, err)
assert.Greater(t, id, int32(0))

err = stats.UpdateWebsocketConnection(ctx, id)
assert.NoError(t, err)

result, err := db.QueryRows[WebsocketConnection](ctx, "SELECT * FROM websocket_connections", nil)
assert.NoError(t, err)
assert.Greater(t, len(result), 0)
assert.Equal(t, "test", result[0].ApiKey)
assert.NotEqual(t, 0, result[0].Duration)
err = db.QueryWithoutResult(ctx, "DELETE FROM websocket_connections", nil)
assert.NoError(t, err)
}

func TestWebsocketSubcription(t *testing.T) {
ctx := context.Background()
id, err := stats.InsertWebsocketConnection(ctx, "test")
assert.NoError(t, err)
assert.Greater(t, id, int32(0))

err = stats.InsertWebsocketSubscription(ctx, id, "test_topic")
assert.NoError(t, err)

result, err := db.QueryRows[WebsocketSubscription](ctx, "SELECT * FROM websocket_subscriptions", nil)
assert.NoError(t, err)
assert.Greater(t, len(result), 0)
assert.Equal(t, "test_topic", result[0].Topic)
err = db.QueryWithoutResult(ctx, "DELETE FROM websocket_subscriptions", nil)
assert.NoError(t, err)
err = db.QueryWithoutResult(ctx, "DELETE FROM websocket_connections", nil)
assert.NoError(t, err)
}
5 changes: 3 additions & 2 deletions node/pkg/dal/utils/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
`
UPDATE_WEBSOCKET_CONNECTIONS = `
UPDATE websocket_connections
SET connection_end = NOW(), duration = NOW() - connection_start
SET connection_end = NOW(), duration = EXTRACT(EPOCH FROM (NOW() - timestamp)) * 1000
WHERE id = @id;
`

Expand All @@ -36,11 +36,12 @@ type websocketId struct {
}

func InsertRestCall(ctx context.Context, apiKey string, endpoint string, statusCode int, responseTime time.Duration) error {
responseTimeMilli := int(responseTime.Milliseconds())
return db.QueryWithoutResult(ctx, INSERT_REST_CALLS, map[string]any{
"api_key": apiKey,
"endpoint": endpoint,
"status_code": statusCode,
"response_time": responseTime,
"response_time": responseTimeMilli,
})
}

Expand Down

0 comments on commit e697827

Please sign in to comment.