Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

13.5 KB
Raw Blame
package main

import (
	"context"
	"encoding/json"
	"errors"
	"reflect"
	"testing"
	"time"

	"github.com/apache/iotdb-client-go/v2/client"
)

func TestHandshakeAdvertisesNativeCapabilities(t *testing.T) {
	result, shutdown, err := newRuntimeServer().dispatch("handshake", nil)
	if err != nil || shutdown {
		t.Fatalf("unexpected handshake: shutdown=%t err=%v", shutdown, err)
	}
	capabilities := result.(map[string]any)["capabilities"].([]string)
	want := []string{
		"connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl",
		"structured_error_v1", "multi_session",
	}
	if !reflect.DeepEqual(capabilities, want) {
		t.Fatalf("unexpected capabilities: %#v", capabilities)
	}
}

func TestExecuteTransactionIsRejectedBeforeConnecting(t *testing.T) {
	server := &server{}
	_, err := server.executeStatements(map[string]json.RawMessage{
		"statements": json.RawMessage(`["INSERT INTO metrics(device, time, value) VALUES ('d1', 1, 1)"]`),
	}, true)
	if err == nil || err.Error() != "IoTDB does not support transactions" {
		t.Fatalf("expected unsupported transaction error, got %v", err)
	}
}

func TestHandleLineClassifiesMissingSession(t *testing.T) {
	response, _ := newRuntimeServer().handleLine(
		`{"jsonrpc":"2.0","id":7,"method":"validate_session","params":{"agentSessionId":"missing"}}`,
	)
	if response.Error == nil || response.Error.Data == nil {
		t.Fatalf("expected structured error: %#v", response)
	}
	if response.Error.Data.Stage != "validate" || response.Error.Data.Category != "protocol" {
		t.Fatalf("unexpected error classification: %#v", response.Error.Data)
	}
}

func TestParseConnectionConfig(t *testing.T) {
	config, err := parseConnectionConfig(connectParams{
		ConnectionString: "jdbc:iotdb://alice:secret@[::1]:7777/dbx_table?sql_dialect=table&fetch_size=2048&connect_retry_max=4",
		URLParams:        "enable_compression=true&node_urls=node1:6667,node2:6667&time_zone=UTC",
	})
	if err != nil {
		t.Fatal(err)
	}
	if config.Host != "::1" || config.Port != 7777 || config.Username != "alice" || config.Password != "secret" {
		t.Fatalf("unexpected endpoint config: %#v", config)
	}
	if config.Database != "dbx_table" || config.Dialect != client.TableSqlDialect || config.FetchSize != 2048 {
		t.Fatalf("unexpected session config: %#v", config)
	}
	if !config.EnableCompression || config.ConnectRetryMax != 4 || config.TimeZone != "UTC" {
		t.Fatalf("unexpected optional config: %#v", config)
	}
	if !reflect.DeepEqual(config.NodeURLs, []string{"node1:6667", "node2:6667"}) {
		t.Fatalf("unexpected cluster nodes: %#v", config.NodeURLs)
	}
}

func TestParseConnectionConfigCompressionAliases(t *testing.T) {
	tests := []struct {
		name             string
		connectionString string
		urlParams        string
		want             bool
	}{
		{name: "default disabled"},
		{name: "documented enabled", urlParams: "enable_compression=true", want: true},
		{name: "documented disabled", urlParams: "enable_compression=false"},
		{name: "existing alias enabled", urlParams: "rpc_compression=true", want: true},
		{name: "existing alias disabled", urlParams: "rpc_compression=false"},
		{name: "JDBC alias enabled in URL parameters", urlParams: "rpc_compress=true", want: true},
		{name: "JDBC alias false", urlParams: "rpc_compress=false"},
		{
			name:             "JDBC alias enabled in connection string",
			connectionString: "jdbc:iotdb://localhost:6667/?rpc_compress=true",
			want:             true,
		},
		{
			name:             "URL parameters override same connection string key",
			connectionString: "jdbc:iotdb://localhost:6667/?rpc_compress=true",
			urlParams:        "rpc_compress=false",
		},
		{
			name:      "documented option takes precedence",
			urlParams: "enable_compression=false&rpc_compression=true&rpc_compress=true",
		},
		{
			name:      "existing alias takes precedence over JDBC alias",
			urlParams: "rpc_compression=false&rpc_compress=true",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			config, err := parseConnectionConfig(connectParams{
				ConnectionString: test.connectionString,
				URLParams:        test.urlParams,
			})
			if err != nil {
				t.Fatal(err)
			}
			if config.EnableCompression != test.want {
				t.Fatalf("EnableCompression = %t, want %t", config.EnableCompression, test.want)
			}
		})
	}
}

func TestBundledTimeZoneDatabase(t *testing.T) {
	dataSet, err := client.NewIoTDBRpcDataSet(
		"", nil, nil, nil, true, false, 0, 0, nil, 0, nil, 0, nil,
		"Asia/Shanghai", "default", 1000, nil,
	)
	if err != nil {
		t.Fatal(err)
	}
	if dataSet == nil {
		t.Fatal("expected IoTDB dataset with bundled timezone data")
	}
	if _, err := client.NewIoTDBRpcDataSet(
		"", nil, nil, nil, true, false, 0, 0, nil, 0, nil, 0, nil,
		"Invalid/Zone", "default", 1000, nil,
	); err == nil {
		t.Fatal("expected invalid timezone to remain rejected")
	}
}

func TestParseConnectionConfigTLS(t *testing.T) {
	config, err := parseConnectionConfig(connectParams{
		Host: "iotdb.example.com", SSL: true, CACertPath: "/tmp/ca.pem",
		ClientCertPath: "/tmp/client.pem", ClientKeyPath: "/tmp/client.key",
		URLParams: "insecure_skip_verify=true",
	})
	if err != nil {
		t.Fatal(err)
	}
	if config.TLSConfig == nil || config.TLSConfig.CAFile != "/tmp/ca.pem" || !config.TLSInsecureSkipVerify {
		t.Fatalf("unexpected TLS config: %#v", config.TLSConfig)
	}
	if _, err := parseConnectionConfig(connectParams{SSL: true, ClientCertPath: "/tmp/client.pem"}); err == nil {
		t.Fatal("expected incomplete mTLS configuration to fail")
	}
	if _, err := parseConnectionConfig(connectParams{URLParams: "sql_dialect=relational"}); err == nil {
		t.Fatal("expected unsupported SQL dialect to fail")
	}
}

func TestMetadataHelpers(t *testing.T) {
	values := []string{"alpha", "beta", "gamma"}
	if got := applyMetadataWindow(values, 1, 1); !reflect.DeepEqual(got, []string{"beta"}) {
		t.Fatalf("unexpected metadata window: %#v", got)
	}
	if !metadataNameMatches("DeviceOne", "vice") || metadataNameMatches("DeviceOne", "table") {
		t.Fatal("unexpected metadata name matching")
	}
	if got := treeDevicePath("root.db", "root.db.d1"); got != "root.db.d1" {
		t.Fatalf("treeDevicePath() = %q", got)
	}
	if got := quoteTreePath("root.db.with-dash"); got != "root.db.`with-dash`" {
		t.Fatalf("quoteTreePath() = %q", got)
	}
	if got := quoteTableIdentifier(`a"b`); got != `"a""b"` {
		t.Fatalf("quoteTableIdentifier() = %q", got)
	}
}

func TestNormalizeIoTDBValues(t *testing.T) {
	when := time.Date(2026, time.August, 10, 12, 34, 56, 123, time.FixedZone("CST", 8*60*60))
	if got := normalizeIoTDBValue(when, "DATETIME"); got != "2026-08-10T12:34:56.000000123+08:00" {
		t.Fatalf("unexpected datetime: %#v", got)
	}
	if got := normalizeIoTDBValue(when, "DATE"); got != "2026-08-10" {
		t.Fatalf("unexpected date: %#v", got)
	}
	if got := normalizeIoTDBValue([]byte{0xde, 0xad}, "BLOB"); got != "dead" {
		t.Fatalf("unexpected blob: %#v", got)
	}
}

func TestTreeTimeColumnPresentation(t *testing.T) {
	columns := []string{"Time", "root.db.d1.s1"}
	for _, precision := range []string{"ms", "us", "ns"} {
		types := normalizedColumnTypes([]string{"INT64", "DOUBLE"}, columns, client.TreeSqlDialect, precision)
		want := []string{"TIMESTAMP(" + precision + ")", "DOUBLE"}
		if !reflect.DeepEqual(types, want) {
			t.Fatalf("unexpected tree column types for %s: %#v", precision, types)
		}
	}
}

func TestTableTimestampColumnPresentation(t *testing.T) {
	columns := []string{"time", "device", "event_time"}
	for _, precision := range []string{"ms", "us", "ns"} {
		got := normalizedColumnTypes([]string{"TIMESTAMP", "STRING", "TIMESTAMP"}, columns, client.TableSqlDialect, precision)
		want := []string{"TIMESTAMP(" + precision + ")", "STRING", "TIMESTAMP(" + precision + ")"}
		if !reflect.DeepEqual(got, want) {
			t.Fatalf("unexpected table column types for %s: %#v", precision, got)
		}
	}
}

func TestUnknownTimestampPrecisionDoesNotEnableFormattingMetadata(t *testing.T) {
	columns := []string{"Time", "root.db.d1.s1"}
	if got := normalizedColumnTypes([]string{"INT64", "DOUBLE"}, columns, client.TreeSqlDialect, ""); !reflect.DeepEqual(got, []string{"TIMESTAMP", "DOUBLE"}) {
		t.Fatalf("unexpected unknown-precision types: %#v", got)
	}
	for _, value := range []string{"ms", "US", " ns "} {
		if normalizeTimestampPrecision(value) == "" {
			t.Fatalf("expected supported precision %q", value)
		}
	}
	if normalizeTimestampPrecision("seconds") != "" {
		t.Fatal("unsupported precision must not be accepted")
	}
}

func TestTreeTimeColumnPresentationDoesNotRewriteOrdinaryInt64(t *testing.T) {
	columns := []string{"Time", "value"}
	if got := normalizedColumnTypes([]string{"INT64", "INT64"}, columns, client.TableSqlDialect, "ms"); !reflect.DeepEqual(got, []string{"INT64", "INT64"}) {
		t.Fatalf("unexpected table column types: %#v", got)
	}
	if got := normalizedColumnTypes([]string{"INT64", "INT64"}, []string{"value", "Time"}, client.TreeSqlDialect, "ms"); !reflect.DeepEqual(got, []string{"INT64", "INT64"}) {
		t.Fatalf("unexpected non-axis column types: %#v", got)
	}
}

func TestTreeTimeAggregationColumnsPresentAsTimestamps(t *testing.T) {
	columns := []string{
		"max_time(root.db.d1.s1)",
		"MIN_TIME(root.db.d1.s1)",
		" max_time(root.db.d1.s1) ",
		"max_by(Time,root.db.d1.s1)",
		"MIN_BY( time , root.db.d1.s1 )",
		"avg(root.db.d1.s1)",
		"count(root.db.d1.s1)",
	}
	got := normalizedColumnTypes([]string{"INT64", "INT64", "INT64", "INT64", "INT64", "DOUBLE", "INT64"}, columns, client.TreeSqlDialect, "ms")
	want := []string{"TIMESTAMP(ms)", "TIMESTAMP(ms)", "TIMESTAMP(ms)", "TIMESTAMP(ms)", "TIMESTAMP(ms)", "DOUBLE", "INT64"}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("unexpected tree aggregation types: %#v", got)
	}
	if name := normalizedColumnTypes([]string{"INT64"}, []string{"max_timer(root.db.d1.s1)"}, client.TreeSqlDialect, "ms"); !reflect.DeepEqual(name, []string{"INT64"}) {
		t.Fatalf("unrelated prefix must stay INT64: %#v", name)
	}
	if table := normalizedColumnTypes([]string{"INT64"}, []string{"max_time(t1.s1)"}, client.TableSqlDialect, "ms"); !reflect.DeepEqual(table, []string{"INT64"}) {
		t.Fatalf("table dialect must stay INT64: %#v", table)
	}
	for _, column := range []string{"max_by(s1,time)", "max_by(event_time,s1)", "max_bytes(time,s1)"} {
		if got := normalizedColumnTypes([]string{"INT64"}, []string{column}, client.TreeSqlDialect, "ms"); !reflect.DeepEqual(got, []string{"INT64"}) {
			t.Fatalf("non-time max_by result %q must stay INT64: %#v", column, got)
		}
	}
}

func TestClassifyIoTDBErrors(t *testing.T) {
	canceled := classifyRPCError("execute_query", "session-1", context.Canceled)
	if canceled.Data.Category != "canceled" || canceled.Data.SessionDisposition != "quarantine" {
		t.Fatalf("unexpected cancellation classification: %#v", canceled)
	}
	timedOut := classifyRPCError("execute_query", "session-1", context.DeadlineExceeded)
	if timedOut.Data.Category != "timeout" || timedOut.Data.SessionDisposition != "quarantine" {
		t.Fatalf("unexpected timeout classification: %#v", timedOut)
	}
}

func TestDecodeQueryOptions(t *testing.T) {
	params := map[string]json.RawMessage{
		"sql":         json.RawMessage(`"SHOW DATABASES"`),
		"maxRows":     json.RawMessage(`100`),
		"fetchSize":   json.RawMessage(`25`),
		"timeoutSecs": json.RawMessage(`5`),
	}
	options := queryOptionsFromParams(params)
	if options.SQL != "SHOW DATABASES" || options.MaxRows != 100 || options.FetchSize != 25 || options.TimeoutSecs != 5 {
		t.Fatalf("unexpected query options: %#v", options)
	}
}

func TestIsQueryStatementRecognizesListStatements(t *testing.T) {
	tests := []struct {
		sql  string
		want bool
	}{
		{sql: "LIST USER", want: true},
		{sql: "list role", want: true},
		{sql: "  LiSt PRIVILEGES OF USER root;  ", want: true},
		{sql: "\tLIST PRIVILEGES OF ROLE dbx7359_role\n", want: true},
		{sql: "SHOW VERSION", want: true},
		{sql: "SELECT * FROM root.dbx_7359.d", want: true},
		{sql: ""},
		{sql: "   ;   "},
		{sql: "INSERT INTO root.dbx_7359.d(time,s) VALUES(1,1)"},
	}

	for _, test := range tests {
		t.Run(test.sql, func(t *testing.T) {
			if got := isQueryStatement(trimStatementSQL(test.sql)); got != test.want {
				t.Fatalf("isQueryStatement(%q) = %t, want %t", test.sql, got, test.want)
			}
		})
	}
}

func TestCancelActiveOperationInvalidatesClient(t *testing.T) {
	blocking := &blockingIoTDBSession{closed: make(chan struct{})}
	connected := &sessionClient{session: blocking, dialect: client.TreeSqlDialect}
	server := &server{client: connected}
	done := make(chan error, 1)
	go func() {
		_, err := runCancelable(server, 0, connected, func(ctx context.Context) (struct{}, error) {
			<-ctx.Done()
			return struct{}{}, ctx.Err()
		})
		done <- err
	}()
	deadline := time.Now().Add(time.Second)
	for {
		server.activeMu.Lock()
		active := server.activeCancel != nil
		server.activeMu.Unlock()
		if active {
			break
		}
		if time.Now().After(deadline) {
			t.Fatal("operation never became active")
		}
		time.Sleep(time.Millisecond)
	}
	server.cancelActiveQuery()
	select {
	case err := <-done:
		if !errors.Is(err, context.Canceled) {
			t.Fatalf("runCancelable() error = %v", err)
		}
	case <-time.After(time.Second):
		t.Fatal("canceled operation did not return")
	}
	server.clientMu.Lock()
	defer server.clientMu.Unlock()
	if server.client != nil {
		t.Fatal("canceled client must be invalidated")
	}
}

type blockingIoTDBSession struct {
	closed chan struct{}
}

func (s *blockingIoTDBSession) ExecuteQueryStatement(context.Context, string, *int64) (*client.SessionDataSet, error) {
	return nil, errors.New("not implemented")
}

func (s *blockingIoTDBSession) ExecuteNonQueryStatement(context.Context, string) error {
	return errors.New("not implemented")
}

func (s *blockingIoTDBSession) Close() error {
	select {
	case <-s.closed:
	default:
		close(s.closed)
	}
	return nil
}