github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/dbutil/file2sqlite/file2sqlite_test.go (about)

     1  package file2sqlite
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gocaveman/caveman/dbutil"
     7  	"github.com/jinzhu/gorm"
     8  
     9  	_ "github.com/mattn/go-sqlite3"
    10  )
    11  
    12  // need a pattern of building Page (or whatever) structs out of pages - Meta should probably
    13  // go in as a JSON field (to be figured out - marshaling is a bit difficult, since Meta should
    14  // also be able to be replaced if needed), Page struct
    15  // should of course have a default but also dev should be able to completely replace struct
    16  // with no issues - if they want more fields or to change stuff, default should implement
    17  // category and tags but this is just a basic ref impl - user could embed this struct and
    18  // add more fields or as above just replace entirely - Go template code would rely on field
    19  // names so if no Category field then those bits that reference it would fail, but otherwise
    20  // things should work correctly; this is a special case but is really just a complex example
    21  // of real life usage of file2sqlite
    22  
    23  type Page struct {
    24  	ID    int
    25  	Title string
    26  	Path  string
    27  	Tags  dbutil.StringValueList `gorm:"type:text"`
    28  }
    29  
    30  func TestF2S(t *testing.T) {
    31  
    32  	connstr := `file:file2sqlite?mode=memory&cache=shared`
    33  
    34  	// rawdb, err := sql.Open("sqlite3", connstr)
    35  	// if err != nil {
    36  	// 	t.Fatal(err)
    37  	// }
    38  	// defer rawdb.Close()
    39  
    40  	// _, err = rawdb.Exec(`CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)`)
    41  	// if err != nil {
    42  	// 	t.Fatal(err)
    43  	// }
    44  
    45  	db, err := gorm.Open("sqlite3", connstr)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	defer db.Close()
    50  
    51  	if errs := db.AutoMigrate(&Page{}).GetErrors(); len(errs) > 0 {
    52  		t.Fatal(errs)
    53  	}
    54  
    55  	if errs := db.Create(&Page{ID: 0, Title: "example 123", Path: "/example123", Tags: []string{"tag1", "tag2"}}).GetErrors(); len(errs) > 0 {
    56  		t.Fatal(errs)
    57  	}
    58  
    59  	p := Page{}
    60  	db.First(&p, 1)
    61  
    62  	t.Logf("p = %#v", p)
    63  
    64  }