Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Add delete option for change log in repo #835

Merged
merged 4 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions backend/app/adapter/sqldb/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ VALUES ($1, $2, $3, $4);
return newChange, nil
}

// DeleteChange removes a change with the given ID
func (c ChangeLogSQL) DeleteChange(ID string) error {
statement := fmt.Sprintf(`
DELETE FROM "%s"
WHERE "%s" = $1;
`,
table.ChangeLog.TableName,
table.ChangeLog.ColumnID,
)

_, err := c.db.Exec(statement, ID)
return err
}

// NewChangeLogSQL creates ChangeLogSQL
func NewChangeLogSQL(db *sql.DB) ChangeLogSQL {
return ChangeLogSQL{
Expand Down
87 changes: 87 additions & 0 deletions backend/app/adapter/sqldb/changelog_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,93 @@ func TestChangeLogSql_CreateChange(t *testing.T) {
}
}

func TestChangeLogSql_DeleteChange(t *testing.T) {
summaryMarkdown1 := "summary 1"
summaryMarkdown2 := "summary 2"

testCases := []struct {
name string
tableRows []changeLogTableRow
deleteChangeId string
expectedChangeLogSize int
expectedChangeLog []entity.Change
}{
{
name: "delete an existing change",
tableRows: []changeLogTableRow{
{
id: "12345",
title: "title 1",
summaryMarkdown: summaryMarkdown1,
},
{
id: "67890",
title: "title 2",
summaryMarkdown: summaryMarkdown2,
},
},
deleteChangeId: "67890",
expectedChangeLogSize: 1,
expectedChangeLog: []entity.Change{
{
ID: "12346",
Title: "title 1",
SummaryMarkdown: &summaryMarkdown1,
},
},
}, {
name: "delete a non existent change",
tableRows: []changeLogTableRow{
{
id: "12345",
title: "title 1",
summaryMarkdown: summaryMarkdown1,
},
{
id: "67890",
title: "title 2",
summaryMarkdown: summaryMarkdown2,
},
},
deleteChangeId: "34567",
expectedChangeLogSize: 2,
expectedChangeLog: []entity.Change{
{
ID: "12345",
Title: "title 1",
SummaryMarkdown: &summaryMarkdown1,
},
{
ID: "67890",
Title: "title 2",
SummaryMarkdown: &summaryMarkdown2,
},
},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
dbtest.AccessTestDB(
dbConnector,
dbMigrationTool,
dbMigrationRoot,
dbConfig,
func(sqlDB *sql.DB) {
insertChangeLogTableRows(t, sqlDB, testCase.tableRows)

changeLogRepo := sqldb.NewChangeLogSQL(sqlDB)

err := changeLogRepo.DeleteChange(testCase.deleteChangeId)
changeLog, _ := changeLogRepo.GetChangeLog()

assert.Equal(t, nil, err)
assert.Equal(t, testCase.expectedChangeLogSize, len(changeLog))
})
})
}
}

func insertChangeLogTableRows(t *testing.T, sqlDB *sql.DB, tableRows []changeLogTableRow) {
for _, tableRow := range tableRows {
_, err := sqlDB.Exec(
Expand Down
1 change: 1 addition & 0 deletions backend/app/usecase/repository/change_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import (
type ChangeLog interface {
GetChangeLog() ([]entity.Change, error)
CreateChange(newChange entity.Change) (entity.Change, error)
DeleteChange(ID string) error
}
20 changes: 20 additions & 0 deletions backend/app/usecase/repository/change_log_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ func (c *ChangeLogFake) CreateChange(newChange entity.Change) (entity.Change, er
return newChange, nil
}

// DeleteChange removes a change based on a given ID
func (c *ChangeLogFake) DeleteChange(ID string) error {
for idx, change := range c.changeLog {
if change.ID == ID {
return c.removeChangeAt(idx)
}
}

return nil
}

func (c *ChangeLogFake) removeChangeAt(idx int) error {
if idx < 0 || idx >= len(c.changeLog) {
return errors.New("index not in range for removing change")
}

c.changeLog = append(c.changeLog[:idx], c.changeLog[idx+1:]...)
return nil
}

// NewChangeLogFake creates ChangeLogFake
func NewChangeLogFake(changeLog []entity.Change) ChangeLogFake {
return ChangeLogFake{
Expand Down