| 27 | ) |
| 28 | |
| 29 | func TestRun(t *testing.T) { |
| 30 | tmpDir := t.TempDir() |
| 31 | outputPath := filepath.Join(tmpDir, "schema.sql") |
| 32 | |
| 33 | // Run the function |
| 34 | if err := run(tmpDir, outputPath); err != nil { |
| 35 | t.Fatalf("run() failed: %v", err) |
| 36 | } |
| 37 | |
| 38 | // Verify schema.sql was created |
| 39 | content, err := os.ReadFile(outputPath) |
| 40 | if err != nil { |
| 41 | t.Fatalf("reading schema.sql: %v", err) |
| 42 | } |
| 43 | |
| 44 | schema := string(content) |
| 45 | |
| 46 | // Verify it has the header |
| 47 | assert.Equal(t, strings.HasPrefix(schema, "-- This is the final state"), true, "schema.sql should have header comment") |
| 48 | |
| 49 | // Verify schema contains expected tables |
| 50 | expectedTables := []string{ |
| 51 | "CREATE TABLE books", |
| 52 | "CREATE TABLE system", |
| 53 | "CREATE TABLE \"notes\"", |
| 54 | "CREATE VIRTUAL TABLE note_fts", |
| 55 | } |
| 56 | |
| 57 | for _, expected := range expectedTables { |
| 58 | assert.Equal(t, strings.Contains(schema, expected), true, fmt.Sprintf("schema should contain %s", expected)) |
| 59 | } |
| 60 | |
| 61 | // Verify schema contains triggers |
| 62 | expectedTriggers := []string{ |
| 63 | "CREATE TRIGGER notes_after_insert", |
| 64 | "CREATE TRIGGER notes_after_delete", |
| 65 | "CREATE TRIGGER notes_after_update", |
| 66 | } |
| 67 | |
| 68 | for _, expected := range expectedTriggers { |
| 69 | assert.Equal(t, strings.Contains(schema, expected), true, fmt.Sprintf("schema should contain %s", expected)) |
| 70 | } |
| 71 | |
| 72 | // Verify schema does not contain sqlite internal tables |
| 73 | assert.Equal(t, strings.Contains(schema, "sqlite_sequence"), false, "schema should not contain sqlite_sequence") |
| 74 | |
| 75 | // Verify system key-value pairs for schema versions are present |
| 76 | expectedSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemSchema) |
| 77 | assert.Equal(t, strings.Contains(schema, expectedSchemaKey), true, "schema should contain schema version INSERT statement") |
| 78 | |
| 79 | expectedRemoteSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemRemoteSchema) |
| 80 | assert.Equal(t, strings.Contains(schema, expectedRemoteSchemaKey), true, "schema should contain remote_schema version INSERT statement") |
| 81 | } |