navidrome/persistence/helpers_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.5 KiB
Go
Raw Permalink Normal View History

2020-03-26 01:33:32 +01:00
package persistence
import (
2020-05-09 06:49:23 +02:00
"time"
2020-03-26 01:33:32 +01:00
"github.com/Masterminds/squirrel"
2022-07-26 22:47:16 +02:00
. "github.com/onsi/ginkgo/v2"
2020-03-26 01:33:32 +01:00
. "github.com/onsi/gomega"
)
var _ = Describe("Helpers", func() {
2020-05-09 06:49:23 +02:00
Describe("toSnakeCase", func() {
It("converts camelCase", func() {
Expect(toSnakeCase("camelCase")).To(Equal("camel_case"))
})
It("converts PascalCase", func() {
Expect(toSnakeCase("PascalCase")).To(Equal("pascal_case"))
})
It("converts ALLCAPS", func() {
Expect(toSnakeCase("ALLCAPS")).To(Equal("allcaps"))
})
It("does not converts snake_case", func() {
Expect(toSnakeCase("snake_case")).To(Equal("snake_case"))
})
})
Describe("toCamelCase", func() {
It("converts snake_case", func() {
Expect(toCamelCase("snake_case")).To(Equal("snakeCase"))
})
It("converts PascalCase", func() {
Expect(toCamelCase("PascalCase")).To(Equal("PascalCase"))
})
It("converts camelCase", func() {
Expect(toCamelCase("camelCase")).To(Equal("camelCase"))
})
It("converts ALLCAPS", func() {
Expect(toCamelCase("ALLCAPS")).To(Equal("ALLCAPS"))
})
})
Describe("toSQLArgs", func() {
type Embed struct{}
2020-05-09 06:49:23 +02:00
type Model struct {
Embed `structs:"-"`
ID string `structs:"id" json:"id"`
AlbumId string `structs:"album_id" json:"albumId"`
PlayCount int `structs:"play_count" json:"playCount"`
UpdatedAt *time.Time `structs:"updated_at"`
CreatedAt time.Time `structs:"created_at"`
2020-05-09 06:49:23 +02:00
}
It("returns a map with snake_case keys", func() {
now := time.Now()
m := &Model{ID: "123", AlbumId: "456", CreatedAt: now, UpdatedAt: &now, PlayCount: 2}
args, err := toSQLArgs(m)
2020-05-09 06:49:23 +02:00
Expect(err).To(BeNil())
Expect(args).To(SatisfyAll(
HaveKeyWithValue("id", "123"),
HaveKeyWithValue("album_id", "456"),
HaveKeyWithValue("play_count", 2),
HaveKeyWithValue("updated_at", now.Format(time.RFC3339Nano)),
HaveKeyWithValue("created_at", now.Format(time.RFC3339Nano)),
Not(HaveKey("Embed")),
))
2020-05-09 06:49:23 +02:00
})
})
Describe("exists", func() {
2020-03-26 01:33:32 +01:00
It("constructs the correct EXISTS query", func() {
2020-05-01 15:17:21 +02:00
e := exists("album", squirrel.Eq{"id": 1})
2020-03-26 01:33:32 +01:00
sql, args, err := e.ToSql()
Expect(sql).To(Equal("exists (select 1 from album where id = ?)"))
2021-07-19 03:55:11 +02:00
Expect(args).To(ConsistOf(1))
2020-03-26 01:33:32 +01:00
Expect(err).To(BeNil())
})
})
2020-10-20 23:16:24 +02:00
Describe("notExists", func() {
It("constructs the correct NOT EXISTS query", func() {
e := notExists("artist", squirrel.ConcatExpr("id = artist_id"))
sql, args, err := e.ToSql()
Expect(sql).To(Equal("not exists (select 1 from artist where id = artist_id)"))
Expect(args).To(BeEmpty())
Expect(err).To(BeNil())
})
})
2020-03-26 01:33:32 +01:00
})