Files
gochat/internal/automation/model_test.go
T
2026-06-04 15:44:48 +08:00

146 lines
3.9 KiB
Go

package automation
import (
"encoding/json"
"testing"
)
func TestConditionsValue_Nil(t *testing.T) {
var c Conditions = nil
val, err := c.Value()
if err != nil {
t.Fatalf("expected no error for nil Conditions Value, got: %v", err)
}
// Should marshal as empty array
var result []Condition
if err := json.Unmarshal(val.([]byte), &result); err != nil {
t.Fatalf("failed to unmarshal nil Conditions value: %v", err)
}
if len(result) != 0 {
t.Fatalf("expected empty array for nil Conditions, got %d items", len(result))
}
}
func TestConditionsValue_NonNil(t *testing.T) {
c := Conditions{
{Attribute: "status", FilterOperator: "equal", Values: []string{"open"}, QueryOperator: "and"},
}
val, err := c.Value()
if err != nil {
t.Fatalf("expected no error for Conditions Value, got: %v", err)
}
var result Conditions
if err := json.Unmarshal(val.([]byte), &result); err != nil {
t.Fatalf("failed to unmarshal Conditions value: %v", err)
}
if len(result) != 1 {
t.Fatalf("expected 1 condition, got %d", len(result))
}
if result[0].Attribute != "status" {
t.Fatalf("expected attribute 'status', got %q", result[0].Attribute)
}
}
func TestConditionsScan_Nil(t *testing.T) {
var c Conditions
err := c.Scan(nil)
if err != nil {
t.Fatalf("expected no error for nil Scan, got: %v", err)
}
if len(c) != 0 {
t.Fatalf("expected empty Conditions after nil Scan, got %d items", len(c))
}
}
func TestConditionsScan_ValidJSON(t *testing.T) {
raw := `[{"attribute":"status","filter_operator":"equal","values":["open"],"query_operator":"and"}]`
var c Conditions
err := c.Scan([]byte(raw))
if err != nil {
t.Fatalf("expected no error for valid JSON Scan, got: %v", err)
}
if len(c) != 1 {
t.Fatalf("expected 1 condition, got %d", len(c))
}
if c[0].Attribute != "status" {
t.Fatalf("expected attribute 'status', got %q", c[0].Attribute)
}
}
func TestConditionsScan_InvalidType(t *testing.T) {
var c Conditions
err := c.Scan("not bytes")
if err == nil {
t.Fatal("expected error for non-byte Scan, got nil")
}
}
func TestActionsValue_Nil(t *testing.T) {
var a Actions = nil
val, err := a.Value()
if err != nil {
t.Fatalf("expected no error for nil Actions Value, got: %v", err)
}
var result []Action
if err := json.Unmarshal(val.([]byte), &result); err != nil {
t.Fatalf("failed to unmarshal nil Actions value: %v", err)
}
if len(result) != 0 {
t.Fatalf("expected empty array for nil Actions, got %d items", len(result))
}
}
func TestActionsValue_NonNil(t *testing.T) {
a := Actions{
{ActionName: "send_message", ActionParams: map[string]interface{}{"content": "hello"}},
}
val, err := a.Value()
if err != nil {
t.Fatalf("expected no error for Actions Value, got: %v", err)
}
var result Actions
if err := json.Unmarshal(val.([]byte), &result); err != nil {
t.Fatalf("failed to unmarshal Actions value: %v", err)
}
if len(result) != 1 {
t.Fatalf("expected 1 action, got %d", len(result))
}
if result[0].ActionName != "send_message" {
t.Fatalf("expected action_name 'send_message', got %q", result[0].ActionName)
}
}
func TestActionsScan_Nil(t *testing.T) {
var a Actions
err := a.Scan(nil)
if err != nil {
t.Fatalf("expected no error for nil Scan, got: %v", err)
}
if len(a) != 0 {
t.Fatalf("expected empty Actions after nil Scan, got %d items", len(a))
}
}
func TestActionsScan_ValidJSON(t *testing.T) {
raw := `[{"action_name":"send_message","action_params":{"content":"hello"}}]`
var a Actions
err := a.Scan([]byte(raw))
if err != nil {
t.Fatalf("expected no error for valid JSON Scan, got: %v", err)
}
if len(a) != 1 {
t.Fatalf("expected 1 action, got %d", len(a))
}
if a[0].ActionName != "send_message" {
t.Fatalf("expected action_name 'send_message', got %q", a[0].ActionName)
}
}
func TestActionsScan_InvalidType(t *testing.T) {
var a Actions
err := a.Scan("not bytes")
if err == nil {
t.Fatal("expected error for non-byte Scan, got nil")
}
}