telegabber/persistence/sessions_test.go

119 lines
2.4 KiB
Go
Raw Permalink Normal View History

2019-12-05 21:21:07 +00:00
package persistence
import (
"reflect"
"testing"
)
func TestEmptySessionsMap(t *testing.T) {
var sm SessionsMap
emptySessionsMap(&sm)
if sm.Sessions == nil {
t.Error("Session map is still empty!")
}
}
func TestNoEmptySessionsMap(t *testing.T) {
var sm SessionsMap
if sm.Sessions != nil {
t.Error("Session map is not empty at start!")
}
}
func TestSessionGet(t *testing.T) {
session := Session{
Timezone: "klsf",
}
tz, err := session.Get("timezone")
if err != nil {
t.Error(err)
}
if tz != "klsf" {
t.Errorf("Wrong value from session: %s", tz)
}
}
func TestSessionGetAbsent(t *testing.T) {
session := Session{
Timezone: "klsf",
}
_, err := session.Get("donkey")
if err == nil {
t.Error("There shouldn't be a donkey!")
}
}
func TestSessionToMap(t *testing.T) {
session := Session{
2022-02-09 16:32:55 +00:00
Timezone: "klsf",
RawMessages: true,
2023-01-16 01:35:13 +00:00
OOBMode: true,
2024-01-29 10:00:42 +00:00
Receipts: true,
2019-12-05 21:21:07 +00:00
}
m := session.ToMap()
sample := map[string]string{
2022-02-09 16:32:55 +00:00
"timezone": "klsf",
"keeponline": "false",
"rawmessages": "true",
"asciiarrows": "false",
2023-01-16 01:35:13 +00:00
"oobmode": "true",
2023-03-18 21:45:45 +00:00
"carbons": "false",
2023-03-19 00:13:00 +00:00
"hideids": "false",
2024-01-29 10:00:42 +00:00
"receipts": "true",
"nativeedits": "false",
2019-12-05 21:21:07 +00:00
}
if !reflect.DeepEqual(m, sample) {
t.Errorf("Map does not match the sample: %v", m)
}
}
func TestSessionSet(t *testing.T) {
session := Session{}
tz, err := session.Set("timezone", "zaz")
if err != nil {
t.Error(err)
}
if tz != "zaz" {
t.Errorf("Wrong set result: %s", tz)
}
if session.Timezone != "zaz" {
t.Errorf("Wrong actual value in the session: %s", session.Timezone)
}
}
func TestSessionSetAbsent(t *testing.T) {
session := Session{}
_, err := session.Set("donkey", "zaz")
if err == nil {
t.Error("There shouldn't come a donkey!")
}
}
func TestSessionIgnore(t *testing.T) {
session := Session{}
if session.IsChatIgnored(3) {
t.Error("Shouldn't be ignored yet")
}
if !session.IgnoreChat(3) {
t.Error("Shouldn't have been ignored")
}
if session.IgnoreChat(3) {
t.Error("Shouldn't ignore second time")
}
if !session.IsChatIgnored(3) {
t.Error("Should be ignored already")
}
if session.IsChatIgnored(-145) {
t.Error("Wrong chat is ignored")
}
if !session.UnignoreChat(3) {
t.Error("Should successfully unignore")
}
if session.UnignoreChat(3) {
t.Error("Should unignore second time")
}
if session.IsChatIgnored(3) {
t.Error("Shouldn't be ignored already")
}
}