2023-01-12 12:42:16 +00:00
package model
2022-11-05 14:40:02 +00:00
import (
2022-11-07 08:08:15 +00:00
"context"
2023-01-13 04:31:35 +00:00
"database/sql"
2023-02-25 15:10:42 +00:00
"fmt"
2023-05-17 14:22:31 +00:00
"github.com/fthvgb1/wp-go/app/pkg/models"
2023-04-06 13:13:02 +00:00
"github.com/fthvgb1/wp-go/safety"
2023-01-18 15:02:59 +00:00
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
2022-11-05 14:40:02 +00:00
"reflect"
"testing"
2023-01-18 15:02:59 +00:00
"time"
2022-11-05 14:40:02 +00:00
)
2023-01-18 15:02:59 +00:00
type post struct {
2023-05-19 15:14:58 +00:00
Id uint64 ` gorm:"column:ID" db:"ID" json:"ID" form:"ID" `
PostAuthor uint64 ` gorm:"column:post_author" db:"post_author" json:"post_author" form:"post_author" `
PostDate time . Time ` gorm:"column:post_date" db:"post_date" json:"post_date" form:"post_date" `
PostDateGmt time . Time ` gorm:"column:post_date_gmt" db:"post_date_gmt" json:"post_date_gmt" form:"post_date_gmt" `
PostContent string ` gorm:"column:post_content" db:"post_content" json:"post_content" form:"post_content" `
PostTitle string ` gorm:"column:post_title" db:"post_title" json:"post_title" form:"post_title" `
PostExcerpt string ` gorm:"column:post_excerpt" db:"post_excerpt" json:"post_excerpt" form:"post_excerpt" `
PostStatus string ` gorm:"column:post_status" db:"post_status" json:"post_status" form:"post_status" `
CommentStatus string ` gorm:"column:comment_status" db:"comment_status" json:"comment_status" form:"comment_status" `
PingStatus string ` gorm:"column:ping_status" db:"ping_status" json:"ping_status" form:"ping_status" `
PostPassword string ` gorm:"column:post_password" db:"post_password" json:"post_password" form:"post_password" `
PostName string ` gorm:"column:post_name" db:"post_name" json:"post_name" form:"post_name" `
ToPing string ` gorm:"column:to_ping" db:"to_ping" json:"to_ping" form:"to_ping" `
Pinged string ` gorm:"column:pinged" db:"pinged" json:"pinged" form:"pinged" `
PostModified time . Time ` gorm:"column:post_modified" db:"post_modified" json:"post_modified" form:"post_modified" `
PostModifiedGmt time . Time ` gorm:"column:post_modified_gmt" db:"post_modified_gmt" json:"post_modified_gmt" form:"post_modified_gmt" `
PostContentFiltered string ` gorm:"column:post_content_filtered" db:"post_content_filtered" json:"post_content_filtered" form:"post_content_filtered" `
PostParent uint64 ` gorm:"column:post_parent" db:"post_parent" json:"post_parent" form:"post_parent" `
Guid string ` gorm:"column:guid" db:"guid" json:"guid" form:"guid" `
MenuOrder int ` gorm:"column:menu_order" db:"menu_order" json:"menu_order" form:"menu_order" `
PostType string ` gorm:"column:post_type" db:"post_type" json:"post_type" form:"post_type" `
PostMimeType string ` gorm:"column:post_mime_type" db:"post_mime_type" json:"post_mime_type" form:"post_mime_type" `
CommentCount int64 ` gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count" `
User * user
2023-05-24 13:33:30 +00:00
Ships * [ ] TermRelationships
2023-05-19 15:14:58 +00:00
PostMeta * [ ] models . PostMeta
2023-05-24 13:33:30 +00:00
TermTaxonomy * [ ] TermTaxonomy
2023-05-27 06:42:14 +00:00
Terms * [ ] models . Terms
2023-05-31 13:54:23 +00:00
CommentMetas [ ] CommentMeta
2023-05-19 15:14:58 +00:00
}
2023-05-18 14:27:28 +00:00
type TermRelationships struct {
ObjectID uint64 ` db:"object_id" `
TermTaxonomyId uint64 ` db:"term_taxonomy_id" `
TermOrder int64 ` db:"term_order" `
2023-05-24 13:33:30 +00:00
TermTaxonomy * [ ] TermTaxonomy
2023-05-18 14:27:28 +00:00
}
2023-01-18 15:02:59 +00:00
type user struct {
Id uint64 ` gorm:"column:ID" db:"ID" json:"ID" `
UserLogin string ` gorm:"column:user_login" db:"user_login" json:"user_login" `
UserPass string ` gorm:"column:user_pass" db:"user_pass" json:"user_pass" `
UserNicename string ` gorm:"column:user_nicename" db:"user_nicename" json:"user_nicename" `
UserEmail string ` gorm:"column:user_email" db:"user_email" json:"user_email" `
UserUrl string ` gorm:"column:user_url" db:"user_url" json:"user_url" `
UserRegistered time . Time ` gorm:"column:user_registered" db:"user_registered" json:"user_registered" `
UserActivationKey string ` gorm:"column:user_activation_key" db:"user_activation_key" json:"user_activation_key" `
UserStatus int ` gorm:"column:user_status" db:"user_status" json:"user_status" `
DisplayName string ` gorm:"column:display_name" db:"display_name" json:"display_name" `
}
type termTaxonomy struct {
TermTaxonomyId uint64 ` gorm:"column:term_taxonomy_id" db:"term_taxonomy_id" json:"term_taxonomy_id" form:"term_taxonomy_id" `
TermId uint64 ` gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id" `
Taxonomy string ` gorm:"column:taxonomy" db:"taxonomy" json:"taxonomy" form:"taxonomy" `
Description string ` gorm:"column:description" db:"description" json:"description" form:"description" `
Parent uint64 ` gorm:"column:parent" db:"parent" json:"parent" form:"parent" `
Count int64 ` gorm:"column:count" db:"count" json:"count" form:"count" `
}
type terms struct {
TermId uint64 ` gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id" `
Name string ` gorm:"column:name" db:"name" json:"name" form:"name" `
Slug string ` gorm:"column:slug" db:"slug" json:"slug" form:"slug" `
TermGroup int64 ` gorm:"column:term_group" db:"term_group" json:"term_group" form:"term_group" `
}
func ( t terms ) PrimaryKey ( ) string {
return "term_id"
}
func ( t terms ) Table ( ) string {
return "wp_terms"
}
func ( w termTaxonomy ) PrimaryKey ( ) string {
return "term_taxonomy_id"
}
func ( w termTaxonomy ) Table ( ) string {
return "wp_term_taxonomy"
}
func ( u user ) Table ( ) string {
return "wp_users"
}
func ( u user ) PrimaryKey ( ) string {
return "ID"
}
func ( p post ) PrimaryKey ( ) string {
return "ID"
}
func ( p post ) Table ( ) string {
return "wp_posts"
}
2022-11-07 08:08:15 +00:00
var ctx = context . Background ( )
2023-02-21 10:25:52 +00:00
var glob * SqlxQuery
2023-02-22 11:05:47 +00:00
var ddb * sqlx . DB
2023-02-21 10:25:52 +00:00
2022-11-05 14:40:02 +00:00
func init ( ) {
2023-01-18 15:02:59 +00:00
db , err := sqlx . Open ( "mysql" , "root:root@tcp(192.168.66.47:3306)/wordpress?charset=utf8mb4&parseTime=True&loc=Local" )
2022-11-05 14:44:35 +00:00
if err != nil {
panic ( err )
}
2023-04-06 13:13:02 +00:00
glob = NewSqlxQuery ( safety . NewVar ( db ) , NewUniversalDb ( func ( ctx2 context . Context , a any , s string , a2 ... any ) error {
2023-02-25 15:10:42 +00:00
x := FormatSql ( s , a2 ... )
fmt . Println ( x )
return glob . Selects ( ctx2 , a , s , a2 ... )
} , func ( ctx2 context . Context , a any , s string , a2 ... any ) error {
x := FormatSql ( s , a2 ... )
fmt . Println ( x )
return glob . Gets ( ctx2 , a , s , a2 ... )
} ) )
2023-02-22 11:05:47 +00:00
ddb = db
2023-02-21 10:25:52 +00:00
InitDB ( glob )
2022-11-05 14:40:02 +00:00
}
func TestFind ( t * testing . T ) {
type args struct {
where ParseWhere
fields string
group string
order SqlBuilder
join SqlBuilder
having SqlBuilder
limit int
in [ ] [ ] any
}
2022-11-06 04:04:41 +00:00
type posts struct {
2023-01-18 15:02:59 +00:00
post
2022-11-06 04:04:41 +00:00
N int ` db:"n" `
}
2022-11-05 14:40:02 +00:00
tests := [ ] struct {
name string
args args
2022-11-06 04:04:41 +00:00
wantR [ ] posts
2022-11-05 14:40:02 +00:00
wantErr bool
} {
2022-11-06 04:04:41 +00:00
{
name : "in,orderBy" ,
args : args {
where : SqlBuilder { {
"post_status" , "publish" ,
} , { "ID" , "in" , "" } } ,
fields : "*" ,
group : "" ,
order : SqlBuilder { { "ID" , "desc" } } ,
join : nil ,
having : nil ,
limit : 0 ,
in : [ ] [ ] any { { 1 , 2 , 3 , 4 } } ,
} ,
wantR : func ( ) [ ] posts {
2022-11-07 08:08:15 +00:00
r , err := Select [ posts ] ( ctx , "select * from " + posts { } . Table ( ) + " where post_status='publish' and ID in (1,2,3,4) order by ID desc" )
2022-11-06 04:04:41 +00:00
if err != nil {
panic ( err )
}
return r
} ( ) ,
wantErr : false ,
} ,
{
name : "or" ,
args : args {
where : SqlBuilder { {
"and" , "ID" , "=" , "1" , "int" ,
} , { "or" , "ID" , "=" , "2" , "int" } } ,
fields : "*" ,
group : "" ,
order : nil ,
join : nil ,
having : nil ,
limit : 0 ,
in : nil ,
} ,
wantR : func ( ) [ ] posts {
2022-11-07 08:08:15 +00:00
r , err := Select [ posts ] ( ctx , "select * from " + posts { } . Table ( ) + " where (ID=1 or ID=2)" )
2022-11-06 04:04:41 +00:00
if err != nil {
panic ( err )
}
return r
} ( ) ,
} ,
{
name : "group,having" ,
args : args {
where : SqlBuilder {
{ "ID" , "<" , "1000" , "int" } ,
} ,
fields : "post_status,count(*) n" ,
group : "post_status" ,
order : nil ,
join : nil ,
having : SqlBuilder {
{ "n" , ">" , "1" } ,
} ,
limit : 0 ,
in : nil ,
} ,
wantR : func ( ) [ ] posts {
2023-01-18 15:02:59 +00:00
r , err := Select [ posts ] ( ctx , "select post_status,count(*) n from " + post { } . Table ( ) + " where ID<1000 group by post_status having n>1" )
2022-11-06 04:04:41 +00:00
if err != nil {
panic ( err )
}
return r
} ( ) ,
} ,
{
name : "or、多个in" ,
args : args {
where : SqlBuilder {
{ "and" , "ID" , "in" , "" , "" , "or" , "ID" , "in" , "" , "" } ,
{ "or" , "post_status" , "=" , "publish" , "" , "and" , "post_status" , "=" , "closed" , "" } ,
} ,
fields : "*" ,
group : "" ,
order : nil ,
join : nil ,
having : nil ,
limit : 0 ,
in : [ ] [ ] any { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ,
} ,
wantR : func ( ) [ ] posts {
2022-11-07 08:08:15 +00:00
r , err := Select [ posts ] ( ctx , "select * from " + posts { } . Table ( ) + " where (ID in (1,2,3) or ID in (4,5,6)) or (post_status='publish' and post_status='closed')" )
2022-11-06 04:04:41 +00:00
if err != nil {
panic ( err )
}
return r
} ( ) ,
} ,
2022-11-17 09:34:03 +00:00
{
name : "all" ,
args : args {
where : SqlBuilder {
{ "b.user_login" , "in" , "" } ,
{ "and" , "a.post_type" , "=" , "post" , "" , "or" , "a.post_type" , "=" , "page" , "" } ,
{ "a.comment_count" , ">" , "0" , "int" } ,
{ "a.post_status" , "publish" } ,
{ "e.name" , "in" , "" } ,
{ "d.taxonomy" , "category" } ,
} ,
fields : "post_author,count(*) n" ,
group : "a.post_author" ,
order : SqlBuilder { { "n" , "desc" } } ,
join : SqlBuilder {
2023-01-18 15:02:59 +00:00
{ "a" , "left join" , user { } . Table ( ) + " b" , "a.post_author=b.ID" } ,
2022-11-17 09:34:03 +00:00
{ "left join" , "wp_term_relationships c" , "a.Id=c.object_id" } ,
2023-01-18 15:02:59 +00:00
{ "left join" , termTaxonomy { } . Table ( ) + " d" , "c.term_taxonomy_id=d.term_taxonomy_id" } ,
{ "left join" , terms { } . Table ( ) + " e" , "d.term_id=e.term_id" } ,
2022-11-17 09:34:03 +00:00
} ,
having : SqlBuilder { { "n" , ">" , "0" , "int" } } ,
limit : 10 ,
in : [ ] [ ] any { { "test" , "test2" } , { "web" , "golang" , "php" } } ,
} ,
wantR : func ( ) [ ] posts {
r , err := Select [ posts ] ( ctx , "select post_author,count(*) n from wp_posts a left join wp_users b on a.post_author=b.ID left join wp_term_relationships c on a.Id=c.object_id left join wp_term_taxonomy d on c.term_taxonomy_id=d.term_taxonomy_id left join wp_terms e on d.term_id=e.term_id where b.user_login in ('test','test2') and b.user_status=0 and (a.post_type='post' or a.post_type='page') and a.comment_count>0 and a.post_status='publish' and e.name in ('web','golang','php') and d.taxonomy='category' group by post_author having n > 0 order by n desc limit 10" )
if err != nil {
panic ( err )
}
return r
} ( ) ,
wantErr : false ,
} ,
2022-11-05 14:40:02 +00:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2022-11-07 08:08:15 +00:00
gotR , err := Find [ posts ] ( ctx , tt . args . where , tt . args . fields , tt . args . group , tt . args . order , tt . args . join , tt . args . having , tt . args . limit , tt . args . in ... )
2022-11-05 14:40:02 +00:00
if ( err != nil ) != tt . wantErr {
t . Errorf ( "Find() error = %v, wantErr %v" , err , tt . wantErr )
return
}
if ! reflect . DeepEqual ( gotR , tt . wantR ) {
t . Errorf ( "Find() gotR = %v, want %v" , gotR , tt . wantR )
}
} )
}
}
func TestFindOneById ( t * testing . T ) {
type args struct {
id int
}
2022-11-06 04:04:41 +00:00
2022-11-05 14:40:02 +00:00
tests := [ ] struct {
name string
args args
2023-01-18 15:02:59 +00:00
want p ost
2022-11-05 14:40:02 +00:00
wantErr bool
} {
{
name : "t1" ,
args : args {
1 ,
} ,
2023-01-18 15:02:59 +00:00
want : func ( ) post {
r , err := Get [ post ] ( ctx , "select * from " + post { } . Table ( ) + " where ID=?" , 1 )
2023-01-13 04:31:35 +00:00
if err != nil && err != sql . ErrNoRows {
2022-11-06 04:04:41 +00:00
panic ( err )
2023-01-13 04:31:35 +00:00
} else if err == sql . ErrNoRows {
err = nil
2022-11-06 04:04:41 +00:00
}
return r
} ( ) ,
2022-11-05 14:40:02 +00:00
wantErr : false ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-01-18 15:02:59 +00:00
got , err := FindOneById [ post ] ( ctx , tt . args . id )
2023-01-13 04:31:35 +00:00
if err == sql . ErrNoRows {
err = nil
}
2022-11-05 14:40:02 +00:00
if ( err != nil ) != tt . wantErr {
t . Errorf ( "FindOneById() error = %v, wantErr %v" , err , tt . wantErr )
return
}
if ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "FindOneById() got = %v, want %v" , got , tt . want )
}
} )
}
}
func TestFirstOne ( t * testing . T ) {
type args struct {
where ParseWhere
fields string
order SqlBuilder
in [ ] [ ] any
}
tests := [ ] struct {
name string
args args
2023-01-18 15:02:59 +00:00
want post
2022-11-05 14:40:02 +00:00
wantErr bool
} {
{
name : "t1" ,
args : args {
where : SqlBuilder { { "post_status" , "publish" } } ,
fields : "*" ,
order : SqlBuilder { { "ID" , "desc" } } ,
in : nil ,
} ,
wantErr : false ,
2023-01-18 15:02:59 +00:00
want : func ( ) post {
r , err := Get [ post ] ( ctx , "select * from " + post { } . Table ( ) + " where post_status='publish' order by ID desc limit 1" )
2023-01-13 04:31:35 +00:00
if err != nil && err != sql . ErrNoRows {
2022-11-06 04:04:41 +00:00
panic ( err )
2023-01-13 04:31:35 +00:00
} else if err == sql . ErrNoRows {
err = nil
2022-11-06 04:04:41 +00:00
}
return r
} ( ) ,
2022-11-05 14:40:02 +00:00
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-01-18 15:02:59 +00:00
got , err := FirstOne [ post ] ( ctx , tt . args . where , tt . args . fields , tt . args . order , tt . args . in ... )
2022-11-05 14:40:02 +00:00
if ( err != nil ) != tt . wantErr {
t . Errorf ( "FirstOne() error = %v, wantErr %v" , err , tt . wantErr )
return
}
if ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "FirstOne() got = %v, want %v" , got , tt . want )
}
} )
}
}
func TestLastOne ( t * testing . T ) {
type args struct {
where ParseWhere
fields string
in [ ] [ ] any
}
tests := [ ] struct {
name string
args args
2023-01-18 15:02:59 +00:00
want post
2022-11-05 14:40:02 +00:00
wantErr bool
} {
2022-11-06 04:04:41 +00:00
{
name : "t1" ,
args : args {
where : SqlBuilder { {
"post_status" , "publish" ,
} } ,
fields : "*" ,
in : nil ,
} ,
2023-01-18 15:02:59 +00:00
want : func ( ) post {
r , err := Get [ post ] ( ctx , "select * from " + post { } . Table ( ) + " where post_status='publish' order by " + post { } . PrimaryKey ( ) + " desc limit 1" )
2022-11-06 04:04:41 +00:00
if err != nil {
panic ( err )
}
return r
} ( ) ,
} ,
2022-11-05 14:40:02 +00:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-01-18 15:02:59 +00:00
got , err := LastOne [ post ] ( ctx , tt . args . where , tt . args . fields , tt . args . in ... )
2022-11-05 14:40:02 +00:00
if ( err != nil ) != tt . wantErr {
t . Errorf ( "LastOne() error = %v, wantErr %v" , err , tt . wantErr )
return
}
if ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "LastOne() got = %v, want %v" , got , tt . want )
}
} )
}
}
func TestSimpleFind ( t * testing . T ) {
type args struct {
where ParseWhere
fields string
in [ ] [ ] any
}
tests := [ ] struct {
name string
args args
2023-01-18 15:02:59 +00:00
want [ ] post
2022-11-05 14:40:02 +00:00
wantErr bool
} {
2023-01-13 04:31:35 +00:00
{
name : "t1" ,
args : args {
where : SqlBuilder {
{ "ID" , "in" , "" } ,
} ,
fields : "*" ,
in : [ ] [ ] any { { 1 , 2 } } ,
} ,
2023-01-18 15:02:59 +00:00
want : func ( ) ( r [ ] post ) {
r , err := Select [ post ] ( ctx , "select * from " + post { } . Table ( ) + " where ID in (?,?)" , 1 , 2 )
2023-01-13 04:31:35 +00:00
if err != nil && err != sql . ErrNoRows {
panic ( err )
} else if err == sql . ErrNoRows {
err = nil
}
return
} ( ) ,
wantErr : false ,
} ,
2022-11-05 14:40:02 +00:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-01-18 15:02:59 +00:00
got , err := SimpleFind [ post ] ( ctx , tt . args . where , tt . args . fields , tt . args . in ... )
2022-11-05 14:40:02 +00:00
if ( err != nil ) != tt . wantErr {
t . Errorf ( "SimpleFind() error = %v, wantErr %v" , err , tt . wantErr )
return
}
if ! reflect . DeepEqual ( got , tt . want ) {
t . Errorf ( "SimpleFind() got = %v, want %v" , got , tt . want )
}
} )
}
}
2023-02-25 15:10:42 +00:00
func Test_pagination ( t * testing . T ) {
2022-11-05 14:40:02 +00:00
type args struct {
2023-02-27 13:34:48 +00:00
db dbQuery
ctx context . Context
2023-05-18 14:27:28 +00:00
q * QueryCondition
2023-02-27 13:34:48 +00:00
page int
pageSize int
2022-11-05 14:40:02 +00:00
}
2023-02-25 15:10:42 +00:00
type testCase [ T Model ] struct {
2022-11-05 14:40:02 +00:00
name string
args args
2023-02-25 15:10:42 +00:00
wantR [ ] T
2022-11-05 14:40:02 +00:00
wantTotal int
wantErr bool
2023-02-25 15:10:42 +00:00
}
tests := [ ] testCase [ post ] {
2023-01-13 04:31:35 +00:00
{
name : "t1" ,
args : args {
2023-02-25 15:10:42 +00:00
db : glob ,
ctx : ctx ,
2023-05-18 14:27:28 +00:00
q : & QueryCondition {
2023-02-25 15:10:42 +00:00
Fields : "post_type,count(*) ID" ,
Group : "post_type" ,
Having : SqlBuilder { { "ID" , ">" , "1" , "int" } } ,
2023-01-13 04:31:35 +00:00
} ,
2023-02-27 13:34:48 +00:00
page : 1 ,
pageSize : 2 ,
2023-01-13 04:31:35 +00:00
} ,
2023-01-18 15:02:59 +00:00
wantR : func ( ) ( r [ ] post ) {
2023-02-25 15:10:42 +00:00
2023-02-27 13:34:48 +00:00
err := glob . Selects ( ctx , & r , "select post_type,count(*) ID from wp_posts group by post_type having `ID`> 1 limit 2" )
2023-02-25 15:10:42 +00:00
if err != nil {
2023-01-13 04:31:35 +00:00
panic ( err )
}
2023-02-25 15:10:42 +00:00
return r
2023-01-13 04:31:35 +00:00
} ( ) ,
2023-02-25 15:10:42 +00:00
wantTotal : 7 ,
2023-01-13 04:31:35 +00:00
wantErr : false ,
} ,
2022-11-05 14:40:02 +00:00
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-02-27 13:34:48 +00:00
gotR , gotTotal , err := pagination [ post ] ( tt . args . db , tt . args . ctx , tt . args . q , tt . args . page , tt . args . pageSize )
2022-11-05 14:40:02 +00:00
if ( err != nil ) != tt . wantErr {
2023-02-25 15:10:42 +00:00
t . Errorf ( "pagination() error = %v, wantErr %v" , err , tt . wantErr )
2022-11-05 14:40:02 +00:00
return
}
if ! reflect . DeepEqual ( gotR , tt . wantR ) {
2023-02-25 15:10:42 +00:00
t . Errorf ( "pagination() gotR = %v, want %v" , gotR , tt . wantR )
2022-11-05 14:40:02 +00:00
}
if gotTotal != tt . wantTotal {
2023-02-25 15:10:42 +00:00
t . Errorf ( "pagination() gotTotal = %v, want %v" , gotTotal , tt . wantTotal )
2022-11-05 14:40:02 +00:00
}
} )
}
}
2023-02-26 13:55:03 +00:00
func Test_paginationToMap ( t * testing . T ) {
type args struct {
2023-02-27 13:34:48 +00:00
db dbQuery
ctx context . Context
2023-05-18 14:27:28 +00:00
q * QueryCondition
2023-02-27 13:34:48 +00:00
page int
pageSize int
2023-02-26 13:55:03 +00:00
}
tests := [ ] struct {
name string
args args
wantR [ ] map [ string ] string
wantTotal int
wantErr bool
} {
{
name : "t1" ,
args : args {
db : glob ,
ctx : ctx ,
2023-05-18 14:27:28 +00:00
q : & QueryCondition {
2023-02-26 13:55:03 +00:00
Fields : "ID" ,
Where : SqlBuilder { { "ID < 200" } } ,
} ,
2023-02-27 13:34:48 +00:00
page : 1 ,
pageSize : 2 ,
2023-02-26 13:55:03 +00:00
} ,
wantR : [ ] map [ string ] string { { "ID" : "63" } , { "ID" : "64" } } ,
wantTotal : 4 ,
} ,
{
name : "t2" ,
args : args {
db : glob ,
ctx : ctx ,
2023-05-18 14:27:28 +00:00
q : & QueryCondition {
2023-02-26 13:55:03 +00:00
Fields : "ID" ,
Where : SqlBuilder { { "ID < 200" } } ,
} ,
2023-02-27 13:34:48 +00:00
page : 2 ,
pageSize : 2 ,
2023-02-26 13:55:03 +00:00
} ,
wantR : [ ] map [ string ] string { { "ID" : "190" } , { "ID" : "193" } } ,
wantTotal : 4 ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
2023-02-27 13:34:48 +00:00
gotR , gotTotal , err := PaginationToMap [ post ] ( tt . args . ctx , tt . args . q , tt . args . page , tt . args . pageSize )
2023-02-26 14:01:08 +00:00
fmt . Println ( gotR , gotTotal , err )
2023-02-27 13:34:48 +00:00
gotR , gotTotal , err = PaginationToMapFromDB [ post ] ( tt . args . db , tt . args . ctx , tt . args . q , tt . args . page , tt . args . pageSize )
2023-02-26 14:01:08 +00:00
fmt . Println ( gotR , gotTotal , err )
2023-02-27 13:34:48 +00:00
gotR , gotTotal , err = paginationToMap [ post ] ( tt . args . db , tt . args . ctx , tt . args . q , tt . args . page , tt . args . pageSize )
2023-02-26 14:01:08 +00:00
2023-02-26 13:55:03 +00:00
if ( err != nil ) != tt . wantErr {
t . Errorf ( "paginationToMap() error = %v, wantErr %v" , err , tt . wantErr )
return
}
if ! reflect . DeepEqual ( gotR , tt . wantR ) {
t . Errorf ( "paginationToMap() gotR = %v, want %v" , gotR , tt . wantR )
}
if gotTotal != tt . wantTotal {
t . Errorf ( "paginationToMap() gotTotal = %v, want %v" , gotTotal , tt . wantTotal )
}
} )
}
}