Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

2.6 KB
Raw Blame
package tidb

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/require"
)

// TestClassifyOmniParseError pins the dispatcher fallback classifier
// against the actual omni error strings. The classifier must label each
// known Tier-4 grammar gap correctly so the
// tidb_dispatcher_omni_fallback_total{reason} counter drives the
// invariant #8 retirement gate (otherwise we ship Option B blind).
func TestClassifyOmniParseError(t *testing.T) {
	cases := []struct {
		name     string
		sql      string
		wantOK   bool   // expect parse to fail
		expected string // counter label
	}{
		{
			name:     "FLASHBACK keyword in error msg AND input",
			sql:      "FLASHBACK TABLE foo TO BEFORE DROP;",
			wantOK:   false,
			expected: "flashback",
		},
		// NOTE: BATCH non-transactional DML is now SUPPORTED by omni (grammar
		// merged in omni #157, consumed via the go.mod bump). It therefore
		// parses successfully and no longer reaches the fallback classifier, so
		// the previous "omni rejects BATCH" cases were removed. The classifier's
		// stale BATCH→batch_dml pattern in metrics.go is dead post-support and is
		// tracked for removal as part of the BATCH-support (B1.2) cleanup.
		{
			name:     "genuine syntax error → unknown (no Tier-4 keyword present)",
			sql:      "SELECT FROM WHERE;",
			wantOK:   false,
			expected: "unknown",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			_, err := ParseTiDBOmni(tc.sql)
			require.Error(t, err, "test setup expects this input to be rejected by current omni")
			got := classifyOmniParseError(err, tc.sql)
			require.Equal(t, tc.expected, got,
				"classifier must return the correct label for omni err %q + sql %q",
				err.Error(), tc.sql)
		})
	}
}

// TestClassifyOmniParseError_NilErr pins the contract that a nil error
// returns "unknown" — the classifier is called only on the fallback path
// in practice, but defensive against future refactors that might call it
// on success.
func TestClassifyOmniParseError_NilErr(t *testing.T) {
	require.Equal(t, "unknown", classifyOmniParseError(nil, "anything"))
}

// TestClassifyOmniParseError_NonOmniErr pins behavior for an error type
// that is not an *omniparser.ParseError — the haystack is just
// err.Error(), so non-omni errors get classified normally too. Useful for
// future-proofing if the dispatcher ever wraps the omni error.
func TestClassifyOmniParseError_NonOmniErr(t *testing.T) {
	plainErr := errors.New("some FLASHBACK-shaped wrapper")
	require.Equal(t, "flashback",
		classifyOmniParseError(plainErr, "irrelevant"),
		"classifier matches the haystack regardless of error type")
}