Add more session and config tests

This commit is contained in:
Tulir Asokan 2018-05-02 23:45:17 +03:00
parent 9dabaa26c7
commit a9390d3b5c
3 changed files with 101 additions and 9 deletions

View File

@ -45,10 +45,8 @@ func TestSession_Load(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-test-8", "/tmp/gomuks-test-8")
cfg.Load()
session := cfg.NewSession("@tulir:maunium.net")
session.NextBatch = "foobar"
session.FilterID = "1234"
assert.Nil(t, session.Save())
session.SaveNextBatch("@tulir:maunium.net", "foobar")
session.SaveFilterID("@tulir:maunium.net", "1234")
cfg = config.NewConfig("/tmp/gomuks-test-8", "/tmp/gomuks-test-8")
cfg.LoadSession("@tulir:maunium.net")
@ -63,10 +61,8 @@ func TestSession_Clear(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-test-9", "/tmp/gomuks-test-9")
cfg.Load()
session := cfg.NewSession("@tulir:maunium.net")
session.NextBatch = "foobar"
session.FilterID = "1234"
assert.Nil(t, session.Save())
session.SaveNextBatch("@tulir:maunium.net", "foobar")
session.SaveFilterID("@tulir:maunium.net", "1234")
cfg = config.NewConfig("/tmp/gomuks-test-9", "/tmp/gomuks-test-9")
cfg.LoadSession("@tulir:maunium.net")
@ -85,3 +81,58 @@ func TestSession_Clear(t *testing.T) {
assert.Empty(t, cfg.Session.NextBatch)
assert.Empty(t, cfg.Session.Rooms)
}
func TestConfig_ClearWithSession(t *testing.T) {
defer os.RemoveAll("/tmp/gomuks-test-9")
cfg := config.NewConfig("/tmp/gomuks-test-9", "/tmp/gomuks-test-9")
cfg.Load()
session := cfg.NewSession("@tulir:maunium.net")
session.SaveNextBatch("@tulir:maunium.net", "foobar")
session.SaveFilterID("@tulir:maunium.net", "1234")
cfg = config.NewConfig("/tmp/gomuks-test-9", "/tmp/gomuks-test-9")
cfg.LoadSession("@tulir:maunium.net")
assert.NotNil(t, cfg.Session)
assert.Equal(t, "foobar", cfg.Session.LoadNextBatch("@tulir:maunium.net"))
assert.Equal(t, "1234", cfg.Session.LoadFilterID("@tulir:maunium.net"))
cfg.Clear()
assert.Empty(t, cfg.Session.FilterID)
assert.Empty(t, cfg.Session.NextBatch)
assert.Empty(t, cfg.Session.Rooms)
cfg = config.NewConfig("/tmp/gomuks-test-9", "/tmp/gomuks-test-9")
cfg.LoadSession("@tulir:maunium.net")
assert.Empty(t, cfg.Session.FilterID)
assert.Empty(t, cfg.Session.NextBatch)
assert.Empty(t, cfg.Session.Rooms)
}
func TestSession_GetRoom(t *testing.T) {
defer os.RemoveAll("/tmp/gomuks-test-10")
cfg := config.NewConfig("/tmp/gomuks-test-10", "/tmp/gomuks-test-10")
cfg.Session = cfg.NewSession("@tulir:maunium.net")
room := cfg.Session.GetRoom("!foo:maunium.net")
assert.NotNil(t, room)
assert.Equal(t, room.Room, cfg.Session.LoadRoom("!foo:maunium.net"))
}
func TestSession_PutRoom(t *testing.T) {
defer os.RemoveAll("/tmp/gomuks-test-11")
cfg := config.NewConfig("/tmp/gomuks-test-11", "/tmp/gomuks-test-11")
cfg.Load()
cfg.LoadSession("@tulir:maunium.net")
room := cfg.Session.GetRoom("!foo:maunium.net")
room.PrevBatch = "foobar"
room.HasLeft = true
cfg.Session.PutRoom(room)
cfg = config.NewConfig("/tmp/gomuks-test-11", "/tmp/gomuks-test-11")
cfg.LoadSession("@tulir:maunium.net")
reloadedRoom := cfg.Session.GetRoom("!foo:maunium.net")
assert.Equal(t, "foobar", reloadedRoom.PrevBatch, "%v %v", room, reloadedRoom)
assert.True(t, reloadedRoom.HasLeft, "%v %v", room, reloadedRoom)
}

View File

@ -14,4 +14,29 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package matrix_test
package matrix
import (
"testing"
"maunium.net/go/gomuks/config"
"github.com/stretchr/testify/assert"
)
func TestContainer_InitClient_Empty(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-mxtest-0", "/tmp/gomuks-mxtest-0")
cfg.HS = "https://matrix.org"
c := Container{config: cfg}
assert.Nil(t, c.InitClient())
}
func TestContainer_renderMarkdown(t *testing.T) {
text := "**foo** _bar_"
c := Container{}
assert.Equal(t, "<strong>foo</strong> <em>bar</em>", c.renderMarkdown(text))
}
func TestContainer_GetCachePath(t *testing.T) {
cfg := config.NewConfig("/tmp/gomuks-mxtest-1", "/tmp/gomuks-mxtest-1")
c := Container{config: cfg}
assert.Equal(t, "/tmp/gomuks-mxtest-1/media/maunium.net/foobar", c.GetCachePath("maunium.net", "foobar"))
}

View File

@ -59,6 +59,22 @@ func TestRoom_GetTopic(t *testing.T) {
assert.Equal(t, "test topic", room.GetTopic())
}
func TestRoom_Tags_Empty(t *testing.T) {
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
assert.Empty(t, room.RawTags)
tags := room.Tags()
assert.Len(t, tags, 1)
assert.Equal(t, "", tags[0].Tag)
assert.Equal(t, 0.5, tags[0].Order)
}
func TestRoom_Tags_NotEmpty(t *testing.T) {
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
room.RawTags = []rooms.RoomTag{{"foo", 1}, {"bar", 1}}
tags := room.Tags()
assert.Equal(t, room.RawTags, tags)
}
func TestRoom_GetAliases(t *testing.T) {
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
addAliases(room)