66
77 "github.com/sqlc-dev/sqlc/internal/sqltest/local"
88 _ "github.com/ydb-platform/ydb-go-sdk/v3"
9+ "github.com/ydb-platform/ydb-go-sdk/v3/query"
910)
1011
1112func ptr (s string ) * string {
@@ -15,10 +16,10 @@ func ptr(s string) *string {
1516func TestAuthors (t * testing.T ) {
1617 ctx := context .Background ()
1718
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 )
2021
21- q := New (test . DB )
22+ q := New (db . Query () )
2223
2324 t .Run ("InsertAuthors" , func (t * testing.T ) {
2425 authorsToInsert := []CreateOrUpdateAuthorParams {
@@ -38,53 +39,12 @@ func TestAuthors(t *testing.T) {
3839 }
3940
4041 for _ , author := range authorsToInsert {
41- if _ , err := q .CreateOrUpdateAuthor (ctx , author ); err != nil {
42+ if err := q .CreateOrUpdateAuthor (ctx , author , query . WithIdempotent () ); err != nil {
4243 t .Fatalf ("failed to insert author %q: %v" , author .P1 , err )
4344 }
4445 }
4546 })
4647
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-
8848 t .Run ("ListAuthors" , func (t * testing.T ) {
8949 authors , err := q .ListAuthors (ctx )
9050 if err != nil {
@@ -115,46 +75,10 @@ func TestAuthors(t *testing.T) {
11575 t .Logf ("- ID: %d | Name: %s | Bio: %s" , singleAuthor .ID , singleAuthor .Name , bio )
11676 })
11777
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-
15478 t .Run ("Delete All Authors" , func (t * testing.T ) {
15579 var i uint64
15680 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 {
15882 t .Fatalf ("failed to delete authors: %v" , err )
15983 }
16084 }
@@ -166,4 +90,11 @@ func TestAuthors(t *testing.T) {
16690 t .Fatalf ("expected no authors, got %d" , len (authors ))
16791 }
16892 })
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+ })
169100}
0 commit comments