vitess.io/vitess@v0.16.2/go/mysql/mysql56_gtid_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mysql 18 19 import ( 20 "strings" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestParseMysql56GTID(t *testing.T) { 28 input := "00010203-0405-0607-0809-0A0B0C0D0E0F:56789" 29 want := Mysql56GTID{ 30 Server: SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 31 Sequence: 56789, 32 } 33 34 got, err := parseMysql56GTID(input) 35 require.NoError(t, err, "unexpected error: %v", err) 36 assert.Equal(t, want, got, "parseMysql56GTID(%#v) = %#v, want %#v", input, got, want) 37 38 } 39 40 func TestParseMysql56GTIDInvalid(t *testing.T) { 41 table := []string{ 42 "", 43 "00010203-0405-0607-0809-0A0B0C0D0E0F", 44 "00010203-0405-0607-0809-0A0B0C0D0E0F:1-5", 45 "00010203-0405-0607-0809-0A0B0C0D0E0F:1:2", 46 "00010203-0405-0607-0809-0A0B0C0D0E0X:1", 47 } 48 49 for _, input := range table { 50 _, err := parseMysql56GTID(input) 51 assert.Error(t, err, "parseMysql56GTID(%#v): expected error, got none", input) 52 53 } 54 } 55 56 func TestSIDString(t *testing.T) { 57 input := SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 58 want := "00010203-0405-0607-0809-0a0b0c0d0e0f" 59 60 if got := strings.ToLower(input.String()); got != want { 61 t.Errorf("%#v.String() = %#v, want %#v", input, got, want) 62 } 63 } 64 65 func TestParseSID(t *testing.T) { 66 input := "00010203-0405-0607-0809-0A0B0C0D0E0F" 67 want := SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 68 69 got, err := ParseSID(input) 70 require.NoError(t, err, "unexpected error: %v", err) 71 assert.Equal(t, want, got, "ParseSID(%#v) = %#v, want %#v", input, got, want) 72 73 } 74 75 func TestParseSIDInvalid(t *testing.T) { 76 table := []string{ 77 "123", 78 "x", 79 "00010203-0405-0607-0809-0A0B0C0D0E0x", 80 "00010203-0405-0607-080900A0B0C0D0E0F", 81 } 82 83 for _, input := range table { 84 _, err := ParseSID(input) 85 assert.Error(t, err, "ParseSID(%#v): expected error, got none", input) 86 87 } 88 } 89 90 func TestMysql56GTIDString(t *testing.T) { 91 input := Mysql56GTID{ 92 Server: SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 93 Sequence: 12345, 94 } 95 want := "00010203-0405-0607-0809-0a0b0c0d0e0f:12345" 96 if got := strings.ToLower(input.String()); got != want { 97 t.Errorf("%#v.String() = %#v, want %#v", input, got, want) 98 } 99 } 100 101 func TestMysql56GTIDFlavor(t *testing.T) { 102 input := Mysql56GTID{} 103 if got, want := input.Flavor(), "MySQL56"; got != want { 104 t.Errorf("%#v.Flavor() = %#v, want %#v", input, got, want) 105 } 106 } 107 108 func TestMysql56SequenceDomain(t *testing.T) { 109 input := Mysql56GTID{} 110 if got, want := input.SequenceDomain(), any(nil); got != want { 111 t.Errorf("%#v.SequenceDomain() = %#v, want %#v", input, got, want) 112 } 113 } 114 115 func TestMysql56SourceServer(t *testing.T) { 116 input := Mysql56GTID{ 117 Server: SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 118 } 119 want := any(SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}) 120 if got := input.SourceServer(); got != want { 121 t.Errorf("%#v.SourceServer() = %#v, want %#v", input, got, want) 122 } 123 } 124 125 func TestMysql56SequenceNumber(t *testing.T) { 126 input := Mysql56GTID{ 127 Server: SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 128 Sequence: 5432, 129 } 130 want := any(int64(5432)) 131 if got := input.SequenceNumber(); got != want { 132 t.Errorf("%#v.SequenceNumber() = %#v, want %#v", input, got, want) 133 } 134 } 135 136 func TestMysql56GTIDGTIDSet(t *testing.T) { 137 sid1 := SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 138 input := Mysql56GTID{Server: sid1, Sequence: 5432} 139 want := Mysql56GTIDSet{sid1: []interval{{5432, 5432}}} 140 if got := input.GTIDSet(); !got.Equal(want) { 141 t.Errorf("%#v.GTIDSet() = %#v, want %#v", input, got, want) 142 } 143 }