-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring_id_extractor_test.go
82 lines (66 loc) · 2.01 KB
/
string_id_extractor_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
package db_test
import (
"github.com/drborges/datastore-model"
"github.com/drborges/goexpect"
"reflect"
"testing"
)
func TestStringIdExtractorExtractsId(t *testing.T) {
t.Parallel()
type CreditCard struct {
db.Model
Owner string `db:"id"`
}
card := &CreditCard{Owner: "Borges"}
field := reflect.TypeOf(card).Elem().Field(1)
value := reflect.ValueOf(card).Elem().Field(1)
meta := &db.Metadata{}
err := db.StringIdExtractor{meta}.Extract(card, field, value)
expect := goexpect.New(t)
expect(err).ToBe(nil)
expect(meta.StringID).ToBe(card.Owner)
}
func TestStringIdExtractorExtractsReturnsErrMissingStringId(t *testing.T) {
t.Parallel()
type CreditCard struct {
db.Model
Owner string `db:"id"`
}
card := &CreditCard{}
field := reflect.TypeOf(card).Elem().Field(1)
value := reflect.ValueOf(card).Elem().Field(1)
meta := &db.Metadata{}
err := db.StringIdExtractor{meta}.Extract(card, field, value)
expect := goexpect.New(t)
expect(err).ToBe(db.ErrMissingStringId)
expect(meta.StringID).ToBe("")
}
func TestStringIdExtractorAcceptsTaggedStringField(t *testing.T) {
t.Parallel()
type MyModel struct {
db.Model
String string `db:"id"`
NotTaggedString string
Slice []string
Map map[string]string
Rune rune
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
}
expect := goexpect.New(t)
extractor := db.StringIdExtractor{}
elem := reflect.TypeOf(&MyModel{}).Elem()
expect(extractor.Accept(elem.Field(0))).ToBe(false)
expect(extractor.Accept(elem.Field(1))).ToBe(true)
expect(extractor.Accept(elem.Field(2))).ToBe(false)
expect(extractor.Accept(elem.Field(3))).ToBe(false)
expect(extractor.Accept(elem.Field(4))).ToBe(false)
expect(extractor.Accept(elem.Field(5))).ToBe(false)
expect(extractor.Accept(elem.Field(6))).ToBe(false)
expect(extractor.Accept(elem.Field(7))).ToBe(false)
expect(extractor.Accept(elem.Field(8))).ToBe(false)
expect(extractor.Accept(elem.Field(9))).ToBe(false)
}