github.com/dolthub/go-mysql-server@v0.18.0/sql/mysql_db/role_edge_test.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mysql_db 16 17 import ( 18 "encoding/json" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 23 "github.com/dolthub/go-mysql-server/sql" 24 ) 25 26 // This test enforces not only that RoleEdge round trips, but that the output is as expected. 27 func TestRoleEdgeJson(t *testing.T) { 28 ctx := sql.NewEmptyContext() 29 testRoleEdge := &RoleEdge{ 30 FromHost: "localhost", 31 FromUser: "some_role", 32 ToHost: "127.0.0.1", 33 ToUser: "some_user", 34 WithAdminOption: true, 35 } 36 jsonStr, err := testRoleEdge.ToJson(ctx) 37 require.NoError(t, err) 38 require.Equal(t, `{"FromHost":"localhost","FromUser":"some_role","ToHost":"127.0.0.1","ToUser":"some_user","WithAdminOption":true}`, jsonStr) 39 newRoleEdge, err := (&RoleEdge{}).FromJson(ctx, jsonStr) 40 require.NoError(t, err) 41 require.True(t, RoleEdgeEquals(testRoleEdge, newRoleEdge)) 42 43 testSlice := []*RoleEdge{testRoleEdge} 44 jsonData, err := json.Marshal(testSlice) 45 require.NoError(t, err) 46 var newSlice []*RoleEdge 47 err = json.Unmarshal(jsonData, &newSlice) 48 require.NoError(t, err) 49 require.Len(t, newSlice, len(testSlice)) 50 for i := range testSlice { 51 require.True(t, RoleEdgeEquals(testSlice[i], newSlice[i])) 52 } 53 }