Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Nov 24, 2021
1 parent f0ad397 commit ea1013e
Showing 1 changed file with 32 additions and 38 deletions.
70 changes: 32 additions & 38 deletions tests/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main_test

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

Expand All @@ -12,20 +11,19 @@ import (
)

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

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

type Category struct {
ID int64
ID int64
Title string
Posts []*Post
}

func TestSqlite3(t *testing.T) {
Expand All @@ -34,53 +32,49 @@ func TestSqlite3(t *testing.T) {
os.Remove("blog.db")
}()
dbGolobby, err := sql.Open("sqlite3", "blog.db")
if err != nil {
assert.NoError(t, err)
}
if err = dbGolobby.Ping(); err != nil {
assert.NoError(t, err)
}
assert.NoError(t, err)
err = dbGolobby.Ping()
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);`
createCategories := `CREATE TABLE IF NOT EXISTS categories (id integer primary key, name text)`
createPostCategories := `CREATE TABLE IF NOT EXISTS post_categories (id integer primary key, post_id integer, category_id integer)`

_, err = dbGolobby.Exec(createPosts)
if err != nil {
assert.NoError(t, err)
}
assert.NoError(t, err)
_, err = dbGolobby.Exec(createComments)
if err != nil {
assert.NoError(t, err)
}
assert.NoError(t, err)
_, err = dbGolobby.Exec(createCategories)
assert.NoError(t, err)
_, err = dbGolobby.Exec(createPostCategories)
assert.NoError(t, err)
postSchema := orm.NewSchema(dbGolobby, orm.Dialects.SQLite3, &Post{})
commentSchema := orm.NewSchema(dbGolobby, orm.Dialects.SQLite3, &Comment{})

firstPost := &Post{
Content: "salam donya",
}


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

assert.NoError(t, err)
assert.EqualValues(t, 1, firstPost.ID)

firstComment := &Comment{
PostID: firstPost.ID,
Content: "comment aval",
}
err = commentSchema.Save(firstComment)
if err != nil {
assert.NoError(t, err)
}
assert.NoError(t, err)
assert.EqualValues(t, 1, firstComment.ID)

var firstPostComment Comment
err = postSchema.NewQuery().Model(&firstPost).HasMany(commentSchema).Bind(&firstPostComment, commentSchema)
if err != nil {
assert.NoError(t, err)
}
fmt.Printf("Loaded post %d comment -> %+v\n", firstPost.ID, firstComment.PostID)
assert.NoError(t, err)
assert.Equal(t, firstComment.ID, firstPostComment.ID)

var newPost Post
err = commentSchema.NewQuery().Model(&firstComment).BelongsTo(postSchema).Bind(&newPost, postSchema)
if err != nil {
assert.NoError(t, err)
}
fmt.Printf("loaded comment %d post %+v", firstComment.PostID, firstPost)
}
assert.NoError(t, err)
assert.Equal(t, firstPost.ID, newPost.ID)
}

0 comments on commit ea1013e

Please sign in to comment.