Skip to content

Commit a2c395f

Browse files
committed
update test cases and examples for better coverage
1 parent 8fa20c7 commit a2c395f

File tree

2 files changed

+82
-61
lines changed

2 files changed

+82
-61
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Here is a sample to use `Struct` as ORM. It should be quite straight forward for
158158

159159
```go
160160
type User struct {
161-
ID int64 `db:"id"`
161+
ID int64 `db:"id" fieldtag:"pk"`
162162
Name string `db:"name"`
163163
Status int `db:"status"`
164164
}

struct_test.go

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -274,33 +274,32 @@ func TestWithAndWithoutTags(t *testing.T) {
274274
a := assert.New(t)
275275

276276
a.Equal(structTags.Columns(), []string{"a", "b", "c", "d", "e", "f", "g", "h"})
277+
a.Equal(structTags.WithTag().Columns(), []string{"a", "b", "c", "d", "e", "f", "g", "h"})
278+
a.Equal(structTags.WithoutTag().Columns(), []string{"a", "b", "c", "d", "e", "f", "g", "h"})
279+
a.Equal(structTags.WithTag("").Columns(), []string{"a", "b", "c", "d", "e", "f", "g", "h"})
280+
a.Equal(structTags.WithoutTag("").Columns(), []string{"a", "b", "c", "d", "e", "f", "g", "h"})
281+
277282
a.Equal(structTags.WithTag("tag1").Columns(), []string{"a", "d", "f", "g"})
278283
a.Equal(structTags.WithTag("tag2").Columns(), []string{"b", "d", "e", "g"})
279284
a.Equal(structTags.WithTag("tag3").Columns(), []string{"c", "e", "f", "g"})
280285

281286
a.Equal(structTags.WithTag("tag1", "tag2").Columns(), []string{"a", "d", "f", "g", "b", "e"})
282287
a.Equal(structTags.WithTag("tag1", "tag3").Columns(), []string{"a", "d", "f", "g", "c", "e"})
283288
a.Equal(structTags.WithTag("tag2", "tag3").Columns(), []string{"b", "d", "e", "g", "c", "f"})
284-
a.Equal(structTags.WithTag("tag2", "tag3", "tag2", "tag3").Columns(), []string{"b", "d", "e", "g", "c", "f"})
289+
a.Equal(structTags.WithTag("tag2", "tag3", "tag2", "", "tag3").Columns(), []string{"b", "d", "e", "g", "c", "f"})
285290

286291
a.Equal(structTags.WithoutTag("tag3").Columns(), []string{"a", "b", "d", "h"})
287292
a.Equal(structTags.WithoutTag("tag3", "tag2").Columns(), []string{"a", "h"})
288-
a.Equal(structTags.WithoutTag("tag3", "tag2", "tag3", "tag2").Columns(), []string{"a", "h"})
293+
a.Equal(structTags.WithoutTag("tag3", "tag2", "tag3", "", "tag2").Columns(), []string{"a", "h"})
289294

290295
a.Equal(structTags.WithTag("tag1", "tag2").WithoutTag("tag3").Columns(), []string{"a", "d", "b"})
291296
a.Equal(structTags.WithoutTag("tag3").WithTag("tag1", "tag2").Columns(), []string{"a", "d", "b"})
292297
a.Equal(structTags.WithTag("tag1", "tag2", "tag3").WithoutTag("tag3").Columns(), []string{"a", "d", "b"})
293298
a.Equal(structTags.WithoutTag("tag3", "tag1").WithTag("tag1", "tag2", "tag3").Columns(), []string{"b"})
294299

295300
a.Equal(structTags.WithTag("tag2").WithTag("tag1").Columns(), []string{"a", "d", "f", "g", "b", "e"})
296-
a.Equal(structTags.WithoutTag("tag3").WithTag("tag1").WithTag("tag3", "tag2").Columns(), []string{"a", "d", "b"})
297-
a.Equal(structTags.WithoutTag("tag3").WithTag("tag1").WithTag("tag3", "tag2").WithoutTag("tag1", "tag3").Columns(), []string{"b"})
298-
}
299-
300-
type User struct {
301-
ID int64 `db:"id"`
302-
Name string `db:"name"`
303-
Status int `db:"status"`
301+
a.Equal(structTags.WithoutTag("tag3").WithTag("tag1").WithTag("tag3", "", "tag2").Columns(), []string{"a", "d", "b"})
302+
a.Equal(structTags.WithoutTag("tag3").WithTag("tag1").WithTag("tag3", "tag2").WithoutTag("tag1", "", "tag3").Columns(), []string{"b"})
304303
}
305304

306305
type State int
@@ -334,7 +333,6 @@ func (rows testRows) Scan(dest ...interface{}) error {
334333
return nil
335334
}
336335

337-
var userStruct = NewStruct(new(User))
338336
var userDB testDB = 0
339337

340338
const (
@@ -344,19 +342,20 @@ const (
344342
)
345343

346344
func ExampleStruct_useStructAsORM() {
347-
// Suppose we defined following type and global variable.
348-
//
349-
// type User struct {
350-
// ID int64 `db:"id"`
351-
// Name string `db:"name"`
352-
// Status int `db:"status"`
353-
// }
354-
//
355-
// var userStruct = NewStruct(new(User))
345+
// Suppose we defined following type for user db.
346+
type User struct {
347+
ID int64 `db:"id" fieldtag:"pk"`
348+
Name string `db:"name"`
349+
Status int `db:"status"`
350+
}
351+
352+
// Parse user struct. The userStruct can be a global variable.
353+
// It's guraanteed to be thread-safe.
354+
var userStruct = NewStruct(new(User))
356355

357356
// Prepare SELECT query.
358357
sb := userStruct.SelectFrom("user")
359-
sb.Where(sb.E("id", 1234))
358+
sb.Where(sb.Equal("id", 1234))
360359

361360
// Execute the query.
362361
sql, args := sb.Build()
@@ -521,24 +520,26 @@ func ExampleStruct_WithoutTag() {
521520
}
522521

523522
func ExampleStruct_buildUPDATE() {
524-
// Suppose we defined following type and global variable.
525-
//
526-
// type User struct {
527-
// ID int64 `db:"id"`
528-
// Name string `db:"name"`
529-
// Status int `db:"status"`
530-
// }
531-
//
532-
// var userStruct = NewStruct(new(User))
523+
// Suppose we defined following type for user db.
524+
type User struct {
525+
ID int64 `db:"id" fieldtag:"pk"`
526+
Name string `db:"name"`
527+
Status int `db:"status"`
528+
}
529+
530+
// Parse user struct. The userStruct can be a global variable.
531+
// It's guraanteed to be thread-safe.
532+
var userStruct = NewStruct(new(User))
533533

534534
// Prepare UPDATE query.
535+
// We should not update the primary key field.
535536
user := &User{
536537
ID: 1234,
537538
Name: "Huan Du",
538539
Status: 1,
539540
}
540-
ub := userStruct.Update("user", user)
541-
ub.Where(ub.E("id", user.ID))
541+
ub := userStruct.WithoutTag("pk").Update("user", user)
542+
ub.Where(ub.Equal("id", user.ID))
542543

543544
// Execute the query.
544545
sql, args := ub.Build()
@@ -548,28 +549,29 @@ func ExampleStruct_buildUPDATE() {
548549
fmt.Println(args)
549550

550551
// Output:
551-
// UPDATE user SET id = ?, name = ?, status = ? WHERE id = ?
552-
// [1234 Huan Du 1 1234]
552+
// UPDATE user SET name = ?, status = ? WHERE id = ?
553+
// [Huan Du 1 1234]
553554
}
554555

555556
func ExampleStruct_buildINSERT() {
556-
// Suppose we defined following type and global variable.
557-
//
558-
// type User struct {
559-
// ID int64 `db:"id"`
560-
// Name string `db:"name"`
561-
// Status int `db:"status"`
562-
// }
563-
//
564-
// var userStruct = NewStruct(new(User))
557+
// Suppose we defined following type for user db.
558+
type User struct {
559+
ID int64 `db:"id" fieldtag:"pk"`
560+
Name string `db:"name"`
561+
Status int `db:"status"`
562+
}
563+
564+
// Parse user struct. The userStruct can be a global variable.
565+
// It's guraanteed to be thread-safe.
566+
var userStruct = NewStruct(new(User))
565567

566568
// Prepare INSERT query.
569+
// Suppose that user id is generated by database.
567570
user := &User{
568-
ID: 1234,
569571
Name: "Huan Du",
570572
Status: 1,
571573
}
572-
ib := userStruct.InsertInto("user", user)
574+
ib := userStruct.WithoutTag("pk").InsertInto("user", user)
573575

574576
// Execute the query.
575577
sql, args := ib.Build()
@@ -579,20 +581,21 @@ func ExampleStruct_buildINSERT() {
579581
fmt.Println(args)
580582

581583
// Output:
582-
// INSERT INTO user (id, name, status) VALUES (?, ?, ?)
583-
// [1234 Huan Du 1]
584+
// INSERT INTO user (name, status) VALUES (?, ?)
585+
// [Huan Du 1]
584586
}
585587

586588
func ExampleStruct_buildDELETE() {
587-
// Suppose we defined following type and global variable.
588-
//
589-
// type User struct {
590-
// ID int64 `db:"id"`
591-
// Name string `db:"name"`
592-
// Status int `db:"status"`
593-
// }
594-
//
595-
// var userStruct = NewStruct(new(User))
589+
// Suppose we defined following type for user db.
590+
type User struct {
591+
ID int64 `db:"id" fieldtag:"pk"`
592+
Name string `db:"name"`
593+
Status int `db:"status"`
594+
}
595+
596+
// Parse user struct. The userStruct can be a global variable.
597+
// It's guraanteed to be thread-safe.
598+
var userStruct = NewStruct(new(User))
596599

597600
// Prepare DELETE query.
598601
user := &User{
@@ -601,7 +604,7 @@ func ExampleStruct_buildDELETE() {
601604
Status: 1,
602605
}
603606
b := userStruct.DeleteFrom("user")
604-
b.Where(b.E("id", user.ID))
607+
b.Where(b.Equal("id", user.ID))
605608

606609
// Execute the query.
607610
sql, args := b.Build()
@@ -616,10 +619,19 @@ func ExampleStruct_buildDELETE() {
616619
}
617620

618621
func ExampleStruct_forPostgreSQL() {
619-
userStruct := NewStruct(new(User)).For(PostgreSQL)
622+
// Suppose we defined following type for user db.
623+
type User struct {
624+
ID int64 `db:"id" fieldtag:"pk"`
625+
Name string `db:"name"`
626+
Status int `db:"status"`
627+
}
628+
629+
// Parse user struct. The userStruct can be a global variable.
630+
// It's guraanteed to be thread-safe.
631+
var userStruct = NewStruct(new(User)).For(PostgreSQL)
620632

621633
sb := userStruct.SelectFrom("user")
622-
sb.Where(sb.E("id", 1234))
634+
sb.Where(sb.Equal("id", 1234))
623635
sql, args := sb.Build()
624636

625637
fmt.Println(sql)
@@ -631,10 +643,19 @@ func ExampleStruct_forPostgreSQL() {
631643
}
632644

633645
func ExampleStruct_forCQL() {
646+
// Suppose we defined following type for user db.
647+
type User struct {
648+
ID int64 `db:"id" fieldtag:"pk"`
649+
Name string `db:"name"`
650+
Status int `db:"status"`
651+
}
652+
653+
// Parse user struct. The userStruct can be a global variable.
654+
// It's guraanteed to be thread-safe.
634655
userStruct := NewStruct(new(User)).For(CQL)
635656

636657
sb := userStruct.SelectFrom("user")
637-
sb.Where(sb.E("id", 1234))
658+
sb.Where(sb.Equal("id", 1234))
638659
sql, args := sb.Build()
639660

640661
fmt.Println(sql)

0 commit comments

Comments
 (0)