Skip to content

Commit

Permalink
more repository tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 20, 2025
1 parent 621b8e3 commit 12936f2
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 72 deletions.
1 change: 1 addition & 0 deletions internal/chunk/dbproc/repository/dbchunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (d DBChunk) values() []any {
}

type ChunkRepository interface {
// Insert should insert dbchunk into the repository and return its ID.
Insert(ctx context.Context, conn sqlx.ExtContext, dbchunk *DBChunk) (int64, error)
}

Expand Down
19 changes: 18 additions & 1 deletion internal/chunk/dbproc/repository/dbchunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

"github.com/stretchr/testify/require"

"github.com/rusq/slackdump/v3/internal/chunk"
"github.com/stretchr/testify/assert"

"github.com/rusq/slackdump/v3/internal/chunk"
)

func Test_chunkRepository_Insert(t *testing.T) {
Expand Down Expand Up @@ -47,6 +48,22 @@ func Test_chunkRepository_Insert(t *testing.T) {
want: 1,
wantErr: assert.NoError,
},
{
name: "missing session",
args: args{
ctx: context.Background(),
conn: testConn(t),
chunk: &DBChunk{
SessionID: 1,
UnixTS: 1234567890,
TypeID: chunk.CMessages,
NumRecords: 100,
Final: true,
},
},
want: 0,
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/chunk/dbproc/repository/dbfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewDBFile(chunkID int64, idx int, channelID, threadTS string, parentMsgTS s
Index: idx,
Mode: file.Mode,
Filename: orNull(file.Name != "", file.Name),
URL: orNull(file.URLPrivate != "", file.URLPrivate),
URL: orNull(file.URLPrivateDownload != "", file.URLPrivateDownload),
Data: data,
}, nil
}
Expand Down
69 changes: 69 additions & 0 deletions internal/chunk/dbproc/repository/dbfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package repository

import (
"testing"
"time"

"github.com/rusq/slack"
"github.com/stretchr/testify/assert"
)

var (
file1 = &slack.File{
ID: "FILE1",
Created: 0,
Timestamp: slack.JSONTime(time.Date(1984, 9, 16, 15, 0, 0, 0, time.UTC).Unix()),
Name: "SOKO.COM",
Title: "Classic Sokoban Game, (c) 1984 Spectrum Holobyte",
URLPrivateDownload: "https://archive.org/details/msdos_sokoban_1984_spectrum_holobyte",
NumStars: 555,
Mode: "hosted",
}

dbFile1, _ = NewDBFile(1, 0, "C1", "1631820000.000000", "1531820000.000000", file1)
)

func TestNewDBFile(t *testing.T) {
type args struct {
chunkID int64
idx int
channelID string
threadTS string
parentMsgTS string
file *slack.File
}
tests := []struct {
name string
args args
want *DBFile
wantErr bool
}{
{
"success",
args{1, 42, "C1", "1631820000.000000", "1531820000.000000", file1},
&DBFile{
ID: "FILE1",
ChunkID: 1,
ChannelID: "C1",
MessageID: ptr[int64](1531820000000000),
ThreadID: ptr[int64](1631820000000000),
Index: 42,
Mode: "hosted",
Filename: ptr("SOKO.COM"),
URL: ptr("https://archive.org/details/msdos_sokoban_1984_spectrum_holobyte"),
Data: must(marshal(file1)),
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDBFile(tt.args.chunkID, tt.args.idx, tt.args.channelID, tt.args.threadTS, tt.args.parentMsgTS, tt.args.file)
if (err != nil) != tt.wantErr {
t.Errorf("NewDBFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
}
74 changes: 71 additions & 3 deletions internal/chunk/dbproc/repository/dbmessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"encoding/json"
"iter"
"reflect"
"testing"

"github.com/jmoiron/sqlx"
"github.com/rusq/slack"
"github.com/stretchr/testify/assert"

"github.com/rusq/slackdump/v3/internal/chunk"
"github.com/rusq/slackdump/v3/internal/fixtures"
"github.com/stretchr/testify/assert"
)

func minifyJSON[T any](t *testing.T, s string) []byte {
Expand Down Expand Up @@ -392,7 +392,21 @@ func Test_messageRepository_CountThread(t *testing.T) {
want int64
wantErr bool
}{
// TODO: Add test cases.
{
name: "ok",
fields: fields{
genericRepository: genericRepository[DBMessage]{DBMessage{}},
},
args: args{
ctx: context.Background(),
conn: testConn(t),
channelID: "C123",
threadID: "123.456",
},
prepFn: threadSetupFn,
want: 4,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -515,3 +529,57 @@ func Test_messageRepository_AllForThread(t *testing.T) {
})
}
}

func TestDBMessage_Val(t *testing.T) {
type fields struct {
ID int64
ChunkID int64
ChannelID string
TS string
ParentID *int64
ThreadTS *string
IsParent bool
Index int
NumFiles int
Text string
Data []byte
}
tests := []struct {
name string
fields fields
want slack.Message
wantErr bool
}{
{
"ok",
fields(*dbmA),
msgA,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dbm := DBMessage{
ID: tt.fields.ID,
ChunkID: tt.fields.ChunkID,
ChannelID: tt.fields.ChannelID,
TS: tt.fields.TS,
ParentID: tt.fields.ParentID,
ThreadTS: tt.fields.ThreadTS,
IsParent: tt.fields.IsParent,
Index: tt.fields.Index,
NumFiles: tt.fields.NumFiles,
Text: tt.fields.Text,
Data: tt.fields.Data,
}
got, err := dbm.Val()
if (err != nil) != tt.wantErr {
t.Errorf("DBMessage.Val() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DBMessage.Val() = %v, want %v", got, tt.want)
}
})
}
}
45 changes: 45 additions & 0 deletions internal/chunk/dbproc/repository/dbsearch_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package repository

import (
"testing"

"github.com/rusq/slack"
"github.com/stretchr/testify/assert"
)

func TestNewDBSearchFile(t *testing.T) {
type args struct {
chunkID int64
n int
sf *slack.File
}
tests := []struct {
name string
args args
want *DBSearchFile
wantErr bool
}{
{
name: "creates a new DBSearchFile",
args: args{chunkID: 42, n: 50, sf: file1},
want: &DBSearchFile{
ID: 0, // autoincrement, handled by the database.
ChunkID: 42,
FileID: "FILE1",
Index: 50,
Data: must(marshal(file1)),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDBSearchFile(tt.args.chunkID, tt.args.n, tt.args.sf)
if (err != nil) != tt.wantErr {
t.Errorf("NewDBSearchFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
}
1 change: 0 additions & 1 deletion internal/chunk/dbproc/repository/dbsearch_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
type DBSearchMessage struct {
ID int64 `db:"ID"`
ChunkID int64 `db:"CHUNK_ID"`
LoadDTTM string `db:"LOAD_DTTM,omitempty"`
ChannelID string `db:"CHANNEL_ID"`
ChannelName *string `db:"CHANNEL_NAME,omitempty"`
TS string `db:"TS"`
Expand Down
67 changes: 67 additions & 0 deletions internal/chunk/dbproc/repository/dbsearch_msg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package repository

import (
"reflect"
"testing"

"github.com/rusq/slack"
)

var srchMsg1 = &slack.SearchMessage{
Type: "message",
Channel: slack.CtxChannel{
ID: "C123",
Name: "chur",
},
User: "U123",
Username: "bob",
Timestamp: "1725318212.603879",
Text: "Hello, world!",
Permalink: "http://slackdump.slack.com/archives/C123/p1725318212603879",
}

func TestNewDBSearchMessage(t *testing.T) {
type args struct {
chunkID int64
idx int
sm *slack.SearchMessage
}
tests := []struct {
name string
args args
want *DBSearchMessage
wantErr bool
}{
{
name: "creates a new DBSearchMessage",
args: args{
chunkID: 42,
idx: 50,
sm: srchMsg1,
},
want: &DBSearchMessage{
ID: 0, // autoincrement, handled by the database.
ChunkID: 42,
ChannelID: "C123",
ChannelName: ptr("chur"),
TS: "1725318212.603879",
Text: ptr("Hello, world!"),
IDX: 50,
Data: must(marshal(srchMsg1)),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDBSearchMessage(tt.args.chunkID, tt.args.idx, tt.args.sm)
if (err != nil) != tt.wantErr {
t.Errorf("NewDBSearchMessage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewDBSearchMessage() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 12936f2

Please sign in to comment.