summaryrefslogtreecommitdiff
path: root/internal/store/availability_test.go
blob: ef3500cec78463c897c48676581c3664c192ba59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package store

import (
	"database/sql"
	"errors"
	"path/filepath"
	"testing"

	_ "github.com/mattn/go-sqlite3"
)

// newAvailabilityTestStore creates a Store backed by a fresh temp sqlite DB
// with just the availability_blocks table.
func newAvailabilityTestStore(t *testing.T) *Store {
	t.Helper()
	dbPath := filepath.Join(t.TempDir(), "test.db")
	db, err := sql.Open("sqlite3", dbPath)
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	if _, err := db.Exec(`
		CREATE TABLE availability_blocks (
			id TEXT PRIMARY KEY,
			weekday INTEGER NOT NULL,
			start_time TEXT NOT NULL,
			end_time TEXT NOT NULL,
			label TEXT DEFAULT ''
		)
	`); err != nil {
		t.Fatal(err)
	}
	return &Store{db: db}
}

func TestCreateAvailabilityBlock_ReturnsCreatedBlock(t *testing.T) {
	s := newAvailabilityTestStore(t)

	block, err := s.CreateAvailabilityBlock(1, "18:00", "20:00", "evening focus")
	if err != nil {
		t.Fatalf("CreateAvailabilityBlock: %v", err)
	}
	if block.ID == "" {
		t.Error("expected a generated ID")
	}
	if block.Weekday != 1 || block.StartTime != "18:00" || block.EndTime != "20:00" || block.Label != "evening focus" {
		t.Errorf("block = %+v, unexpected field values", block)
	}
}

func TestGetAvailabilityBlocks_ReturnsAllOrderedByWeekday(t *testing.T) {
	s := newAvailabilityTestStore(t)

	if _, err := s.CreateAvailabilityBlock(3, "09:00", "10:00", ""); err != nil {
		t.Fatal(err)
	}
	if _, err := s.CreateAvailabilityBlock(1, "18:00", "20:00", ""); err != nil {
		t.Fatal(err)
	}

	blocks, err := s.GetAvailabilityBlocks()
	if err != nil {
		t.Fatalf("GetAvailabilityBlocks: %v", err)
	}
	if len(blocks) != 2 {
		t.Fatalf("len(blocks) = %d, want 2", len(blocks))
	}
	if blocks[0].Weekday != 1 || blocks[1].Weekday != 3 {
		t.Errorf("expected weekday-ascending order, got %d then %d", blocks[0].Weekday, blocks[1].Weekday)
	}
}

func TestDeleteAvailabilityBlock_RemovesIt(t *testing.T) {
	s := newAvailabilityTestStore(t)

	block, err := s.CreateAvailabilityBlock(2, "07:00", "08:00", "")
	if err != nil {
		t.Fatal(err)
	}

	if err := s.DeleteAvailabilityBlock(block.ID); err != nil {
		t.Fatalf("DeleteAvailabilityBlock: %v", err)
	}

	blocks, err := s.GetAvailabilityBlocks()
	if err != nil {
		t.Fatal(err)
	}
	if len(blocks) != 0 {
		t.Errorf("expected no blocks after delete, got %d", len(blocks))
	}
}

func TestDeleteAvailabilityBlock_UnknownID_ReturnsErrNotFound(t *testing.T) {
	s := newAvailabilityTestStore(t)

	err := s.DeleteAvailabilityBlock("does-not-exist")
	if !errors.Is(err, ErrNativeTaskNotFound) {
		t.Errorf("err = %v, want ErrNativeTaskNotFound", err)
	}
}