diff options
author | Frédéric Guillot <fred@miniflux.net> | 2018-06-23 16:16:54 -0700 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-06-23 16:16:54 -0700 |
commit | 7039df9af1de1aea72e90d4aa9fa6a37d21e1be0 (patch) | |
tree | 99df837fcec01a2972b2da91a34f9044cb016f72 /model | |
parent | cd77ebd7422b0988df9a5c017901a51507e79abd (diff) |
Improve feed and user API updates with optional values
Diffstat (limited to 'model')
-rw-r--r-- | model/feed.go | 39 | ||||
-rw-r--r-- | model/feed_test.go | 59 | ||||
-rw-r--r-- | model/user.go | 27 | ||||
-rw-r--r-- | model/user_test.go | 32 |
4 files changed, 0 insertions, 157 deletions
diff --git a/model/feed.go b/model/feed.go index 9c7ecca..d019eeb 100644 --- a/model/feed.go +++ b/model/feed.go @@ -42,44 +42,5 @@ func (f *Feed) String() string { ) } -// Merge combine override to the current struct -func (f *Feed) Merge(override *Feed) { - if override.Title != "" && override.Title != f.Title { - f.Title = override.Title - } - - if override.SiteURL != "" && override.SiteURL != f.SiteURL { - f.SiteURL = override.SiteURL - } - - if override.FeedURL != "" && override.FeedURL != f.FeedURL { - f.FeedURL = override.FeedURL - } - - if override.ScraperRules != "" && override.ScraperRules != f.ScraperRules { - f.ScraperRules = override.ScraperRules - } - - if override.RewriteRules != "" && override.RewriteRules != f.RewriteRules { - f.RewriteRules = override.RewriteRules - } - - if override.Crawler != f.Crawler { - f.Crawler = override.Crawler - } - - if override.Category != nil && override.Category.ID != 0 && override.Category.ID != f.Category.ID { - f.Category.ID = override.Category.ID - } - - if override.Username != f.Username { - f.Username = override.Username - } - - if override.Password != f.Password { - f.Password = override.Password - } -} - // Feeds is a list of feed type Feeds []*Feed diff --git a/model/feed_test.go b/model/feed_test.go deleted file mode 100644 index 138949d..0000000 --- a/model/feed_test.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2017 Frédéric Guillot. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. - -package model - -import "testing" - -func TestMergeFeedTitle(t *testing.T) { - feed1 := &Feed{Title: "Feed 1"} - feed2 := &Feed{Title: "Feed 2"} - feed1.Merge(feed2) - - if feed1.Title != "Feed 2" { - t.Fatal(`The title of feed1 should be merged`) - } - - feed1 = &Feed{Title: "Feed 1"} - feed2 = &Feed{} - feed1.Merge(feed2) - - if feed1.Title != "Feed 1" { - t.Fatal(`The title of feed1 should not be merged`) - } - - feed1 = &Feed{Title: "Feed 1"} - feed2 = &Feed{Title: "Feed 1"} - feed1.Merge(feed2) - - if feed1.Title != "Feed 1" { - t.Fatal(`The title of feed1 should not be changed`) - } -} - -func TestMergeFeedCategory(t *testing.T) { - feed1 := &Feed{Category: &Category{ID: 222}} - feed2 := &Feed{Category: &Category{ID: 333}} - feed1.Merge(feed2) - - if feed1.Category.ID != 333 { - t.Fatal(`The category of feed1 should be merged`) - } - - feed1 = &Feed{Category: &Category{ID: 222}} - feed2 = &Feed{} - feed1.Merge(feed2) - - if feed1.Category.ID != 222 { - t.Fatal(`The category of feed1 should not be merged`) - } - - feed1 = &Feed{Category: &Category{ID: 222}} - feed2 = &Feed{Category: &Category{ID: 0}} - feed1.Merge(feed2) - - if feed1.Category.ID != 222 { - t.Fatal(`The category of feed1 should not be merged`) - } -} diff --git a/model/user.go b/model/user.go index d2283f1..6a985e3 100644 --- a/model/user.go +++ b/model/user.go @@ -74,33 +74,6 @@ func (u User) ValidatePassword() error { return nil } -// Merge update the current user with another user. -func (u *User) Merge(override *User) { - if override.Username != "" && u.Username != override.Username { - u.Username = override.Username - } - - if override.Password != "" && u.Password != override.Password { - u.Password = override.Password - } - - if u.IsAdmin != override.IsAdmin { - u.IsAdmin = override.IsAdmin - } - - if override.Theme != "" && u.Theme != override.Theme { - u.Theme = override.Theme - } - - if override.Language != "" && u.Language != override.Language { - u.Language = override.Language - } - - if override.Timezone != "" && u.Timezone != override.Timezone { - u.Timezone = override.Timezone - } -} - // UseTimezone converts last login date to the given timezone. func (u *User) UseTimezone(tz string) { if u.LastLoginAt != nil { diff --git a/model/user_test.go b/model/user_test.go index ac075a8..e7bcb1e 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -59,35 +59,3 @@ func TestValidateUserModification(t *testing.T) { t.Error(`An invalid password should generate an error`) } } - -func TestMergeUsername(t *testing.T) { - user1 := &User{ID: 42, Username: "user1", Password: "secret", Theme: "default"} - user2 := &User{ID: 42, Username: "user2"} - user1.Merge(user2) - - if user1.Username != "user2" { - t.Fatal(`The username should be merged into user1`) - } - - if user1.Theme != "default" { - t.Fatal(`The theme should not be merged into user1`) - } -} - -func TestMergeIsAdmin(t *testing.T) { - user1 := &User{ID: 42, Username: "user1", Password: "secret", Theme: "default"} - user2 := &User{ID: 42, IsAdmin: true} - user1.Merge(user2) - - if !user1.IsAdmin { - t.Fatal(`The is_admin flag should be merged into user1`) - } - - user1 = &User{ID: 42, Username: "user1", Password: "secret", Theme: "default"} - user2 = &User{ID: 42} - user1.Merge(user2) - - if user1.IsAdmin { - t.Fatal(`The is_admin flag should not be merged into user1`) - } -} |