github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/types/json_test.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestJSONString(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	j := JSON("hello")
    13  	if j.String() != "hello" {
    14  		t.Errorf("Expected %q, got %s", "hello", j.String())
    15  	}
    16  }
    17  
    18  func TestJSONUnmarshal(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	type JSONTest struct {
    22  		Name string
    23  		Age  int
    24  	}
    25  	var jt JSONTest
    26  
    27  	j := JSON(`{"Name":"hi","Age":15}`)
    28  	err := j.Unmarshal(&jt)
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  
    33  	if jt.Name != "hi" {
    34  		t.Errorf("Expected %q, got %s", "hi", jt.Name)
    35  	}
    36  	if jt.Age != 15 {
    37  		t.Errorf("Expected %v, got %v", 15, jt.Age)
    38  	}
    39  }
    40  
    41  func TestJSONMarshal(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	type JSONTest struct {
    45  		Name string
    46  		Age  int
    47  	}
    48  	jt := JSONTest{
    49  		Name: "hi",
    50  		Age:  15,
    51  	}
    52  
    53  	var j JSON
    54  	err := j.Marshal(jt)
    55  	if err != nil {
    56  		t.Error(err)
    57  	}
    58  
    59  	if j.String() != `{"Name":"hi","Age":15}` {
    60  		t.Errorf("expected %s, got %s", `{"Name":"hi","Age":15}`, j.String())
    61  	}
    62  }
    63  
    64  func TestJSONUnmarshalJSON(t *testing.T) {
    65  	t.Parallel()
    66  
    67  	j := JSON(nil)
    68  
    69  	err := j.UnmarshalJSON(JSON(`"hi"`))
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  
    74  	if j.String() != `"hi"` {
    75  		t.Errorf("Expected %q, got %s", "hi", j.String())
    76  	}
    77  }
    78  
    79  func TestJSONMarshalJSON(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	j := JSON(`"hi"`)
    83  	res, err := j.MarshalJSON()
    84  	if err != nil {
    85  		t.Error(err)
    86  	}
    87  
    88  	if !bytes.Equal(res, []byte(`"hi"`)) {
    89  		t.Errorf("Expected %q, got %v", `"hi"`, res)
    90  	}
    91  }
    92  
    93  func TestJSONValue(t *testing.T) {
    94  	t.Parallel()
    95  
    96  	j := JSON(`{"Name":"hi","Age":15}`)
    97  	v, err := j.Value()
    98  	if err != nil {
    99  		t.Error(err)
   100  	}
   101  
   102  	if !bytes.Equal(j, v.([]byte)) {
   103  		t.Errorf("byte mismatch, %v %v", j, v)
   104  	}
   105  }
   106  
   107  func TestJSONScan(t *testing.T) {
   108  	t.Parallel()
   109  
   110  	j := JSON{}
   111  
   112  	err := j.Scan(`"hello"`)
   113  	if err != nil {
   114  		t.Error(err)
   115  	}
   116  
   117  	if !bytes.Equal(j, []byte(`"hello"`)) {
   118  		t.Errorf("bad []byte: %#v ≠ %#v\n", j, string([]byte(`"hello"`)))
   119  	}
   120  }
   121  
   122  func BenchmarkJSON_Scan(b *testing.B) {
   123  	data := `"` + strings.Repeat("A", 1024) + `"`
   124  	for i := 0; i < b.N; i++ {
   125  		var j JSON
   126  		err := j.Scan(data)
   127  		if err != nil {
   128  			b.Error(err)
   129  		}
   130  	}
   131  }