Files
Asim Aslam c7657f73f4
goreleaser / goreleaser (push) Has been cancelled
Refactor agent plan storage, update docs, and release v6 (#2977)
* test(harness): read agent plan from the scoped store

The store-scoping change moved an agent's plan from the default table
key agent/{name}/plan to its own table (database "agent", table {name},
key "plan"). The plan-delegate harness tests still read the old key and
failed with 'not found'; read through store.Scope(mem, "agent", name)
like the agent does.

* docs: orient agents-first across README, landing, and docs overview

Lead with agents (then services and flows), surface MCP + A2A as the
interop story, and frame agents as services. Landing hero and feature
grid reordered agents-first with an A2A gateway card.

* v6: module path go-micro.dev/v6, TLS secure by default, NewService

Cut v6. Three breaking changes, bundled so the major bump is paid once:

- Module path go-micro.dev/v5 -> go-micro.dev/v6 across all imports + go.mod.
- TLS verification on by default (was off). MICRO_TLS_SECURE removed;
  MICRO_TLS_INSECURE=true opts out for self-signed/dev.
- micro.NewService(name, opts...) is the canonical service constructor,
  symmetric with NewAgent/NewFlow; micro.New kept as a deprecated alias;
  the old name-less NewService(opts...) removed. Generators emit NewService.

Also ports the JWT auth token provider in-module (go-micro.dev/v6/auth/jwt/token
on golang-jwt/jwt/v5), dropping the v5-pinned github.com/micro/plugins/v5/auth/jwt
and the deprecated dgrijalva/jwt-go.

Docs/README/landing updated to v6 and @latest; v5->v6 migration guide added;
CHANGELOG cut as [6.0.0]. Blog posts left at their historical versions.

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-06-18 11:55:35 +01:00

222 lines
4.9 KiB
Go

package sqlite
import (
"context"
"testing"
"go-micro.dev/v6/model"
)
type User struct {
ID string `json:"id" model:"key"`
Name string `json:"name" model:"index"`
Email string `json:"email"`
Age int `json:"age"`
}
func setup(t *testing.T) model.Model {
t.Helper()
db := New(":memory:")
if err := db.Register(&User{}); err != nil {
t.Fatalf("register: %v", err)
}
return db
}
func TestCRUD(t *testing.T) {
db := setup(t)
ctx := context.Background()
// Create
err := db.Create(ctx, &User{ID: "1", Name: "Alice", Email: "alice@test.com", Age: 30})
if err != nil {
t.Fatalf("create: %v", err)
}
// Read
u := &User{}
err = db.Read(ctx, "1", u)
if err != nil {
t.Fatalf("read: %v", err)
}
if u.Name != "Alice" {
t.Errorf("expected Alice, got %s", u.Name)
}
if u.Age != 30 {
t.Errorf("expected age 30, got %d", u.Age)
}
// Update
u.Name = "Alice Updated"
u.Age = 31
err = db.Update(ctx, u)
if err != nil {
t.Fatalf("update: %v", err)
}
u2 := &User{}
db.Read(ctx, "1", u2)
if u2.Name != "Alice Updated" {
t.Errorf("expected 'Alice Updated', got %s", u2.Name)
}
if u2.Age != 31 {
t.Errorf("expected age 31, got %d", u2.Age)
}
// Delete
err = db.Delete(ctx, "1", &User{})
if err != nil {
t.Fatalf("delete: %v", err)
}
err = db.Read(ctx, "1", &User{})
if err != model.ErrNotFound {
t.Errorf("expected ErrNotFound, got %v", err)
}
}
func TestDuplicateKey(t *testing.T) {
db := setup(t)
ctx := context.Background()
db.Create(ctx, &User{ID: "1", Name: "Alice"})
err := db.Create(ctx, &User{ID: "1", Name: "Bob"})
if err != model.ErrDuplicateKey {
t.Errorf("expected ErrDuplicateKey, got %v", err)
}
}
func TestNotFound(t *testing.T) {
db := setup(t)
ctx := context.Background()
err := db.Read(ctx, "nonexistent", &User{})
if err != model.ErrNotFound {
t.Errorf("expected ErrNotFound, got %v", err)
}
err = db.Update(ctx, &User{ID: "nonexistent"})
if err != model.ErrNotFound {
t.Errorf("expected ErrNotFound on update, got %v", err)
}
err = db.Delete(ctx, "nonexistent", &User{})
if err != model.ErrNotFound {
t.Errorf("expected ErrNotFound on delete, got %v", err)
}
}
func TestListWithFilter(t *testing.T) {
db := setup(t)
ctx := context.Background()
db.Create(ctx, &User{ID: "1", Name: "Alice", Age: 30})
db.Create(ctx, &User{ID: "2", Name: "Bob", Age: 25})
db.Create(ctx, &User{ID: "3", Name: "Alice", Age: 35})
var results []*User
err := db.List(ctx, &results, model.Where("name", "Alice"))
if err != nil {
t.Fatalf("list: %v", err)
}
if len(results) != 2 {
t.Errorf("expected 2 Alices, got %d", len(results))
}
}
func TestListWithOrder(t *testing.T) {
db := setup(t)
ctx := context.Background()
db.Create(ctx, &User{ID: "1", Name: "Charlie", Age: 35})
db.Create(ctx, &User{ID: "2", Name: "Alice", Age: 30})
db.Create(ctx, &User{ID: "3", Name: "Bob", Age: 25})
var results []*User
err := db.List(ctx, &results, model.OrderAsc("name"))
if err != nil {
t.Fatalf("list: %v", err)
}
if len(results) != 3 {
t.Fatalf("expected 3, got %d", len(results))
}
if results[0].Name != "Alice" {
t.Errorf("expected Alice first, got %s", results[0].Name)
}
if results[2].Name != "Charlie" {
t.Errorf("expected Charlie last, got %s", results[2].Name)
}
}
func TestListWithLimitOffset(t *testing.T) {
db := setup(t)
ctx := context.Background()
db.Create(ctx, &User{ID: "1", Name: "A", Age: 1})
db.Create(ctx, &User{ID: "2", Name: "B", Age: 2})
db.Create(ctx, &User{ID: "3", Name: "C", Age: 3})
db.Create(ctx, &User{ID: "4", Name: "D", Age: 4})
var results []*User
err := db.List(ctx, &results,
model.OrderAsc("name"),
model.Limit(2),
model.Offset(1),
)
if err != nil {
t.Fatalf("list: %v", err)
}
if len(results) != 2 {
t.Fatalf("expected 2, got %d", len(results))
}
if results[0].Name != "B" {
t.Errorf("expected B, got %s", results[0].Name)
}
if results[1].Name != "C" {
t.Errorf("expected C, got %s", results[1].Name)
}
}
func TestCount(t *testing.T) {
db := setup(t)
ctx := context.Background()
db.Create(ctx, &User{ID: "1", Name: "Alice", Age: 30})
db.Create(ctx, &User{ID: "2", Name: "Bob", Age: 25})
db.Create(ctx, &User{ID: "3", Name: "Alice", Age: 35})
count, err := db.Count(ctx, &User{})
if err != nil {
t.Fatalf("count: %v", err)
}
if count != 3 {
t.Errorf("expected 3, got %d", count)
}
count, err = db.Count(ctx, &User{}, model.Where("name", "Alice"))
if err != nil {
t.Fatalf("count with filter: %v", err)
}
if count != 2 {
t.Errorf("expected 2, got %d", count)
}
}
func TestWhereOp(t *testing.T) {
db := setup(t)
ctx := context.Background()
db.Create(ctx, &User{ID: "1", Name: "Alice", Age: 30})
db.Create(ctx, &User{ID: "2", Name: "Bob", Age: 25})
db.Create(ctx, &User{ID: "3", Name: "Charlie", Age: 35})
var results []*User
err := db.List(ctx, &results, model.WhereOp("age", ">", 28))
if err != nil {
t.Fatalf("list: %v", err)
}
if len(results) != 2 {
t.Errorf("expected 2 (age > 28), got %d", len(results))
}
}