aboutsummaryrefslogtreecommitdiff
path: root/badger/ids_test.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2023-06-03 07:20:03 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2023-06-03 07:20:03 +0300
commit9a84e9a8b6b7a6f953301e54f19cdf4be73592e1 (patch)
tree33e96558211827f696868815222e2d36abbd8fcb /badger/ids_test.go
parent8663a29e157aae6e68cc880a86b3a666da37bfc9 (diff)
Store message ids in Badger DB
Diffstat (limited to 'badger/ids_test.go')
-rw-r--r--badger/ids_test.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/badger/ids_test.go b/badger/ids_test.go
new file mode 100644
index 0000000..efafdeb
--- /dev/null
+++ b/badger/ids_test.go
@@ -0,0 +1,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")
+ }
+}