1+ package collections
2+
3+ import (
4+ "context"
5+ "database/sql"
6+ "testing"
7+
8+ "github.com/maniac-en/req/internal/crud"
9+ "github.com/maniac-en/req/internal/database"
10+ _ "github.com/mattn/go-sqlite3"
11+ )
12+
13+ func setupTestDB (t * testing.T ) * database.Queries {
14+ db , err := sql .Open ("sqlite3" , ":memory:" )
15+ if err != nil {
16+ t .Fatalf ("Failed to open test database: %v" , err )
17+ }
18+
19+ schema := `
20+ CREATE TABLE collections (
21+ id INTEGER PRIMARY KEY AUTOINCREMENT,
22+ name TEXT NOT NULL,
23+ created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
24+ updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
25+ );`
26+
27+ if _ , err := db .Exec (schema ); err != nil {
28+ t .Fatalf ("Failed to create test schema: %v" , err )
29+ }
30+
31+ return database .New (db )
32+ }
33+
34+ func TestCollectionsManagerCRUD (t * testing.T ) {
35+ db := setupTestDB (t )
36+ manager := NewCollectionsManager (db )
37+ ctx := context .Background ()
38+
39+ t .Run ("Create" , func (t * testing.T ) {
40+ collection , err := manager .Create (ctx , "Test Collection" )
41+ if err != nil {
42+ t .Fatalf ("Create failed: %v" , err )
43+ }
44+ if collection .GetName () != "Test Collection" {
45+ t .Errorf ("Expected name 'Test Collection', got %s" , collection .GetName ())
46+ }
47+ if collection .GetID () <= 0 {
48+ t .Errorf ("Expected positive ID, got %d" , collection .GetID ())
49+ }
50+ })
51+
52+ t .Run ("Read" , func (t * testing.T ) {
53+ created , _ := manager .Create (ctx , "Read Test" )
54+ collection , err := manager .Read (ctx , created .GetID ())
55+ if err != nil {
56+ t .Fatalf ("Read failed: %v" , err )
57+ }
58+ if collection .GetName () != "Read Test" {
59+ t .Errorf ("Expected name 'Read Test', got %s" , collection .GetName ())
60+ }
61+ })
62+
63+ t .Run ("Update" , func (t * testing.T ) {
64+ created , _ := manager .Create (ctx , "Update Test" )
65+ updated , err := manager .Update (ctx , created .GetID (), "Updated Name" )
66+ if err != nil {
67+ t .Fatalf ("Update failed: %v" , err )
68+ }
69+ if updated .GetName () != "Updated Name" {
70+ t .Errorf ("Expected name 'Updated Name', got %s" , updated .GetName ())
71+ }
72+ })
73+
74+ t .Run ("Delete" , func (t * testing.T ) {
75+ created , _ := manager .Create (ctx , "Delete Test" )
76+ err := manager .Delete (ctx , created .GetID ())
77+ if err != nil {
78+ t .Fatalf ("Delete failed: %v" , err )
79+ }
80+ _ , err = manager .Read (ctx , created .GetID ())
81+ if err != crud .ErrNotFound {
82+ t .Errorf ("Expected ErrNotFound after delete, got %v" , err )
83+ }
84+ })
85+
86+ t .Run ("List" , func (t * testing.T ) {
87+ manager .Create (ctx , "List Test 1" )
88+ manager .Create (ctx , "List Test 2" )
89+ collections , err := manager .List (ctx )
90+ if err != nil {
91+ t .Fatalf ("List failed: %v" , err )
92+ }
93+ if len (collections ) < 2 {
94+ t .Errorf ("Expected at least 2 collections, got %d" , len (collections ))
95+ }
96+ })
97+ }
98+
99+ func TestCollectionsManagerValidation (t * testing.T ) {
100+ db := setupTestDB (t )
101+ manager := NewCollectionsManager (db )
102+ ctx := context .Background ()
103+
104+ t .Run ("Create with empty name" , func (t * testing.T ) {
105+ _ , err := manager .Create (ctx , "" )
106+ if err != crud .ErrInvalidInput {
107+ t .Errorf ("Expected ErrInvalidInput, got %v" , err )
108+ }
109+ })
110+
111+ t .Run ("Read with invalid ID" , func (t * testing.T ) {
112+ _ , err := manager .Read (ctx , - 1 )
113+ if err != crud .ErrInvalidInput {
114+ t .Errorf ("Expected ErrInvalidInput, got %v" , err )
115+ }
116+ })
117+
118+ t .Run ("Read non-existent" , func (t * testing.T ) {
119+ _ , err := manager .Read (ctx , 99999 )
120+ if err != crud .ErrNotFound {
121+ t .Errorf ("Expected ErrNotFound, got %v" , err )
122+ }
123+ })
124+ }
0 commit comments