Skip to content

Commit 922af7f

Browse files
authored
fix: should return error when sql is invalid (#40)
* fix: should return error when sql is invalid * fix the docker compose order issues --------- Co-authored-by: rick <[email protected]>
1 parent 03bb10c commit 922af7f

File tree

6 files changed

+103
-28
lines changed

6 files changed

+103
-28
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023-2024 API Testing Authors.
2+
Copyright 2023-2025 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

e2e/entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ kind=orm target=greptimedb:4002 driver=greptime dbname=public atest run -p testi
1717
kind=orm target=tdengine:6041 driver=tdengine password=taosdata dbname=information_schema atest run -p testing-data-query.yaml
1818
kind=orm target=postgres driver=postgres atest run -p testing-data-query.yaml
1919

20+
kind=orm target=mysql driver=mysql atest run -p testing-data-query-mysql.yaml
21+
2022
cat /root/.config/atest/stores.yaml

e2e/start.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ fi
88

99
docker compose version
1010
docker compose -f "$file" down
11-
docker compose -f "$file" up --build testing --exit-code-from testing --remove-orphans
11+
docker compose -f "$file" build extension
12+
docker compose -f "$file" up --exit-code-from testing --remove-orphans

e2e/testing-data-query-mysql.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!api-testing
2+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-schema.json
3+
name: atest
4+
api: |
5+
{{default "http://localhost:8080" (env "SERVER")}}/api/v1
6+
param:
7+
store: "{{randAlpha 3}}"
8+
server: |
9+
{{default "http://localhost:8080" (env "SERVER")}}
10+
items:
11+
- name: CreateStore
12+
before:
13+
items:
14+
- httpReady("{{.param.server}}/healthz", 2400)
15+
request:
16+
api: /stores
17+
method: POST
18+
body: |
19+
{
20+
"name": "{{.param.store}}",
21+
"url": "{{env "target"}}",
22+
"username": "{{default "root" (env "username")}}",
23+
"password": "{{default "root" (env "password")}}",
24+
"kind": {
25+
"name": "atest-store-{{env "kind"}}"
26+
},
27+
"properties": [{
28+
"key": "driver",
29+
"value": "{{default "mysql" (env "driver")}}"
30+
}, {
31+
"key": "database",
32+
"value": "{{default "atest" (env "dbname")}}"
33+
}, {
34+
"key": "bucket",
35+
"value": "bucket"
36+
}, {
37+
"key": "region",
38+
"value": "cn"
39+
}, {
40+
"key": "disablessl",
41+
"value": "true"
42+
}, {
43+
"key": "targetPath",
44+
"value": "api-testing"
45+
}]
46+
}
47+
- name: query
48+
before:
49+
items:
50+
- sleep(3)
51+
request:
52+
api: /data/query
53+
method: POST
54+
header:
55+
X-Store-Name: "{{.param.store}}"
56+
body: |
57+
{
58+
"sql": "",
59+
"key": ""
60+
}
61+
- name: invalid-sql
62+
request:
63+
api: /data/query
64+
method: POST
65+
header:
66+
X-Store-Name: "{{.param.store}}"
67+
body: |
68+
{
69+
"sql": "selec * from user where id <> 1",
70+
"key": ""
71+
}
72+
expect:
73+
statusCode: 500
74+
bodyFieldsExpect:
75+
code: 2

main.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
/*
2-
*
3-
MIT License
2+
Copyright 2023 API Testing Authors.
43
5-
Copyright (c) 2023 API Testing Authors.
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
67
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:
8+
http://www.apache.org/licenses/LICENSE-2.0
139
14-
The above copyright notice and this permission notice shall be included in all
15-
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 THE
23-
SOFTWARE.
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.
2415
*/
2516

2617
package main

pkg/data_query.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,25 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result *
5252
go func() {
5353
defer wg.Done()
5454
// query database and tables
55-
if result.Meta.Databases, err = dbQuery.GetDatabases(ctx); err != nil {
56-
log.Printf("failed to query databases: %v\n", err)
55+
var innerErr error
56+
if result.Meta.Databases, innerErr = dbQuery.GetDatabases(ctx); innerErr != nil {
57+
log.Printf("failed to query databases: %v\n", innerErr)
5758
}
5859
}()
5960

6061
wg.Add(1)
6162
go func() {
6263
defer wg.Done()
6364
if result.Meta.CurrentDatabase = query.Key; query.Key == "" {
64-
if result.Meta.CurrentDatabase, err = dbQuery.GetCurrentDatabase(); err != nil {
65-
log.Printf("failed to query current database: %v\n", err)
65+
var queryDBErr error
66+
if result.Meta.CurrentDatabase, queryDBErr = dbQuery.GetCurrentDatabase(); queryDBErr != nil {
67+
log.Printf("failed to query current database: %v\n", queryDBErr)
6668
}
6769
}
6870

69-
if result.Meta.Tables, err = dbQuery.GetTables(ctx, result.Meta.CurrentDatabase); err != nil {
70-
log.Printf("failed to query tables: %v\n", err)
71+
var queryTableErr error
72+
if result.Meta.Tables, queryTableErr = dbQuery.GetTables(ctx, result.Meta.CurrentDatabase); err != nil {
73+
log.Printf("failed to query tables: %v\n", queryTableErr)
7174
}
7275
}()
7376

@@ -97,6 +100,8 @@ func (s *dbserver) Query(ctx context.Context, query *server.DataQuery) (result *
97100

98101
wg.Wait()
99102
result.Meta.Labels = append(result.Meta.Labels, dataResult.Meta.Labels...)
103+
} else {
104+
wg.Wait()
100105
}
101106
return
102107
}
@@ -140,7 +145,8 @@ func sqlQuery(ctx context.Context, sqlText string, db *gorm.DB) (result *server.
140145
}
141146
}
142147

143-
columns, err := rows.Columns()
148+
var columns []string
149+
columns, err = rows.Columns()
144150
if err != nil {
145151
return
146152
}

0 commit comments

Comments
 (0)