Skip to content

Commit

Permalink
Implement GetStoryIdFromComment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexferrari88 committed Oct 4, 2022
1 parent 91818db commit 8295819
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
20 changes: 20 additions & 0 deletions pkg/gohn/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"encoding/json"
"errors"
"fmt"
"sync/atomic"
)
Expand Down Expand Up @@ -159,3 +160,22 @@ func (s Story) IsTopLevelComment(item Item) bool {
}
return false
}

// GetStoryIdFromComment returns the ID of the story for a given comment.
func (c client) GetStoryIdFromComment(item Item) (int, error) {
if item.Type != "comment" {
return 0, errors.New("item is not a comment")
}
var storyId int
for {
if item.Type == "story" {
storyId = item.ID
break
}
if item.Parent == 0 {
break
}
item, _ = c.GetItem(item.Parent)
}
return storyId, nil
}
40 changes: 39 additions & 1 deletion test/gohn/items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
)

func setupMocks() (parent gohn.Item, kids []gohn.Item, mockClient *mocks.MockHTTPClient, err error) {
// parent is the main item with ID = 1. It has 3 kids
// parent is the main item with ID = 1. It has 3 kids (IDs = 2, 3, 4)
// kid[0] (ID = 2) has 2 kids (kid[3] (ID = 5) and kid[4] (ID = 6))
// kid[1] (ID = 3) has 1 kid (kid[5] (ID = 7))
// kid[2] (ID = 4) has 0 kids
mockItems := mocks.NewMockItems(7)
parent = mockItems[0]
parent.Type = "story"
kids = mockItems[1:]
mocks.AddKidsToMockItem(&parent, kids[0:3])
mocks.AddKidsToMockItem(&kids[0], kids[3:5])
Expand Down Expand Up @@ -195,3 +196,40 @@ func TestGetOrderedCommentsIDs(t *testing.T) {
t.Errorf("expected order %v, got %v", expectedIDs, orderedIDs)
}
}

func TestGetStoryIdFromComment(t *testing.T) {
parent, kids, mockClient, err := setupMocks()

if err != nil {
t.Error(err)
}

client := gohn.NewClient(context.Background(), mockClient)
items := client.RetrieveKidsItems(parent, nil)

if len(items) != 6 {
t.Fatalf("expected 6 items, got %v", len(items))
}

mockRespKid0JSON, _ := mocks.NewMockResponse(http.StatusOK, kids[0])
mockRespParentJSON, _ := mocks.NewMockResponse(http.StatusOK, parent)
mockClient = mocks.NewMockClient([]string{
fmt.Sprintf(gohn.ITEM_URL, 2),
fmt.Sprintf(gohn.ITEM_URL, 1),
}, []*http.Response{
mockRespKid0JSON,
mockRespParentJSON,
})

client = gohn.NewClient(context.Background(), mockClient)

expectedStoryID := 1
storyID, err := client.GetStoryIdFromComment(items[5])
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if expectedStoryID != storyID {
t.Errorf("expected story ID %v, got %v", expectedStoryID, storyID)
}
}
8 changes: 4 additions & 4 deletions test/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func NewMockItems(num int) []gohn.Item {
items[i] = gohn.Item{
ID: i + 1,
Text: "test",
Type: "comment",
}
}
return items
Expand All @@ -61,10 +62,9 @@ func AddParentToMockItem(item *gohn.Item, parent *gohn.Item) {
}

func AddKidsToMockItem(item *gohn.Item, kids []gohn.Item) {
for _, kid := range kids {
kid := kid
AddParentToMockItem(&kid, item)
item.Kids = append(item.Kids, kid.ID)
for i := range kids {
AddParentToMockItem(&kids[i], item)
item.Kids = append(item.Kids, kids[i].ID)
}
}

Expand Down

0 comments on commit 8295819

Please sign in to comment.