List unit types

This commit is contained in:
Matthew Dillon 2014-11-30 15:33:28 -09:00
parent b7caffa373
commit d535cd85ff
9 changed files with 152 additions and 0 deletions

View file

@ -34,3 +34,15 @@ func (s *unitTypesStore) Create(unit_type *models.UnitType) (bool, error) {
}
return true, nil
}
func (s *unitTypesStore) List(opt *models.UnitTypeListOptions) ([]*models.UnitType, error) {
if opt == nil {
opt = &models.UnitTypeListOptions{}
}
var unit_types []*models.UnitType
err := s.dbh.Select(&unit_types, `SELECT * FROM unit_types LIMIT $1 OFFSET $2;`, opt.PerPageOrDefault(), opt.Offset())
if err != nil {
return nil, err
}
return unit_types, nil
}

View file

@ -62,3 +62,26 @@ func TestUnitTypesStore_Create_db(t *testing.T) {
t.Error("want nonzero unit_type.Id after submitting")
}
}
func TestUnitTypesStore_List_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
want_unit_type := insertUnitType(t, tx)
want := []*models.UnitType{want_unit_type}
d := NewDatastore(tx)
unit_types, err := d.UnitTypes.List(&models.UnitTypeListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
if err != nil {
t.Fatal(err)
}
for i := range want {
normalizeTime(&want[i].CreatedAt, &want[i].UpdatedAt, &want[i].DeletedAt)
normalizeTime(&unit_types[i].CreatedAt, &unit_types[i].UpdatedAt, &unit_types[i].DeletedAt)
}
if !reflect.DeepEqual(unit_types, want) {
t.Errorf("got unit_types %+v, want %+v", unit_types, want)
}
}