example_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package todo
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "entgo.io/ent/dialect"
  7. _ "github.com/mattn/go-sqlite3"
  8. "todo/ent"
  9. "todo/ent/todo"
  10. )
  11. func setup() (context.Context, *ent.Client, func()) {
  12. client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1")
  13. if err != nil {
  14. log.Fatalf("failed opening connection to sqlite: %v", err)
  15. }
  16. closer := func() { _ = client.Close() }
  17. ctx := context.Background()
  18. // Run the automatic migration tool to create all schema resources
  19. if err := client.Schema.Create(ctx); err != nil {
  20. log.Fatalf("failed creating schema resources: %v", err)
  21. }
  22. return ctx, client, closer
  23. }
  24. func addTwo(ctx context.Context, client *ent.Client) (*ent.Todo, *ent.Todo) {
  25. task1, err := client.Todo.Create().SetText("Add GraphQL Example").Save(ctx)
  26. if err != nil {
  27. log.Fatalf("failed creating a todo: %v", err)
  28. }
  29. task2, err := client.Todo.Create().SetText("Add Tracing Example").Save(ctx)
  30. if err != nil {
  31. log.Fatalf("failed creating a todo: %v", err)
  32. }
  33. return task1, task2
  34. }
  35. func addTwoConnected(ctx context.Context, client *ent.Client) (*ent.Todo, *ent.Todo) {
  36. task1, task2 := addTwo(ctx, client)
  37. if err := task2.Update().SetParent(task1).Exec(ctx); err != nil {
  38. log.Fatalf("failed connecting todo2 to its parent: %v", err)
  39. }
  40. return task1, task2
  41. }
  42. func Example_todo1() {
  43. _, _, closer := setup()
  44. defer closer()
  45. // Output:
  46. }
  47. func Example_todo2() {
  48. ctx, client, closer := setup()
  49. defer closer()
  50. task1, err := client.Todo.Create().SetText(" ").Save(ctx)
  51. if err != nil {
  52. log.Fatalf("failed creating a todo: %v", err)
  53. }
  54. fmt.Printf("Todo(id=%d)\n", task1.ID)
  55. // Output:
  56. // Todo(id=1)
  57. }
  58. func Example_todo3() {
  59. ctx, client, closer := setup()
  60. defer closer()
  61. task1, task2 := addTwo(ctx, client)
  62. fmt.Printf("%d: %q\n", task1.ID, task1.Text)
  63. fmt.Printf("%d: %q\n", task2.ID, task2.Text)
  64. // Output:
  65. // 1: "Add GraphQL Example"
  66. // 2: "Add Tracing Example"
  67. }
  68. func Example_todo4() {
  69. ctx, client, closer := setup()
  70. defer closer()
  71. _, _ = addTwoConnected(ctx, client)
  72. // Output:
  73. }
  74. func Example_todo5() {
  75. ctx, client, closer := setup()
  76. defer closer()
  77. _, _ = addTwoConnected(ctx, client)
  78. // Query all todo items.
  79. items, err := client.Todo.Query().All(ctx)
  80. if err != nil {
  81. log.Fatalf("failed querying all todos: %v", err)
  82. }
  83. for _, item := range items {
  84. fmt.Printf("%d: %q\n", item.ID, item.Text)
  85. }
  86. // Output:
  87. // 1: "Add GraphQL Example"
  88. // 2: "Add Tracing Example"
  89. }
  90. func Example_todo6() {
  91. ctx, client, closer := setup()
  92. defer closer()
  93. _, _ = addTwoConnected(ctx, client)
  94. // Query all todo items that depend on other items
  95. items, err := client.Todo.Query().Where(todo.HasParent()).All(ctx)
  96. if err != nil {
  97. log.Fatalf("failed querying all todos: %v", err)
  98. }
  99. for _, item := range items {
  100. fmt.Printf("%d: %q\n", item.ID, item.Text)
  101. }
  102. // Output:
  103. // 2: "Add Tracing Example"
  104. }
  105. func Example_todo7() {
  106. ctx, client, closer := setup()
  107. defer closer()
  108. _, _ = addTwoConnected(ctx, client)
  109. // Query all todo items that don't depend on other items and have items that depend on them
  110. items, err := client.Todo.Query().Where(
  111. todo.Not(todo.HasParent()),
  112. todo.HasChildren(),
  113. ).All(ctx)
  114. if err != nil {
  115. log.Fatalf("failed querying todos: %v", err)
  116. }
  117. for _, item := range items {
  118. fmt.Printf("%d: %q\n", item.ID, item.Text)
  119. }
  120. // Output:
  121. // 1: "Add GraphQL Example"
  122. }
  123. func Example_todo8() {
  124. ctx, client, closer := setup()
  125. defer closer()
  126. _, _ = addTwoConnected(ctx, client)
  127. // Query parent through its children
  128. parent, err := client.Todo.Query().
  129. Where(todo.HasParent()). // Filter children
  130. QueryParent(). // Climb to their parent
  131. Only(ctx) // Expect exactly one result
  132. if err != nil {
  133. log.Fatalf("failed querying todos: %v", err)
  134. }
  135. fmt.Printf("%d: %q\n", parent.ID, parent.Text)
  136. // Output:
  137. // 1: "Add GraphQL Example"
  138. }