summaryrefslogtreecommitdiffstats
path: root/store/sql_preference_store_test.go
blob: 6d5ec0ecd0896d3453cdcdb728f992ef423e07f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

package store

import (
	"github.com/mattermost/platform/model"
	"testing"
)

func TestPreferenceSave(t *testing.T) {
	Setup()

	id := model.NewId()

	preferences := model.Preferences{
		{
			UserId:   id,
			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
			Name:     model.PREFERENCE_NAME_SHOW,
			Value:    "value1a",
		},
		{
			UserId:   id,
			Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
			Name:     model.PREFERENCE_NAME_TEST,
			Value:    "value1b",
		},
	}
	if count := Must(store.Preference().Save(&preferences)); count != 2 {
		t.Fatal("got incorrect number of rows saved")
	}

	for _, preference := range preferences {
		if data := Must(store.Preference().GetByName(preference.UserId, preference.Category, preference.Name)).(model.Preferences); len(data) != 1 {
			t.Fatal("got incorrect number of preferences after first Save")
		} else if *preference != *data[0] {
			t.Fatal("got incorrect preference after first Save")
		}
	}

	preferences[0].Value = "value2a"
	preferences[1].Value = "value2b"
	if count := Must(store.Preference().Save(&preferences)); count != 2 {
		t.Fatal("got incorrect number of rows saved")
	}

	for _, preference := range preferences {
		if data := Must(store.Preference().GetByName(preference.UserId, preference.Category, preference.Name)).(model.Preferences); len(data) != 1 {
			t.Fatal("got incorrect number of preferences after second Save")
		} else if *preference != *data[0] {
			t.Fatal("got incorrect preference after second Save")
		}
	}
}

func TestPreferenceGetByName(t *testing.T) {
	Setup()

	userId := model.NewId()
	category := model.PREFERENCE_CATEGORY_DIRECT_CHANNELS
	name := model.PREFERENCE_NAME_SHOW
	altId := model.NewId()

	preferences := model.Preferences{
		{
			UserId:   userId,
			Category: category,
			Name:     name,
			AltId:    altId,
		},
		// same user/category/name, different alt id
		{
			UserId:   userId,
			Category: category,
			Name:     name,
			AltId:    model.NewId(),
		},
		// same user/category/alt id, different name
		{
			UserId:   userId,
			Category: category,
			Name:     model.PREFERENCE_NAME_TEST,
			AltId:    altId,
		},
		// same user/name/alt id, different category
		{
			UserId:   userId,
			Category: model.PREFERENCE_CATEGORY_TEST,
			Name:     name,
			AltId:    altId,
		},
		// same name/category/alt id, different user
		{
			UserId:   model.NewId(),
			Category: category,
			Name:     name,
			AltId:    altId,
		},
	}

	Must(store.Preference().Save(&preferences))

	if result := <-store.Preference().GetByName(userId, category, name); result.Err != nil {
		t.Fatal(result.Err)
	} else if data := result.Data.(model.Preferences); len(data) != 2 {
		t.Fatal("got the wrong number of preferences")
	} else if !((*data[0] == *preferences[0] && *data[1] == *preferences[1]) || (*data[0] == *preferences[1] && *data[1] == *preferences[0])) {
		t.Fatal("got incorrect preferences")
	}
}