github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/preference_store_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  	"github.com/mattermost/mattermost-server/store/storetest"
    12  )
    13  
    14  func TestPreferenceStore(t *testing.T) {
    15  	StoreTest(t, storetest.TestPreferenceStore)
    16  }
    17  
    18  func TestDeleteUnusedFeatures(t *testing.T) {
    19  	StoreTest(t, func(t *testing.T, ss store.Store) {
    20  		userId1 := model.NewId()
    21  		userId2 := model.NewId()
    22  		category := model.PREFERENCE_CATEGORY_ADVANCED_SETTINGS
    23  		feature1 := "feature1"
    24  		feature2 := "feature2"
    25  
    26  		features := model.Preferences{
    27  			{
    28  				UserId:   userId1,
    29  				Category: category,
    30  				Name:     store.FEATURE_TOGGLE_PREFIX + feature1,
    31  				Value:    "true",
    32  			},
    33  			{
    34  				UserId:   userId2,
    35  				Category: category,
    36  				Name:     store.FEATURE_TOGGLE_PREFIX + feature1,
    37  				Value:    "false",
    38  			},
    39  			{
    40  				UserId:   userId1,
    41  				Category: category,
    42  				Name:     store.FEATURE_TOGGLE_PREFIX + feature2,
    43  				Value:    "false",
    44  			},
    45  			{
    46  				UserId:   userId2,
    47  				Category: category,
    48  				Name:     store.FEATURE_TOGGLE_PREFIX + feature2,
    49  				Value:    "true",
    50  			},
    51  		}
    52  
    53  		store.Must(ss.Preference().Save(&features))
    54  
    55  		ss.Preference().(*SqlPreferenceStore).DeleteUnusedFeatures()
    56  
    57  		//make sure features with value "false" have actually been deleted from the database
    58  		if val, err := ss.Preference().(*SqlPreferenceStore).GetReplica().SelectInt(`SELECT COUNT(*)
    59                              FROM Preferences
    60                      WHERE Category = :Category
    61                      AND Value = :Val
    62                      AND Name LIKE '`+store.FEATURE_TOGGLE_PREFIX+`%'`, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_ADVANCED_SETTINGS, "Val": "false"}); err != nil {
    63  			t.Fatal(err)
    64  		} else if val != 0 {
    65  			t.Fatalf("Found %d features with value 'false', expected all to be deleted", val)
    66  		}
    67  		//
    68  		// make sure features with value "true" remain saved
    69  		if val, err := ss.Preference().(*SqlPreferenceStore).GetReplica().SelectInt(`SELECT COUNT(*)
    70                              FROM Preferences
    71                      WHERE Category = :Category
    72                      AND Value = :Val
    73                      AND Name LIKE '`+store.FEATURE_TOGGLE_PREFIX+`%'`, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_ADVANCED_SETTINGS, "Val": "true"}); err != nil {
    74  			t.Fatal(err)
    75  		} else if val == 0 {
    76  			t.Fatalf("Found %d features with value 'true', expected to find at least %d features", val, 2)
    77  		}
    78  	})
    79  }