Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 85 additions & 8 deletions examples/go.work.sum

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion examples/real-project/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
github.com/Liphium/magic/v2 v2.0.0-00010101000000-000000000000
github.com/gofiber/fiber/v2 v2.52.9
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.11.1
gorm.io/driver/postgres v1.6.0
gorm.io/gorm v1.31.0
resty.dev/v3 v3.0.0-beta.6
)

require (
Expand All @@ -33,6 +35,7 @@ require (
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand Down Expand Up @@ -69,6 +72,7 @@ require (
github.com/muesli/termenv v0.16.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand All @@ -81,7 +85,9 @@ require (
go.opentelemetry.io/otel/metric v1.39.0 // indirect
go.opentelemetry.io/otel/trace v1.39.0 // indirect
golang.org/x/crypto v0.46.0 // indirect
golang.org/x/net v0.48.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
)
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions examples/real-project/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
Expand Down Expand Up @@ -143,6 +147,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -176,6 +182,8 @@ golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -186,6 +194,8 @@ golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand All @@ -197,3 +207,5 @@ gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk=
pgregory.net/rapid v1.2.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
resty.dev/v3 v3.0.0-beta.6 h1:ghRdNpoE8/wBCv+kTKIOauW1aCrSIeTq7GxtfYgtevU=
resty.dev/v3 v3.0.0-beta.6/go.mod h1:NTOerrC/4T7/FE6tXIZGIysXXBdgNqwMZuKtxpea9NM=
1 change: 0 additions & 1 deletion examples/real-project/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build release
// +build release

package main

Expand Down
1 change: 0 additions & 1 deletion examples/real-project/main_magic.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !release
// +build !release

package main

Expand Down
24 changes: 20 additions & 4 deletions examples/real-project/starter/scripts_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"os"
"real-project/database"
"real-project/util"

"github.com/gofiber/fiber/v2"
"resty.dev/v3"
)

// This method would ideally be created in a shared package between all the scripts.
Expand All @@ -17,14 +19,28 @@ func GetPath() string {
//
// You could go into the database and add it there, but we want to be able to call the endpoint using scripts.
func CreatePost(post database.Post) error {
_, err := util.Post[interface{}](GetPath()+"/posts", post, util.Headers{})
client := resty.New()
defer client.Close()

_, err := client.R().
SetBody(post).
Post(GetPath() + "/posts")
return err
}

// Script for printing all the posts using the endpoint.
func PrintPosts() error {
posts, err := util.Get[[]database.Post](GetPath()+"/posts", util.Headers{})
if err != nil {
client := resty.New()
defer client.Close()

res, err := client.R().
Get(GetPath() + "/posts")
if err != nil || res.StatusCode() != fiber.StatusOK {
return fmt.Errorf("couldn't get posts: %v", err)
}

var posts []database.Post
if err := json.Unmarshal(res.Bytes(), &posts); err != nil {
return err
}

Expand Down
60 changes: 32 additions & 28 deletions examples/real-project/starter/start_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package starter_test

import (
"encoding/json"
"real-project/database"
"real-project/starter"
"real-project/util"
"reflect"
"slices"
"testing"

"github.com/Liphium/magic/v2"
"github.com/google/uuid"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"resty.dev/v3"
)

// Let Magic start the app and needed containers right here so it runs before any tests can run.
Expand All @@ -21,36 +21,39 @@ func TestApp(t *testing.T) {
database.Connect()

t.Run("post is added properly", func(t *testing.T) {
client := resty.New()
defer client.Close()

testPost := database.Post{
Author: "Test",
Content: "Hello world!",
}

created, err := util.Post[database.Post](starter.GetPath()+"/posts", testPost, util.Headers{})
if err != nil {
t.Fatal("Failed to create post:", err)
}
res, err := client.R().
SetBody(testPost).
Post(starter.GetPath() + "/posts")
assert.Nil(t, err)
assert.Equal(t, fiber.StatusCreated, res.StatusCode())

var created database.Post
assert.Nil(t, json.Unmarshal(res.Bytes(), &created))

testPost.ID = created.ID
if !reflect.DeepEqual(testPost, created) {
t.Fatalf("Post is not equal: expected=%+v got=%+v", testPost, created)
}
assert.EqualValues(t, testPost, created)

// You can check if it was actually created straight in the database.
// In this case it might not be so useful, but when you call complex endpoints, direct access to the database
// can be really handy to be able to fully test if the endpoint did the correct thing.
var post database.Post
if err := database.DBConn.Where("id = ?", created.ID).Take(&post).Error; err != nil {
t.Fatal("Couldn't retrive created post:", err)
}

if !reflect.DeepEqual(testPost, created) {
t.Fatalf("Post is not equal to one in database: expected=%+v got=%+v", testPost, post)
}
err = database.DBConn.Where("id = ?", created.ID).Take(&post).Error
assert.Nil(t, err)

assert.EqualValues(t, testPost, created)
})

t.Run("posts can be retrived", func(t *testing.T) {
client := resty.New()
defer client.Close()

// You can clear databases here, but if you don't rely on an empty database for a test, just not doing it is fine, too.
magic.GetTestRunner().ClearDatabases()
Expand All @@ -60,17 +63,18 @@ func TestApp(t *testing.T) {
t.Fatal("Couldn't seed database:", err)
}

posts, err := util.Get[[]database.Post](starter.GetPath()+"/posts", util.Headers{})
if err != nil {
t.Fatalf("Couldn't get posts from the backend: %v", err)
}
res, err := client.R().
Get(starter.GetPath() + "/posts")
assert.Nil(t, err)
assert.Equal(t, fiber.StatusOK, res.StatusCode())

var posts []database.Post
assert.Nil(t, json.Unmarshal(res.Bytes(), &posts))
assert.Equal(t, len(starter.SamplePosts), len(posts))

if !slices.EqualFunc(starter.SamplePosts, posts, func(e1, e2 database.Post) bool {
e1.ID = uuid.Nil
e2.ID = uuid.Nil
return reflect.DeepEqual(e1, e2)
}) {
t.Fatalf("Gotten posts don't match sample posts: expected=%v got=%v", starter.SamplePosts, posts)
for i, post := range starter.SamplePosts {
assert.Equal(t, post.Author, posts[i].Author)
assert.Equal(t, post.Content, posts[i].Content)
}
})

Expand Down
93 changes: 0 additions & 93 deletions examples/real-project/util/requests.go

This file was deleted.