go.temporal.io/server@v1.23.0/common/persistence/query_util_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package persistence 26 27 import ( 28 "bytes" 29 "io" 30 "testing" 31 32 "github.com/stretchr/testify/require" 33 "github.com/stretchr/testify/suite" 34 35 "go.temporal.io/server/common/log" 36 ) 37 38 type ( 39 queryUtilSuite struct { 40 suite.Suite 41 // override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test, 42 // not merely log an error 43 *require.Assertions 44 logger log.Logger 45 } 46 ) 47 48 func TestQueryUtilSuite(t *testing.T) { 49 s := new(queryUtilSuite) 50 suite.Run(t, s) 51 } 52 53 func (s *queryUtilSuite) SetupTest() { 54 s.logger = log.NewTestLogger() 55 // Have to define our overridden assertions in the test setup. If we did it earlier, s.T() will return nil 56 s.Assertions = require.New(s.T()) 57 } 58 59 func (s *queryUtilSuite) TestLoadAndSplitQueryFromReaders() { 60 input := ` 61 CREATE TABLE test ( 62 id BIGINT not null, 63 col1 BIGINT, -- comment with unmatched parenthesis ) 64 col2 VARCHAR(255), 65 PRIMARY KEY (id) 66 ); 67 68 CREATE INDEX test_idx ON test (col1); 69 70 --begin 71 CREATE TRIGGER test_ai AFTER INSERT ON test 72 BEGIN 73 SELECT *, 'string with unmatched chars ")' FROM test; 74 --end 75 END; 76 77 -- trailing comment 78 ` 79 statements, err := LoadAndSplitQueryFromReaders([]io.Reader{bytes.NewBufferString(input)}) 80 s.NoError(err) 81 s.Equal(3, len(statements)) 82 s.Equal( 83 `CREATE TABLE test ( 84 id BIGINT not null, 85 col1 BIGINT, 86 col2 VARCHAR(255), 87 PRIMARY KEY (id) 88 );`, 89 statements[0], 90 ) 91 s.Equal(`CREATE INDEX test_idx ON test (col1);`, statements[1]) 92 // comments are removed, but the inner content is not trimmed 93 s.Equal( 94 `CREATE TRIGGER test_ai AFTER INSERT ON test 95 BEGIN 96 SELECT *, 'string with unmatched chars ")' FROM test; 97 98 END;`, 99 statements[2], 100 ) 101 102 input = "CREATE TABLE test (;" 103 statements, err = LoadAndSplitQueryFromReaders([]io.Reader{bytes.NewBufferString(input)}) 104 s.Error(err, "error reading contents: unmatched left parenthesis") 105 s.Nil(statements) 106 107 input = "CREATE TABLE test ());" 108 statements, err = LoadAndSplitQueryFromReaders([]io.Reader{bytes.NewBufferString(input)}) 109 s.Error(err, "error reading contents: unmatched right parenthesis") 110 s.Nil(statements) 111 112 input = "begin" 113 statements, err = LoadAndSplitQueryFromReaders([]io.Reader{bytes.NewBufferString(input)}) 114 s.Error(err, "error reading contents: unmatched `BEGIN` keyword") 115 s.Nil(statements) 116 117 input = "end" 118 statements, err = LoadAndSplitQueryFromReaders([]io.Reader{bytes.NewBufferString(input)}) 119 s.Error(err, "error reading contents: unmatched `END` keyword") 120 s.Nil(statements) 121 122 input = "select ' from test;" 123 statements, err = LoadAndSplitQueryFromReaders([]io.Reader{bytes.NewBufferString(input)}) 124 s.Error(err, "error reading contents: unmatched quotes") 125 s.Nil(statements) 126 } 127 128 func (s *queryUtilSuite) TestHasWordAt() { 129 s.True(hasWordAt("BEGIN", "BEGIN", 0)) 130 s.True(hasWordAt(" BEGIN ", "BEGIN", 1)) 131 s.True(hasWordAt(")BEGIN;", "BEGIN", 1)) 132 s.False(hasWordAt("BEGIN", "BEGIN", 1)) 133 s.False(hasWordAt("sBEGIN", "BEGIN", 1)) 134 s.False(hasWordAt("BEGINs", "BEGIN", 0)) 135 s.False(hasWordAt("7BEGIN", "BEGIN", 1)) 136 s.False(hasWordAt("BEGIN7", "BEGIN", 0)) 137 }