172 lines
4.8 KiB
Go
172 lines
4.8 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"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestContacts_Add(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
owner, _ := db.Users.Create(context.Background())
|
|
target, _ := db.Users.Create(context.Background())
|
|
|
|
contact := &database.Contact{
|
|
OwnerID: owner.ID,
|
|
TargetID: target.ID,
|
|
Name: "брат",
|
|
}
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
err := db.Contacts.Add(context.Background(), contact)
|
|
require.NoError(t, err)
|
|
})
|
|
|
|
t.Run("already exists", func(t *testing.T) {
|
|
err := db.Contacts.Add(context.Background(), contact)
|
|
assert.ErrorIs(t, err, database.ErrAlreadyExists)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db.Close()
|
|
err := db.Contacts.Add(context.Background(), contact)
|
|
assert.Error(t, err)
|
|
assert.NotErrorIs(t, err, database.ErrAlreadyExists)
|
|
})
|
|
}
|
|
|
|
func TestContacts_Get(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
owner, _ := db.Users.Create(context.Background())
|
|
target, _ := db.Users.Create(context.Background())
|
|
|
|
contact := &database.Contact{
|
|
OwnerID: owner.ID,
|
|
TargetID: target.ID,
|
|
Name: "брат",
|
|
}
|
|
db.Contacts.Add(context.Background(), contact)
|
|
|
|
t.Run("found", func(t *testing.T) {
|
|
got, err := db.Contacts.Get(context.Background(), owner.ID, "брат")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, owner.ID, got.OwnerID)
|
|
assert.Equal(t, target.ID, got.TargetID)
|
|
assert.Equal(t, "брат", got.Name)
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
_, err := db.Contacts.Get(context.Background(), owner.ID, "сестра")
|
|
assert.ErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db.Close()
|
|
_, err := db.Contacts.Get(context.Background(), owner.ID, "брат")
|
|
assert.Error(t, err)
|
|
assert.NotErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
}
|
|
|
|
func TestContacts_List(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
owner, _ := db.Users.Create(context.Background())
|
|
target1, _ := db.Users.Create(context.Background())
|
|
target2, _ := db.Users.Create(context.Background())
|
|
|
|
db.Contacts.Add(context.Background(), &database.Contact{OwnerID: owner.ID, TargetID: target1.ID, Name: "брат"})
|
|
db.Contacts.Add(context.Background(), &database.Contact{OwnerID: owner.ID, TargetID: target2.ID, Name: "друг"})
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
contacts, err := db.Contacts.List(context.Background(), owner.ID)
|
|
require.NoError(t, err)
|
|
assert.Len(t, contacts, 2)
|
|
})
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
other, _ := db.Users.Create(context.Background())
|
|
contacts, err := db.Contacts.List(context.Background(), other.ID)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, contacts)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db.Close()
|
|
_, err := db.Contacts.List(context.Background(), owner.ID)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestContacts_Delete(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
owner, _ := db.Users.Create(context.Background())
|
|
target, _ := db.Users.Create(context.Background())
|
|
|
|
db.Contacts.Add(context.Background(), &database.Contact{OwnerID: owner.ID, TargetID: target.ID, Name: "брат"})
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
err := db.Contacts.Delete(context.Background(), owner.ID, "брат")
|
|
require.NoError(t, err)
|
|
|
|
contacts, _ := db.Contacts.List(context.Background(), owner.ID)
|
|
assert.Empty(t, contacts)
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
|
err := db.Contacts.Delete(context.Background(), owner.ID, "брат")
|
|
assert.ErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
|
|
t.Run("database error", func(t *testing.T) {
|
|
db.Close()
|
|
err := db.Contacts.Delete(context.Background(), owner.ID, "брат")
|
|
assert.Error(t, err)
|
|
assert.NotErrorIs(t, err, database.ErrNotFound)
|
|
})
|
|
}
|
|
|
|
func TestContacts_List_ScanError(t *testing.T) {
|
|
conn, mock, err := sqlmock.New()
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
db := &Contacts{conn: conn}
|
|
ownerID := uuid.New()
|
|
|
|
rows := sqlmock.NewRows([]string{"owner_id", "target_id", "name"}).
|
|
AddRow(uuid.New(), uuid.New(), nil) // NULL в name
|
|
|
|
mock.ExpectQuery(`SELECT owner_id, target_id, name FROM contacts WHERE owner_id = \$1`).
|
|
WithArgs(ownerID).
|
|
WillReturnRows(rows)
|
|
|
|
_, err = db.List(context.Background(), ownerID)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestContacts_List_RowsErr(t *testing.T) {
|
|
conn, mock, err := sqlmock.New()
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
db := &Contacts{conn: conn}
|
|
ownerID := uuid.New()
|
|
|
|
rows := sqlmock.NewRows([]string{"owner_id", "target_id", "name"}).
|
|
AddRow(uuid.New(), uuid.New(), "брат").
|
|
RowError(0, errors.New("connection lost"))
|
|
|
|
mock.ExpectQuery(`SELECT owner_id, target_id, name FROM contacts WHERE owner_id = \$1`).
|
|
WithArgs(ownerID).
|
|
WillReturnRows(rows)
|
|
|
|
_, err = db.List(context.Background(), ownerID)
|
|
assert.Error(t, err)
|
|
}
|