summaryrefslogtreecommitdiff
path: root/statement_test.go
blob: 01405cfd6ee87f7aefb4a1e01a39bf48b2f45295 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
//
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

package mariadb

import (
	"bytes"
	"database/sql/driver"
	"encoding/json"
	"testing"
)

func TestConvertDerivedString(t *testing.T) {
	type derived string

	output, err := converter{}.ConvertValue(derived("value"))
	if err != nil {
		t.Fatal("Derived string type not convertible", err)
	}

	if output != "value" {
		t.Fatalf("Derived string type not converted, got %#v %T", output, output)
	}
}

func TestConvertDerivedByteSlice(t *testing.T) {
	type derived []uint8

	output, err := converter{}.ConvertValue(derived("value"))
	if err != nil {
		t.Fatal("Byte slice not convertible", err)
	}

	if !bytes.Equal(output.([]byte), []byte("value")) {
		t.Fatalf("Byte slice not converted, got %#v %T", output, output)
	}
}

func TestConvertDerivedUnsupportedSlice(t *testing.T) {
	type derived []int

	_, err := converter{}.ConvertValue(derived{1})
	if err == nil || err.Error() != "unsupported type mysql.derived, a slice of int" {
		t.Fatal("Unexpected error", err)
	}
}

func TestConvertDerivedBool(t *testing.T) {
	type derived bool

	output, err := converter{}.ConvertValue(derived(true))
	if err != nil {
		t.Fatal("Derived bool type not convertible", err)
	}

	if output != true {
		t.Fatalf("Derived bool type not converted, got %#v %T", output, output)
	}
}

func TestConvertPointer(t *testing.T) {
	str := "value"

	output, err := converter{}.ConvertValue(&str)
	if err != nil {
		t.Fatal("Pointer type not convertible", err)
	}

	if output != "value" {
		t.Fatalf("Pointer type not converted, got %#v %T", output, output)
	}
}

func TestConvertSignedIntegers(t *testing.T) {
	values := []interface{}{
		int8(-42),
		int16(-42),
		int32(-42),
		int64(-42),
		int(-42),
	}

	for _, value := range values {
		output, err := converter{}.ConvertValue(value)
		if err != nil {
			t.Fatalf("%T type not convertible %s", value, err)
		}

		if output != int64(-42) {
			t.Fatalf("%T type not converted, got %#v %T", value, output, output)
		}
	}
}

type myUint64 struct {
	value uint64
}

func (u myUint64) Value() (driver.Value, error) {
	return u.value, nil
}

func TestConvertUnsignedIntegers(t *testing.T) {
	values := []interface{}{
		uint8(42),
		uint16(42),
		uint32(42),
		uint64(42),
		uint(42),
		myUint64{uint64(42)},
	}

	for _, value := range values {
		output, err := converter{}.ConvertValue(value)
		if err != nil {
			t.Fatalf("%T type not convertible %s", value, err)
		}

		if output != uint64(42) {
			t.Fatalf("%T type not converted, got %#v %T", value, output, output)
		}
	}

	output, err := converter{}.ConvertValue(^uint64(0))
	if err != nil {
		t.Fatal("uint64 high-bit not convertible", err)
	}

	if output != ^uint64(0) {
		t.Fatalf("uint64 high-bit converted, got %#v %T", output, output)
	}
}

func TestConvertJSON(t *testing.T) {
	raw := json.RawMessage("{}")

	out, err := converter{}.ConvertValue(raw)

	if err != nil {
		t.Fatal("json.RawMessage was failed in convert", err)
	}

	if _, ok := out.(json.RawMessage); !ok {
		t.Fatalf("json.RawMessage converted, got %#v %T", out, out)
	}
}

Snix LLC Git Repository Holder Copyright(C) 2022 All Rights Reserved Email To Snix.IR