2016-01-07 22:59:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2016-01-17 20:15:29 +00:00
|
|
|
"strconv"
|
2016-01-07 22:59:38 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func tempdir() string {
|
|
|
|
f, _ := ioutil.TempDir("", "")
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUser(t *testing.T) {
|
2016-01-15 01:27:30 +00:00
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
assert.Nil(t, r)
|
|
|
|
}()
|
|
|
|
|
2016-01-07 22:59:38 +00:00
|
|
|
Initialize(tempdir())
|
|
|
|
Open()
|
|
|
|
|
|
|
|
srv := Server{
|
2016-01-14 05:17:53 +00:00
|
|
|
Name: "Freenode",
|
|
|
|
Host: "irc.freenode.net",
|
|
|
|
Nick: "test",
|
2016-01-07 22:59:38 +00:00
|
|
|
}
|
|
|
|
chan1 := Channel{
|
2016-01-14 05:17:53 +00:00
|
|
|
Server: srv.Host,
|
2016-01-07 22:59:38 +00:00
|
|
|
Name: "#test",
|
|
|
|
}
|
|
|
|
chan2 := Channel{
|
2016-01-14 05:17:53 +00:00
|
|
|
Server: srv.Host,
|
2016-01-07 22:59:38 +00:00
|
|
|
Name: "#testing",
|
|
|
|
}
|
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
user, err := NewUser()
|
|
|
|
assert.Nil(t, err)
|
2016-01-07 22:59:38 +00:00
|
|
|
user.AddServer(srv)
|
|
|
|
user.AddChannel(chan1)
|
|
|
|
user.AddChannel(chan2)
|
|
|
|
user.Close()
|
|
|
|
|
|
|
|
users := LoadUsers()
|
|
|
|
assert.Len(t, users, 1)
|
|
|
|
|
|
|
|
user = users[0]
|
2016-01-15 01:27:30 +00:00
|
|
|
assert.Equal(t, uint64(1), user.ID)
|
2016-01-07 22:59:38 +00:00
|
|
|
|
|
|
|
servers := user.GetServers()
|
|
|
|
assert.Len(t, servers, 1)
|
|
|
|
assert.Equal(t, srv, servers[0])
|
|
|
|
|
|
|
|
channels := user.GetChannels()
|
|
|
|
assert.Len(t, channels, 2)
|
|
|
|
assert.Equal(t, chan1, channels[0])
|
|
|
|
assert.Equal(t, chan2, channels[1])
|
|
|
|
|
2016-01-14 05:17:53 +00:00
|
|
|
user.SetNick("bob", srv.Host)
|
2016-01-07 22:59:38 +00:00
|
|
|
assert.Equal(t, "bob", user.GetServers()[0].Nick)
|
|
|
|
|
2016-01-14 05:17:53 +00:00
|
|
|
user.RemoveChannel(srv.Host, chan1.Name)
|
2016-01-07 22:59:38 +00:00
|
|
|
channels = user.GetChannels()
|
|
|
|
assert.Len(t, channels, 1)
|
|
|
|
assert.Equal(t, chan2, channels[0])
|
|
|
|
|
2016-01-14 05:17:53 +00:00
|
|
|
user.RemoveServer(srv.Host)
|
2016-01-07 22:59:38 +00:00
|
|
|
assert.Len(t, user.GetServers(), 0)
|
|
|
|
assert.Len(t, user.GetChannels(), 0)
|
|
|
|
}
|
2016-01-17 20:15:29 +00:00
|
|
|
|
|
|
|
func TestMessages(t *testing.T) {
|
|
|
|
Initialize(tempdir())
|
|
|
|
Open()
|
|
|
|
|
|
|
|
user, err := NewUser()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
messages, err := user.GetMessages("irc.freenode.net", "#go-nuts", 10, 6)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 0)
|
|
|
|
|
|
|
|
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 0)
|
|
|
|
|
|
|
|
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 0)
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
err = user.LogMessage("irc.freenode.net", "nick", "#go-nuts", "message"+strconv.Itoa(i))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 6)
|
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
|
|
|
assert.Equal(t, "message4", messages[4].Content)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 5)
|
|
|
|
|
|
|
|
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 100)
|
2016-01-17 22:33:52 +00:00
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
|
|
|
assert.Equal(t, "message4", messages[4].Content)
|
2016-01-17 20:15:29 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 5)
|
|
|
|
|
|
|
|
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 4)
|
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
|
|
|
assert.Equal(t, "message2", messages[2].Content)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 3)
|
|
|
|
|
|
|
|
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
|
|
|
|
assert.Equal(t, "message0", messages[0].Content)
|
|
|
|
assert.Equal(t, "message2", messages[2].Content)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 5)
|
|
|
|
|
|
|
|
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 3)
|
|
|
|
assert.Equal(t, "message2", messages[0].Content)
|
|
|
|
assert.Equal(t, "message4", messages[2].Content)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 3)
|
|
|
|
|
|
|
|
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, messages, 5)
|
|
|
|
|
|
|
|
Close()
|
|
|
|
}
|