Skip to content

Commit 9af6f3f

Browse files
committed
Merge branch 'main' into zachmu/types
2 parents 24b5216 + fc59d38 commit 9af6f3f

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2023 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 main
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"os"
21+
"strings"
22+
23+
"github.com/dolthub/go-mysql-server/sql"
24+
"github.com/jackc/pgx/v5"
25+
26+
framework "github.com/dolthub/doltgresql/testing/go"
27+
)
28+
29+
// GenerateRowTests uses the given StatementGenerator to return a random set of queries along with their results that
30+
// were retrieved from a Postgres instance. Uses the MaxTestCount to determine the number of tests to generate. The
31+
// returned map uses the query as the key, with the results being the value.
32+
func GenerateRowTests(stmtGen StatementGenerator) (map[string][]sql.Row, error) {
33+
randomInts, err := GenerateRandomInts(MaxTestCount, stmtGen.Permutations())
34+
if err != nil {
35+
return nil, err
36+
}
37+
allResults := make(map[string][]sql.Row)
38+
for _, randomInt := range randomInts {
39+
stmtGen.SetConsumeIterations(randomInt)
40+
generatedString := stmtGen.String()
41+
result, err := GetRowResults(generatedString)
42+
if err != nil {
43+
return nil, err
44+
}
45+
allResults[generatedString] = result
46+
}
47+
return allResults, nil
48+
}
49+
50+
// GetSynopsisStatementGenerator returns the StatementGenerator for the given synopsis. The synopsisData should be the
51+
// string that has been loaded from the synopses directory. includeRepetition states whether repetition is included in
52+
// the returned StatementGenerator.
53+
func GetSynopsisStatementGenerator(synopsisData string, prefix string, includeRepetition bool) (StatementGenerator, error) {
54+
scanner := NewScanner(synopsisData)
55+
tokens, err := scanner.Process()
56+
if err != nil {
57+
return nil, err
58+
}
59+
stmtGen, err := ParseTokens(tokens, includeRepetition)
60+
if err != nil {
61+
return nil, err
62+
}
63+
// Not all variables have their definitions set in the synopsis, so we'll handle them here
64+
unsetVariables, err := UnsetVariables(stmtGen)
65+
if err != nil {
66+
return nil, err
67+
}
68+
customVariableDefinitions := make(map[string]StatementGenerator)
69+
for _, unsetVariable := range unsetVariables {
70+
// Check for a specific definition first
71+
if prefixVariables, ok := PrefixCustomVariables[prefix]; ok {
72+
if variableDefinition, ok := prefixVariables[unsetVariable]; ok {
73+
customVariableDefinitions[unsetVariable] = variableDefinition
74+
continue
75+
}
76+
}
77+
// Check the global definitions if there isn't a specific definition
78+
if variableDefinition, ok := GlobalCustomVariables[unsetVariable]; ok {
79+
customVariableDefinitions[unsetVariable] = variableDefinition
80+
continue
81+
}
82+
}
83+
if err = ApplyVariableDefinition(stmtGen, customVariableDefinitions); err != nil {
84+
return nil, err
85+
}
86+
return stmtGen, nil
87+
}
88+
89+
// LoadSynopsis loads the given synopsis from the synopses directory. The synopsis name may be the file name, or the
90+
// name of the statement (also known as the prefix). Case-insensitive.
91+
func LoadSynopsis(synopsis string) (data string, prefix string, err error) {
92+
prefix = strings.ToUpper(
93+
strings.ReplaceAll(
94+
strings.ReplaceAll(
95+
synopsis,
96+
".txt",
97+
"",
98+
),
99+
"_",
100+
" ",
101+
),
102+
)
103+
fileName := strings.ToLower(strings.ReplaceAll(prefix+".txt", " ", "_"))
104+
parentFolder, err := GetCommandDocsFolder()
105+
if err != nil {
106+
return "", "", err
107+
}
108+
dataBytes, err := os.ReadFile(fmt.Sprintf("%s/synopses/%s", parentFolder, fileName))
109+
if err != nil {
110+
return "", "", err
111+
}
112+
return strings.TrimSpace(string(dataBytes)), prefix, nil
113+
}
114+
115+
// GetRowResults runs the query against a Postgres server and returns the resulting rows.
116+
func GetRowResults(query string) ([]sql.Row, error) {
117+
var err error
118+
ctx := context.Background()
119+
if postgresVerificationConnection == nil {
120+
connectionString := fmt.Sprintf("postgres://postgres:[email protected]:%d/", 5432)
121+
postgresVerificationConnection, err = pgx.Connect(ctx, connectionString)
122+
if err != nil {
123+
return nil, err
124+
}
125+
}
126+
pgxRows, err := postgresVerificationConnection.Query(ctx, query)
127+
if err != nil {
128+
return nil, err
129+
}
130+
return framework.ReadRows(pgxRows)
131+
}
File renamed without changes.

0 commit comments

Comments
 (0)