aboutsummaryrefslogtreecommitdiff
path: root/badger/ids_test.go
blob: efafdeb3848718bb5ce79455d7b229a5aa9f0529 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package badger

import (
	"reflect"
	"testing"
)

func TestToKeyPrefix(t *testing.T) {
	if !reflect.DeepEqual(toKeyPrefix("+123456789", "test@example.com"), []byte("+123456789/test@example.com/")) {
		t.Error("Wrong prefix")
	}
}

func TestToByteKey(t *testing.T) {
	if !reflect.DeepEqual(toByteKey([]byte("ababa/galamaga/"), []byte("123"), "ppp"), []byte("ababa/galamaga/ppp/123")) {
		t.Error("Wrong key")
	}
}

func TestToTgByteString(t *testing.T) {
	if !reflect.DeepEqual(toTgByteString(-2345, 6789), []byte("-2345/6789")) {
		t.Error("Wrong tg string")
	}
}

func TestToXmppByteString(t *testing.T) {
	if !reflect.DeepEqual(toXmppByteString("aboba"), []byte("aboba")) {
		t.Error("Wrong xmpp string")
	}
}

func TestSplitTgByteStringUnparsable(t *testing.T) {
	_, _, err := splitTgByteString([]byte("@#U*&$(@#"))
	if err == nil {
		t.Error("Unparsable should not be parsed")
		return
	}
	if err.Error() != "Couldn't parse tg id pair" {
		t.Error("Wrong parse error")
	}
}

func TestSplitTgByteManyParts(t *testing.T) {
	_, _, err := splitTgByteString([]byte("a/b/c/d"))
	if err == nil {
		t.Error("Should not parse many parts")
		return
	}
	if err.Error() != "Couldn't parse tg id pair" {
		t.Error("Wrong parse error")
	}
}

func TestSplitTgByteNonNumeric(t *testing.T) {
	_, _, err := splitTgByteString([]byte("0/a"))
	if err == nil {
		t.Error("Should not parse non-numeric msgid")
	}
}

func TestSplitTgByteSuccess(t *testing.T) {
	chatId, msgId, err := splitTgByteString([]byte("-198282398/23798478"))
	if err != nil {
		t.Error("Should be parsed well")
	}
	if chatId != -198282398 {
		t.Error("Wrong chatId")
	}
	if msgId != 23798478 {
		t.Error("Wrong msgId")
	}
}