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

FEAT: Func tests added #5

Merged
merged 1 commit into from
Jul 7, 2024
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/rabbitmq/amqp091-go v1.10.0
github.com/stretchr/testify v1.9.0
go.mongodb.org/mongo-driver v1.8.3
google.golang.org/api v0.187.0
google.golang.org/grpc v1.64.0
Expand All @@ -37,6 +38,7 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down Expand Up @@ -65,6 +67,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/temoto/robotstxt v1.1.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions internal/searcher/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func MustLoad() *Config {
panic("config path is empty")
}

return MustLoadByPath(path)
}

func MustLoadByPath(path string) *Config {
if _, err := os.Stat(path); os.IsNotExist(err) {
panic("config file doesn't exist: " + path)
}
Expand Down
55 changes: 55 additions & 0 deletions tests/searcher/searcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package tests

import (
searcherv1 "github.com/getz-devs/librakeeper-protos/gen/go/searcher"
"github.com/getz-devs/librakeeper-server/tests/searcher/suite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
"time"
)

const (
realIsbn = "978-5-4461-2058-1"
)

func Test1RequestPendingSecondIsFinished(t *testing.T) {
ctx, st := suite.New(t)

randomIsbn := GenerateRandomIsbn()
response, err := st.SearcherServ.SearchByISBN(ctx, &searcherv1.SearchByISBNRequest{Isbn: randomIsbn})
require.NoError(t, err)
assert.True(t, response.GetStatus() == searcherv1.SearchByISBNResponse_PROCESSING)

time.Sleep(5 * time.Second)
response, err = st.SearcherServ.SearchByISBN(ctx, &searcherv1.SearchByISBNRequest{Isbn: randomIsbn})
require.NoError(t, err)
assert.True(t, response.GetStatus() == searcherv1.SearchByISBNResponse_SUCCESS)
}

func TestRealIsbn(t *testing.T) {
ctx, st := suite.New(t)
// do-while proccesing
for {
response, err := st.SearcherServ.SearchByISBN(ctx, &searcherv1.SearchByISBNRequest{Isbn: realIsbn})
require.NoError(t, err)
if response.GetStatus() == searcherv1.SearchByISBNResponse_SUCCESS {
require.NotEmpty(t, response.GetBooks())
break
}
time.Sleep(2 * time.Second)
}
}

func GenerateRandomIsbn() string {
// Generate a random 10-digit ISBN
isbn := make([]byte, 10)
for i := 0; i < 9; i++ {
isbn[i] = byte(rand.Intn(10) + '0')
}
isbn[9] = 'X'

// Convert the byte slice to a string
return string(isbn)
}
59 changes: 59 additions & 0 deletions tests/searcher/suite/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package suite

import (
"context"
searcherv1 "github.com/getz-devs/librakeeper-protos/gen/go/searcher"
"github.com/getz-devs/librakeeper-server/internal/searcher/config"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"net"
"strconv"

"testing"
)

type Suite struct {
*testing.T
Cfg *config.Config
SearcherServ searcherv1.SearcherClient
}

const (
grpcHost = "localhost"
)

func New(t *testing.T) (context.Context, *Suite) {
t.Helper()
t.Parallel()

// TODO: Read test config from env

cfgManager := config.MustLoadByPath("../config/searcher/local.yaml")
ctx, cancelCtx := context.WithTimeout(context.Background(), cfgManager.GRPC.Timeout)

t.Cleanup(func() {
t.Helper()
cancelCtx()
})

//cc, err := grpc.DialContext(ctx,
// grpcAdress(&cfgManager.GRPC),
// grpc.WithTransportCredentials(insecure.NewCredentials()),
//)
cc, err := grpc.NewClient(grpcAdress(&cfgManager.GRPC), grpc.WithTransportCredentials(insecure.NewCredentials()))

if err != nil {
t.Fatalf("grpc server connection failed %v", err)
}

return ctx, &Suite{
T: t,
Cfg: cfgManager,
SearcherServ: searcherv1.NewSearcherClient(cc),
}

}

func grpcAdress(cfg *config.GRPCConfig) string {
return net.JoinHostPort(grpcHost, strconv.Itoa(cfg.Port))
}