Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pirosiki197 committed Jan 30, 2025
1 parent 712c35f commit 6aab09a
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
19 changes: 19 additions & 0 deletions test/cache/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ const cachePlanRaw = `queries:
operator: eq
placeholder:
index: 0
- query: UPDATE ` + "`" + `users` + "`" + ` SET ` + "`" + `name` + "`" + ` = ? WHERE ` + "`" + `id` + "`" + ` = ?;
type: update
table: users
targets:
- column: name
placeholder:
index: 0
conditions:
- column: id
operator: eq
placeholder:
index: 1
- query: INSERT INTO ` + "`" + `users` + "`" + ` (` + "`" + `name` + "`" + `, ` + "`" + `age` + "`" + `, ` + "`" + `created_at` + "`" + `) VALUES (?, ?, ?);
type: insert
table: users
columns:
- name
- age
- created_at
`

const schemaRaw = `CREATE TABLE ` + "`" + `users` + "`" + ` (
Expand Down
15 changes: 15 additions & 0 deletions test/cache/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cache

import (
"time"

"github.com/motoki317/sc"
)

func ResetCache() {
for key := range caches {
v := caches[key]
*v.cache = *sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute)
caches[key] = v
}
}
95 changes: 94 additions & 1 deletion test/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/traP-jp/isuc/normalizer"
"github.com/traP-jp/isuc/test/cache"
)

func TestSimpleQuery(t *testing.T) {
cache.PurgeAllCaches()
cache.ResetCache()
db := NewDB(t)

var user User
Expand All @@ -24,3 +25,95 @@ func TestSimpleQuery(t *testing.T) {
assert.Equal(t, 0, stats.Hits)
assert.Equal(t, 1, stats.Misses)
}

func TestSimpleQueryCache(t *testing.T) {
cache.ResetCache()
db := NewDB(t)

var user User
err := db.Get(&user, "SELECT * FROM `users` WHERE `id` = ?", 1)
if err != nil {
t.Fatal(err)
}

AssertUser(t, InitialData[0], user)

var user2 User
err = db.Get(&user2, "SELECT * FROM `users` WHERE `id` = ?", 1)
if err != nil {
t.Fatal(err)
}

AssertUser(t, InitialData[0], user2)

stats := cache.ExportCacheStats()[normalizer.NormalizeQuery("SELECT * FROM `users` WHERE `id` = ?")]
assert.Equal(t, 1, stats.Hits)
assert.Equal(t, 1, stats.Misses)
}

func TestSelectAfterUpdate(t *testing.T) {
cache.ResetCache()
db := NewDB(t)

var user User
err := db.Get(&user, "SELECT * FROM `users` WHERE `id` = ?", 1)
if err != nil {
t.Fatal(err)
}

AssertUser(t, InitialData[0], user)

_, err = db.Exec("UPDATE `users` SET `name` = ? WHERE `id` = ?", "updated", 1)
if err != nil {
t.Fatal(err)
}
user.Name = "updated"

// no cache hit because users with id=1 is updated
var user2 User
err = db.Get(&user2, "SELECT * FROM `users` WHERE `id` = ?", 1)
if err != nil {
t.Fatal(err)
}

AssertUser(t, user, user2)

stats := cache.ExportCacheStats()[normalizer.NormalizeQuery("SELECT * FROM `users` WHERE `id` = ?")]
assert.Equal(t, 0, stats.Hits)
assert.Equal(t, 2, stats.Misses)
}

func TestSelectAfterInsert(t *testing.T) {
cache.ResetCache()
db := NewDB(t)

var user User
err := db.Get(&user, "SELECT * FROM `users` WHERE `id` = ?", 1)
if err != nil {
t.Fatal(err)
}

AssertUser(t, InitialData[0], user)

newUser := User{
Name: "new",
Age: 10,
CreatedAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
}
_, err = db.Exec("INSERT INTO `users` (`name`, `age`, `created_at`) VALUES (?, ?, ?)", newUser.Name, newUser.Age, newUser.CreatedAt)
if err != nil {
t.Fatal(err)
}

// cache hit because users with id=1 is not updated
err = db.Get(&user, "SELECT * FROM `users` WHERE `id` = ?", 1)
if err != nil {
t.Fatal(err)
}

AssertUser(t, InitialData[0], user)

stats := cache.ExportCacheStats()[normalizer.NormalizeQuery("SELECT * FROM `users` WHERE `id` = ?")]
assert.Equal(t, 1, stats.Hits)
assert.Equal(t, 1, stats.Misses)
}
19 changes: 19 additions & 0 deletions test/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,22 @@ queries:
operator: eq
placeholder:
index: 0
- query: UPDATE `users` SET `name` = ? WHERE `id` = ?;
type: update
table: users
targets:
- column: name
placeholder:
index: 0
conditions:
- column: id
operator: eq
placeholder:
index: 1
- query: INSERT INTO `users` (`name`, `age`, `created_at`) VALUES (?, ?, ?);
type: insert
table: users
columns:
- name
- age
- created_at
1 change: 1 addition & 0 deletions test/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type User struct {
}

func AssertUser(t *testing.T, expected, actual User) {
t.Helper()
assert.Equal(t, expected.ID, actual.ID, "ID")
assert.Equal(t, expected.Name, actual.Name, "Name")
assert.Equal(t, expected.Age, actual.Age, "Age")
Expand Down

0 comments on commit 6aab09a

Please sign in to comment.