Skip to content

Commit

Permalink
More negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalyisaev2 committed Oct 15, 2024
1 parent c2f4c59 commit 5c5d667
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
5 changes: 3 additions & 2 deletions tools/ydb/query_service_negative/init/01_basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

/ydb -p tests-ydb-client yql -s '
CREATE TABLE simple (id Int32 NOT NULL, col1 String NOT NULL, col2 Int32 NOT NULL, PRIMARY KEY (id));
CREATE TABLE simple (id Int32 NOT NULL, col1 String, col2 Int32, PRIMARY KEY (id));
COMMIT;
INSERT INTO simple (id, col1, col2) VALUES
(1, "ydb_a", 10),
(2, "ydb_b", 20),
(3, "ydb_c", 30),
(4, "ydb_d", 40),
(5, "ydb_e", 50);
(5, "ydb_e", 50),
(6, "ydb_g", NULL);
COMMIT;
'

40 changes: 34 additions & 6 deletions tools/ydb/query_service_negative/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/query"
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
Expand All @@ -23,14 +24,10 @@ const (
)

func main() {
log.Println("Correct credentials")
obtainTableDesciption(dbEndpoint, "admin", "password")

log.Println("Invalid credentials")
obtainTableDesciption(dbEndpoint, "admin2", "password")
run(dbEndpoint, "admin", "password")
}

func obtainTableDesciption(endpoint, login, password string) {
func run(endpoint, login, password string) {
ydbDriver, err := makeDriver(endpoint, login, password)
if err != nil {
log.Fatal(err)
Expand All @@ -48,6 +45,11 @@ func obtainTableDesciption(endpoint, login, password string) {
}

log.Printf("Table description: %+v", desc)

err = getData(ydbDriver)
if err != nil {
log.Fatal(err)
}
}

func makeDriver(endpoint, login, password string) (*ydb.Driver, error) {
Expand Down Expand Up @@ -94,3 +96,29 @@ func getTableDescription(ydbDriver *ydb.Driver) (*options.Description, error) {

return &desc, nil
}

func getData(ydbDriver *ydb.Driver) error {
finalErr := ydbDriver.Query().Do(context.Background(), func(ctx context.Context, s query.Session) error {
queryText := `
DECLARE $p0 AS Optional<Int32>;
SELECT * FROM %s WHERE col2 = $p0;
`

paramsBuilder := ydb.ParamsBuilder()
paramsBuilder = paramsBuilder.Param("$p0").BeginOptional().Int32(nil).EndOptional()

result, err := s.Query(ctx, fmt.Sprintf(queryText, tableName), query.WithParameters(paramsBuilder.Build()))
if err != nil {
return fmt.Errorf("query error: %w", err)
}

fmt.Println(result)
return nil
})

if finalErr != nil {
return fmt.Errorf("get data: %w", finalErr)
}

return nil
}

0 comments on commit 5c5d667

Please sign in to comment.