| 587 | } |
| 588 | |
| 589 | func (s *AdapterTests) TestOptionTypes() { |
| 590 | sess := s.Session() |
| 591 | |
| 592 | optionTypes := sess.Collection("option_types") |
| 593 | err := optionTypes.Truncate() |
| 594 | s.NoError(err) |
| 595 | |
| 596 | // TODO: lets do some benchmarking on these auto-wrapped option types.. |
| 597 | |
| 598 | // TODO: add nullable jsonb field mapped to a []string |
| 599 | |
| 600 | // A struct with wrapped option types defined in the struct tags |
| 601 | // for postgres string array and jsonb types |
| 602 | type optionType struct { |
| 603 | ID int64 `db:"id,omitempty"` |
| 604 | Name string `db:"name"` |
| 605 | Tags []string `db:"tags"` |
| 606 | Settings map[string]interface{} `db:"settings"` |
| 607 | } |
| 608 | |
| 609 | // Item 1 |
| 610 | item1 := optionType{ |
| 611 | Name: "Food", |
| 612 | Tags: []string{"toronto", "pizza"}, |
| 613 | Settings: map[string]interface{}{"a": 1, "b": 2}, |
| 614 | } |
| 615 | |
| 616 | record, err := optionTypes.Insert(item1) |
| 617 | s.NoError(err) |
| 618 | |
| 619 | if pk, ok := record.ID().(int64); !ok || pk == 0 { |
| 620 | s.T().Errorf("Expecting an ID.") |
| 621 | } |
| 622 | |
| 623 | var item1Chk optionType |
| 624 | err = optionTypes.Find(record).One(&item1Chk) |
| 625 | s.NoError(err) |
| 626 | |
| 627 | s.Equal(float64(1), item1Chk.Settings["a"]) |
| 628 | s.Equal("toronto", item1Chk.Tags[0]) |
| 629 | |
| 630 | // Item 1 B |
| 631 | item1b := &optionType{ |
| 632 | Name: "Golang", |
| 633 | Tags: []string{"love", "it"}, |
| 634 | Settings: map[string]interface{}{"go": 1, "lang": 2}, |
| 635 | } |
| 636 | |
| 637 | record, err = optionTypes.Insert(item1b) |
| 638 | s.NoError(err) |
| 639 | |
| 640 | if pk, ok := record.ID().(int64); !ok || pk == 0 { |
| 641 | s.T().Errorf("Expecting an ID.") |
| 642 | } |
| 643 | |
| 644 | var item1bChk optionType |
| 645 | err = optionTypes.Find(record).One(&item1bChk) |
| 646 | s.NoError(err) |