-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
891 lines (755 loc) · 23.6 KB
/
integration_test.go
File metadata and controls
891 lines (755 loc) · 23.6 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
package main
import (
"database/sql"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
_ "modernc.org/sqlite"
)
const (
testDBSchema = `
CREATE TABLE authors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
bio TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
isbn TEXT UNIQUE NOT NULL,
author_id INTEGER NOT NULL,
category_id INTEGER NOT NULL,
published_date DATE,
pages INTEGER,
price DECIMAL(10,2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES authors(id),
FOREIGN KEY (category_id) REFERENCES categories(id)
);
CREATE TABLE book_tags (
book_id INTEGER NOT NULL,
tag_name TEXT NOT NULL,
PRIMARY KEY (book_id, tag_name),
FOREIGN KEY (book_id) REFERENCES books(id)
);
CREATE VIEW book_summary AS
SELECT
b.id,
b.title,
a.name as author_name,
c.name as category_name,
b.price
FROM books b
JOIN authors a ON b.author_id = a.id
JOIN categories c ON b.category_id = c.id;
`
sqlBoilerConfig = `
[psql]
dbname = "test_db"
host = "localhost"
port = 5432
user = "test"
pass = "test"
sslmode = "disable"
[sqlite3]
dbname = "%s"
[mysql]
dbname = "test_db"
host = "localhost"
port = 3306
user = "test"
pass = "test"
`
)
// IntegrationTestSuite holds the state for the integration tests
type IntegrationTestSuite struct {
tempDir string
projectDir string
dbPath string
modelsDir string
seedsDir string
binPath string
originalDir string
}
func TestBoilingSeedIntegration(t *testing.T) {
suite := &IntegrationTestSuite{}
// Setup
if err := suite.Setup(t); err != nil {
t.Fatalf("Setup failed: %v", err)
}
defer suite.Cleanup(t)
// Run integration tests
t.Run("DatabaseSetup", suite.TestDatabaseSetup)
t.Run("ProjectStructure", suite.TestProjectStructure)
t.Run("SQLBoilerGeneration", suite.TestSQLBoilerGeneration)
t.Run("BoilingSeedGeneration", suite.TestBoilingSeedGeneration)
t.Run("GeneratedCodeCompilation", suite.TestGeneratedCodeCompilation)
t.Run("SeederExecution", suite.TestSeederExecution)
t.Run("CustomSeederFunctions", suite.TestCustomSeederFunctions)
t.Run("ForeignKeyRelationships", suite.TestForeignKeyRelationships)
t.Run("ConfigurationOptions", suite.TestConfigurationOptions)
}
func (s *IntegrationTestSuite) Setup(t *testing.T) error {
var err error
// Remember original directory
s.originalDir, err = os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
// Create temporary directory
s.tempDir, err = os.MkdirTemp("", "boilingseed_test_*")
if err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
// Set up project structure
s.projectDir = filepath.Join(s.tempDir, "testproject")
s.dbPath = filepath.Join(s.projectDir, "test.db")
s.modelsDir = filepath.Join(s.projectDir, "models")
s.seedsDir = filepath.Join(s.projectDir, "seeds")
s.binPath = filepath.Join(s.tempDir, "boilingseed")
// Create project directory
if err := os.MkdirAll(s.projectDir, 0o755); err != nil {
return fmt.Errorf("failed to create project directory: %w", err)
}
// Build boilingseed binary
if err := s.buildBoilingSeed(); err != nil {
return fmt.Errorf("failed to build boilingseed: %w", err)
}
// Change to project directory
if err := os.Chdir(s.projectDir); err != nil {
return fmt.Errorf("failed to change to project directory: %w", err)
}
// Initialize go module
if err := s.runCommand("go", "mod", "init", "testproject"); err != nil {
return fmt.Errorf("failed to initialize go module: %w", err)
}
// Create database
if err := s.createDatabase(); err != nil {
return fmt.Errorf("failed to create database: %w", err)
}
return nil
}
func (s *IntegrationTestSuite) Cleanup(t *testing.T) {
if s.originalDir != "" {
os.Chdir(s.originalDir)
}
if s.tempDir != "" {
os.RemoveAll(s.tempDir)
}
}
func (s *IntegrationTestSuite) buildBoilingSeed() error {
cmd := exec.Command("go", "build", "-o", s.binPath, ".")
cmd.Dir = s.originalDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (s *IntegrationTestSuite) createDatabase() error {
db, err := sql.Open("sqlite", s.dbPath)
if err != nil {
return fmt.Errorf("failed to open database: %w", err)
}
defer db.Close()
if _, err := db.Exec(testDBSchema); err != nil {
return fmt.Errorf("failed to execute schema: %w", err)
}
return nil
}
func (s *IntegrationTestSuite) runCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Dir = s.projectDir
// Add Go bin directory to PATH
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = filepath.Join(os.Getenv("HOME"), "go")
}
goBin := filepath.Join(gopath, "bin")
currentPath := os.Getenv("PATH")
cmd.Env = append(os.Environ(), "PATH="+goBin+":"+currentPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (s *IntegrationTestSuite) runCommandWithOutput(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
cmd.Dir = s.projectDir
// Add Go bin directory to PATH
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = filepath.Join(os.Getenv("HOME"), "go")
}
goBin := filepath.Join(gopath, "bin")
currentPath := os.Getenv("PATH")
cmd.Env = append(os.Environ(), "PATH="+goBin+":"+currentPath)
output, err := cmd.CombinedOutput()
return string(output), err
}
func (s *IntegrationTestSuite) TestDatabaseSetup(t *testing.T) {
// Verify database exists and has correct schema
db, err := sql.Open("sqlite", s.dbPath)
if err != nil {
t.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
// Test table creation
tables := []string{"authors", "categories", "books", "book_tags"}
for _, table := range tables {
var count int
query := fmt.Sprintf("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='%s'", table)
if err := db.QueryRow(query).Scan(&count); err != nil {
t.Errorf("Failed to check table %s: %v", table, err)
}
if count != 1 {
t.Errorf("Table %s not found", table)
}
}
// Test view creation
var count int
if err := db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type='view' AND name='book_summary'").Scan(&count); err != nil {
t.Errorf("Failed to check view: %v", err)
}
if count != 1 {
t.Errorf("View book_summary not found")
}
}
func (s *IntegrationTestSuite) TestProjectStructure(t *testing.T) {
// Create sqlboiler config
configContent := fmt.Sprintf(sqlBoilerConfig, s.dbPath)
configPath := filepath.Join(s.projectDir, "sqlboiler.toml")
if err := os.WriteFile(configPath, []byte(configContent), 0o644); err != nil {
t.Fatalf("Failed to create config file: %v", err)
}
// Verify config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
t.Error("Config file was not created")
}
// Verify database file exists
if _, err := os.Stat(s.dbPath); os.IsNotExist(err) {
t.Error("Database file was not created")
}
}
func (s *IntegrationTestSuite) TestSQLBoilerGeneration(t *testing.T) {
// Add sqlboiler dependencies
if err := s.runCommand("go", "get", "github.com/aarondl/sqlboiler/v4"); err != nil {
t.Fatalf("Failed to get sqlboiler dependency: %v", err)
}
if err := s.runCommand("go", "get", "github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-sqlite3"); err != nil {
t.Fatalf("Failed to get sqlite driver: %v", err)
}
// Generate models with sqlboiler
if err := s.runCommand("sqlboiler", "sqlite3"); err != nil {
t.Fatalf("Failed to generate models: %v", err)
}
// Verify models were generated
expectedFiles := []string{
"models/authors.go",
"models/books.go",
"models/categories.go",
"models/book_tags.go",
"models/boil_queries.go",
"models/boil_table_names.go",
"models/boil_types.go",
}
for _, file := range expectedFiles {
path := filepath.Join(s.projectDir, file)
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Errorf("Expected model file %s was not generated", file)
}
}
// Verify the models compile
if err := s.runCommand("go", "mod", "tidy"); err != nil {
t.Errorf("Failed to tidy modules after model generation: %v", err)
}
if err := s.runCommand("go", "build", "./models"); err != nil {
t.Errorf("Generated models failed to compile: %v", err)
}
}
func (s *IntegrationTestSuite) TestBoilingSeedGeneration(t *testing.T) {
// Run boilingseed to generate seeders
if err := s.runCommand(s.binPath, "sqlite3"); err != nil {
t.Fatalf("Failed to generate seeders: %v", err)
}
// Verify seed files were generated
expectedFiles := []string{
"seeds/boilingseed_main.go",
"seeds/authors.go",
"seeds/books.go",
"seeds/categories.go",
"seeds/book_tags.go",
}
for _, file := range expectedFiles {
path := filepath.Join(s.projectDir, file)
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Errorf("Expected seed file %s was not generated", file)
}
}
// Verify that views are not generated as seeds (book_summary should not exist)
viewSeedPath := filepath.Join(s.projectDir, "seeds/book_summary.go")
if _, err := os.Stat(viewSeedPath); !os.IsNotExist(err) {
t.Error("View seed file should not be generated")
}
}
func (s *IntegrationTestSuite) TestGeneratedCodeCompilation(t *testing.T) {
// Add required dependencies for seeds
dependencies := []string{
"github.com/aarondl/randomize",
"github.com/lib/pq", // for database drivers in tests
}
for _, dep := range dependencies {
if err := s.runCommand("go", "get", dep); err != nil {
t.Errorf("Failed to get dependency %s: %v", dep, err)
}
}
if err := s.runCommand("go", "mod", "tidy"); err != nil {
t.Errorf("Failed to tidy modules: %v", err)
}
// Test compilation of seed package
if err := s.runCommand("go", "build", "./seeds"); err != nil {
t.Fatalf("Generated seed code failed to compile: %v", err)
}
// Create a simple test program to verify the seeder interface
testProgram := `package main
import (
"database/sql"
"fmt"
"log"
_ "modernc.org/sqlite"
"testproject/seeds"
)
func main() {
db, err := sql.Open("sqlite", "test.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
seeder := seeds.Seeder{
MinAuthorsToSeed: 1,
MinCategoriesToSeed: 1,
MinBooksToSeed: 1,
}
fmt.Printf("Seeder initialized with %d min authors\n", seeder.MinAuthorsToSeed)
}
`
testPath := filepath.Join(s.projectDir, "test_seeder.go")
if err := os.WriteFile(testPath, []byte(testProgram), 0o644); err != nil {
t.Fatalf("Failed to create test program: %v", err)
}
if err := s.runCommand("go", "build", "test_seeder.go"); err != nil {
t.Errorf("Test program failed to compile: %v", err)
}
}
func (s *IntegrationTestSuite) TestSeederExecution(t *testing.T) {
// Create test program that actually runs the seeder
testProgram := `package main
import (
"context"
"database/sql"
"fmt"
"log"
"math/rand"
"time"
_ "modernc.org/sqlite"
"testproject/seeds"
"testproject/models"
"github.com/aarondl/sqlboiler/v4/boil"
)
func main() {
rand.Seed(time.Now().UnixNano())
db, err := sql.Open("sqlite", "test.db")
if err != nil {
log.Fatal("Failed to open database:", err)
}
defer db.Close()
// Set single connection to avoid database lock issues
db.SetMaxOpenConns(1)
// Set debug mode
boil.DebugMode = true
// Clear existing data first
if _, err := db.Exec("DELETE FROM book_tags"); err != nil {
log.Fatal("Failed to clear book_tags:", err)
}
if _, err := db.Exec("DELETE FROM books"); err != nil {
log.Fatal("Failed to clear books:", err)
}
if _, err := db.Exec("DELETE FROM authors"); err != nil {
log.Fatal("Failed to clear authors:", err)
}
if _, err := db.Exec("DELETE FROM categories"); err != nil {
log.Fatal("Failed to clear categories:", err)
}
ctx := context.Background()
seeder := seeds.Seeder{
MinAuthorsToSeed: 2,
MinCategoriesToSeed: 3,
MinBooksToSeed: 5,
Retries: 10,
// Use custom random functions to avoid duplicates
RandomCategory: func() (*models.Category, error) {
return &models.Category{
Name: fmt.Sprintf("Category_%d", rand.Intn(100000)), // Ensure unique names
}, nil
},
RandomAuthor: func() (*models.Author, error) {
return &models.Author{
Name: fmt.Sprintf("Author_%d", rand.Intn(100000)),
Email: fmt.Sprintf("author_%d@example.com", rand.Intn(100000)),
}, nil
},
RandomBook: func() (*models.Book, error) {
return &models.Book{
Title: fmt.Sprintf("Book_%d", rand.Intn(100000)),
Isbn: fmt.Sprintf("ISBN-%d", rand.Intn(100000000)), // Ensure unique ISBN
// Don't set foreign keys here - let the seeder handle them
}, nil
},
}
fmt.Println("Starting seeder...")
if err := seeder.Run(ctx, db); err != nil {
log.Fatal("Seeder failed:", err)
}
fmt.Println("Seeding completed successfully!")
// Verify data was inserted
authorCount, err := models.Authors().Count(ctx, db)
if err != nil {
log.Fatal("Failed to count authors:", err)
}
fmt.Printf("Authors created: %d\n", authorCount)
categoryCount, err := models.Categories().Count(ctx, db)
if err != nil {
log.Fatal("Failed to count categories:", err)
}
fmt.Printf("Categories created: %d\n", categoryCount)
bookCount, err := models.Books().Count(ctx, db)
if err != nil {
log.Fatal("Failed to count books:", err)
}
fmt.Printf("Books created: %d\n", bookCount)
if authorCount < 2 {
log.Fatal("Expected at least 2 authors")
}
if categoryCount < 3 {
log.Fatal("Expected at least 3 categories")
}
if bookCount < 5 {
log.Fatal("Expected at least 5 books")
}
}
`
testPath := filepath.Join(s.projectDir, "run_seeder.go")
if err := os.WriteFile(testPath, []byte(testProgram), 0o644); err != nil {
t.Fatalf("Failed to create seeder test program: %v", err)
}
// Get additional dependencies
if err := s.runCommand("go", "get", "github.com/aarondl/null/v8"); err != nil {
t.Errorf("Failed to get null dependency: %v", err)
}
if err := s.runCommand("go", "mod", "tidy"); err != nil {
t.Errorf("Failed to tidy modules: %v", err)
}
// Build and run the seeder test
if err := s.runCommand("go", "build", "-o", "run_seeder", "run_seeder.go"); err != nil {
t.Fatalf("Failed to build seeder test: %v", err)
}
output, err := s.runCommandWithOutput("./run_seeder")
if err != nil {
t.Fatalf("Failed to run seeder: %v\nOutput: %s", err, output)
}
if !strings.Contains(output, "Seeding completed successfully!") {
t.Error("Seeder did not complete successfully")
}
// Verify the output contains expected counts
expectedStrings := []string{
"Authors created:",
"Categories created:",
"Books created:",
}
for _, expected := range expectedStrings {
if !strings.Contains(output, expected) {
t.Errorf("Expected output to contain '%s', but it didn't. Output: %s", expected, output)
}
}
}
func (s *IntegrationTestSuite) TestCustomSeederFunctions(t *testing.T) {
// Create test with custom seeder functions
testProgram := `package main
import (
"context"
"database/sql"
"fmt"
"log"
"time"
_ "modernc.org/sqlite"
"testproject/seeds"
"testproject/models"
"github.com/aarondl/null/v8"
)
func main() {
db, err := sql.Open("sqlite", "test.db")
if err != nil {
log.Fatal("Failed to open database:", err)
}
defer db.Close()
// Set single connection to avoid database lock issues
db.SetMaxOpenConns(1)
// Clear existing data
if _, err := db.Exec("DELETE FROM book_tags"); err != nil {
log.Fatal("Failed to clear book_tags:", err)
}
if _, err := db.Exec("DELETE FROM books"); err != nil {
log.Fatal("Failed to clear books:", err)
}
if _, err := db.Exec("DELETE FROM authors"); err != nil {
log.Fatal("Failed to clear authors:", err)
}
if _, err := db.Exec("DELETE FROM categories"); err != nil {
log.Fatal("Failed to clear categories:", err)
}
ctx := context.Background()
seeder := seeds.Seeder{
MinAuthorsToSeed: 1,
MinCategoriesToSeed: 1,
MinBooksToSeed: 1,
// Custom author generator
RandomAuthor: func() (*models.Author, error) {
author := &models.Author{
Name: "Custom Author",
Email: fmt.Sprintf("custom%d@example.com", randomInt()),
Bio: null.StringFrom("This is a custom author bio"),
}
return author, nil
},
// After authors added callback
AfterAuthorsAdded: func(ctx context.Context) error {
fmt.Println("Custom callback: Authors have been added!")
return nil
},
}
fmt.Println("Running seeder with custom functions...")
if err := seeder.Run(ctx, db); err != nil {
log.Fatal("Seeder failed:", err)
}
// Verify custom author was created
authors, err := models.Authors().All(ctx, db)
if err != nil {
log.Fatal("Failed to get authors:", err)
}
if len(authors) == 0 {
log.Fatal("No authors found")
}
customAuthorFound := false
for _, author := range authors {
if author.Name == "Custom Author" {
customAuthorFound = true
fmt.Printf("Found custom author: %s (%s)\n", author.Name, author.Email)
}
}
if !customAuthorFound {
log.Fatal("Custom author not found")
}
fmt.Println("Custom seeder functions test passed!")
}
func randomInt() int {
return int(time.Now().UnixNano() % 10000)
}
`
testPath := filepath.Join(s.projectDir, "custom_seeder.go")
if err := os.WriteFile(testPath, []byte(testProgram), 0o644); err != nil {
t.Fatalf("Failed to create custom seeder test: %v", err)
}
if err := s.runCommand("go", "build", "-o", "custom_seeder", "custom_seeder.go"); err != nil {
t.Fatalf("Failed to build custom seeder test: %v", err)
}
output, err := s.runCommandWithOutput("./custom_seeder")
if err != nil {
t.Fatalf("Failed to run custom seeder: %v\nOutput: %s", err, output)
}
if !strings.Contains(output, "Custom callback: Authors have been added!") {
t.Error("Custom callback was not executed")
}
if !strings.Contains(output, "Found custom author: Custom Author") {
t.Error("Custom author function was not used")
}
}
func (s *IntegrationTestSuite) TestForeignKeyRelationships(t *testing.T) {
// Test that foreign key relationships are properly handled
testProgram := `package main
import (
"context"
"database/sql"
"fmt"
"log"
"math/rand"
"time"
_ "modernc.org/sqlite"
"testproject/seeds"
"testproject/models"
"github.com/aarondl/null/v8"
)
func main() {
rand.Seed(time.Now().UnixNano())
db, err := sql.Open("sqlite", "test.db")
if err != nil {
log.Fatal("Failed to open database:", err)
}
defer db.Close()
// Set single connection to avoid database lock issues
db.SetMaxOpenConns(1)
// Clear existing data
if _, err := db.Exec("DELETE FROM book_tags"); err != nil {
log.Fatal("Failed to clear book_tags:", err)
}
if _, err := db.Exec("DELETE FROM books"); err != nil {
log.Fatal("Failed to clear books:", err)
}
if _, err := db.Exec("DELETE FROM authors"); err != nil {
log.Fatal("Failed to clear authors:", err)
}
if _, err := db.Exec("DELETE FROM categories"); err != nil {
log.Fatal("Failed to clear categories:", err)
}
ctx := context.Background()
seeder := seeds.Seeder{
MinAuthorsToSeed: 3,
MinCategoriesToSeed: 2,
MinBooksToSeed: 5,
// Add custom random functions to prevent UNIQUE constraint violations
RandomCategory: func() (*models.Category, error) {
return &models.Category{
Name: fmt.Sprintf("Category_%d", rand.Intn(100000)),
}, nil
},
RandomAuthor: func() (*models.Author, error) {
return &models.Author{
Name: fmt.Sprintf("Author_%d", rand.Intn(100000)),
Email: fmt.Sprintf("author_%d@example.com", rand.Intn(100000)),
}, nil
},
RandomBook: func() (*models.Book, error) {
return &models.Book{
Title: fmt.Sprintf("Book_%d", rand.Intn(100000)),
Isbn: fmt.Sprintf("ISBN-%d", rand.Intn(100000000)),
// Don't set foreign keys here - let the seeder handle them
}, nil
},
}
fmt.Println("Testing foreign key relationships...")
if err := seeder.Run(ctx, db); err != nil {
log.Fatal("Seeder failed:", err)
}
// Verify that all books have valid foreign keys
books, err := models.Books().All(ctx, db)
if err != nil {
log.Fatal("Failed to load books:", err)
}
fmt.Printf("Found %d books\n", len(books))
for _, book := range books {
fmt.Printf("Book: %s (AuthorID: %d, CategoryID: %d)\n", book.Title, book.AuthorID, book.CategoryID)
// Check if foreign keys are valid (non-zero)
if book.AuthorID == 0 {
log.Fatal("Book has invalid author ID (0)")
}
if book.CategoryID == 0 {
log.Fatal("Book has invalid category ID (0)")
}
// Verify the foreign key references exist by manually querying
author, err := models.FindAuthor(ctx, db, null.Int64From(book.AuthorID))
if err != nil {
log.Fatalf("Failed to find author with ID %d: %v", book.AuthorID, err)
}
category, err := models.FindCategory(ctx, db, null.Int64From(book.CategoryID))
if err != nil {
log.Fatalf("Failed to find category with ID %d: %v", book.CategoryID, err)
}
fmt.Printf("Verified relationship: Book '%s' by '%s' in category '%s'\n",
book.Title, author.Name, category.Name)
}
fmt.Println("Foreign key relationships test passed!")
fmt.Println("Foreign key relationships test passed!")
}
`
testPath := filepath.Join(s.projectDir, "fk_demo.go")
if err := os.WriteFile(testPath, []byte(testProgram), 0o644); err != nil {
t.Fatalf("Failed to create FK test: %v", err)
}
output, err := s.runCommandWithOutput("go", "run", "fk_demo.go")
if err != nil {
t.Fatalf("Failed to run FK test: %v\nOutput: %s", err, output)
}
if !strings.Contains(output, "Foreign key relationships test passed!") {
t.Error("Foreign key relationships test failed")
}
// Should see books with author and category info
if !strings.Contains(output, "Book:") || !strings.Contains(output, "by") || !strings.Contains(output, "in category") {
t.Error("Expected to see book relationship information in output")
}
}
func (s *IntegrationTestSuite) TestConfigurationOptions(t *testing.T) {
// Test different configuration options
customOutputDir := filepath.Join(s.projectDir, "custom_seeds")
// Test custom output directory and package name
if err := s.runCommand(s.binPath,
"-o", "custom_seeds",
"-p", "customseeds",
"sqlite3"); err != nil {
t.Fatalf("Failed to generate with custom config: %v", err)
}
// Verify custom output directory was created
if _, err := os.Stat(customOutputDir); os.IsNotExist(err) {
t.Error("Custom output directory was not created")
}
// Verify files were generated in custom directory
expectedFiles := []string{
"boilingseed_main.go",
"authors.go",
"books.go",
"categories.go",
}
for _, file := range expectedFiles {
path := filepath.Join(customOutputDir, file)
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Errorf("Expected file %s was not generated in custom directory", file)
}
}
// Verify package name was changed
mainFile := filepath.Join(customOutputDir, "boilingseed_main.go")
content, err := os.ReadFile(mainFile)
if err != nil {
t.Fatalf("Failed to read main file: %v", err)
}
if !strings.Contains(string(content), "package customseeds") {
t.Error("Package name was not changed to customseeds")
}
// Test wipe option
// First create a dummy file in the directory
dummyFile := filepath.Join(customOutputDir, "dummy.txt")
if err := os.WriteFile(dummyFile, []byte("dummy"), 0o644); err != nil {
t.Fatalf("Failed to create dummy file: %v", err)
}
// Run with wipe option
if err := s.runCommand(s.binPath,
"-o", "custom_seeds",
"-p", "customseeds",
"--wipe",
"sqlite3"); err != nil {
t.Fatalf("Failed to generate with wipe option: %v", err)
}
// Verify dummy file was removed
if _, err := os.Stat(dummyFile); !os.IsNotExist(err) {
t.Error("Dummy file was not removed by wipe option")
}
// Verify seed files were still generated
if _, err := os.Stat(mainFile); os.IsNotExist(err) {
t.Error("Main file was not regenerated after wipe")
}
}