package cql

import (
	"bytes"
	"testing"

	"github.com/everything-on-mysql/cassandra-on-mysql/pkg/cql/executor"
)

func TestMapPositionalValuesUnset(t *testing.T) {
	values := []QueryValue{
		{Name: "", Value: []byte("a")},
		{Name: "", Unset: true},
		{Name: "", Value: nil},
	}
	params := mapPositionalValuesToParams(values)
	if len(params) != 3 {
		t.Fatalf("expected 3 params, got %d", len(params))
	}
	if got, ok := params[0].([]byte); !ok || !bytes.Equal(got, []byte("a")) {
		t.Fatalf("expected first param 'a', got %#v", params[0])
	}
	if params[1] != executor.UnsetValue {
		t.Fatalf("expected second param unset sentinel, got %#v", params[1])
	}
	if params[2] != nil {
		t.Fatalf("expected third param nil, got %#v", params[2])
	}
}

func TestMapNamedValuesUnset(t *testing.T) {
	order := []string{"a", "b"}
	values := []QueryValue{
		{Name: "b", Unset: true},
		{Name: "a", Value: []byte("x")},
	}
	params, err := mapNamedValuesToParams(order, values)
	if err != nil {
		t.Fatalf("mapNamedValuesToParams failed: %v", err)
	}
	if len(params) != 2 {
		t.Fatalf("expected 2 params, got %d", len(params))
	}
	if got, ok := params[0].([]byte); !ok || !bytes.Equal(got, []byte("x")) {
		t.Fatalf("expected first param 'x', got %#v", params[0])
	}
	if params[1] != executor.UnsetValue {
		t.Fatalf("expected second param unset sentinel, got %#v", params[1])
	}
}
