MCPcopy
hub / github.com/jmoiron/sqlx / TestFieldsEmbedded

Function TestFieldsEmbedded

reflectx/reflect_test.go:298–386  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

296}
297
298func TestFieldsEmbedded(t *testing.T) {
299 m := NewMapper("db")
300
301 type Person struct {
302 Name string `db:"name,size=64"`
303 }
304 type Place struct {
305 Name string `db:"name"`
306 }
307 type Article struct {
308 Title string `db:"title"`
309 }
310 type PP struct {
311 Person `db:"person,required"`
312 Place `db:",someflag"`
313 Article `db:",required"`
314 }
315 // PP columns: (person.name name title)
316
317 pp := PP{}
318 pp.Person.Name = "Peter"
319 pp.Place.Name = "Toronto"
320 pp.Article.Title = "Best city ever"
321
322 fields := m.TypeMap(reflect.TypeOf(pp))
323 // for i, f := range fields {
324 // log.Println(i, f)
325 // }
326
327 ppv := reflect.ValueOf(pp)
328
329 v := m.FieldByName(ppv, "person.name")
330 if v.Interface().(string) != pp.Person.Name {
331 t.Errorf("Expecting %s, got %s", pp.Person.Name, v.Interface().(string))
332 }
333
334 v = m.FieldByName(ppv, "name")
335 if v.Interface().(string) != pp.Place.Name {
336 t.Errorf("Expecting %s, got %s", pp.Place.Name, v.Interface().(string))
337 }
338
339 v = m.FieldByName(ppv, "title")
340 if v.Interface().(string) != pp.Article.Title {
341 t.Errorf("Expecting %s, got %s", pp.Article.Title, v.Interface().(string))
342 }
343
344 fi := fields.GetByPath("person")
345 if _, ok := fi.Options["required"]; !ok {
346 t.Errorf("Expecting required option to be set")
347 }
348 if !fi.Embedded {
349 t.Errorf("Expecting field to be embedded")
350 }
351 if len(fi.Index) != 1 || fi.Index[0] != 0 {
352 t.Errorf("Expecting index to be [0]")
353 }
354
355 fi = fields.GetByPath("person.name")

Callers

nothing calls this directly

Calls 7

TypeMapMethod · 0.95
FieldByNameMethod · 0.95
TraversalsByNameMethod · 0.95
NewMapperFunction · 0.85
ErrorfMethod · 0.80
GetByPathMethod · 0.80
GetByTraversalMethod · 0.80

Tested by

no test coverage detected