Simple blogging system written in Go https://goblog.app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

63 lines
1.2 KiB

package main
import (
"testing"
)
func Test_database(t *testing.T) {
t.Run("Basic Database Test", func(t *testing.T) {
app := &goBlog{
cfg: &config{},
}
db, err := app.openDatabase(":memory:", false)
if err != nil {
t.Fatalf("Error: %v", err)
}
_, err = db.exec("create table test(test text);")
if err != nil {
t.Fatalf("Error: %v", err)
}
_, err = db.exec("insert into test (test) values ('Test')")
if err != nil {
t.Fatalf("Error: %v", err)
}
row, err := db.queryRow("select count(test) from test")
if err != nil {
t.Fatalf("Error: %v", err)
}
var test1 int
err = row.Scan(&test1)
if err != nil {
t.Fatalf("Error: %v", err)
}
if test1 != 1 {
t.Error("Wrong result")
}
rows, err := db.query("select count(test), test from test")
if err != nil {
t.Fatalf("Error: %v", err)
}
var test2 int
var testStr string
if !rows.Next() {
t.Error("No result row")
}
err = rows.Scan(&test2, &testStr)
if err != nil {
t.Fatalf("Error: %v", err)
}
if test2 != 1 || testStr != "Test" {
t.Error("Wrong result")
}
err = db.close()
if err != nil {
t.Fatalf("Error: %v", err)
}
})
}