165 lines
4.5 KiB
Go
165 lines
4.5 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/d1nch8g/jules/database"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestChats_Attach(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
user, err := db.Users.Create(context.Background())
|
|
assert.NoError(t, err)
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
err := db.Chats.Attach(context.Background(), user.ID, "telegram", "@test_attach")
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("already exists", func(t *testing.T) {
|
|
err := db.Chats.Attach(context.Background(), user.ID, "telegram", "@test_attach")
|
|
assert.ErrorIs(t, err, database.ErrAlreadyExists)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db2 := setupTestDB(t)
|
|
db2.Close()
|
|
err := db2.Chats.Attach(context.Background(), user.ID, "whatsapp", "@test_attach")
|
|
assert.Error(t, err)
|
|
assert.NotErrorIs(t, err, database.ErrAlreadyExists)
|
|
})
|
|
}
|
|
|
|
func TestChats_GetUserID(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
user, err := db.Users.Create(context.Background())
|
|
assert.NoError(t, err)
|
|
|
|
err = db.Chats.Attach(context.Background(), user.ID, "telegram", "@test_get")
|
|
assert.NoError(t, err)
|
|
|
|
t.Run("found", func(t *testing.T) {
|
|
got, err := db.Chats.GetUserID(context.Background(), "telegram", "@test_get")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, user.ID, got)
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
_, err := db.Chats.GetUserID(context.Background(), "telegram", "@notfound")
|
|
assert.ErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db2 := setupTestDB(t)
|
|
db2.Close()
|
|
_, err := db2.Chats.GetUserID(context.Background(), "telegram", "@test_get")
|
|
assert.Error(t, err)
|
|
assert.NotErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
}
|
|
|
|
func TestChats_List(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
user, _ := db.Users.Create(context.Background())
|
|
db.Chats.Attach(context.Background(), user.ID, "telegram", "@test")
|
|
db.Chats.Attach(context.Background(), user.ID, "email", "test@example.com")
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
chats, err := db.Chats.List(context.Background(), user.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, chats, 2)
|
|
})
|
|
|
|
t.Run("empty list", func(t *testing.T) {
|
|
otherUser, _ := db.Users.Create(context.Background())
|
|
chats, err := db.Chats.List(context.Background(), otherUser.ID)
|
|
assert.NoError(t, err)
|
|
assert.Empty(t, chats)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db.Close()
|
|
_, err := db.Chats.List(context.Background(), user.ID)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestChats_Detach(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
user, _ := db.Users.Create(context.Background())
|
|
db.Chats.Attach(context.Background(), user.ID, "telegram", "@test")
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
err := db.Chats.Detach(context.Background(), user.ID, "telegram")
|
|
assert.NoError(t, err)
|
|
|
|
chats, _ := db.Chats.List(context.Background(), user.ID)
|
|
assert.Empty(t, chats)
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
err := db.Chats.Detach(context.Background(), user.ID, "telegram")
|
|
assert.ErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db.Close()
|
|
err := db.Chats.Detach(context.Background(), user.ID, "telegram")
|
|
assert.Error(t, err)
|
|
assert.NotErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
}
|
|
|
|
func TestChats_List_ScanError(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
user, _ := db.Users.Create(context.Background())
|
|
|
|
_, err := db.conn.ExecContext(context.Background(), `
|
|
ALTER TABLE chats ALTER COLUMN identifier DROP NOT NULL
|
|
`)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = db.conn.ExecContext(context.Background(), `
|
|
INSERT INTO chats (user_id, platform, identifier)
|
|
VALUES ($1, 'telegram', NULL)
|
|
`, user.ID)
|
|
assert.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
db.conn.ExecContext(context.Background(), `
|
|
DELETE FROM chats WHERE user_id = $1 AND platform = 'telegram'
|
|
`, user.ID)
|
|
db.conn.ExecContext(context.Background(), `
|
|
ALTER TABLE chats ALTER COLUMN identifier SET NOT NULL
|
|
`)
|
|
})
|
|
|
|
_, err = db.Chats.List(context.Background(), user.ID)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestChats_List_RowsErr(t *testing.T) {
|
|
conn, mock, err := sqlmock.New()
|
|
assert.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
db := &Chats{conn: conn}
|
|
userID := uuid.New()
|
|
|
|
rows := sqlmock.NewRows([]string{"user_id", "platform", "identifier"}).
|
|
AddRow(uuid.New(), "telegram", "@test").
|
|
RowError(0, errors.New("connection lost"))
|
|
|
|
mock.ExpectQuery(`SELECT user_id, platform, identifier FROM chats WHERE user_id = \$1`).
|
|
WithArgs(userID).
|
|
WillReturnRows(rows)
|
|
|
|
_, err = db.List(context.Background(), userID)
|
|
assert.Error(t, err)
|
|
}
|