Files
ipe/app_test.go
T
claudemiro 0e3440bc35 Fixed app and connection tests.
The tests was not updated.
2015-01-13 13:29:52 -03:00

168 lines
3.3 KiB
Go

// Copyright 2014 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"strconv"
"testing"
)
var id = 0
func newApp() App {
a := App{Name: "Test", AppID: strconv.Itoa(id), Key: "123", Secret: "123", OnlySSL: false, ApplicationDisabled: false, UserEvents: true}
a.Init()
id++
return a
}
func Test_add_channels(t *testing.T) {
app := newApp()
// Public
if len(app.PublicChannels()) != 0 {
t.Error("Length of public channels must be 0 before test")
}
app.AddChannel(NewChannel("ID"))
if len(app.PublicChannels()) != 1 {
t.Error("Length os public channels after insert must be 1")
}
// Presence
if len(app.PresenceChannels()) != 0 {
t.Error("Length of presence channels must be 0 before test")
}
app.AddChannel(NewChannel("presence-test"))
if len(app.PresenceChannels()) != 1 {
t.Error("Length os presence channels after insert must be 1")
}
// Private
if len(app.PrivateChannels()) != 0 {
t.Error("Length of private channels must be 0 before test")
}
app.AddChannel(NewChannel("private-test"))
if len(app.PrivateChannels()) != 1 {
t.Error("Length os private channels after insert must be 1")
}
}
func Test_AllChannels(t *testing.T) {
app := newApp()
app.AddChannel(NewChannel("private-test"))
app.AddChannel(NewChannel("presence-test"))
app.AddChannel(NewChannel("test"))
if len(app.Channels) != 3 {
t.Error("Must have 3 channels")
}
}
func Test_New_Subscriber(t *testing.T) {
app := newApp()
if len(app.Subscribers) != 0 {
t.Error("Length of subscribers before test must be 0")
}
conn := NewSubscriber("1", nil)
app.Connect(conn)
if len(app.Subscribers) != 1 {
t.Error("Length os subscribers after test must be 1")
}
}
func Test_find_subscriber(t *testing.T) {
app := newApp()
conn := NewSubscriber("1", nil)
app.Connect(conn)
conn, err := app.FindSubscriber("1")
if err != nil {
t.Error(err)
}
if conn.SocketID != "1" {
t.Error("Wrong subscriber.")
}
// Find a wrong subscriber
conn, err = app.FindSubscriber("DoesNotExists")
if err == nil {
t.Error("Opps, Must be nil")
}
if conn != nil {
t.Error("Opps, Must be nil")
}
}
func Test_find_or_create_channels(t *testing.T) {
app := newApp()
// Public
if len(app.PublicChannels()) != 0 {
t.Error("Length of public channels must be 0 before test")
}
c := app.FindOrCreateChannelByChannelID("id")
if len(app.PublicChannels()) != 1 {
t.Error("Length os public channels after insert must be 1")
}
if c.ChannelID != "id" {
t.Error("Opps wrong channel")
}
// Presence
if len(app.PresenceChannels()) != 0 {
t.Error("Length of presence channels must be 0 before test")
}
c = app.FindOrCreateChannelByChannelID("presence-test")
if len(app.PresenceChannels()) != 1 {
t.Error("Length os presence channels after insert must be 1")
}
if c.ChannelID != "presence-test" {
t.Error("Opps wrong channel")
}
// Private
if len(app.PrivateChannels()) != 0 {
t.Error("Length of private channels must be 0 before test")
}
c = app.FindOrCreateChannelByChannelID("private-test")
if len(app.PrivateChannels()) != 1 {
t.Error("Length os private channels after insert must be 1")
}
if c.ChannelID != "private-test" {
t.Error("Opps wrong channel")
}
}