Skip to content

Commit

Permalink
refactor(audio): moved audio logic to new business services
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Apr 12, 2024
1 parent 7986abc commit 594ec48
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 337 deletions.
64 changes: 8 additions & 56 deletions bot/internal/joke/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package joke
import (
"context"
"github.com/wittano/komputer/db"
"github.com/wittano/komputer/test"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"testing"
"time"
Expand All @@ -25,19 +25,6 @@ var (
}
)

type testMongodbService struct {
client *mongo.Client
ctx context.Context
}

func (t testMongodbService) Close() error {
return t.client.Disconnect(t.ctx)
}

func (t testMongodbService) Client(_ context.Context) (*mongo.Client, error) {
return t.client, nil
}

func createMTest(t *testing.T) *mtest.T {
return mtest.New(t, mtest.NewOptions().
ClientType(mtest.Mock).
Expand All @@ -54,12 +41,7 @@ func TestJokeService_Add(t *testing.T) {

ctx := context.Background()

mongodbService := testMongodbService{
t.Client,
ctx,
}

service := DatabaseJokeService{&mongodbService}
service := DatabaseJokeService{test.NewMockedMognodbService(ctx, t.Client)}

if _, err := service.Add(ctx, testJoke); err != nil {
mt.Fatal(err)
Expand All @@ -71,12 +53,7 @@ func TestJokeService_AddButContextCancelled(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
cancel()

mongodbService := testMongodbService{
nil,
ctx,
}

service := DatabaseJokeService{&mongodbService}
service := DatabaseJokeService{test.NewMockedMognodbService(ctx, nil)}

if _, err := service.Add(ctx, testJoke); err == nil {
t.Fatal("Context wasn't cancelled")
Expand All @@ -87,12 +64,7 @@ func TestJokeService_SearchButContextWasCancelled(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
cancel()

mongodbService := testMongodbService{
nil,
ctx,
}

service := DatabaseJokeService{&mongodbService}
service := DatabaseJokeService{test.NewMockedMognodbService(ctx, nil)}

if _, err := service.Get(ctx, testJokeSearch); err == nil {
t.Fatal("Context wasn't cancelled")
Expand All @@ -107,12 +79,7 @@ func TestJokeService_SearchButNotingFound(t *testing.T) {

ctx := context.Background()

mongodbService := testMongodbService{
t.Client,
ctx,
}

service := DatabaseJokeService{&mongodbService}
service := DatabaseJokeService{test.NewMockedMognodbService(ctx, t.Client)}

if _, err := service.Get(ctx, testJokeSearch); err == nil {
mt.Fatal("Something was found in database, but it shouldn't")
Expand All @@ -137,12 +104,7 @@ func TestJokeService_SearchButFindRandomJoke(t *testing.T) {

ctx := context.Background()

mongodbService := testMongodbService{
t.Client,
ctx,
}

service := DatabaseJokeService{&mongodbService}
service := DatabaseJokeService{test.NewMockedMognodbService(ctx, t.Client)}

joke, err := service.Get(ctx, SearchParameters{})
if err != nil {
Expand Down Expand Up @@ -173,12 +135,7 @@ func TestDatabaseJokeService_ActiveButContextCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

mongodbService := testMongodbService{
t.Client,
ctx,
}

service := DatabaseJokeService{&mongodbService}
service := DatabaseJokeService{test.NewMockedMognodbService(ctx, t.Client)}

if service.Active(ctx) {
t.Fatal("service can still running and handle new requests")
Expand All @@ -194,12 +151,7 @@ func TestDatabaseJokeService_Active(t *testing.T) {

ctx := context.Background()

mongodbService := testMongodbService{
t.Client,
ctx,
}

service := DatabaseJokeService{&mongodbService}
service := DatabaseJokeService{test.NewMockedMognodbService(ctx, t.Client)}

if !service.Active(ctx) {
t.Fatal("service isn't responding")
Expand Down
25 changes: 25 additions & 0 deletions test/mongodb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package test

import (
"context"
"github.com/wittano/komputer/db"
"go.mongodb.org/mongo-driver/mongo"
)

// TestMongodbService This service is mock. It shouldn't use in production code
type TestMongodbService struct {
client *mongo.Client
ctx context.Context
}

func (t TestMongodbService) Close() error {
return t.client.Disconnect(t.ctx)
}

func (t TestMongodbService) Client(_ context.Context) (*mongo.Client, error) {
return t.client, nil
}

func NewMockedMognodbService(ctx context.Context, client *mongo.Client) db.MongodbService {
return &TestMongodbService{client, ctx}
}
67 changes: 67 additions & 0 deletions test/multipart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package test

import (
"bytes"
"errors"
"io"
"mime/multipart"
"os"
"path/filepath"
"testing"
)

func CreateTempAudioFiles(t *testing.T) (string, error) {
dir := t.TempDir()
f, err := os.CreateTemp(dir, "test.*.mp3")
if err != nil {
return "", err
}
defer f.Close()

_, err = f.Write([]byte{0xff, 0xfb})
if err != nil {
return "", err
}

return f.Name(), nil
}

func CreateMultipartFileHeader(path string) (*multipart.FileHeader, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()

var buf bytes.Buffer

formWriter := multipart.NewWriter(&buf)
filename := filepath.Base(path)
formPart, err := formWriter.CreateFormFile(filename, filepath.Base(path))
if err != nil {
return nil, err
}

if _, err = io.Copy(formPart, f); err != nil {
return nil, err
}

err = formWriter.Close()
if err != nil {
return nil, err
}

reader := bytes.NewReader(buf.Bytes())
formReader := multipart.NewReader(reader, formWriter.Boundary())

multipartForm, err := formReader.ReadForm(1 << 20)
if err != nil {
return nil, err
}

if file, ok := multipartForm.File[filename]; !ok || len(file) <= 0 {
return nil, errors.New("failed create multipart audio")
} else {
return file[0], nil
}
}
18 changes: 18 additions & 0 deletions test/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test

import (
"github.com/wittano/komputer/web/settings"
"path/filepath"
"testing"
)

func LoadDefaultConfig(t *testing.T) error {
const defaultConfigFileName = "config.yml"
configFile := filepath.Join(t.TempDir(), defaultConfigFileName)

if err := settings.Load(configFile); err != nil {
return err
}

return settings.Config.Update(settings.Settings{AssetDir: filepath.Join(t.TempDir(), "assets")})
}
12 changes: 8 additions & 4 deletions web/internal/audio/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import (

const audioCollectionName = "audio"

func saveFileDataInDatabase(ctx context.Context, filename string) error {
client, err := db.Mongodb(ctx).Client(ctx)
type DatabaseService struct {
Database db.MongodbService
}

func (a DatabaseService) save(ctx context.Context, filename string) error {
client, err := a.Database.Client(ctx)
if err != nil {
return err
}
Expand All @@ -24,13 +28,13 @@ func saveFileDataInDatabase(ctx context.Context, filename string) error {
return err
}

func GetAudioInfo(ctx context.Context, id string) (result db.AudioInfo, err error) {
func (a DatabaseService) Get(ctx context.Context, id string) (result db.AudioInfo, err error) {
hex, err := primitive.ObjectIDFromHex(id)
if err != nil {
return
}

client, err := db.Mongodb(ctx).Client(ctx)
client, err := a.Database.Client(ctx)
if err != nil {
return db.AudioInfo{}, err
}
Expand Down
Loading

0 comments on commit 594ec48

Please sign in to comment.