a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
package indexer
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/zzet/gortex/internal/config"
|
|
"github.com/zzet/gortex/internal/graph"
|
|
"github.com/zzet/gortex/internal/parser"
|
|
"github.com/zzet/gortex/internal/parser/languages"
|
|
)
|
|
|
|
func newSQLTestIndexer(g graph.Store) *Indexer {
|
|
reg := parser.NewRegistry()
|
|
reg.Register(languages.NewSQLExtractor())
|
|
cfg := config.Default().Index
|
|
cfg.Workers = 2
|
|
return New(g, reg, cfg, zap.NewNop())
|
|
}
|
|
|
|
func findKindNamed(g graph.Store, kind graph.NodeKind, name string) *graph.Node {
|
|
for _, n := range g.FindNodesByName(name) {
|
|
if n.Kind == kind {
|
|
return n
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TestIndex_MigrationDDLIngestedWithSQLDomainOff verifies migration DDL is
|
|
// high-signal enough to ingest as canonical table + column graph nodes even
|
|
// though the `sql` coverage domain is off by default (it gates only the
|
|
// noisy code-side string-literal SQL).
|
|
func TestIndex_MigrationDDLIngestedWithSQLDomainOff(t *testing.T) {
|
|
dir := t.TempDir()
|
|
mig := filepath.Join(dir, "migrations")
|
|
require.NoError(t, os.MkdirAll(mig, 0o755))
|
|
writeFile(t, filepath.Join(mig, "001_init.sql"), `CREATE TABLE users (
|
|
id bigint NOT NULL,
|
|
email text NOT NULL,
|
|
PRIMARY KEY (id)
|
|
);`)
|
|
|
|
g := graph.New()
|
|
idx := newSQLTestIndexer(g)
|
|
require.False(t, idx.config.Coverage.IsEnabled("sql"), "test premise: sql domain off by default")
|
|
_, err := idx.Index(dir)
|
|
require.NoError(t, err)
|
|
|
|
if findKindNamed(g, graph.KindTable, "users") == nil {
|
|
t.Fatal("canonical KindTable node for migration table 'users' missing with sql domain off")
|
|
}
|
|
if findKindNamed(g, graph.KindMigration, "001_init.sql") == nil {
|
|
t.Error("KindMigration node missing")
|
|
}
|
|
for _, col := range []string{"id", "email"} {
|
|
if findKindNamed(g, graph.KindColumn, col) == nil {
|
|
t.Errorf("canonical KindColumn node %q missing", col)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestIndex_GeneratedSchemaIngestedAnywhere verifies a `gortex db schema`
|
|
// dump is recognised by its header marker and ingested with the real
|
|
// dialect even when saved outside a migrations/ directory.
|
|
func TestIndex_GeneratedSchemaIngestedAnywhere(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeFile(t, filepath.Join(dir, "schema.sql"), "-- Generated by `gortex db schema` from a live postgres database. Do not edit.\n\n"+
|
|
"CREATE TABLE accounts (\n id bigint NOT NULL,\n owner text NOT NULL,\n PRIMARY KEY (id)\n);\n")
|
|
|
|
g := graph.New()
|
|
idx := newSQLTestIndexer(g)
|
|
_, err := idx.Index(dir)
|
|
require.NoError(t, err)
|
|
|
|
table := findKindNamed(g, graph.KindTable, "accounts")
|
|
if table == nil {
|
|
t.Fatal("generated-schema table node 'accounts' missing (not under migrations/)")
|
|
}
|
|
if d, _ := table.Meta["dialect"].(string); d != "postgres" {
|
|
t.Errorf("dialect = %q, want postgres (from the generated header)", d)
|
|
}
|
|
if findKindNamed(g, graph.KindColumn, "owner") == nil {
|
|
t.Error("generated-schema column node 'owner' missing")
|
|
}
|
|
}
|