-
Notifications
You must be signed in to change notification settings - Fork 3
/
examples_test.go
97 lines (75 loc) · 2.01 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package gormtestutil
import (
"fmt"
"sync"
"testing"
"time"
)
func ExampleNewMemoryDatabase() {
type MyObject struct {
ID int
}
t := new(testing.T)
database := NewMemoryDatabase(t)
var count int64
database.Model(&MyObject{}).Count(&count)
fmt.Printf("There are %d objects\n", count)
}
func ExampleNewMemoryDatabase_ignoringForeignKeys() {
type MyObject struct {
ID int
}
t := new(testing.T)
database := NewMemoryDatabase(t, WithoutForeignKeys())
var count int64
database.Model(&MyObject{}).Count(&count)
fmt.Printf("There are %d objects\n", count)
}
func ExampleNewMemoryDatabase_withSingularConnection() {
type MyObject struct {
ID int
}
t := new(testing.T)
database1 := NewMemoryDatabase(t, WithName(t.Name()))
database2 := NewMemoryDatabase(t, WithName(t.Name()))
database1.Create(&MyObject{ID: 2})
var result MyObject
database2.First(&result)
fmt.Println(result.ID)
}
// example where the default arguments are used (expect created once and no previous expectations)
// with an upper limit of test time
func ExampleExpectCreated_defaults() {
var t *testing.T
// arrange
type SomeModel struct {
Name string
}
database := NewMemoryDatabase(t)
expectation := ExpectCreated(t, database, &SomeModel{})
// Act
_ = database.Create(SomeModel{Name: "Hello, world!"})
// Assert
if ok := EnsureCompletion(t, expectation); !ok {
t.FailNow()
}
}
// example where the default arguments are used (expect created once and no previous expectations)
// with an upper limit of test time
func ExampleExpectCreated_withVarArgs() {
var t *testing.T
var exp *sync.WaitGroup
// arrange
type SomeModel struct {
Name string
}
database := NewMemoryDatabase(t)
expectation := ExpectCreated(t, database, &SomeModel{}, WithCalls(2), WithExpectation(exp), WithoutMaximum())
// Act
_ = database.Create(SomeModel{Name: "Hello, world!"})
_ = database.Create(SomeModel{Name: "And another time"})
// Assert
if ok := EnsureCompletion(t, expectation, WithTimeout(15*time.Second)); !ok {
t.FailNow()
}
}