Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package internaldto_test

import (
"bytes"
"errors"
"io"
"testing"

"github.com/stackql/any-sdk/pkg/dto"
"github.com/stackql/stackql/internal/stackql/internal_data_transfer/internaldto"
)

func TestNewBasicPrimitiveContext(t *testing.T) {
var w, ew bytes.Buffer
authFn := func(string) (*dto.AuthCtx, error) { return &dto.AuthCtx{}, nil }

ctx := internaldto.NewBasicPrimitiveContext(authFn, &w, &ew)
if ctx == nil {
t.Fatal("NewBasicPrimitiveContext returned nil")
}
}

func TestBasicPrimitiveContext_GetWriter(t *testing.T) {
var buf bytes.Buffer
authFn := func(string) (*dto.AuthCtx, error) { return &dto.AuthCtx{}, nil }

ctx := internaldto.NewBasicPrimitiveContext(authFn, &buf, io.Discard)
if got := ctx.GetWriter(); got != &buf {
t.Error("GetWriter() did not return the expected writer")
}
}

func TestBasicPrimitiveContext_GetErrWriter(t *testing.T) {
var buf bytes.Buffer
authFn := func(string) (*dto.AuthCtx, error) { return &dto.AuthCtx{}, nil }

ctx := internaldto.NewBasicPrimitiveContext(authFn, io.Discard, &buf)
if got := ctx.GetErrWriter(); got != &buf {
t.Error("GetErrWriter() did not return the expected writer")
}
}

func TestBasicPrimitiveContext_GetWriter_NilWriter(t *testing.T) {
authFn := func(string) (*dto.AuthCtx, error) { return &dto.AuthCtx{}, nil }

ctx := internaldto.NewBasicPrimitiveContext(authFn, nil, nil)
if got := ctx.GetWriter(); got != nil {
t.Errorf("GetWriter() = %v, want nil", got)
}
if got := ctx.GetErrWriter(); got != nil {
t.Errorf("GetErrWriter() = %v, want nil", got)
}
}

func TestBasicPrimitiveContext_GetAuthContext_Success(t *testing.T) {
expected := &dto.AuthCtx{}
authFn := func(prov string) (*dto.AuthCtx, error) {
if prov != "test-provider" {
t.Errorf("GetAuthContext() called with %q, want 'test-provider'", prov)
}
return expected, nil
}

ctx := internaldto.NewBasicPrimitiveContext(authFn, io.Discard, io.Discard)
got, err := ctx.GetAuthContext("test-provider")
if err != nil {
t.Fatalf("GetAuthContext() error = %v", err)
}
if got != expected {
t.Errorf("GetAuthContext() = %v, want %v", got, expected)
}
}

func TestBasicPrimitiveContext_GetAuthContext_Error(t *testing.T) {
wantErr := errors.New("auth failed")
authFn := func(string) (*dto.AuthCtx, error) {
return nil, wantErr
}

ctx := internaldto.NewBasicPrimitiveContext(authFn, io.Discard, io.Discard)
_, err := ctx.GetAuthContext("provider")
if !errors.Is(err, wantErr) {
t.Errorf("GetAuthContext() error = %v, want %v", err, wantErr)
}
}

func TestBasicPrimitiveContext_GetAuthContext_DifferentProviders(t *testing.T) {
calls := make([]string, 0)
authFn := func(prov string) (*dto.AuthCtx, error) {
calls = append(calls, prov)
return &dto.AuthCtx{}, nil
}

ctx := internaldto.NewBasicPrimitiveContext(authFn, io.Discard, io.Discard)
ctx.GetAuthContext("provider-a")
ctx.GetAuthContext("provider-b")
ctx.GetAuthContext("provider-c")

if len(calls) != 3 {
t.Errorf("GetAuthContext() called %d times, want 3", len(calls))
}
if calls[0] != "provider-a" || calls[1] != "provider-b" || calls[2] != "provider-c" {
t.Errorf("providers = %v, want [provider-a, provider-b, provider-c]", calls)
}
}
147 changes: 147 additions & 0 deletions internal/stackql/iqlerror/iqlerror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package iqlerror_test

import (
"bytes"
"strings"
"testing"

"github.com/stackql/stackql/internal/stackql/iqlerror"
)

func TestGetStatementNotSupportedError(t *testing.T) {
err := iqlerror.GetStatementNotSupportedError("SELECT")
if err == nil {
t.Fatal("GetStatementNotSupportedError returned nil")
}
msg := err.Error()
if !strings.Contains(msg, "SELECT") {
t.Errorf("error message should contain 'SELECT', got %q", msg)
}
if !strings.Contains(msg, "not yet supported") {
t.Errorf("error message should contain 'not yet supported', got %q", msg)
}
}

func TestGetStatementNotSupportedError_EmptyStatement(t *testing.T) {
err := iqlerror.GetStatementNotSupportedError("")
if err == nil {
t.Fatal("GetStatementNotSupportedError returned nil")
}
msg := err.Error()
if !strings.Contains(msg, "not yet supported") {
t.Errorf("error message should contain 'not yet supported', got %q", msg)
}
}

func TestGetStatementNotSupportedError_SpecialChars(t *testing.T) {
err := iqlerror.GetStatementNotSupportedError("INSERT INTO")
if err == nil {
t.Fatal("GetStatementNotSupportedError returned nil")
}
msg := err.Error()
if !strings.Contains(msg, "INSERT INTO") {
t.Errorf("error message should contain 'INSERT INTO', got %q", msg)
}
}

func TestHandlePanic_NoPanic(t *testing.T) {
var buf bytes.Buffer
defer func() {
if r := recover(); r != nil {
t.Errorf("HandlePanic should not panic when there is no panic: %v", r)
}
if buf.Len() > 0 {
t.Errorf("HandlePanic should not write anything when there is no panic, wrote: %q", buf.String())
}
}()

iqlerror.HandlePanic(&buf)
}

func TestHandlePanic_WithPanic(t *testing.T) {
var buf bytes.Buffer
// Deferred recover only runs when HandlePanic does NOT recover from the panic.
// If HandlePanic works correctly, it calls recover() first and returns normally,
// so this deferred function never sees a panic.
defer func() {
if r := recover(); r != nil {
t.Fatalf("panic was not recovered by HandlePanic: %v", r)
}
}()
// Deferred HandlePanic runs during panic unwinding and calls recover() to stop it.
defer iqlerror.HandlePanic(&buf)
panic("test panic")
}

func TestHandlePanic_WithNilWriter(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("HandlePanic should have panicked when there was no panic")
}
if r.(string) != "nil writer test" {
t.Errorf("panic value = %v, want 'nil writer test'", r)
}
}()

// Should panic even with nil writer
iqlerror.HandlePanic(nil)
panic("nil writer test")
}

func TestHandlePanic_WithIntegerPanic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("HandlePanic should have panicked")
}
if r.(int) != 42 {
t.Errorf("panic value = %v, want 42", r)
}
}()

iqlerror.HandlePanic(&bytes.Buffer{})
panic(42)
}

func TestHandlePanic_WithErrorPanic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("HandlePanic should have panicked")
}
}()

var buf bytes.Buffer
iqlerror.HandlePanic(&buf)
panic("oops")
}

func TestHandlePanic_WritesMessageToWriter(t *testing.T) {
var buf bytes.Buffer
defer func() {
recover() // stop panic
}()

defer iqlerror.HandlePanic(&buf)
panic("buffer test")

// This line is unreachable after panic
}

func TestHandlePanic_WritesMessageFormat(t *testing.T) {
var buf bytes.Buffer
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("unexpected panic: %v", r)
}
}()
defer iqlerror.HandlePanic(&buf)
panic("format test")
}()

if !strings.Contains(buf.String(), "format test") {
t.Errorf("HandlePanic wrote %q, expected it to contain 'format test'", buf.String())
}
}
126 changes: 126 additions & 0 deletions internal/stackql/kstore/kstore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package kstore_test

import (
"testing"

"github.com/stackql/any-sdk/pkg/dto"
"github.com/stackql/stackql/internal/stackql/kstore"
)

func TestGetKStore_ReturnsNonNil(t *testing.T) {
ks, err := kstore.GetKStore(dto.KStoreCfg{})
if err != nil {
t.Fatalf("GetKStore() error = %v", err)
}
if ks == nil {
t.Fatal("GetKStore() returned nil")
}
}

func TestKStore_Put(t *testing.T) {
// Use a key that is unlikely to be used by other tests
// (tests run alphabetically, so later tests can depend on earlier ones)
ks, _ := kstore.GetKStore(dto.KStoreCfg{})
ks.Put(9999)
ks.Del(9999) // Clean up after ourselves
}

func TestKStore_Del(t *testing.T) {
ks, _ := kstore.GetKStore(dto.KStoreCfg{})
ks.Put(8888)
ks.Del(8888)
}

func TestKStore_Min_ReturnsValue(t *testing.T) {
// Put a unique key and verify Min returns something
ks, _ := kstore.GetKStore(dto.KStoreCfg{})
uniqueKey := 7777
ks.Put(uniqueKey)
defer ks.Del(uniqueKey)

got, ok := ks.Min()
if !ok {
t.Fatal("Min() should return ok=true after Put")
}
if got != uniqueKey {
t.Errorf("Min() = %d, want %d", got, uniqueKey)
}
}

func TestKStore_PutAndDel_Deterministic(t *testing.T) {
// Put a value and verify Min works, then delete it
ks, _ := kstore.GetKStore(dto.KStoreCfg{})
testKey := 6666

// Verify Min fails before Put
_, ok := ks.Min()
if ok {
t.Log("note: Min() returned ok=true before Put (store may have pre-existing keys)")
}

ks.Put(testKey)
defer ks.Del(testKey)

got, ok := ks.Min()
if !ok {
t.Fatal("Min() should return ok=true after Put")
}
if got != testKey {
t.Errorf("Min() = %d, want %d", got, testKey)
}

ks.Del(testKey)
_, ok = ks.Min()
if ok {
t.Log("note: Min() still returns ok=true after Del (store may have other keys)")
}
}

func TestKStore_PutDuplicate_StillWorks(t *testing.T) {
ks, _ := kstore.GetKStore(dto.KStoreCfg{})
testKey := 5555
ks.Put(testKey)
defer ks.Del(testKey)

ks.Put(testKey) // Duplicate
// Should still work - no error expected
}

func TestKStore_ConcurrentOps(t *testing.T) {
ks, _ := kstore.GetKStore(dto.KStoreCfg{})
done := make(chan bool, 50)

for i := 0; i < 50; i++ {
go func(id int) {
ks.Put(10000 + id)
done <- true
}(i)
}

for i := 0; i < 50; i++ {
<-done
}

// Verify the store still works (may have pre-existing keys too)
_, ok := ks.Min()
if !ok {
t.Error("Min() should work after concurrent puts")
}

// Cleanup: delete our keys
for i := 0; i < 50; i++ {
ks.Del(10000 + i)
}
}

func TestKStore_Interface(t *testing.T) {
// Verify the KStore interface is implemented correctly
var ks kstore.KStore
ks, _ = kstore.GetKStore(dto.KStoreCfg{})

// Test that all interface methods exist and are callable
// (this is a compile-time check, but explicit is better)
_ = interface{}(ks.Put)
_ = interface{}(ks.Del)
_ = interface{}(ks.Min)
}
Loading
Loading