Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Add firebase filtering to Trash.GetNotPublished
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kibish committed Dec 14, 2017
1 parent 0d7c6fd commit fa7970a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
8 changes: 6 additions & 2 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

type mockFirebase struct {
set func(path string, v interface{}) (err error)
get func(path string) (result json.RawMessage, err error)
set func(path string, v interface{}) (err error)
get func(path string) (result json.RawMessage, err error)
filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error)
}

func (m mockFirebase) Set(path string, v interface{}) (err error) {
Expand All @@ -23,6 +24,9 @@ func (m mockFirebase) Set(path string, v interface{}) (err error) {
func (m mockFirebase) Get(path string) (result json.RawMessage, err error) {
return m.get(path)
}
func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) {
return m.filterEqual(path, field, value)
}

type mockSlack struct {
oAuthAccess func(code string) (r *slack.OAuthAccessResponse, err error)
Expand Down
13 changes: 13 additions & 0 deletions pkg/firebase/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Firebase struct {
type IFirebase interface {
Set(path string, v interface{}) (err error)
Get(path string) (result json.RawMessage, err error)
FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error)
}

// New return new instance of the Firebase
Expand Down Expand Up @@ -63,3 +64,15 @@ func (f *Firebase) Get(path string) (result json.RawMessage, err error) {

return
}

// FilterEqual filter records with field equal to specific value
func (f *Firebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) {
ref, err := f.realFirebase.Ref(path)
if err != nil {
return
}

err = ref.OrderBy(field).EqualToValue(value).Value(&result)

return
}
9 changes: 7 additions & 2 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

type mockFirebase struct {
set func(path string, v interface{}) (err error)
get func(path string) (result json.RawMessage, err error)
set func(path string, v interface{}) (err error)
get func(path string) (result json.RawMessage, err error)
filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error)
}

func (m mockFirebase) Set(path string, v interface{}) (err error) {
Expand All @@ -17,6 +18,10 @@ func (m mockFirebase) Get(path string) (result json.RawMessage, err error) {
return m.get(path)
}

func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) {
return m.filterEqual(path, field, value)
}

func TestNew(t *testing.T) {
fm := &mockFirebase{}

Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (t *Trash) Set(trash *TrashData) (err error) {

// GetNotPublished return all not published records
func (t *Trash) GetNotPublished() (finResult map[string]*TrashData, err error) {
result, err := t.firebase.Get(trashPath)
result, err := t.firebase.FilterEqual(trashPath, "published", false)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/trash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestTrashSetErr(t *testing.T) {

func TestGetNotPublished(t *testing.T) {
fm := &mockFirebase{}
fm.get = func(path string) (result json.RawMessage, err error) {
fm.filterEqual = func(path, field string, value interface{}) (result json.RawMessage, err error) {
return []byte(`{"12":{"id":"12","data":"aaa","published":true},"23":{"id":"23","data":"bbb","published":false}}`), nil
}

Expand All @@ -55,7 +55,7 @@ func TestGetNotPublished(t *testing.T) {

func TestGetNotPublishedErr(t *testing.T) {
fm := &mockFirebase{}
fm.get = func(path string) (result json.RawMessage, err error) {
fm.filterEqual = func(path, field string, value interface{}) (result json.RawMessage, err error) {
return []byte(``), errors.New("Oops")
}

Expand Down

0 comments on commit fa7970a

Please sign in to comment.