DDD Meilisearch showcase: domain/app/infra/interfaces, Taskfile, tests
This commit is contained in:
commit
a9674ce14e
22 files changed
+1584
No files matched your search
@@ -0,0 +1,86 @@
|
||||
//go:build integration
|
||||
|
||||
// Package test exercises the real Meilisearch adapter. Run with:
|
||||
//
|
||||
// task meili:up && task test:integration
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/app"
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/domain/catalog"
|
||||
"git.jokresner.de/johannes/meilisearch-showcase/internal/infra/meili"
|
||||
)
|
||||
|
||||
func newService(t *testing.T) (*app.CatalogService, context.Context) {
|
||||
t.Helper()
|
||||
host := os.Getenv("MEILI_HOST")
|
||||
if host == "" {
|
||||
host = "http://localhost:7700"
|
||||
}
|
||||
idx := meili.NewMovieIndex(host, os.Getenv("MEILI_MASTER_KEY"), "movies_test")
|
||||
t.Cleanup(idx.Close)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
svc := app.NewCatalogService(idx)
|
||||
if err := svc.Setup(ctx); err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
if _, err := svc.ImportMovies(ctx, seed); err != nil {
|
||||
t.Fatalf("import: %v", err)
|
||||
}
|
||||
return svc, ctx
|
||||
}
|
||||
|
||||
var seed = []app.MovieInput{
|
||||
{ID: "1", Title: "The Matrix", Overview: "A hacker learns reality is a simulation.", Genres: []string{"Sci-Fi", "Action"}, Year: 1999, Rating: 8.7},
|
||||
{ID: "2", Title: "Alien", Overview: "A lethal organism aboard a towing ship.", Genres: []string{"Sci-Fi", "Horror"}, Year: 1979, Rating: 8.5},
|
||||
{ID: "3", Title: "Whiplash", Overview: "A drummer pushed to his limits.", Genres: []string{"Drama", "Music"}, Year: 2014, Rating: 8.5},
|
||||
}
|
||||
|
||||
func TestTypoTolerance(t *testing.T) {
|
||||
svc, ctx := newService(t)
|
||||
res, err := svc.Search(ctx, catalog.Query{Text: "matrx"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res.Hits) == 0 || res.Hits[0].Movie.Title() != "The Matrix" {
|
||||
t.Fatalf("typo search failed: %+v", res.Hits)
|
||||
}
|
||||
if res.Hits[0].Formatted == "" {
|
||||
t.Fatal("expected highlighted title")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterAndFacets(t *testing.T) {
|
||||
svc, ctx := newService(t)
|
||||
res, err := svc.Search(ctx, catalog.Query{Genre: "Sci-Fi", MinYear: 1990})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res.Hits) != 1 || res.Hits[0].Movie.Title() != "The Matrix" {
|
||||
t.Fatalf("filter failed: %+v", res.Hits)
|
||||
}
|
||||
if len(res.Genres) == 0 {
|
||||
t.Fatal("expected genre facets")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAndDelete(t *testing.T) {
|
||||
svc, ctx := newService(t)
|
||||
if _, err := svc.Get(ctx, "2"); err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if err := svc.Delete(ctx, "2"); err != nil {
|
||||
t.Fatalf("delete: %v", err)
|
||||
}
|
||||
if _, err := svc.Get(ctx, "2"); err != catalog.ErrNotFound {
|
||||
t.Fatalf("want ErrNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user