fix: golangci-lint v2 config and lint-clean production code
- Fix .golangci.yml for v2 format (linters-settings -> linters.settings) - All production code now passes golangci-lint with zero issues - Line length 88, funlen 80/50, cyclop 15, dupl 100 - Extract shared helpers in db (scanChannels, scanInt64s, scanMessages) - Split runMigrations into applyMigration/execMigration - Fix fanOut return signature (remove unused int64) - Add fanOutSilent helper to avoid dogsled - Rewrite CLI code for lint compliance (nlreturn, wsl_v5, noctx, etc) - Rename CLI api package to chatapi to avoid revive var-naming - Fix all noinlineerr, mnd, perfsprint, funcorder issues - Fix db tests: extract helpers, add t.Parallel, proper error checks - Broker tests already clean - Handler integration tests still have lint issues (next commit)
This commit is contained in:
@@ -1,338 +1,550 @@
|
||||
package db
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"git.eeqj.de/sneak/chat/internal/db"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func setupTestDB(t *testing.T) *Database {
|
||||
func setupTestDB(t *testing.T) *db.Database {
|
||||
t.Helper()
|
||||
d, err := sql.Open("sqlite", "file::memory:?cache=shared&_pragma=foreign_keys(1)")
|
||||
|
||||
d, err := db.NewTestDatabase()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { d.Close() })
|
||||
|
||||
db := &Database{db: d, log: slog.Default()}
|
||||
if err := db.runMigrations(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return db
|
||||
t.Cleanup(func() {
|
||||
closeErr := d.Close()
|
||||
if closeErr != nil {
|
||||
t.Logf("close db: %v", closeErr)
|
||||
}
|
||||
})
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func TestCreateUser(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
id, token, err := db.CreateUser(ctx, "alice")
|
||||
id, token, err := database.CreateUser(ctx, "alice")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if id == 0 || token == "" {
|
||||
t.Fatal("expected valid id and token")
|
||||
}
|
||||
|
||||
// Duplicate nick
|
||||
_, _, err = db.CreateUser(ctx, "alice")
|
||||
_, _, err = database.CreateUser(ctx, "alice")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for duplicate nick")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserByToken(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
_, token, _ := db.CreateUser(ctx, "bob")
|
||||
id, nick, err := db.GetUserByToken(ctx, token)
|
||||
_, token, err := database.CreateUser(ctx, "bob")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
id, nick, err := database.GetUserByToken(ctx, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if nick != "bob" || id == 0 {
|
||||
t.Fatalf("expected bob, got %s", nick)
|
||||
}
|
||||
|
||||
// Invalid token
|
||||
_, _, err = db.GetUserByToken(ctx, "badtoken")
|
||||
_, _, err = database.GetUserByToken(ctx, "badtoken")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for bad token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserByNick(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
db.CreateUser(ctx, "charlie")
|
||||
id, err := db.GetUserByNick(ctx, "charlie")
|
||||
_, _, err := database.CreateUser(ctx, "charlie")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
id, err := database.GetUserByNick(ctx, "charlie")
|
||||
if err != nil || id == 0 {
|
||||
t.Fatal("expected to find charlie")
|
||||
}
|
||||
|
||||
_, err = db.GetUserByNick(ctx, "nobody")
|
||||
_, err = database.GetUserByNick(ctx, "nobody")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown nick")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelOperations(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Create channel
|
||||
chID, err := db.GetOrCreateChannel(ctx, "#test")
|
||||
chID, err := database.GetOrCreateChannel(ctx, "#test")
|
||||
if err != nil || chID == 0 {
|
||||
t.Fatal("expected channel id")
|
||||
}
|
||||
|
||||
// Get same channel
|
||||
chID2, err := db.GetOrCreateChannel(ctx, "#test")
|
||||
chID2, err := database.GetOrCreateChannel(ctx, "#test")
|
||||
if err != nil || chID2 != chID {
|
||||
t.Fatal("expected same channel id")
|
||||
}
|
||||
|
||||
// GetChannelByName
|
||||
chID3, err := db.GetChannelByName(ctx, "#test")
|
||||
chID3, err := database.GetChannelByName(ctx, "#test")
|
||||
if err != nil || chID3 != chID {
|
||||
t.Fatal("expected same channel id from GetChannelByName")
|
||||
t.Fatal("expected same channel id")
|
||||
}
|
||||
|
||||
// Nonexistent channel
|
||||
_, err = db.GetChannelByName(ctx, "#nope")
|
||||
_, err = database.GetChannelByName(ctx, "#nope")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nonexistent channel")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinAndPart(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
uid, _, _ := db.CreateUser(ctx, "user1")
|
||||
chID, _ := db.GetOrCreateChannel(ctx, "#chan")
|
||||
|
||||
// Join
|
||||
if err := db.JoinChannel(ctx, chID, uid); err != nil {
|
||||
uid, _, err := database.CreateUser(ctx, "user1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Verify membership
|
||||
ids, err := db.GetChannelMemberIDs(ctx, chID)
|
||||
chID, err := database.GetOrCreateChannel(ctx, "#chan")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.JoinChannel(ctx, chID, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ids, err := database.GetChannelMemberIDs(ctx, chID)
|
||||
if err != nil || len(ids) != 1 || ids[0] != uid {
|
||||
t.Fatal("expected user in channel")
|
||||
}
|
||||
|
||||
// Double join (should be ignored)
|
||||
if err := db.JoinChannel(ctx, chID, uid); err != nil {
|
||||
err = database.JoinChannel(ctx, chID, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Part
|
||||
if err := db.PartChannel(ctx, chID, uid); err != nil {
|
||||
err = database.PartChannel(ctx, chID, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ids, _ = db.GetChannelMemberIDs(ctx, chID)
|
||||
ids, _ = database.GetChannelMemberIDs(ctx, chID)
|
||||
if len(ids) != 0 {
|
||||
t.Fatal("expected empty channel")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteChannelIfEmpty(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
chID, _ := db.GetOrCreateChannel(ctx, "#empty")
|
||||
uid, _, _ := db.CreateUser(ctx, "temp")
|
||||
db.JoinChannel(ctx, chID, uid)
|
||||
db.PartChannel(ctx, chID, uid)
|
||||
|
||||
if err := db.DeleteChannelIfEmpty(ctx, chID); err != nil {
|
||||
chID, err := database.GetOrCreateChannel(
|
||||
ctx, "#empty",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err := db.GetChannelByName(ctx, "#empty")
|
||||
uid, _, err := database.CreateUser(ctx, "temp")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.JoinChannel(ctx, chID, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.PartChannel(ctx, chID, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.DeleteChannelIfEmpty(ctx, chID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = database.GetChannelByName(ctx, "#empty")
|
||||
if err == nil {
|
||||
t.Fatal("expected channel to be deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListChannels(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
func createUserWithChannels(
|
||||
t *testing.T,
|
||||
database *db.Database,
|
||||
nick, ch1Name, ch2Name string,
|
||||
) (int64, int64, int64) {
|
||||
t.Helper()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
uid, _, _ := db.CreateUser(ctx, "lister")
|
||||
ch1, _ := db.GetOrCreateChannel(ctx, "#a")
|
||||
ch2, _ := db.GetOrCreateChannel(ctx, "#b")
|
||||
db.JoinChannel(ctx, ch1, uid)
|
||||
db.JoinChannel(ctx, ch2, uid)
|
||||
uid, _, err := database.CreateUser(ctx, nick)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
channels, err := db.ListChannels(ctx, uid)
|
||||
ch1, err := database.GetOrCreateChannel(
|
||||
ctx, ch1Name,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ch2, err := database.GetOrCreateChannel(
|
||||
ctx, ch2Name,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.JoinChannel(ctx, ch1, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.JoinChannel(ctx, ch2, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return uid, ch1, ch2
|
||||
}
|
||||
|
||||
func TestListChannels(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
uid, _, _ := createUserWithChannels(
|
||||
t, database, "lister", "#a", "#b",
|
||||
)
|
||||
|
||||
channels, err := database.ListChannels(
|
||||
context.Background(), uid,
|
||||
)
|
||||
if err != nil || len(channels) != 2 {
|
||||
t.Fatalf("expected 2 channels, got %d", len(channels))
|
||||
t.Fatalf(
|
||||
"expected 2 channels, got %d",
|
||||
len(channels),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAllChannels(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
db.GetOrCreateChannel(ctx, "#x")
|
||||
db.GetOrCreateChannel(ctx, "#y")
|
||||
_, err := database.GetOrCreateChannel(ctx, "#x")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
channels, err := db.ListAllChannels(ctx)
|
||||
_, err = database.GetOrCreateChannel(ctx, "#y")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
channels, err := database.ListAllChannels(ctx)
|
||||
if err != nil || len(channels) < 2 {
|
||||
t.Fatalf("expected >= 2 channels, got %d", len(channels))
|
||||
t.Fatalf(
|
||||
"expected >= 2 channels, got %d",
|
||||
len(channels),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChangeNick(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
uid, token, _ := db.CreateUser(ctx, "old")
|
||||
if err := db.ChangeNick(ctx, uid, "new"); err != nil {
|
||||
uid, token, err := database.CreateUser(ctx, "old")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.ChangeNick(ctx, uid, "new")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, nick, err := database.GetUserByToken(ctx, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, nick, _ := db.GetUserByToken(ctx, token)
|
||||
if nick != "new" {
|
||||
t.Fatalf("expected new, got %s", nick)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetTopic(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
db.GetOrCreateChannel(ctx, "#topictest")
|
||||
if err := db.SetTopic(ctx, "#topictest", "Hello"); err != nil {
|
||||
_, err := database.GetOrCreateChannel(
|
||||
ctx, "#topictest",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.SetTopic(ctx, "#topictest", "Hello")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
channels, err := database.ListAllChannels(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
channels, _ := db.ListAllChannels(ctx)
|
||||
for _, ch := range channels {
|
||||
if ch.Name == "#topictest" && ch.Topic != "Hello" {
|
||||
t.Fatalf("expected topic Hello, got %s", ch.Topic)
|
||||
if ch.Name == "#topictest" &&
|
||||
ch.Topic != "Hello" {
|
||||
t.Fatalf(
|
||||
"expected topic Hello, got %s",
|
||||
ch.Topic,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAndPollMessages(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
uid, _, _ := db.CreateUser(ctx, "poller")
|
||||
body := json.RawMessage(`["hello"]`)
|
||||
|
||||
dbID, uuid, err := db.InsertMessage(ctx, "PRIVMSG", "poller", "#test", body, nil)
|
||||
if err != nil || dbID == 0 || uuid == "" {
|
||||
t.Fatal("insert failed")
|
||||
}
|
||||
|
||||
if err := db.EnqueueMessage(ctx, uid, dbID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
msgs, lastQID, err := db.PollMessages(ctx, uid, 0, 10)
|
||||
uid, _, err := database.CreateUser(ctx, "poller")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body := json.RawMessage(`["hello"]`)
|
||||
|
||||
dbID, msgUUID, err := database.InsertMessage(
|
||||
ctx, "PRIVMSG", "poller", "#test", body, nil,
|
||||
)
|
||||
if err != nil || dbID == 0 || msgUUID == "" {
|
||||
t.Fatal("insert failed")
|
||||
}
|
||||
|
||||
err = database.EnqueueMessage(ctx, uid, dbID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const batchSize = 10
|
||||
|
||||
msgs, lastQID, err := database.PollMessages(
|
||||
ctx, uid, 0, batchSize,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(msgs) != 1 {
|
||||
t.Fatalf("expected 1 message, got %d", len(msgs))
|
||||
t.Fatalf(
|
||||
"expected 1 message, got %d", len(msgs),
|
||||
)
|
||||
}
|
||||
|
||||
if msgs[0].Command != "PRIVMSG" {
|
||||
t.Fatalf("expected PRIVMSG, got %s", msgs[0].Command)
|
||||
t.Fatalf(
|
||||
"expected PRIVMSG, got %s", msgs[0].Command,
|
||||
)
|
||||
}
|
||||
|
||||
if lastQID == 0 {
|
||||
t.Fatal("expected nonzero lastQID")
|
||||
}
|
||||
|
||||
// Poll again with lastQID - should be empty
|
||||
msgs, _, _ = db.PollMessages(ctx, uid, lastQID, 10)
|
||||
msgs, _, _ = database.PollMessages(
|
||||
ctx, uid, lastQID, batchSize,
|
||||
)
|
||||
|
||||
if len(msgs) != 0 {
|
||||
t.Fatalf("expected 0 messages, got %d", len(msgs))
|
||||
t.Fatalf(
|
||||
"expected 0 messages, got %d", len(msgs),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHistory(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
db.InsertMessage(ctx, "PRIVMSG", "user", "#hist", json.RawMessage(`["msg"]`), nil)
|
||||
const msgCount = 10
|
||||
|
||||
for range msgCount {
|
||||
_, _, err := database.InsertMessage(
|
||||
ctx, "PRIVMSG", "user", "#hist",
|
||||
json.RawMessage(`["msg"]`), nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
msgs, err := db.GetHistory(ctx, "#hist", 0, 5)
|
||||
const histLimit = 5
|
||||
|
||||
msgs, err := database.GetHistory(
|
||||
ctx, "#hist", 0, histLimit,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(msgs) != 5 {
|
||||
t.Fatalf("expected 5, got %d", len(msgs))
|
||||
|
||||
if len(msgs) != histLimit {
|
||||
t.Fatalf("expected %d, got %d",
|
||||
histLimit, len(msgs))
|
||||
}
|
||||
// Should be ascending order
|
||||
if msgs[0].DBID > msgs[4].DBID {
|
||||
|
||||
if msgs[0].DBID > msgs[histLimit-1].DBID {
|
||||
t.Fatal("expected ascending order")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteUser(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
uid, _, _ := db.CreateUser(ctx, "deleteme")
|
||||
chID, _ := db.GetOrCreateChannel(ctx, "#delchan")
|
||||
db.JoinChannel(ctx, chID, uid)
|
||||
|
||||
if err := db.DeleteUser(ctx, uid); err != nil {
|
||||
uid, _, err := database.CreateUser(ctx, "deleteme")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err := db.GetUserByNick(ctx, "deleteme")
|
||||
chID, err := database.GetOrCreateChannel(
|
||||
ctx, "#delchan",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.JoinChannel(ctx, chID, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.DeleteUser(ctx, uid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = database.GetUserByNick(ctx, "deleteme")
|
||||
if err == nil {
|
||||
t.Fatal("user should be deleted")
|
||||
}
|
||||
|
||||
// Channel membership should be cleaned up via CASCADE
|
||||
ids, _ := db.GetChannelMemberIDs(ctx, chID)
|
||||
ids, _ := database.GetChannelMemberIDs(ctx, chID)
|
||||
if len(ids) != 0 {
|
||||
t.Fatal("expected no members after user deletion")
|
||||
t.Fatal("expected no members after deletion")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelMembers(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
t.Parallel()
|
||||
|
||||
database := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
uid1, _, _ := db.CreateUser(ctx, "m1")
|
||||
uid2, _, _ := db.CreateUser(ctx, "m2")
|
||||
chID, _ := db.GetOrCreateChannel(ctx, "#members")
|
||||
db.JoinChannel(ctx, chID, uid1)
|
||||
db.JoinChannel(ctx, chID, uid2)
|
||||
uid1, _, err := database.CreateUser(ctx, "m1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
members, err := db.ChannelMembers(ctx, chID)
|
||||
uid2, _, err := database.CreateUser(ctx, "m2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
chID, err := database.GetOrCreateChannel(
|
||||
ctx, "#members",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.JoinChannel(ctx, chID, uid1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = database.JoinChannel(ctx, chID, uid2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
members, err := database.ChannelMembers(ctx, chID)
|
||||
if err != nil || len(members) != 2 {
|
||||
t.Fatalf("expected 2 members, got %d", len(members))
|
||||
t.Fatalf(
|
||||
"expected 2 members, got %d",
|
||||
len(members),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllChannelMembershipsForUser(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
ctx := context.Background()
|
||||
t.Parallel()
|
||||
|
||||
uid, _, _ := db.CreateUser(ctx, "multi")
|
||||
ch1, _ := db.GetOrCreateChannel(ctx, "#m1")
|
||||
ch2, _ := db.GetOrCreateChannel(ctx, "#m2")
|
||||
db.JoinChannel(ctx, ch1, uid)
|
||||
db.JoinChannel(ctx, ch2, uid)
|
||||
database := setupTestDB(t)
|
||||
uid, _, _ := createUserWithChannels(
|
||||
t, database, "multi", "#m1", "#m2",
|
||||
)
|
||||
|
||||
channels, err := db.GetAllChannelMembershipsForUser(ctx, uid)
|
||||
channels, err :=
|
||||
database.GetAllChannelMembershipsForUser(
|
||||
context.Background(), uid,
|
||||
)
|
||||
if err != nil || len(channels) != 2 {
|
||||
t.Fatalf("expected 2 channels, got %d", len(channels))
|
||||
t.Fatalf(
|
||||
"expected 2 channels, got %d",
|
||||
len(channels),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user