Files
at-container-registry/pkg/appview/db/schema_test.go
2025-12-18 12:29:20 -06:00

93 lines
2.6 KiB
Go

package db
import (
"testing"
)
func TestSplitSQLStatements(t *testing.T) {
tests := []struct {
name string
query string
expected []string
}{
{
name: "single statement",
query: "SELECT 1",
expected: []string{"SELECT 1"},
},
{
name: "single statement with semicolon",
query: "SELECT 1;",
expected: []string{"SELECT 1"},
},
{
name: "two statements",
query: "SELECT 1; SELECT 2;",
expected: []string{"SELECT 1", "SELECT 2"},
},
{
name: "statements with comments",
query: `-- This is a comment
ALTER TABLE foo ADD COLUMN bar TEXT;
-- Another comment
UPDATE foo SET bar = 'test';`,
expected: []string{
"-- This is a comment\nALTER TABLE foo ADD COLUMN bar TEXT",
"-- Another comment\nUPDATE foo SET bar = 'test'",
},
},
{
name: "comment-only sections filtered",
query: `-- Just a comment
;
SELECT 1;`,
expected: []string{"SELECT 1"},
},
{
name: "empty query",
query: "",
expected: nil,
},
{
name: "whitespace only",
query: " \n\t ",
expected: nil,
},
{
name: "migration 0005 format",
query: `-- Add is_attestation column to track attestation manifests
-- Attestation manifests have vnd.docker.reference.type = "attestation-manifest"
ALTER TABLE manifest_references ADD COLUMN is_attestation BOOLEAN DEFAULT FALSE;
-- Mark existing unknown/unknown platforms as attestations
-- Docker BuildKit attestation manifests always have unknown/unknown platform
UPDATE manifest_references
SET is_attestation = 1
WHERE platform_os = 'unknown' AND platform_architecture = 'unknown';`,
expected: []string{
"-- Add is_attestation column to track attestation manifests\n-- Attestation manifests have vnd.docker.reference.type = \"attestation-manifest\"\nALTER TABLE manifest_references ADD COLUMN is_attestation BOOLEAN DEFAULT FALSE",
"-- Mark existing unknown/unknown platforms as attestations\n-- Docker BuildKit attestation manifests always have unknown/unknown platform\nUPDATE manifest_references\nSET is_attestation = 1\nWHERE platform_os = 'unknown' AND platform_architecture = 'unknown'",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := splitSQLStatements(tt.query)
if len(result) != len(tt.expected) {
t.Errorf("got %d statements, want %d\ngot: %v\nwant: %v",
len(result), len(tt.expected), result, tt.expected)
return
}
for i := range result {
if result[i] != tt.expected[i] {
t.Errorf("statement %d:\ngot: %q\nwant: %q", i, result[i], tt.expected[i])
}
}
})
}
}