Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for resolvers without any tests #296

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ build:
@cd cmd/api && go build

test:
@go test ./... -tags test

vtest:
@go test -v ./... -tags test

check: lint
1 change: 1 addition & 0 deletions internal/caches/twitchusernamecache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package twitchusernamecache
3 changes: 0 additions & 3 deletions internal/db/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ type Pool interface {
}

func NewPool(ctx context.Context, dsn string) (Pool, error) {

var err error

pool, err := pgxpool.Connect(ctx, dsn)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/db/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package db
25 changes: 25 additions & 0 deletions internal/logger/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package logger

import (
"context"
"log"
)

type contextKeyType string

var (
contextKey = contextKeyType("logger")
)

func FromContext(ctx context.Context) Logger {
if v := ctx.Value(contextKey); v != nil {
return v.(Logger)
}

log.Fatal("No logger found in context")
return nil
}

func OnContext(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, contextKey, logger)
}
1 change: 1 addition & 0 deletions internal/logger/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package logger
22 changes: 0 additions & 22 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package logger

import (
"context"
"log"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -25,25 +22,6 @@ type Logger interface {
Sync() error
}

type contextKeyType string

var (
contextKey = contextKeyType("logger")
)

func FromContext(ctx context.Context) Logger {
if v := ctx.Value(contextKey); v != nil {
return v.(Logger)
}

log.Fatal("No logger found in context")
return nil
}

func OnContext(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, contextKey, logger)
}

func New(logLevel zap.AtomicLevel, logDevelopment bool) Logger {
zapConfig := zap.Config{
Level: logLevel,
Expand Down
28 changes: 28 additions & 0 deletions internal/resolvers/default/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package defaultresolver

import (
"context"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/go-chi/chi/v5"
"github.com/pashagolub/pgxmock"

qt "github.com/frankban/quicktest"
)

func TestInitialize(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)

pool, err := pgxmock.NewPool()
c.Assert(err, qt.IsNil)

r := chi.NewRouter()

c.Run("No credentials", func(c *qt.C) {
cfg := config.APIConfig{}
Initialize(ctx, cfg, pool, r, nil)
})
}
39 changes: 39 additions & 0 deletions internal/resolvers/discord/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package discord

import (
"context"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
"github.com/pashagolub/pgxmock"

qt "github.com/frankban/quicktest"
)

func TestInitialize(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)

pool, err := pgxmock.NewPool()
c.Assert(err, qt.IsNil)

c.Run("No credentials", func(c *qt.C) {
cfg := config.APIConfig{}
customResolvers := []resolver.Resolver{}
c.Assert(customResolvers, qt.HasLen, 0)
Initialize(ctx, cfg, pool, &customResolvers)
c.Assert(customResolvers, qt.HasLen, 0)
})

c.Run("Credentials", func(c *qt.C) {
cfg := config.APIConfig{
DiscordToken: "test",
}
customResolvers := []resolver.Resolver{}
c.Assert(customResolvers, qt.HasLen, 0)
Initialize(ctx, cfg, pool, &customResolvers)
c.Assert(customResolvers, qt.HasLen, 1)
})
}
29 changes: 29 additions & 0 deletions internal/resolvers/livestreamfails/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package livestreamfails

import (
"context"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
"github.com/pashagolub/pgxmock"

qt "github.com/frankban/quicktest"
)

func TestInitialize(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)

pool, err := pgxmock.NewPool()
c.Assert(err, qt.IsNil)

c.Run("No credentials", func(c *qt.C) {
cfg := config.APIConfig{}
customResolvers := []resolver.Resolver{}
c.Assert(customResolvers, qt.HasLen, 0)
Initialize(ctx, cfg, pool, &customResolvers)
c.Assert(customResolvers, qt.HasLen, 1)
})
}
32 changes: 32 additions & 0 deletions internal/resolvers/oembed/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package oembed

import (
"context"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
"github.com/pashagolub/pgxmock"

qt "github.com/frankban/quicktest"
)

func TestInitialize(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)

pool, err := pgxmock.NewPool()
c.Assert(err, qt.IsNil)

c.Run("No file", func(c *qt.C) {
dir := t.TempDir()
cfg := config.APIConfig{
OembedProvidersPath: dir + "/providers.json",
}
customResolvers := []resolver.Resolver{}
c.Assert(customResolvers, qt.HasLen, 0)
Initialize(ctx, cfg, pool, &customResolvers)
c.Assert(customResolvers, qt.HasLen, 0)
})
}
29 changes: 29 additions & 0 deletions internal/resolvers/supinic/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package supinic

import (
"context"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
"github.com/pashagolub/pgxmock"

qt "github.com/frankban/quicktest"
)

func TestInitialize(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)

pool, err := pgxmock.NewPool()
c.Assert(err, qt.IsNil)

c.Run("No credentials", func(c *qt.C) {
cfg := config.APIConfig{}
customResolvers := []resolver.Resolver{}
c.Assert(customResolvers, qt.HasLen, 0)
Initialize(ctx, cfg, pool, &customResolvers)
c.Assert(customResolvers, qt.HasLen, 1)
})
}
67 changes: 67 additions & 0 deletions internal/routes/twitchemotes/initialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package twitchemotes

import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"

"github.com/Chatterino/api/internal/db"
"github.com/Chatterino/api/pkg/cache"
"github.com/Chatterino/api/pkg/config"
"github.com/go-chi/chi/v5"
"github.com/nicklaw5/helix"
)

func setHandler(ctx context.Context, twitchemotesCache cache.Cache, w http.ResponseWriter, r *http.Request) {
setID := chi.URLParam(r, "setID")
w.Header().Set("Content-Type", "application/json")

response, err := twitchemotesCache.Get(ctx, setID, nil)
if err != nil {
var perr *twitchEmotesError
if errors.As(err, &perr) {
w.WriteHeader(perr.Status)
data, err := json.Marshal(&TwitchEmotesErrorResponse{
Status: perr.Status,
Message: perr.Error(),
})
if err != nil {
log.Println("Error marshalling twitch emotes error response:", err)
}
_, err = w.Write(data)
if err != nil {
log.Println("Error writing response:", err)
}
} else {
fmt.Println("Unknown error in twitchemotes set handler:", err)
}

return
}

_, err = w.Write(response)
if err != nil {
log.Println("Error writing response:", err)
}
}

// Initialize servers the /twitchemotes/set/{setID} route
// In newer versions of Chatterino this data is fetched client-side instead.
// To support older versions of Chattterino that relied on this API we will keep this API functional for some time longer.
func Initialize(ctx context.Context, cfg config.APIConfig, pool db.Pool, router *chi.Mux, helixClient *helix.Client, helixUsernameCache cache.Cache) error {
loader := &TwitchemotesLoader{
helixAPI: helixClient,
helixUsernameCache: helixUsernameCache,
}
twitchemotesCache := cache.NewPostgreSQLCache(ctx, cfg, pool, "twitchemotes", loader, time.Duration(30)*time.Minute)

router.Get("/twitchemotes/set/{setID}", func(w http.ResponseWriter, r *http.Request) {
setHandler(ctx, twitchemotesCache, w, r)
})

return nil
}
28 changes: 28 additions & 0 deletions internal/routes/twitchemotes/initialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package twitchemotes

import (
"context"
"testing"

"github.com/Chatterino/api/internal/logger"
"github.com/Chatterino/api/pkg/config"
"github.com/go-chi/chi/v5"
"github.com/pashagolub/pgxmock"

qt "github.com/frankban/quicktest"
)

func TestInitialize(t *testing.T) {
ctx := logger.OnContext(context.Background(), logger.NewTest())
c := qt.New(t)

pool, err := pgxmock.NewPool()
c.Assert(err, qt.IsNil)

r := chi.NewRouter()

c.Run("No credentials", func(c *qt.C) {
cfg := config.APIConfig{}
Initialize(ctx, cfg, pool, r, nil, nil)
})
}
Loading