Skip to content

Commit

Permalink
feat: template test (must be synced)
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Feb 3, 2025
1 parent 96c5ef4 commit dc9c5b5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cli/isuc/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
h24w17 "github.com/traP-jp/isuc"
"github.com/traP-jp/isuc/template"
)

var generateCmd = &cobra.Command{
Expand Down Expand Up @@ -34,7 +34,7 @@ var generateCmd = &cobra.Command{
return fmt.Errorf("error reading schema file: %v", err)
}

g := h24w17.NewGenerator(planContent, schemaContent)
g := template.NewGenerator(planContent, schemaContent)
g.Generate(distPath)

return nil
Expand Down
9 changes: 8 additions & 1 deletion template/cache.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func cacheKey(args []driver.Value) string {
}

func replaceFn(ctx context.Context, key string) (*cacheRows, error) {
start := time.Now()
queryerCtx, ok := ctx.Value(queryerCtxKey{}).(driver.QueryerContext)
if ok {
query := ctx.Value(queryKey{}).(string)
Expand All @@ -155,7 +156,13 @@ func replaceFn(ctx context.Context, key string) (*cacheRows, error) {
if err != nil {
return nil, err
}
return cacheRows.clone(), nil
result := cacheRows.clone()

if cache, ok := ctx.Value(cacheWithInfoKey{}).(*cacheWithInfo); ok {
elapsed := time.Since(start)
cache.RecordReplaceTime(elapsed)
}
return result, nil
}

type syncMap[T any] struct {
Expand Down
1 change: 0 additions & 1 deletion template/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var tableSchema = make(map[string]domains.TableSchema)

// TODO: generate
const cachePlanRaw = ``

const schemaRaw = ``

func init() {
Expand Down
4 changes: 2 additions & 2 deletions template/driver.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var queryMap = make(map[string]domains.CachePlanQuery)

var tableSchema = make(map[string]domains.TableSchema)

// TODO: generate
const cachePlanRaw = {{ .CachePlanRaw }}

const schemaRaw = {{ .TableSchemaRaw }}

func init() {
Expand Down Expand Up @@ -49,7 +49,7 @@ func init() {
}

conditions := query.Select.Conditions
if isSingleUniqueCondition(conditions, query.Select.Table) {
if isSingleUniqueCondition(conditions, query.Select.Table) {
caches[normalized] = &cacheWithInfo{
Cache: sc.NewMust(replaceFn, 10*time.Minute, 10*time.Minute),
query: normalized,
Expand Down
10 changes: 5 additions & 5 deletions generator.go → template/generator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package h24w17
package template

import (
"embed"
Expand All @@ -21,14 +21,14 @@ type data struct {
TableSchemaRaw string
}

//go:embed template/*.tmpl
//go:embed *.tmpl
var templates embed.FS

func NewGenerator(cachePlanRaw string, tableSchemaRaw string) *Generator {
return &Generator{
driverTmpl: template.Must(template.ParseFS(templates, "template/driver.tmpl")),
stmtTmpl: template.Must(template.ParseFS(templates, "template/stmt.tmpl")),
cacheTmpl: template.Must(template.ParseFS(templates, "template/cache.tmpl")),
driverTmpl: template.Must(template.ParseFS(templates, "driver.tmpl")),
stmtTmpl: template.Must(template.ParseFS(templates, "stmt.tmpl")),
cacheTmpl: template.Must(template.ParseFS(templates, "cache.tmpl")),
data: data{CachePlanRaw: toEscapedGoStringLiteral(cachePlanRaw), TableSchemaRaw: toEscapedGoStringLiteral(tableSchemaRaw)},
}
}
Expand Down
40 changes: 40 additions & 0 deletions template/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package template

import (
"embed"
"strings"
"testing"
"text/template"

"github.com/stretchr/testify/assert"
)

//go:embed *.go
var expectedFiles embed.FS

func TestGenerator(t *testing.T) {
g := NewGenerator("", "")
g.data.PackageName = "template"

writer := strings.Builder{}

tests := []struct {
name string
template *template.Template
}{
{"driver", g.driverTmpl},
{"stmt", g.stmtTmpl},
{"cache", g.cacheTmpl},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
expected, err := expectedFiles.ReadFile(test.name + ".go")
assert.NoError(t, err)
writer.Reset()
err = test.template.Execute(&writer, g.data)
assert.NoError(t, err)
assert.Equal(t, string(expected), writer.String())
})
}
}

0 comments on commit dc9c5b5

Please sign in to comment.