test: engine package

pull/2/head
sundowndev 2020-04-12 22:59:30 +01:00
parent ee9c15e52c
commit 9a7c7d1ac0
2 changed files with 91 additions and 4 deletions

77
engine/redis_test.go Normal file
View File

@ -0,0 +1,77 @@
package engine
import (
"log"
"testing"
"github.com/go-redis/redis/v7"
"github.com/stretchr/testify/assert"
)
const (
addr = "0.0.0.0"
port = "6379"
)
func TestRedisClient(t *testing.T) {
assert := assert.New(t)
client := redis.NewClient(&redis.Options{
Addr: addr + ":" + port,
Password: "", // no password set
DB: 0, // use default DB
})
defer client.FlushAll()
defer client.Close()
redisClient, err := NewRedisClient(addr, port)
if err != nil {
log.Fatal(err)
}
defer redisClient.Close()
t.Run("RedisClient", func(t *testing.T) {
t.Run("should use ZADD", func(t *testing.T) {
client.FlushAll()
err := redisClient.AddFile("file1", "word")
assert.Equal(nil, err, "should be equal")
err = redisClient.AddFile("file2", "word")
assert.Equal(nil, err, "should be equal")
values, err2 := client.ZRevRange("file1", 0, -1).Result()
assert.Equal(nil, err2, "should be equal")
assert.Equal([]string{"word"}, values, "should be equal")
})
t.Run("should use ZREVRANGE", func(t *testing.T) {
client.FlushAll()
err := client.ZAdd("word", &redis.Z{
Score: float64(1),
Member: "file",
}).Err()
assert.Equal(nil, err, "should be equal")
values, err2 := redisClient.GetKey("word")
assert.Equal(nil, err2, "should be equal")
assert.Equal([]string{"file"}, values, "should be equal")
})
t.Run("should use FLIUSHALL", func(t *testing.T) {
client.FlushAll()
err := client.Set("test", "value", 0).Err()
assert.Equal(nil, err, "should be equal")
err = redisClient.FlushAll()
assert.Equal(nil, err, "should be equal")
err = client.Get("test").Err()
assert.Equal(redis.Nil, err, "should be equal")
})
})
}

View File

@ -10,10 +10,20 @@ func TestText(t *testing.T) {
assert := assert.New(t)
t.Run("CountWord", func(t *testing.T) {
t.Run("should count words in text", func(t *testing.T) {
text := "Contrairement à une opinion répandue, le Lorem Ipsum n'est pas simplement du texte aléatoire. Il trouve ses racines dans une oeuvre de la littérature latine classique datant de 45 av. J.-C., le rendant vieux de 2000 ans. Un professeur du Hampden-Sydney College, en Virginie, s'est intéressé à un des mots latins les plus obscurs, consectetur, extrait d'un passage du Lorem Ipsum, et en étudiant tous les usages de ce mot dans la littérature classique, découvrit la source incontestable du Lorem Ipsum."
assert.Equal(3, CountWord(text, "la"), "should be equal")
})
t.Run("GetWordsFromText", func(t *testing.T) {
text := "Un professeur du Hampden-Sydney College, en Virginie, s'est intéressé à un des mots latins les plus obscurs, consectetur, extrait d'un passage du Lorem Ipsum."
assert.Equal([]string{"un", "professeur", "du", "hampden-sydney", "college,", "en", "virginie,", "s'est", "intéressé", "à", "un", "des", "mots", "latins", "les", "plus", "obscurs,", "consectetur,", "extrait", "d'un", "passage", "du", "lorem", "ipsum."}, GetWordsFromText(text), "should be equal")
})
t.Run("IsTextFile", func(t *testing.T) {
assert.Equal(true, IsTextFile([]byte("Here is a string....")), "should be equal")
assert.Equal(false, IsTextFile([]byte("<html></html>")), "should be equal")
})
}