add test for submitImages

This commit is contained in:
Umputun
2019-03-24 03:16:37 -05:00
parent ded10dde6c
commit 2c0cd1dec7
2 changed files with 34 additions and 0 deletions
+2
View File
@@ -103,10 +103,12 @@ func (s *DataStore) submitImages(comment store.Comment) {
c := comment
cc, err := s.Get(c.Locator, c.ID) // this can be called after last edit, we have to retrieve fresh comment
if err != nil {
log.Printf("[WARN] can't get comment's %s text for image extraction, %v", c.ID, err)
return nil
}
imgIds, err := s.ImageService.ExtractPictures(cc.Text)
if err != nil {
log.Printf("[WARN] can't get extract pictures from %s, %v", c.ID, err)
return nil
}
if len(imgIds) > 0 {
+32
View File
@@ -13,9 +13,12 @@ import (
"time"
bolt "github.com/coreos/bbolt"
"github.com/go-pkgz/lgr"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/umputun/remark/backend/app/store/image"
"github.com/umputun/remark/backend/app/store"
"github.com/umputun/remark/backend/app/store/admin"
@@ -686,6 +689,35 @@ func TestService_Find(t *testing.T) {
assert.InDelta(t, 0, res[1].Controversy, 0.01)
}
func TestService_submitImages(t *testing.T) {
defer os.Remove(testDb)
lgr.Setup(lgr.Debug, lgr.CallerFile, lgr.CallerFunc)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockStore := image.NewMockStore(ctrl)
imgSvc := &image.Service{Store: mockStore, TTL: time.Millisecond * 50}
mockStore.EXPECT().Commit(gomock.Any()).Times(2)
// two comments for https://radio-t.com
b := DataStore{Interface: prepStoreEngine(t), EditDuration: 50 * time.Millisecond,
AdminStore: admin.NewStaticKeyStore("secret 123"), ImageService: imgSvc}
c := store.Comment{
ID: "id-22",
Text: `some text <img src="/images/dev/pic1.png"/> xx <img src="/images/dev/pic2.png"/>`,
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.Local),
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
User: store.User{ID: "user1", Name: "user name"},
}
_, err := b.Interface.Create(c) // create directly with engine, doesn't call submitImages
assert.NoError(t, err)
b.submitImages(c)
time.Sleep(250 * time.Millisecond)
}
// makes new boltdb, put two records
func prepStoreEngine(t *testing.T) engine.Interface {
os.Remove(testDb)