Skip to content

Commit

Permalink
updates integration tests, some fixes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Nov 24, 2021
1 parent 8094183 commit f0ad397
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 31 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Example Blog",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "./examples/blog/main.go"
}
]
}
6 changes: 6 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package orm_test

import "testing"


func TestIntegration_Sqlite3(t *testing.T) {}
5 changes: 3 additions & 2 deletions obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *Schema) valuesOf(o interface{}, withPK bool) []interface{} {
}
t := reflect.TypeOf(o)
v := reflect.ValueOf(o)
if t.Kind() == reflect.Ptr {
for t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}
Expand Down Expand Up @@ -112,7 +112,8 @@ func (s *Schema) getPkValue(v interface{}) interface{} {
}
t := reflect.TypeOf(v)
val := reflect.ValueOf(v)
if t.Kind() == reflect.Ptr {
for t.Kind() == reflect.Ptr {
t = t.Elem()
val = val.Elem()
}
for i, field := range s.metadata.Fields {
Expand Down
14 changes: 13 additions & 1 deletion relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package orm

import (
"fmt"

"github.com/gertd/go-pluralize"
)

Expand Down Expand Up @@ -101,11 +102,13 @@ func (q *QueryBuilder) HasOne(schema *Schema, configs ...HasOneConfigurator) *Qu
type BelongsToConfig struct {
OwnerTable string
LocalForeignKey string
OwnerForeignColumn string
}
type BelongsToConfigurator func(config *BelongsToConfig)
type _BelongsToConfigurator struct {
OwnerTable func(name string) BelongsToConfigurator
LocalKey func(name string) BelongsToConfigurator
OwnerForeignColumn func(name string) BelongsToConfigurator
}

var BelongsToConfigurators = &_BelongsToConfigurator{
Expand All @@ -119,6 +122,11 @@ var BelongsToConfigurators = &_BelongsToConfigurator{
config.LocalForeignKey = name
}
},
OwnerForeignColumn: func(name string) BelongsToConfigurator {
return func(config *BelongsToConfig) {
config.OwnerForeignColumn = name
}
},
}

func (q *QueryBuilder) BelongsTo(schema *Schema, configs ...BelongsToConfigurator) *QueryBuilder {
Expand All @@ -132,6 +140,9 @@ func (q *QueryBuilder) BelongsTo(schema *Schema, configs ...BelongsToConfigurato
if c.LocalForeignKey == "" {
c.LocalForeignKey = pluralize.NewClient().Singular(c.OwnerTable) + "_id"
}
if c.OwnerForeignColumn == "" {
c.OwnerForeignColumn = "id"
}

ph := q.schema.dialect.PlaceholderChar
if q.schema.dialect.IncludeIndexInPlaceholder {
Expand All @@ -142,13 +153,14 @@ func (q *QueryBuilder) BelongsTo(schema *Schema, configs ...BelongsToConfigurato
for idx, field := range q.schema.metadata.Fields {
if field.Name == c.LocalForeignKey {
ownerIDidx = idx
break
}
}

ownerID := q.schema.valuesOf(q.model, true)[ownerIDidx]
q.
From(c.OwnerTable).
Where(WhereHelpers.Equal(c.LocalForeignKey, ph)).
Where(WhereHelpers.Equal(c.OwnerForeignColumn, ph)).
WithArgs(ownerID)

return q
Expand Down
15 changes: 15 additions & 0 deletions tests/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go"
}
]
}
6 changes: 4 additions & 2 deletions examples/blog/go.mod → tests/go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module github.com/golobby/orm/examples/blog
module github.com/golobby/orm/integrations/sqlite

go 1.17

replace github.com/golobby/orm => ../..
replace github.com/golobby/orm => ./..

require (
github.com/golobby/orm v1.0.0
github.com/mattn/go-sqlite3 v1.14.9
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gertd/go-pluralize v0.1.7 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
1 change: 1 addition & 0 deletions examples/blog/go.sum → tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
Expand Down
55 changes: 29 additions & 26 deletions examples/blog/main.go → tests/sqlite_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package main
package main_test

import (
"database/sql"
"fmt"
"os"
"testing"

"github.com/golobby/orm"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)

type Post struct {
ID int64
Comments []*Comment
Content string
Categories []*Category
}

type Comment struct {
ID int64
PostID int64
Post Post
Content string
}

Expand All @@ -27,57 +28,59 @@ type Category struct {
Posts []*Post
}

func main() {
func TestSqlite3(t *testing.T) {
_ = os.Remove("blog.db")
defer func() {
os.Remove("blog.db")
}()
dbGolobby, err := sql.Open("sqlite3", "blog.db")
if err != nil {
panic(err)
assert.NoError(t, err)
}
if err = dbGolobby.Ping(); err != nil {
panic(err)
assert.NoError(t, err)
}
createPosts := `CREATE TABLE IF NOT EXISTS posts (id integer primary key, content text);`
createComments := `CREATE TABLE IF NOT EXISTS comments (id integer primary key, post_id integer, content text);`
_, err = dbGolobby.Exec(createPosts)
if err != nil {
panic(err)
assert.NoError(t, err)
}
_, err = dbGolobby.Exec(createComments)
if err != nil {
panic(err)
assert.NoError(t, err)
}
postRepository := orm.NewRepository(dbGolobby, orm.Dialects.SQLite3, &Post{})
commentRepository := orm.NewRepository(dbGolobby, orm.Dialects.SQLite3, &Comment{})
postSchema := orm.NewSchema(dbGolobby, orm.Dialects.SQLite3, &Post{})
commentSchema := orm.NewSchema(dbGolobby, orm.Dialects.SQLite3, &Comment{})

firstPost := &Post{
Content: "salam donya",
}
err = postRepository.Entity(firstPost).Save()

err = postSchema.Save(firstPost)
if err != nil {
panic(err)
assert.NoError(t, err)
}
fmt.Println("Post primary key is ", firstPost.ID)

firstComment := &Comment{
PostID: firstPost.ID,
Content: "comment aval",
}
err = commentRepository.Entity(firstComment).Save()
err = commentSchema.Save(firstComment)
if err != nil {
panic(err)
assert.NoError(t, err)
}
err = postRepository.Entity(firstPost).HasMany(&firstPost.Comments)
var firstPostComment Comment
err = postSchema.NewQuery().Model(&firstPost).HasMany(commentSchema).Bind(&firstPostComment, commentSchema)
if err != nil {
panic(err)
assert.NoError(t, err)
}
fmt.Printf("Loaded post %d comment -> %+v\n", firstPost.ID, firstPost.Comments[0])
fmt.Printf("Loaded post %d comment -> %+v\n", firstPost.ID, firstComment.PostID)
var newPost Post
err = commentRepository.Entity(firstComment).BelongsTo(&newPost)
err = commentSchema.NewQuery().Model(&firstComment).BelongsTo(postSchema).Bind(&newPost, postSchema)
if err != nil {
panic(err)
assert.NoError(t, err)
}
fmt.Printf("loaded comment %d post %+v", firstComment.PostID, firstPost)

//err = postRepository.Entity(firstPost).ManyToMany(&firstPost.Categories)
//if err != nil {
// panic(err)
//}
//fmt.Printf("loaded post %d categories %+v", firstPost.ID, firstPost.Categories)
}

0 comments on commit f0ad397

Please sign in to comment.