github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/types/types_test.go (about)

     1  package types
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestMetadata_Value(t *testing.T) {
     9  	// Test when the metadata is not empty.
    10  	metadata := Metadata{"key": "value"}
    11  	_, err := metadata.Value()
    12  	if err != nil {
    13  		t.Errorf("Expected no error, got %v", err)
    14  	}
    15  
    16  	// Test when the metadata is empty.
    17  	metadata = Metadata{}
    18  	_, err = metadata.Value()
    19  	if err != nil {
    20  		t.Errorf("Expected no error, got %v", err)
    21  	}
    22  }
    23  
    24  func TestMetadata_Scan(t *testing.T) {
    25  	// Test when the source is a valid JSON.
    26  	source := []byte(`{"key": "value"}`)
    27  	var metadata Metadata
    28  	err := metadata.Scan(source)
    29  	if err != nil {
    30  		t.Errorf("Expected no error, got %v", err)
    31  	}
    32  
    33  	// Test when the source is an empty JSON object.
    34  	source = []byte(`{}`)
    35  	err = metadata.Scan(source)
    36  	if err != nil {
    37  		t.Errorf("Expected no error, got %v", err)
    38  	}
    39  
    40  	// Test when the source is not a valid JSON.
    41  	source = []byte(`invalid-json`)
    42  	err = metadata.Scan(source)
    43  	if err == nil {
    44  		t.Errorf("Expected an error, got nil")
    45  	}
    46  }
    47  
    48  func TestIntArray_Value(t *testing.T) {
    49  	// Test when the IntArray is not empty.
    50  	intArray := IntArray{1, 2, 3}
    51  	_, err := intArray.Value()
    52  	if err != nil {
    53  		t.Errorf("Expected no error, got %v", err)
    54  	}
    55  
    56  	// Test when the IntArray is empty.
    57  	intArray = IntArray{}
    58  	_, err = intArray.Value()
    59  	if err != nil {
    60  		t.Errorf("Expected no error, got %v", err)
    61  	}
    62  }
    63  
    64  func TestIntArray_Scan(t *testing.T) {
    65  	// Test when the source is a valid integer array.
    66  	source := []byte(`{1, 2, 3}`)
    67  	var intArray IntArray
    68  	err := intArray.Scan(source)
    69  	if err != nil {
    70  		t.Errorf("Expected no error, got %v", err)
    71  	}
    72  
    73  	// Test when the source is an empty integer array.
    74  	source = []byte(`{}`)
    75  	err = intArray.Scan(source)
    76  	if err != nil {
    77  		t.Errorf("Expected no error, got %v", err)
    78  	}
    79  
    80  	// Test when the source is not a valid integer array.
    81  	source = []byte(`invalid-array`)
    82  	err = intArray.Scan(source)
    83  	if err == nil {
    84  		t.Errorf("Expected an error, got nil")
    85  	}
    86  }
    87  
    88  func TestStringArray_Value(t *testing.T) {
    89  	// Test when the StringArray is not empty.
    90  	stringArray := StringArray{"one", "two", "three"}
    91  	_, err := stringArray.Value()
    92  	if err != nil {
    93  		t.Errorf("Expected no error, got %v", err)
    94  	}
    95  
    96  	// Test when the StringArray contains special characters.
    97  	stringArray = StringArray{"with space", "with,comma", "with\"quote"}
    98  	_, err = stringArray.Value()
    99  	if err != nil {
   100  		t.Errorf("Expected no error, got %v", err)
   101  	}
   102  
   103  	// Test when the StringArray is empty.
   104  	stringArray = StringArray{}
   105  	_, err = stringArray.Value()
   106  	if err != nil {
   107  		t.Errorf("Expected no error, got %v", err)
   108  	}
   109  }
   110  
   111  func TestStringArray_Scan(t *testing.T) {
   112  	// Test when the source is a valid string array.
   113  	source := []byte(`{"one", "two", "three"}`)
   114  	var stringArray StringArray
   115  	err := stringArray.Scan(source)
   116  	if err != nil {
   117  		t.Errorf("Expected no error, got %v", err)
   118  	}
   119  
   120  	// Test when the source is an empty string array.
   121  	source = []byte(`{}`)
   122  	err = stringArray.Scan(source)
   123  	if err != nil {
   124  		t.Errorf("Expected no error, got %v", err)
   125  	}
   126  
   127  	// Test when the source contains special characters.
   128  	source = []byte(`{"with space", "with,comma", "with\"quote"}`)
   129  	err = stringArray.Scan(source)
   130  	if err != nil {
   131  		t.Errorf("Expected no error, got %v", err)
   132  	}
   133  
   134  	// Test when the source is not a valid string array.
   135  	stringArray = StringArray{}
   136  	empty := StringArray{}
   137  	source = []byte(``)
   138  	err = stringArray.Scan(source)
   139  	if !reflect.DeepEqual(stringArray, empty) {
   140  		t.Errorf("Expected empty, got something")
   141  	}
   142  }