Skip to content

Commit

Permalink
Merge pull request #162 from joshsoftware/report-review-recognition-api
Browse files Browse the repository at this point in the history
APIs for report and review a recognition
  • Loading branch information
iamanujvrma authored Jun 18, 2020
2 parents 0db900c + 1157c00 commit 7ed039f
Show file tree
Hide file tree
Showing 18 changed files with 828 additions and 6 deletions.
8 changes: 8 additions & 0 deletions go-backend/db/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
logger "github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"testing"
"time"
)

var (
now time.Time
mockedRows *sqlmock.Rows
)

func InitMockDB() (s Storer, sqlConn *sqlx.DB, sqlmockInstance sqlmock.Sqlmock) {
Expand All @@ -27,4 +33,6 @@ func InitMockDB() (s Storer, sqlConn *sqlx.DB, sqlmockInstance sqlmock.Sqlmock)
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(OrganizationTestSuite))
suite.Run(t, new(RecognitionHi5TestSuite))
suite.Run(t, new(ReportedRecognitionTestSuite))
suite.Run(t, new(RecognitionModerationTestSuite))
}
6 changes: 6 additions & 0 deletions go-backend/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ type Storer interface {

//Recognition
CreateRecognitionHi5(context.Context, RecognitionHi5, int) error

//Reported Recognition
CreateReportedRecognition(context.Context, int64, ReportedRecognition) (ReportedRecognition, error)

//Recognition Moderation
CreateRecognitionModeration(context.Context, int64, RecognitionModeration) (RecognitionModeration, error)
}
10 changes: 10 additions & 0 deletions go-backend/db/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,13 @@ func (m *DBMockStore) UpdateUser(ctx context.Context, user User, id int) (update
args := m.Called(ctx, user, id)
return args.Get(0).(User), args.Error(1)
}

func (m *DBMockStore) CreateReportedRecognition(ctx context.Context, recognitionID int64, reportedRecognition ReportedRecognition) (resp ReportedRecognition, err error) {
args := m.Called(ctx, recognitionID, reportedRecognition)
return args.Get(0).(ReportedRecognition), args.Error(1)
}

func (m *DBMockStore) CreateRecognitionModeration(ctx context.Context, recognitionID int64, recognitionModeration RecognitionModeration) (resp RecognitionModeration, err error) {
args := m.Called(ctx, recognitionID, recognitionModeration)
return args.Get(0).(RecognitionModeration), args.Error(1)
}
2 changes: 0 additions & 2 deletions go-backend/db/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type OrganizationTestSuite struct {
sqlmock sqlmock.Sqlmock
}

var mockedRows *sqlmock.Rows

var expectedOrg = Organization{
ID: 1,
Name: "test organization",
Expand Down
64 changes: 64 additions & 0 deletions go-backend/db/recognition_moderation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package db

import (
"context"
"time"

logger "github.com/sirupsen/logrus"
)

const (
createRecognitionModerationQuery = `INSERT INTO recognition_moderation (recognition_id, is_inappropriate,
moderator_comment, moderated_by, moderated_at, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, recognition_id, is_inappropriate, moderator_comment, moderated_by, moderated_at, created_at, updated_at`
)

type RecognitionModeration struct {
ID int64 `db:"id" json:"id"`
RecognitionID int64 `db:"recognition_id" json:"recognition_id"`
IsInappropriate *bool `db:"is_inappropriate" json:"is_inappropriate"`
ModeratorComment string `db:"moderator_comment" json:"comment"`
ModeratedBy int64 `db:"moderated_by" json:"moderated_by"`
ModeratedAt int64 `db:"moderated_at" json:"moderated_at"`
CreatedAt time.Time `db:"created_at" json:"-"`
UpdatedAt time.Time `db:"updated_at" json:"-"`
}

func (recognitionModeration *RecognitionModeration) Validate() (valid bool, errFields map[string]string) {
errFields = make(map[string]string)

if recognitionModeration.IsInappropriate == nil {
errFields["is_inappropriate"] = "Can't be blank"
}

if len(errFields) == 0 {
valid = true
}
return
}

func (s *pgStore) CreateRecognitionModeration(ctx context.Context, recognitionID int64, recognitionModeration RecognitionModeration) (resp RecognitionModeration, err error) {
now := time.Now()
err = s.db.GetContext(
ctx,
&resp,
createRecognitionModerationQuery,
recognitionID,
recognitionModeration.IsInappropriate,
recognitionModeration.ModeratorComment,
recognitionModeration.ModeratedBy,
now.Unix(),
now,
now,
)
if err != nil {
logger.WithFields(logger.Fields{
"err": err.Error(),
"recognitionID": recognitionID,
"recognition_moderation_params": recognitionModeration,
}).Error("Error while creating recognition moderation")
return
}

return
}
86 changes: 86 additions & 0 deletions go-backend/db/recognition_moderation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package db

import (
"context"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"time"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including assertion methods.
type RecognitionModerationTestSuite struct {
suite.Suite
dbStore Storer
db *sqlx.DB
sqlmock sqlmock.Sqlmock
}

func (suite *RecognitionModerationTestSuite) SetupTest() {
dbStore, dbConn, sqlmock := InitMockDB()
suite.dbStore = dbStore
suite.db = dbConn
suite.sqlmock = sqlmock
now = time.Now()
mockedRows = suite.getMockedRows(now)
}

func (suite *RecognitionModerationTestSuite) TearDownTest() {
suite.db.Close()
}

func (suite *RecognitionModerationTestSuite) getMockedRows(now time.Time) (mockedRows *sqlmock.Rows) {
mockedRows = suite.sqlmock.NewRows([]string{"id", "recognition_id", "is_inappropriate", "moderator_comment", "moderated_by", "moderated_at", "created_at", "updated_at"}).
AddRow(1, 1, true, "Test Comment", 1, now.Unix(), now, now)
return
}

func (suite *RecognitionModerationTestSuite) TestCreateRecognitionModerationSuccess() {
isInappropriate := true
expectedRecognitionModeration := RecognitionModeration{
ID: int64(1),
RecognitionID: int64(1),
IsInappropriate: &isInappropriate,
ModeratorComment: "Test Comment",
ModeratedBy: int64(1),
ModeratedAt: now.Unix(),
CreatedAt: now,
UpdatedAt: now,
}

suite.sqlmock.ExpectQuery("INSERT INTO recognition_moderation").
WithArgs(1, true, "Test Comment", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()).
WillReturnRows(mockedRows)

resp, err := suite.dbStore.CreateRecognitionModeration(context.Background(), expectedRecognitionModeration.ID, expectedRecognitionModeration)

assert.Equal(suite.T(), expectedRecognitionModeration, resp)
assert.Nil(suite.T(), err)
}

func (suite *RecognitionModerationTestSuite) TestCreateRecognitionModerationFailure() {
suite.db.Close()

isInappropriate := true
expectedRecognitionModeration := RecognitionModeration{
ID: int64(1),
RecognitionID: int64(1),
IsInappropriate: &isInappropriate,
ModeratorComment: "Test Comment",
ModeratedBy: int64(1),
ModeratedAt: now.Unix(),
CreatedAt: now,
UpdatedAt: now,
}

suite.sqlmock.ExpectQuery("INSERT INTO recognition_moderation").
WithArgs(1, true, "Test Comment", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()).
WillReturnRows(mockedRows)

resp, err := suite.dbStore.CreateRecognitionModeration(context.Background(), expectedRecognitionModeration.ID, expectedRecognitionModeration)

assert.Equal(suite.T(), RecognitionModeration{}, resp)
assert.NotNil(suite.T(), err)
}
86 changes: 86 additions & 0 deletions go-backend/db/reported_recognition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package db

import (
"context"
"strings"
"time"

logger "github.com/sirupsen/logrus"
)

var REPORTED_RECOGNITION_TYPE = []string{"fraud", "not_relevant", "incorrect"}

const (
createReportedRecognitionQuery = `INSERT INTO reported_recognitions (recognition_id, type_of_reporting,
reason_for_reporting, reported_by, reported_at, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, recognition_id, type_of_reporting, reason_for_reporting, reported_by, reported_at, created_at, updated_at`
)

type ReportedRecognition struct {
ID int64 `db:"id" json:"id"`
RecognitionID int64 `db:"recognition_id" json:"recognition_id"`
TypeOfReporting string `db:"type_of_reporting" json:"mark_as"`
ReasonForReporting string `db:"reason_for_reporting" json:"reason"`
ReportedBy int64 `db:"reported_by" json:"reported_by"`
ReportedAt int64 `db:"reported_at" json:"reported_at"`
CreatedAt time.Time `db:"created_at" json:"-"`
UpdatedAt time.Time `db:"updated_at" json:"-"`
}

func include(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}

func (reportedRecognition *ReportedRecognition) Validate() (valid bool, errFields map[string]string) {
errFields = make(map[string]string)

if reportedRecognition.TypeOfReporting == "" {
errFields["mark_as"] = "Can't be blank"
} else {
reportedRecognition.TypeOfReporting = strings.ToLower(reportedRecognition.TypeOfReporting)
ok := include(REPORTED_RECOGNITION_TYPE, reportedRecognition.TypeOfReporting)
if !ok {
errFields["mark_as"] = "Invalid reported recognition type"
}
}

if reportedRecognition.ReasonForReporting == "" {
errFields["reason"] = "Can't be blank"
}

if len(errFields) == 0 {
valid = true
}
return
}

func (s *pgStore) CreateReportedRecognition(ctx context.Context, recognitionID int64, reportedRecognition ReportedRecognition) (resp ReportedRecognition, err error) {
now := time.Now()
err = s.db.GetContext(
ctx,
&resp,
createReportedRecognitionQuery,
recognitionID,
reportedRecognition.TypeOfReporting,
reportedRecognition.ReasonForReporting,
reportedRecognition.ReportedBy,
now.Unix(),
now,
now,
)
if err != nil {
logger.WithFields(logger.Fields{
"err": err.Error(),
"recognitionID": recognitionID,
"reported_recognition_params": reportedRecognition,
}).Error("Error while creating reported recognition")
return
}

return
}
84 changes: 84 additions & 0 deletions go-backend/db/reported_recognition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package db

import (
"context"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"time"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including assertion methods.
type ReportedRecognitionTestSuite struct {
suite.Suite
dbStore Storer
db *sqlx.DB
sqlmock sqlmock.Sqlmock
}

func (suite *ReportedRecognitionTestSuite) SetupTest() {
dbStore, dbConn, sqlmock := InitMockDB()
suite.dbStore = dbStore
suite.db = dbConn
suite.sqlmock = sqlmock
now = time.Now()
mockedRows = suite.getMockedRows()
}

func (suite *ReportedRecognitionTestSuite) TearDownTest() {
suite.db.Close()
}

func (suite *ReportedRecognitionTestSuite) getMockedRows() (mockedRows *sqlmock.Rows) {
mockedRows = suite.sqlmock.NewRows([]string{"id", "recognition_id", "type_of_reporting", "reason_for_reporting", "reported_by", "reported_at", "created_at", "updated_at"}).
AddRow(1, 1, "fraud", "Test Reason", 1, now.Unix(), now, now)
return
}

func (suite *ReportedRecognitionTestSuite) TestCreateReportedRecognitionSuccess() {
expectedReportedRecognition := ReportedRecognition{
ID: int64(1),
RecognitionID: int64(1),
TypeOfReporting: "fraud",
ReasonForReporting: "Test Reason",
ReportedBy: int64(1),
ReportedAt: now.Unix(),
CreatedAt: now,
UpdatedAt: now,
}

suite.sqlmock.ExpectQuery("INSERT INTO reported_recognitions").
WithArgs(1, "fraud", "Test Reason", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()).
WillReturnRows(mockedRows)

resp, err := suite.dbStore.CreateReportedRecognition(context.Background(), expectedReportedRecognition.ID, expectedReportedRecognition)

assert.Equal(suite.T(), expectedReportedRecognition, resp)
assert.Nil(suite.T(), err)
}

func (suite *ReportedRecognitionTestSuite) TestCreateReportedRecognitionFailure() {
suite.db.Close()

expectedReportedRecognition := ReportedRecognition{
ID: int64(1),
RecognitionID: int64(1),
TypeOfReporting: "fraud",
ReasonForReporting: "Test Reason",
ReportedBy: int64(1),
ReportedAt: now.Unix(),
CreatedAt: now,
UpdatedAt: now,
}

suite.sqlmock.ExpectQuery("INSERT INTO reported_recognitions").
WithArgs(1, "fraud", "Test Reason", 1, now.Unix(), sqlmock.AnyArg(), sqlmock.AnyArg()).
WillReturnRows(mockedRows)

resp, err := suite.dbStore.CreateReportedRecognition(context.Background(), expectedReportedRecognition.ID, expectedReportedRecognition)

assert.Equal(suite.T(), ReportedRecognition{}, resp)
assert.NotNil(suite.T(), err)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE reported_recognitions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE reported_recognitions (
id SERIAL PRIMARY KEY NOT NULL UNIQUE,
recognition_id integer REFERENCES recognitions(id),
type_of_reporting varchar(50),
reason_for_reporting TEXT,
reported_by INTEGER REFERENCES users(id),
reported_at BIGINT,
created_at timestamp with time zone NOT NULL default current_timestamp,
updated_at timestamp with time zone NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE recognition_moderation;
Loading

0 comments on commit 7ed039f

Please sign in to comment.