Repository: Ptt-official-app/Ptt-backend
Pull request #87 (2021-02-21 10:51:58)

Add mock for repository.GetPopularArticleRecords
and repository.BBSPopularArticleRecord

since go-bbs has not implemented it yet

<!-- 原則上不接受沒有 Issue ID 的 PR。 -->
<!-- We don't accept PRs which has no Issue ID. -->

## 👏 解決掉的 issue / Resolved Issues
- close https://github.com/Ptt-official-app/Ptt-backend/issues/86

## 📝 相關的 issue / Related Issues
- https://github.com/Ptt-official-app/Ptt-backend/issues/50

## ⛏ 變更內容 / Details of Changes
- add `BBSPopularArticleRecord` to mock `bbs.PopularArticleRecord` since go-bbs has not implemented it yet
- add `GetPopularArticles()` to interface `Repository`


Patch:
diff --git a/internal/repository/article.go b/internal/repository/article.go
index 1adef4b..7834a1d 100644
--- a/internal/repository/article.go
+++ b/internal/repository/article.go
@@ -6,7 +6,27 @@ import (
 	"github.com/PichuChen/go-bbs"
 )
 
-func (repo *repository) GetPopularArticles(ctx context.Context) ([]bbs.ArticleRecord, error) {
-	// TODO: should implement popular articles there
-	return []bbs.ArticleRecord{}, nil
+// PopularArticleRecord is an ArticleRecord which has boardID information.
+type PopularArticleRecord interface {
+	// Note: go-bbs has not implemented this yet
+	// TODO: use bbs.PopularArticleRecord or something when it is ready
+	bbs.ArticleRecord
+	BoardID() string
+}
+
+type popularArticleRecord struct {
+	// Note: go-bbs has not implemented this yet
+	// TODO: use bbs.PopularArticleRecord or something when it is ready
+	bbs.ArticleRecord
+}
+
+// BoardID returns the board id of the popular article
+func (record *popularArticleRecord) BoardID() string {
+	return ""
+}
+
+func (repo *repository) GetPopularArticles(ctx context.Context) ([]PopularArticleRecord, error) {
+	// Note: go-bbs has not implemented this yet
+	// TODO: delegate to repo.db when it is ready
+	return []PopularArticleRecord{}, nil
 }
diff --git a/internal/repository/repository.go b/internal/repository/repository.go
index 2d1ca2c..db6ad38 100644
--- a/internal/repository/repository.go
+++ b/internal/repository/repository.go
@@ -33,7 +33,7 @@ type Repository interface {
 
 	// article.go
 	// GetPopularArticles returns all popular articles
-	GetPopularArticles(ctx context.Context) ([]bbs.ArticleRecord, error)
+	GetPopularArticles(ctx context.Context) ([]PopularArticleRecord, error)
 }
 
 type repository struct {
diff --git a/internal/usecase/article.go b/internal/usecase/article.go
index 1fffe6b..fdc2524 100644
--- a/internal/usecase/article.go
+++ b/internal/usecase/article.go
@@ -4,11 +4,11 @@ import (
 	"context"
 	"fmt"
 
-	"github.com/PichuChen/go-bbs"
+	"github.com/Ptt-official-app/Ptt-backend/internal/repository"
 )
 
 // GetPopularArticles returns articles by descending comment_count
-func (usecase *usecase) GetPopularArticles(ctx context.Context) ([]bbs.ArticleRecord, error) {
+func (usecase *usecase) GetPopularArticles(ctx context.Context) ([]repository.PopularArticleRecord, error) {
 	articles, err := usecase.repo.GetPopularArticles(ctx)
 	if err != nil {
 		return nil, fmt.Errorf("GetPopularArticles error: %w", err)
diff --git a/internal/usecase/usecase.go b/internal/usecase/usecase.go
index 55693fc..7f97679 100644
--- a/internal/usecase/usecase.go
+++ b/internal/usecase/usecase.go
@@ -45,7 +45,7 @@ type Usecase interface {
 
 	// article.go
 	// GetPopularArticles returns all popular articles
-	GetPopularArticles(ctx context.Context) ([]bbs.ArticleRecord, error)
+	GetPopularArticles(ctx context.Context) ([]repository.PopularArticleRecord, error)
 }
 
 type usecase struct {


Test patch:
diff --git a/internal/delivery/http/route_articles_test.go b/internal/delivery/http/route_articles_test.go
index 5ce8a00..33913ae 100644
--- a/internal/delivery/http/route_articles_test.go
+++ b/internal/delivery/http/route_articles_test.go
@@ -3,9 +3,9 @@ package http
 import (
 	"context"
 
-	"github.com/PichuChen/go-bbs"
+	"github.com/Ptt-official-app/Ptt-backend/internal/repository"
 )
 
-func (usecase *MockUsecase) GetPopularArticles(ctx context.Context) ([]bbs.ArticleRecord, error) {
-	return []bbs.ArticleRecord{}, nil
+func (usecase *MockUsecase) GetPopularArticles(ctx context.Context) ([]repository.PopularArticleRecord, error) {
+	return []repository.PopularArticleRecord{}, nil
 }
diff --git a/internal/usecase/article_mock_test.go b/internal/usecase/article_mock_test.go
deleted file mode 100644
index 97bbfe6..0000000
--- a/internal/usecase/article_mock_test.go
+++ /dev/null
@@ -1,29 +0,0 @@
-package usecase
-
-import (
-	"context"
-	"time"
-
-	"github.com/PichuChen/go-bbs"
-)
-
-func (repo *MockRepository) GetPopularArticles(ctx context.Context) ([]bbs.ArticleRecord, error) {
-	ret := []bbs.ArticleRecord{
-		&MockArticle{"Popular Article 1"},
-		&MockArticle{"Popular Article 2"},
-		&MockArticle{"Popular Article 3"},
-	}
-	return ret, nil
-}
-
-type MockArticle struct {
-	title string
-}
-
-func (a *MockArticle) Filename() string    { return "" }
-func (a *MockArticle) Modified() time.Time { return time.Unix(0, 0) }
-func (a *MockArticle) Recommend() int      { return 0 }
-func (a *MockArticle) Date() string        { return "" }
-func (a *MockArticle) Title() string       { return a.title }
-func (a *MockArticle) Money() int          { return 0 }
-func (a *MockArticle) Owner() string       { return "" }
diff --git a/internal/usecase/board_mock_test.go b/internal/usecase/board_mock_test.go
index 810747b..48a98cb 100644
--- a/internal/usecase/board_mock_test.go
+++ b/internal/usecase/board_mock_test.go
@@ -5,6 +5,7 @@ import (
 	"time"
 
 	"github.com/PichuChen/go-bbs"
+	"github.com/Ptt-official-app/Ptt-backend/internal/repository"
 )
 
 func (repo *MockRepository) GetBoards(ctx context.Context) []bbs.BoardRecord {
@@ -41,6 +42,47 @@ func (repo *MockRepository) GetBoardArticleRecords(ctx context.Context, boardID
 	}
 	return result, nil
 }
+
+func (repo *MockRepository) GetPopularArticles(ctx context.Context) ([]repository.PopularArticleRecord, error) {
+	articleRecords := []MockPopularArticle{
+		{
+			filename:       "",
+			modified:       time.Time{},
+			recommendCount: 10,
+			owner:          "SYSOP",
+			date:           "",
+			title:          "Popular Article 1",
+			money:          0,
+			boardID: "Gossiping",
+		},
+		{
+			filename:       "",
+			modified:       time.Time{},
+			recommendCount: -20,
+			owner:          "XDXD",
+			date:           "",
+			title:          "Popular Article 2",
+			money:          0,
+			boardID: "Gossiping",
+		},
+		{
+			filename:       "",
+			modified:       time.Time{},
+			recommendCount: 0,
+			owner:          "TEST",
+			date:           "",
+			title:          "Popular Article 3",
+			money:          0,
+			boardID: "Joke",
+		},
+	}
+	result := make([]repository.PopularArticleRecord, len(articleRecords))
+	for i, v := range articleRecords {
+		result[i] = v
+	}
+	return result, nil
+}
+
 func (repo *MockRepository) GetBoardTreasureRecords(ctx context.Context, boardID string, treasureIDs []string) ([]bbs.ArticleRecord, error) {
 	return []bbs.ArticleRecord{}, nil
 }
@@ -62,3 +104,23 @@ func (m MockArticleRecord) Owner() string       { return m.owner }
 func (m MockArticleRecord) Date() string        { return m.date }
 func (m MockArticleRecord) Title() string       { return m.title }
 func (m MockArticleRecord) Money() int          { return m.money }
+
+type MockPopularArticle struct {
+	filename       string
+	modified       time.Time
+	recommendCount int
+	owner          string
+	date           string
+	title          string
+	money          int
+	boardID        string
+}
+
+func (m MockPopularArticle) Filename() string    { return "" }
+func (m MockPopularArticle) Modified() time.Time { return time.Unix(0, 0) }
+func (m MockPopularArticle) Recommend() int      { return 0 }
+func (m MockPopularArticle) Date() string        { return "" }
+func (m MockPopularArticle) Title() string       { return m.title }
+func (m MockPopularArticle) Money() int          { return 0 }
+func (m MockPopularArticle) Owner() string       { return "" }
+func (m MockPopularArticle) BoardID() string     { return m.boardID }