mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
examples: make examples testable
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_EntcPkg() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
@@ -27,4 +27,5 @@ func main() {
|
||||
}
|
||||
usr := client.User.Create().SaveX(ctx)
|
||||
fmt.Println("boring user:", usr)
|
||||
// Output: boring user: User(id=1)
|
||||
}
|
||||
@@ -8,3 +8,9 @@ Each group **has many** users, and each user can be joined to **many** groups.
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Example
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -9,28 +9,31 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent/user"
|
||||
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent"
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent/group"
|
||||
"github.com/facebookincubator/ent/examples/m2m2types/ent/user"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_M2M2Types() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// [Group(id=1, name=GitHub) Group(id=2, name=GitLab)]
|
||||
// [Group(id=1, name=GitHub)]
|
||||
// [User(id=1, age=30, name=a8m) User(id=2, age=28, name=nati)]
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -8,3 +8,9 @@ Each user can **have many** friends. If user A becomes a friend of B, B is also
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Example
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_M2MBidi() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
@@ -29,6 +29,10 @@ func main() {
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// [User(id=1, age=30, name=a8m)]
|
||||
// [User(id=2, age=28, name=nati)]
|
||||
// [User(id=1, age=30, name=a8m) User(id=2, age=28, name=nati)]
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -8,3 +8,9 @@ can follow **many** users, and can have **many** followers.
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Example
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -16,20 +16,27 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_M2MRecur() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// [User(id=2, age=28, name=nati)]
|
||||
// []
|
||||
// []
|
||||
// [User(id=1, age=30, name=a8m)]
|
||||
// [28]
|
||||
// [a8m]
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -10,3 +10,9 @@ a pet B using the pets edge, B can get its owner using the owner edge.
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Example
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -14,20 +14,24 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_O2M2Types() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// User created: User(id=1, age=30, name=a8m)
|
||||
// a8m
|
||||
// 2
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -9,3 +9,9 @@ B can get its owner using the `owner` edge.
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Examples
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -15,20 +15,24 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_O2MRecur() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// Tree leafs [1 3 5]
|
||||
// [1 3 5]
|
||||
// Node(id=1, value=2)
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -8,3 +8,8 @@ In the example, a User can have only one card, and a card must have exactly one
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Examples
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -15,20 +15,25 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_O2O2Types() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// user: User(id=1, age=30, name=Mashraki)
|
||||
// card: Card(id=1, expired=Sun Dec 8 15:04:05 2019, number=1020)
|
||||
// card: Card(id=1, expired=Sun Dec 8 15:04:05 2019, number=1020)
|
||||
// owner: User(id=1, age=30, name=Mashraki)
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -40,30 +45,34 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating user: %v", err)
|
||||
}
|
||||
log.Println("user:", a8m)
|
||||
fmt.Println("user:", a8m)
|
||||
expired, err := time.Parse(time.RFC3339, "2019-12-08T15:04:05Z")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
card1, err := client.Card.
|
||||
Create().
|
||||
SetOwner(a8m).
|
||||
SetNumber("1020").
|
||||
SetExpired(time.Now().Add(time.Minute)).
|
||||
SetExpired(expired).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating card: %v", err)
|
||||
}
|
||||
log.Println("card:", card1)
|
||||
fmt.Println("card:", card1)
|
||||
// Only returns the card of the user,
|
||||
// and expects that there's only one.
|
||||
card2, err := a8m.QueryCard().Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("querying card: %v", err)
|
||||
}
|
||||
log.Println("card:", card2)
|
||||
fmt.Println("card:", card2)
|
||||
// The Card entity is able to query its owner using
|
||||
// its back-reference.
|
||||
owner, err := card2.QueryOwner().Only(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("querying owner: %v", err)
|
||||
}
|
||||
log.Println("owner:", owner)
|
||||
fmt.Println("owner:", owner)
|
||||
return nil
|
||||
}
|
||||
@@ -9,3 +9,9 @@ B can get its spouse using the `spouse` edge.
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Examples
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -15,20 +15,25 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_O2OBidi() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// a8m
|
||||
// nati
|
||||
// 2
|
||||
// nati
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -9,3 +9,9 @@ B can get its pointer using `prev`.
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Example
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -15,20 +15,23 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_O2ORecur() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
if err := Do(ctx, client); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// 1 2 3 4 5
|
||||
// true
|
||||
}
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) error {
|
||||
@@ -54,7 +57,7 @@ func Do(ctx context.Context, client *ent.Client) error {
|
||||
|
||||
// Loop over the list and print it. `FirstX` panics if an error occur.
|
||||
for curr = head; curr != nil; curr = curr.QueryNext().FirstX(ctx) {
|
||||
fmt.Printf("%d ", curr.Value)
|
||||
fmt.Printf(" %d", curr.Value)
|
||||
}
|
||||
// Output: 1 2 3 4 5
|
||||
|
||||
@@ -7,3 +7,9 @@ See example in https://entgo.io/docs/traversals
|
||||
```console
|
||||
go generate ./...
|
||||
```
|
||||
|
||||
### Run Examples
|
||||
|
||||
```console
|
||||
go test
|
||||
```
|
||||
|
||||
@@ -9,25 +9,23 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent/user"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent/pet"
|
||||
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent"
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent/group"
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent/pet"
|
||||
"github.com/facebookincubator/ent/examples/traversal/ent/user"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Example_Traversal() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
@@ -54,6 +52,13 @@ func main() {
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Output:
|
||||
// Pets created: Pet(id=1, name=Pedro) Pet(id=2, name=Xabi) Pet(id=3, name=Coco)
|
||||
// User(id=3, age=37, name=Alex)
|
||||
// [Pet(id=1, name=Pedro) Pet(id=2, name=Xabi)]
|
||||
// User(id=5, age=30, name=Ariel)
|
||||
// Pets created: Pet(id=4, name=Pedro) Pet(id=5, name=Xabi) Pet(id=6, name=Coco)
|
||||
// Pets created: Pet(id=7, name=Pedro) Pet(id=8, name=Xabi) Pet(id=9, name=Coco)
|
||||
}
|
||||
|
||||
func Gen(ctx context.Context, client *ent.Client) error {
|
||||
Reference in New Issue
Block a user