-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add some test, fix panic when send smr result for slack
- Loading branch information
1 parent
b77ea8d
commit a709ac1
Showing
11 changed files
with
208 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package smr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nekomeowww/insights-bot/internal/models/smr" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestService_botExists(t *testing.T) { | ||
t.Run("BotNotExists", func(t *testing.T) { | ||
a := assert.New(t) | ||
a.False(testService.botExists(smr.FromPlatformDiscord)) | ||
a.False(testService.botExists(smr.FromPlatformSlack)) | ||
a.False(testService.botExists(smr.FromPlatformTelegram)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package smr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nekomeowww/insights-bot/internal/configs" | ||
"github.com/nekomeowww/insights-bot/internal/datastore" | ||
"github.com/nekomeowww/insights-bot/internal/lib" | ||
"github.com/nekomeowww/insights-bot/internal/models/smr" | ||
"github.com/nekomeowww/insights-bot/pkg/tutils" | ||
) | ||
|
||
var testService *Service | ||
|
||
func TestMain(m *testing.M) { | ||
config := configs.NewTestConfig()() | ||
redis, _ := datastore.NewRedis()(datastore.NewRedisParams{ | ||
Configs: config, | ||
}) | ||
testService, _ = NewService()(NewServiceParam{ | ||
Config: config, | ||
Model: smr.NewModel()(smr.NewModelParams{ | ||
Logger: lib.NewLogger()(lib.NewLoggerParams{ | ||
Configs: config, | ||
}), | ||
}), | ||
Logger: lib.NewLogger()(lib.NewLoggerParams{ | ||
Configs: config, | ||
}), | ||
RedisClient: redis, | ||
LifeCycle: tutils.NewEmtpyLifecycle(), | ||
}) | ||
|
||
m.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package smr | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/nekomeowww/insights-bot/internal/models/smr" | ||
"github.com/nekomeowww/insights-bot/internal/services/smr/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestService_AddTask(t *testing.T) { | ||
a := assert.New(t) | ||
r := require.New(t) | ||
taskInfo := types.TaskInfo{ | ||
Platform: smr.FromPlatformDiscord, | ||
Url: "https://an.example.url/article", | ||
ChatID: 114514, | ||
MessageID: 1919810, | ||
ChannelID: "CHANNEL", | ||
TeamID: "A_TEAM", | ||
} | ||
err := testService.AddTask(taskInfo) | ||
a.Empty(err) | ||
|
||
// clean up | ||
defer func() { | ||
err = testService.redisClient.Do(context.Background(), testService.redisClient.B().Del().Key("smr/task").Build()).Error() | ||
r.Empty(err) | ||
}() | ||
|
||
// try get task | ||
var taskResult []string | ||
taskResult, err = testService.redisClient.Do(context.Background(), testService.redisClient.B().Brpop().Key("smr/task").Timeout(0).Build()).AsStrSlice() | ||
r.Empty(err) | ||
a.Equal("smr/task", taskResult[0]) | ||
|
||
expect, err := json.Marshal(&taskInfo) | ||
r.Empty(err) | ||
a.JSONEq(string(expect), taskResult[1]) | ||
} | ||
|
||
func TestService_getTask(t *testing.T) { | ||
a := assert.New(t) | ||
r := require.New(t) | ||
expect := types.TaskInfo{ | ||
Platform: smr.FromPlatformDiscord, | ||
Url: "https://an.example.url/article", | ||
ChatID: 114514, | ||
MessageID: 1919810, | ||
ChannelID: "CHANNEL", | ||
TeamID: "A_TEAM", | ||
} | ||
|
||
expectJson, err := json.Marshal(&expect) | ||
r.Empty(err) | ||
|
||
err = testService.redisClient.Do(context.Background(), testService.redisClient.B().Lpush().Key("smr/task").Element(string(expectJson)).Build()).Error() | ||
r.Empty(err) | ||
|
||
// clean up | ||
defer func() { | ||
err = testService.redisClient.Do(context.Background(), testService.redisClient.B().Del().Key("smr/task").Build()).Error() | ||
r.Empty(err) | ||
}() | ||
|
||
// try get task | ||
actual, err := testService.getTask() | ||
r.Empty(err) | ||
a.Equal(expect, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package smr | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/samber/lo" | ||
) | ||
|
||
func CheckUrl(urlString string) error { | ||
if urlString == "" { | ||
return ErrNoLink | ||
} | ||
|
||
parsedURL, err2 := url.Parse(urlString) | ||
if err2 != nil { | ||
return ErrParse | ||
} | ||
if parsedURL.Scheme == "" || !lo.Contains([]string{"http", "https"}, parsedURL.Scheme) { | ||
return ErrScheme | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package smr | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckUrl(t *testing.T) { | ||
a := assert.New(t) | ||
err := CheckUrl("") | ||
a.Equal(err.Error(), ErrNoLink.Error()) | ||
err = CheckUrl("not a url") | ||
a.Equal(err.Error(), ErrParse.Error()) | ||
err = CheckUrl("://test.com") | ||
a.Equal(err.Error(), ErrScheme.Error()) | ||
err = CheckUrl("wss://test.com") | ||
a.Equal(err.Error(), ErrScheme.Error()) | ||
err = CheckUrl("https://test.com") | ||
a.NoError(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters