Skip to content

Commit

Permalink
v0.11.2
Browse files Browse the repository at this point in the history
- 优化部分单元测量mock方式
  • Loading branch information
wetor committed Feb 2, 2024
1 parent d261a39 commit e5802c3
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 99 deletions.
75 changes: 29 additions & 46 deletions internal/animego/anisource/anisource_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package anisource_test

import (
"context"
"fmt"
"io"
"net/url"
"os"
"path"
"sync"
"testing"

Expand All @@ -17,61 +15,23 @@ import (
"github.com/wetor/AnimeGo/internal/animego/anisource/mikan"
"github.com/wetor/AnimeGo/internal/animego/anisource/themoviedb"
"github.com/wetor/AnimeGo/internal/api"
"github.com/wetor/AnimeGo/internal/constant"
"github.com/wetor/AnimeGo/internal/exceptions"
"github.com/wetor/AnimeGo/internal/models"
"github.com/wetor/AnimeGo/internal/pkg/request"
"github.com/wetor/AnimeGo/internal/plugin"
"github.com/wetor/AnimeGo/internal/wire"
"github.com/wetor/AnimeGo/pkg/cache"
"github.com/wetor/AnimeGo/pkg/json"
"github.com/wetor/AnimeGo/pkg/log"
"github.com/wetor/AnimeGo/pkg/utils"
"github.com/wetor/AnimeGo/pkg/xpath"
"github.com/wetor/AnimeGo/test"
)

var (
mikanSource api.AniSource
ctx, cancel = context.WithCancel(context.Background())
)

func HookGetWriter(uri string, w io.Writer) error {
log.Infof("Mock HTTP GET %s", uri)
id := path.Base(xpath.P(uri))
jsonData, err := test.GetData("mikan", id)
if err != nil {
return err
}
_, err = w.Write(jsonData)
if err != nil {
return err
}
return nil
}

func HookGet(uri string, body interface{}) error {
log.Infof("Mock HTTP GET %s", uri)
u, err := url.Parse(uri)
if err != nil {
return err
}
id := u.Query().Get("with_text_query")
if len(id) == 0 {
id = path.Base(xpath.P(u.Path))
}

p := test.GetDataPath("themoviedb", id)
if !utils.IsExist(p) {
p = test.GetDataPath("bangumi", id)
}

jsonData, err := os.ReadFile(p)
if err != nil {
return err
}
_ = json.Unmarshal(jsonData, body)
return nil
}

func TestMain(m *testing.M) {
fmt.Println("begin")
_ = utils.CreateMutiDir("data")
Expand Down Expand Up @@ -104,7 +64,33 @@ func TestMain(m *testing.M) {
Cache: b,
CacheTime: int64(7 * 24 * 60 * 60),
})

bangumiHost := test.MockBangumiStart(ctx)
mikanHost := test.MockMikanStart(ctx)
themoviedbHost := test.MockThemoviedbStart(ctx)
request.Init(&request.Options{
Host: map[string]*request.HostOptions{
constant.BangumiHost: {
Redirect: bangumiHost,
},
constant.MikanHost: {
Redirect: mikanHost,
Cookie: map[string]string{
constant.MikanAuthCookie: "MikanAuthCookie",
},
},
constant.ThemoviedbHost: {
Redirect: themoviedbHost,
Params: map[string]string{
constant.ThemoviedbApiKey: "123456",
},
},
},
})

m.Run()

cancel()
b.Close()
bangumiCache.Close()
_ = log.Close()
Expand Down Expand Up @@ -177,9 +163,6 @@ func TestMikan_Parse(t *testing.T) {
},
}

test.Hook(request.GetWriter, HookGetWriter)
test.Hook(request.Get, HookGet)
defer test.UnHook()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAnime, err := mikanSource.Parse(tt.args.opts)
Expand Down
15 changes: 10 additions & 5 deletions internal/animego/anisource/bangumi/bangumi_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bangumi_test

import (
"context"
"fmt"
"os"
"sync"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/wetor/AnimeGo/internal/animego/anisource/bangumi"
"github.com/wetor/AnimeGo/internal/constant"
"github.com/wetor/AnimeGo/internal/exceptions"
"github.com/wetor/AnimeGo/internal/pkg/request"
"github.com/wetor/AnimeGo/pkg/cache"
Expand All @@ -18,10 +20,9 @@ import (
"github.com/wetor/AnimeGo/test"
)

const testdata = "bangumi"

var (
bangumiInst *bangumi.Bangumi
ctx, cancel = context.WithCancel(context.Background())
)

func TestMain(m *testing.M) {
Expand All @@ -31,11 +32,14 @@ func TestMain(m *testing.M) {
File: "data/log.log",
Debug: true,
})
host := test.MockBangumiStart(ctx)
request.Init(&request.Options{
Debug: true,
Host: map[string]*request.HostOptions{
constant.BangumiHost: {
Redirect: host,
},
},
})
test.HookAll(testdata, nil)
defer test.UnHook()
mutex := sync.Mutex{}

db := cache.NewBolt()
Expand All @@ -50,6 +54,7 @@ func TestMain(m *testing.M) {
})
m.Run()

cancel()
db.Close()
bangumiCache.Close()
_ = log.Close()
Expand Down
23 changes: 18 additions & 5 deletions internal/animego/anisource/mikan/mikan_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mikan_test

import (
"context"
"fmt"
"os"
"testing"
Expand All @@ -9,16 +10,17 @@ import (
"github.com/stretchr/testify/assert"

"github.com/wetor/AnimeGo/internal/animego/anisource/mikan"
"github.com/wetor/AnimeGo/internal/constant"
"github.com/wetor/AnimeGo/internal/exceptions"
"github.com/wetor/AnimeGo/internal/pkg/request"
"github.com/wetor/AnimeGo/pkg/cache"
"github.com/wetor/AnimeGo/pkg/log"
"github.com/wetor/AnimeGo/test"
)

const testdata = "mikan"

var (
mikanInst *mikan.Mikan
mikanInst *mikan.Mikan
ctx, cancel = context.WithCancel(context.Background())
)

func TestMain(m *testing.M) {
Expand All @@ -27,8 +29,18 @@ func TestMain(m *testing.M) {
File: "data/test.log",
Debug: true,
})
test.HookAll(testdata, nil)
defer test.UnHook()

host := test.MockMikanStart(ctx)
request.Init(&request.Options{
Host: map[string]*request.HostOptions{
constant.MikanHost: {
Redirect: host,
Cookie: map[string]string{
constant.MikanAuthCookie: "MikanAuthCookie",
},
},
},
})

db := cache.NewBolt()
db.Open("data/bolt.db")
Expand All @@ -38,6 +50,7 @@ func TestMain(m *testing.M) {
})
m.Run()

cancel()
db.Close()
_ = log.Close()
_ = os.RemoveAll("data")
Expand Down
33 changes: 14 additions & 19 deletions internal/animego/anisource/themoviedb/themoviedb_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package themoviedb_test

import (
"context"
"fmt"
"net/url"
"os"
"path"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/wetor/AnimeGo/internal/animego/anisource/themoviedb"
"github.com/wetor/AnimeGo/internal/constant"
"github.com/wetor/AnimeGo/internal/exceptions"
"github.com/wetor/AnimeGo/internal/pkg/request"
"github.com/wetor/AnimeGo/pkg/cache"
"github.com/wetor/AnimeGo/pkg/log"
"github.com/wetor/AnimeGo/pkg/xpath"
"github.com/wetor/AnimeGo/test"
)

const testdata = "themoviedb"

var (
tmdbInst *themoviedb.Themoviedb
tmdbInst *themoviedb.Themoviedb
ctx, cancel = context.WithCancel(context.Background())
)

func TestMain(m *testing.M) {
Expand All @@ -36,23 +34,20 @@ func TestMain(m *testing.M) {
tmdbInst = themoviedb.NewThemoviedb(&themoviedb.Options{
Cache: db,
})
host := test.MockThemoviedbStart(ctx)
request.Init(&request.Options{
Debug: true,
})
test.HookGet(testdata, func(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return ""
}
id := u.Query().Get("with_text_query")
if len(id) == 0 {
id = path.Base(xpath.P(u.Path))
}
return id
Host: map[string]*request.HostOptions{
constant.ThemoviedbHost: {
Redirect: host,
Params: map[string]string{
constant.ThemoviedbApiKey: "123456",
},
},
},
})
defer test.UnHook()
m.Run()

cancel()
db.Close()
_ = log.Close()
_ = os.RemoveAll("data")
Expand Down
34 changes: 19 additions & 15 deletions internal/animego/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package parser_test

import (
"context"
"fmt"
"net/url"
"os"
"path"
"testing"

"github.com/pkg/errors"
Expand All @@ -14,20 +13,24 @@ import (
"github.com/wetor/AnimeGo/internal/animego/anisource"
"github.com/wetor/AnimeGo/internal/animego/anisource/mikan"
"github.com/wetor/AnimeGo/internal/animego/parser"
"github.com/wetor/AnimeGo/internal/constant"
"github.com/wetor/AnimeGo/internal/exceptions"
"github.com/wetor/AnimeGo/internal/models"
"github.com/wetor/AnimeGo/internal/pkg/request"
"github.com/wetor/AnimeGo/internal/pkg/torrent"
"github.com/wetor/AnimeGo/internal/plugin"
"github.com/wetor/AnimeGo/pkg/cache"
pkgExceptions "github.com/wetor/AnimeGo/pkg/exceptions"
"github.com/wetor/AnimeGo/pkg/log"
"github.com/wetor/AnimeGo/pkg/xpath"
"github.com/wetor/AnimeGo/test"
)

const testdata = "parser"

var mgr *parser.Manager
var (
mgr *parser.Manager
ctx, cancel = context.WithCancel(context.Background())
)

func TestMain(m *testing.M) {
fmt.Println("begin")
Expand All @@ -39,17 +42,17 @@ func TestMain(m *testing.M) {
torrent.Init(&torrent.Options{
TempPath: "data",
})
test.HookGetWriter(testdata, nil)
test.HookGet(testdata, func(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return ""
}
id := u.Query().Get("with_text_query")
if len(id) == 0 {
id = path.Base(xpath.P(u.Path))
}
return id
host := test.MockThemoviedbStart(ctx)
request.Init(&request.Options{
Host: map[string]*request.HostOptions{
constant.ThemoviedbHost: {
Redirect: host,
Params: map[string]string{
constant.ThemoviedbApiKey: "123456",
"testdata": testdata,
},
},
},
})
test.Hook(torrent.LoadUri, HookLoadUri)
defer test.UnHook()
Expand Down Expand Up @@ -84,6 +87,7 @@ func TestMain(m *testing.M) {

m.Run()

cancel()
b.Close()
_ = log.Close()
_ = os.RemoveAll("data")
Expand Down
9 changes: 0 additions & 9 deletions test/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ const (
)

var (
testdataAll string // 目录名
filenameAll func(string) string

testdata map[string]string // 目录名
filename map[string]func(string) string // 文件名函数

Expand All @@ -32,12 +29,6 @@ func init() {
filename = make(map[string]func(string) string)
}

func HookAll(testdataDir string, filenameFunc func(string) string) {
HookGetWriter(testdataDir, filenameFunc)
HookGet(testdataDir, filenameFunc)
HookGetString(testdataDir, filenameFunc)
}

func UnHook() {
patches.Reset()
}
Expand Down
Loading

0 comments on commit e5802c3

Please sign in to comment.