Skip to content

Commit

Permalink
♻️ Refactor Ticker Reset and Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Jan 20, 2025
1 parent 1555119 commit 2728ee2
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 174 deletions.
47 changes: 7 additions & 40 deletions internal/api/tickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ func (h *handler) DeleteTickerTelegram(c *gin.Context) {
return
}

ticker.Telegram.Reset()

err = h.storage.SaveTicker(&ticker)
err = h.storage.DeleteTelegram(&ticker)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
Expand Down Expand Up @@ -233,9 +231,7 @@ func (h *handler) DeleteTickerMastodon(c *gin.Context) {
return
}

ticker.Mastodon.Reset()

err = h.storage.SaveTicker(&ticker)
err = h.storage.DeleteMastodon(&ticker)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
Expand Down Expand Up @@ -286,9 +282,7 @@ func (h *handler) DeleteTickerBluesky(c *gin.Context) {
return
}

ticker.Bluesky.Reset()

err = h.storage.SaveTicker(&ticker)
err = h.storage.DeleteBluesky(&ticker)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
Expand Down Expand Up @@ -352,9 +346,7 @@ func (h *handler) DeleteTickerSignalGroup(c *gin.Context) {
return
}

ticker.SignalGroup.Reset()

err = h.storage.SaveTicker(&ticker)
err = h.storage.DeleteSignalGroup(&ticker)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
Expand Down Expand Up @@ -398,17 +390,9 @@ func (h *handler) DeleteTicker(c *gin.Context) {
return
}

err = h.storage.DeleteMessages(ticker)
if err != nil {
log.WithError(err).Error("failed to delete message for ticker")
}
err = h.storage.DeleteUploadsByTicker(ticker)
err = h.storage.DeleteTicker(&ticker)
if err != nil {
log.WithError(err).Error("failed to delete uploads for ticker")
}
err = h.storage.DeleteTicker(ticker)
if err != nil {
c.JSON(http.StatusNotFound, response.ErrorResponse(response.CodeNotFound, response.StorageError))
c.JSON(http.StatusInternalServerError, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
}

Expand Down Expand Up @@ -452,24 +436,7 @@ func (h *handler) ResetTicker(c *gin.Context) {
return
}

err = h.storage.DeleteMessages(ticker)
if err != nil {
log.WithError(err).WithField("ticker", ticker.ID).Error("error while deleting messages")
}
err = h.storage.DeleteUploadsByTicker(ticker)
if err != nil {
log.WithError(err).WithField("ticker", ticker.ID).Error("error while deleting remaining uploads")
}

ticker.Reset()

err = h.storage.SaveTicker(&ticker)
if err != nil {
c.JSON(http.StatusInternalServerError, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
}

err = h.storage.DeleteTickerUsers(&ticker)
err = h.storage.ResetTicker(&ticker)
if err != nil {
c.JSON(http.StatusInternalServerError, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
Expand Down
44 changes: 11 additions & 33 deletions internal/api/tickers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (s *TickerTestSuite) TestDeleteTickerTelegram() {

s.Run("when storage returns error", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("SaveTicker", mock.Anything).Return(errors.New("storage error")).Once()
s.store.On("DeleteTelegram", mock.Anything).Return(errors.New("storage error")).Once()
h := s.handler()
h.DeleteTickerTelegram(s.ctx)

Expand All @@ -337,7 +337,7 @@ func (s *TickerTestSuite) TestDeleteTickerTelegram() {

s.Run("when storage returns ticker", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("SaveTicker", mock.Anything).Return(nil).Once()
s.store.On("DeleteTelegram", mock.Anything).Return(nil).Once()
h := s.handler()
h.DeleteTickerTelegram(s.ctx)

Expand Down Expand Up @@ -428,7 +428,7 @@ func (s *TickerTestSuite) TestDeleteTickerMastodon() {

s.Run("when storage returns error", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("SaveTicker", mock.Anything).Return(errors.New("storage error")).Once()
s.store.On("DeleteMastodon", mock.Anything).Return(errors.New("storage error")).Once()
h := s.handler()
h.DeleteTickerMastodon(s.ctx)

Expand All @@ -438,7 +438,7 @@ func (s *TickerTestSuite) TestDeleteTickerMastodon() {

s.Run("when storage returns ticker", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("SaveTicker", mock.Anything).Return(nil).Once()
s.store.On("DeleteMastodon", mock.Anything).Return(nil).Once()
h := s.handler()
h.DeleteTickerMastodon(s.ctx)

Expand Down Expand Up @@ -555,7 +555,7 @@ func (s *TickerTestSuite) TestDeleteTickerBluesky() {

s.Run("when storage returns error", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("SaveTicker", mock.Anything).Return(errors.New("storage error")).Once()
s.store.On("DeleteBluesky", mock.Anything).Return(errors.New("storage error")).Once()
h := s.handler()
h.DeleteTickerBluesky(s.ctx)

Expand All @@ -565,7 +565,7 @@ func (s *TickerTestSuite) TestDeleteTickerBluesky() {

s.Run("when storage returns ticker", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("SaveTicker", mock.Anything).Return(nil).Once()
s.store.On("DeleteBluesky", mock.Anything).Return(nil).Once()
h := s.handler()
h.DeleteTickerBluesky(s.ctx)

Expand Down Expand Up @@ -982,7 +982,7 @@ func (s *TickerTestSuite) TestDeleteTickerSignalGroup() {
})
s.ctx.Request = httptest.NewRequest(http.MethodDelete, "/v1/admin/tickers/1/signal_group", nil)
s.ctx.Request.Header.Add("Content-Type", "application/json")
s.store.On("SaveTicker", mock.Anything).Return(errors.New("storage error")).Once()
s.store.On("DeleteSignalGroup", mock.Anything).Return(errors.New("storage error")).Once()
h := s.handler()
h.DeleteTickerSignalGroup(s.ctx)

Expand Down Expand Up @@ -1061,7 +1061,7 @@ func (s *TickerTestSuite) TestDeleteTickerSignalGroup() {
})
s.ctx.Request = httptest.NewRequest(http.MethodDelete, "/v1/admin/tickers/1/signal_group", nil)
s.ctx.Request.Header.Add("Content-Type", "application/json")
s.store.On("SaveTicker", mock.Anything).Return(nil).Once()
s.store.On("DeleteSignalGroup", mock.Anything).Return(nil).Once()
h := s.handler()
h.DeleteTickerSignalGroup(s.ctx)

Expand Down Expand Up @@ -1156,21 +1156,17 @@ func (s *TickerTestSuite) TestDeleteTicker() {

s.Run("when storage returns error", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("DeleteMessages", mock.Anything).Return(errors.New("storage error"))
s.store.On("DeleteUploadsByTicker", mock.Anything).Return(errors.New("storage error"))
s.store.On("DeleteTicker", mock.Anything).Return(errors.New("storage error"))
h := s.handler()
h.DeleteTicker(s.ctx)

s.Equal(http.StatusNotFound, s.w.Code)
s.Equal(http.StatusInternalServerError, s.w.Code)
s.store.AssertExpectations(s.T())
})

s.Run("happy path", func() {
s.cache.Set("response:localhost:/v1/init", true, time.Minute)
s.ctx.Set("ticker", storage.Ticker{Domain: "localhost"})
s.store.On("DeleteMessages", mock.Anything).Return(nil)
s.store.On("DeleteUploadsByTicker", mock.Anything).Return(nil)
s.store.On("DeleteTicker", mock.Anything).Return(nil)
h := s.handler()
h.DeleteTicker(s.ctx)
Expand Down Expand Up @@ -1246,22 +1242,7 @@ func (s *TickerTestSuite) TestResetTicker() {

s.Run("when storage returns error", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("DeleteMessages", mock.Anything).Return(errors.New("storage error")).Once()
s.store.On("DeleteUploadsByTicker", mock.Anything).Return(errors.New("storage error")).Once()
s.store.On("SaveTicker", mock.Anything).Return(errors.New("storage error")).Once()
h := s.handler()
h.ResetTicker(s.ctx)

s.Equal(http.StatusInternalServerError, s.w.Code)
s.store.AssertExpectations(s.T())
})

s.Run("when deleting users fails", func() {
s.ctx.Set("ticker", storage.Ticker{})
s.store.On("DeleteMessages", mock.Anything).Return(nil).Once()
s.store.On("DeleteUploadsByTicker", mock.Anything).Return(nil).Once()
s.store.On("SaveTicker", mock.Anything).Return(nil).Once()
s.store.On("DeleteTickerUsers", mock.Anything).Return(errors.New("storage error")).Once()
s.store.On("ResetTicker", mock.Anything).Return(errors.New("storage error")).Once()
h := s.handler()
h.ResetTicker(s.ctx)

Expand All @@ -1272,10 +1253,7 @@ func (s *TickerTestSuite) TestResetTicker() {
s.Run("happy path", func() {
s.cache.Set("response:localhost:/v1/init", true, time.Minute)
s.ctx.Set("ticker", storage.Ticker{Domain: "localhost"})
s.store.On("DeleteMessages", mock.Anything).Return(nil).Once()
s.store.On("DeleteUploadsByTicker", mock.Anything).Return(nil).Once()
s.store.On("SaveTicker", mock.Anything).Return(nil).Once()
s.store.On("DeleteTickerUsers", mock.Anything).Return(nil).Once()
s.store.On("ResetTicker", mock.Anything).Return(nil).Once()
h := s.handler()
h.ResetTicker(s.ctx)

Expand Down
Loading

0 comments on commit 2728ee2

Please sign in to comment.