6
6
7
7
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
8
8
_ "github.com/ydb-platform/ydb-go-sdk/v3"
9
+ "github.com/ydb-platform/ydb-go-sdk/v3/query"
9
10
)
10
11
11
12
func ptr (s string ) * string {
@@ -15,10 +16,10 @@ func ptr(s string) *string {
15
16
func TestAuthors (t * testing.T ) {
16
17
ctx := context .Background ()
17
18
18
- test := local .YDB (t , []string {"schema.sql" })
19
- defer test . DB . Close ()
19
+ db := local .YDB (t , []string {"schema.sql" })
20
+ defer db . Close (ctx )
20
21
21
- q := New (test . DB )
22
+ q := New (db . Query () )
22
23
23
24
t .Run ("InsertAuthors" , func (t * testing.T ) {
24
25
authorsToInsert := []CreateOrUpdateAuthorParams {
@@ -38,53 +39,12 @@ func TestAuthors(t *testing.T) {
38
39
}
39
40
40
41
for _ , author := range authorsToInsert {
41
- if _ , err := q .CreateOrUpdateAuthor (ctx , author ); err != nil {
42
+ if err := q .CreateOrUpdateAuthor (ctx , author , query . WithIdempotent () ); err != nil {
42
43
t .Fatalf ("failed to insert author %q: %v" , author .P1 , err )
43
44
}
44
45
}
45
46
})
46
47
47
- t .Run ("CreateOrUpdateAuthorReturningBio" , func (t * testing.T ) {
48
- newBio := "Обновленная биография автора"
49
- arg := CreateOrUpdateAuthorReturningBioParams {
50
- P0 : 3 ,
51
- P1 : "Тестовый Автор" ,
52
- P2 : & newBio ,
53
- }
54
-
55
- returnedBio , err := q .CreateOrUpdateAuthorReturningBio (ctx , arg )
56
- if err != nil {
57
- t .Fatalf ("failed to create or update author: %v" , err )
58
- }
59
-
60
- if returnedBio == nil {
61
- t .Fatal ("expected non-nil bio, got nil" )
62
- }
63
- if * returnedBio != newBio {
64
- t .Fatalf ("expected bio %q, got %q" , newBio , * returnedBio )
65
- }
66
-
67
- t .Logf ("Author created or updated successfully with bio: %s" , * returnedBio )
68
- })
69
-
70
- t .Run ("Update Author" , func (t * testing.T ) {
71
- arg := UpdateAuthorByIDParams {
72
- P0 : "Максим Горький" ,
73
- P1 : ptr ("Обновленная биография" ),
74
- P2 : 10 ,
75
- }
76
-
77
- singleAuthor , err := q .UpdateAuthorByID (ctx , arg )
78
- if err != nil {
79
- t .Fatal (err )
80
- }
81
- bio := "Null"
82
- if singleAuthor .Bio != nil {
83
- bio = * singleAuthor .Bio
84
- }
85
- t .Logf ("- ID: %d | Name: %s | Bio: %s" , singleAuthor .ID , singleAuthor .Name , bio )
86
- })
87
-
88
48
t .Run ("ListAuthors" , func (t * testing.T ) {
89
49
authors , err := q .ListAuthors (ctx )
90
50
if err != nil {
@@ -115,46 +75,10 @@ func TestAuthors(t *testing.T) {
115
75
t .Logf ("- ID: %d | Name: %s | Bio: %s" , singleAuthor .ID , singleAuthor .Name , bio )
116
76
})
117
77
118
- t .Run ("GetAuthorByName" , func (t * testing.T ) {
119
- authors , err := q .GetAuthorsByName (ctx , "Александр Пушкин" )
120
- if err != nil {
121
- t .Fatal (err )
122
- }
123
- if len (authors ) == 0 {
124
- t .Fatal ("expected at least one author with this name, got none" )
125
- }
126
- t .Log ("Authors with this name:" )
127
- for _ , a := range authors {
128
- bio := "Null"
129
- if a .Bio != nil {
130
- bio = * a .Bio
131
- }
132
- t .Logf ("- ID: %d | Name: %s | Bio: %s" , a .ID , a .Name , bio )
133
- }
134
- })
135
-
136
- t .Run ("ListAuthorsWithNullBio" , func (t * testing.T ) {
137
- authors , err := q .ListAuthorsWithNullBio (ctx )
138
- if err != nil {
139
- t .Fatal (err )
140
- }
141
- if len (authors ) == 0 {
142
- t .Fatal ("expected at least one author with NULL bio, got none" )
143
- }
144
- t .Log ("Authors with NULL bio:" )
145
- for _ , a := range authors {
146
- bio := "Null"
147
- if a .Bio != nil {
148
- bio = * a .Bio
149
- }
150
- t .Logf ("- ID: %d | Name: %s | Bio: %s" , a .ID , a .Name , bio )
151
- }
152
- })
153
-
154
78
t .Run ("Delete All Authors" , func (t * testing.T ) {
155
79
var i uint64
156
80
for i = 1 ; i <= 13 ; i ++ {
157
- if err := q .DeleteAuthor (ctx , i ); err != nil {
81
+ if err := q .DeleteAuthor (ctx , i , query . WithIdempotent () ); err != nil {
158
82
t .Fatalf ("failed to delete authors: %v" , err )
159
83
}
160
84
}
@@ -166,4 +90,11 @@ func TestAuthors(t *testing.T) {
166
90
t .Fatalf ("expected no authors, got %d" , len (authors ))
167
91
}
168
92
})
93
+
94
+ t .Run ("Drop Table Authors" , func (t * testing.T ) {
95
+ err := q .DropTable (ctx )
96
+ if err != nil {
97
+ t .Fatal (err )
98
+ }
99
+ })
169
100
}
0 commit comments