dbtest

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package dbtest provides shared database test helpers for seeding and querying test databases. It is designed to be importable from any test package without circular dependency issues (it does not import internal/query).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StrPtr

func StrPtr(s string) *string

StrPtr returns a pointer to a string (useful for optional fields in test opts).

Types

type ConversationOpts

type ConversationOpts struct {
	SourceID int64  // defaults to 1
	Title    string // defaults to "Test Thread"
}

ConversationOpts configures a conversation to insert.

type LabelOpts

type LabelOpts struct {
	SourceID      int64  // defaults to 1
	SourceLabelID string // defaults to Name
	Name          string // required
	Type          string // defaults to "user"
}

LabelOpts configures a label to insert.

type MessageOpts

type MessageOpts struct {
	Subject        string
	SentAt         string // e.g. "2024-05-01 10:00:00"
	SizeEstimate   int
	HasAttachments bool
	FromID         int64   // participant ID for 'from' recipient; 0 = no from
	ToIDs          []int64 // participant IDs for 'to' recipients
	CcIDs          []int64 // participant IDs for 'cc' recipients
	BccIDs         []int64 // participant IDs for 'bcc' recipients
	SourceID       int64   // defaults to 1 if 0
	ConversationID int64   // defaults to 1 if 0
}

MessageOpts configures a message to insert.

type ParticipantOpts

type ParticipantOpts struct {
	Email       *string // nil = NULL; use StrPtr("x") for a value
	DisplayName *string // nil = NULL
	Domain      string
}

ParticipantOpts configures a participant to insert.

type SourceOpts

type SourceOpts struct {
	Identifier  string // defaults to "other@gmail.com"
	DisplayName string // defaults to "Other Account"
	Type        string // defaults to "gmail"
}

SourceOpts configures a source to insert.

type TestDB

type TestDB struct {
	DB *sql.DB
	T  testing.TB
	// contains filtered or unexported fields
}

TestDB wraps a *sql.DB with auto-increment counters and builder helpers for seeding test data.

func NewTestDB

func NewTestDB(t testing.TB, schemaPath string) *TestDB

NewTestDB creates an in-memory SQLite database with the production schema loaded. schemaPath is the path to schema.sql (e.g. "../store/schema.sql" from the caller's package). The FTS table is dropped so tests start without it by default.

func (*TestDB) AddConversation

func (tdb *TestDB) AddConversation(opts ConversationOpts) int64

AddConversation inserts a conversation and returns its ID.

func (*TestDB) AddLabel

func (tdb *TestDB) AddLabel(opts LabelOpts) int64

AddLabel inserts a label and returns its ID.

func (*TestDB) AddMessage

func (tdb *TestDB) AddMessage(opts MessageOpts) int64

AddMessage inserts a message with its from/to/cc recipients and returns the message ID.

func (*TestDB) AddMessageLabel

func (tdb *TestDB) AddMessageLabel(messageID, labelID int64)

AddMessageLabel associates a message with a label.

func (*TestDB) AddParticipant

func (tdb *TestDB) AddParticipant(opts ParticipantOpts) int64

AddParticipant inserts a participant and returns its ID.

func (*TestDB) AddSource

func (tdb *TestDB) AddSource(opts SourceOpts) int64

AddSource inserts a source and returns its ID.

func (*TestDB) EnableFTS

func (tdb *TestDB) EnableFTS()

EnableFTS creates the FTS5 virtual table and populates it from existing test data. Skips the test if FTS5 is not available in this SQLite build.

func (*TestDB) LastMessageID

func (tdb *TestDB) LastMessageID() int64

LastMessageID returns the ID of the most recently added message.

func (*TestDB) MarkDeletedByID

func (tdb *TestDB) MarkDeletedByID(id int64)

MarkDeletedByID marks a message as deleted by its internal ID.

func (*TestDB) MarkDeletedBySourceID

func (tdb *TestDB) MarkDeletedBySourceID(sourceID string)

MarkDeletedBySourceID marks a message as deleted by its source message ID.

func (*TestDB) MustLookupParticipant added in v0.4.0

func (tdb *TestDB) MustLookupParticipant(email string) int64

MustLookupParticipant returns the ID of the participant with the given email, failing the test if not found.

func (*TestDB) SeedStandardDataSet

func (tdb *TestDB) SeedStandardDataSet()

SeedStandardDataSet inserts the standard test data set: 1 source (test@gmail.com), 3 participants (Alice, Bob, Carol), 1 conversation, 5 messages, 3 labels, and 3 attachments.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL