generated from PenaSide/GolangTemplate
added mongo validate
This commit is contained in:
parent
2eff1775f9
commit
76595cb3d2
@ -4,10 +4,13 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"gitea.pena/PenaSide/common/encrypt"
|
||||
"gitea.pena/PenaSide/common/mongo"
|
||||
"gitea.pena/PenaSide/customer/internal/models"
|
||||
"github.com/caarlos0/env/v8"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"log"
|
||||
"regexp"
|
||||
"time"
|
||||
@ -206,3 +209,90 @@ func validateTG(notificationBotToken string, notificationRsPayChannel int64, not
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type t struct {
|
||||
ID string `bson:"_id,omitempty"`
|
||||
I int `bson:"i"`
|
||||
}
|
||||
|
||||
// todo в будущем в монге будут запрещены некоторые операции, надо будет обновлять
|
||||
func validateMongo(cfg mongo.Configuration) error {
|
||||
if cfg.Host == "" {
|
||||
return fmt.Errorf("mongo host is empty")
|
||||
}
|
||||
if cfg.Port == "" {
|
||||
return fmt.Errorf("mongo port is empty")
|
||||
}
|
||||
if cfg.DatabaseName == "" {
|
||||
return fmt.Errorf("mongo database name is empty")
|
||||
}
|
||||
if cfg.Auth == "" {
|
||||
return fmt.Errorf("mongo database auth is empty")
|
||||
}
|
||||
if cfg.User == "" {
|
||||
return fmt.Errorf("mongo database user is empty")
|
||||
}
|
||||
if cfg.Password == "" {
|
||||
return fmt.Errorf("mongo database password is empty")
|
||||
}
|
||||
|
||||
cfg.DatabaseName = "testDBName"
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
database, err := mongo.Connect(ctx, &mongo.ConnectDeps{
|
||||
Configuration: &cfg,
|
||||
Timeout: 10 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer database.Drop(ctx)
|
||||
|
||||
testCollection := database.Collection(cfg.DatabaseName)
|
||||
|
||||
receivedChannel := make(chan string, 10)
|
||||
errorChannel := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
defer close(receivedChannel)
|
||||
defer close(errorChannel)
|
||||
for i := 0; i <= 100; i++ {
|
||||
d := t{
|
||||
ID: primitive.NewObjectID().Hex(),
|
||||
I: i,
|
||||
}
|
||||
|
||||
_, err = testCollection.InsertOne(ctx, d)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
|
||||
receivedChannel <- d.ID
|
||||
}
|
||||
}()
|
||||
timeout := time.After(30 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case err = <-errorChannel:
|
||||
if err != nil {
|
||||
return fmt.Errorf("error document insert: %w", err)
|
||||
}
|
||||
case id := <-receivedChannel:
|
||||
result := t{}
|
||||
err := testCollection.FindOne(ctx, bson.M{"_id": id}).Decode(&result)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mongo error finding document: %v", err)
|
||||
}
|
||||
if id != result.ID {
|
||||
return fmt.Errorf("invalid id received")
|
||||
}
|
||||
if result.I == 100 {
|
||||
return nil
|
||||
}
|
||||
case <-timeout:
|
||||
return fmt.Errorf("timeout")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitea.pena/PenaSide/common/mongo"
|
||||
"gitea.pena/PenaSide/customer/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
@ -38,3 +39,15 @@ func TestValidateURLs(t *testing.T) {
|
||||
err := validateURLs(urls)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestValidateMongo(t *testing.T) {
|
||||
err := validateMongo(mongo.Configuration{
|
||||
Host: "localhost",
|
||||
Port: "27020",
|
||||
User: "test",
|
||||
Password: "test",
|
||||
DatabaseName: "admin",
|
||||
Auth: "admin",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user