summaryrefslogtreecommitdiffstats
path: root/store/storetest/system_store.go
blob: a06b72a8372ce32b5b7e0401ba4538662ae90eda (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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package storetest

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/store"
)

func TestSystemStore(t *testing.T, ss store.Store) {
	t.Run("", func(t *testing.T) { testSystemStore(t, ss) })
	t.Run("SaveOrUpdate", func(t *testing.T) { testSystemStoreSaveOrUpdate(t, ss) })
	t.Run("PermanentDeleteByName", func(t *testing.T) { testSystemStorePermanentDeleteByName(t, ss) })
}

func testSystemStore(t *testing.T, ss store.Store) {
	system := &model.System{Name: model.NewId(), Value: "value"}
	store.Must(ss.System().Save(system))

	result := <-ss.System().Get()
	systems := result.Data.(model.StringMap)

	if systems[system.Name] != system.Value {
		t.Fatal()
	}

	system.Value = "value2"
	store.Must(ss.System().Update(system))

	result2 := <-ss.System().Get()
	systems2 := result2.Data.(model.StringMap)

	if systems2[system.Name] != system.Value {
		t.Fatal()
	}

	result3 := <-ss.System().GetByName(system.Name)
	rsystem := result3.Data.(*model.System)
	if rsystem.Value != system.Value {
		t.Fatal()
	}
}

func testSystemStoreSaveOrUpdate(t *testing.T, ss store.Store) {
	system := &model.System{Name: model.NewId(), Value: "value"}

	if err := (<-ss.System().SaveOrUpdate(system)).Err; err != nil {
		t.Fatal(err)
	}

	system.Value = "value2"

	if r := <-ss.System().SaveOrUpdate(system); r.Err != nil {
		t.Fatal(r.Err)
	}
}

func testSystemStorePermanentDeleteByName(t *testing.T, ss store.Store) {
	s1 := &model.System{Name: model.NewId(), Value: "value"}
	s2 := &model.System{Name: model.NewId(), Value: "value"}

	store.Must(ss.System().Save(s1))
	store.Must(ss.System().Save(s2))

	res1 := <-ss.System().GetByName(s1.Name)
	assert.Nil(t, res1.Err)

	res2 := <-ss.System().GetByName(s2.Name)
	assert.Nil(t, res2.Err)

	res3 := <-ss.System().PermanentDeleteByName(s1.Name)
	assert.Nil(t, res3.Err)

	res4 := <-ss.System().GetByName(s1.Name)
	assert.NotNil(t, res4.Err)

	res5 := <-ss.System().GetByName(s2.Name)
	assert.Nil(t, res5.Err)

	res6 := <-ss.System().PermanentDeleteByName(s2.Name)
	assert.Nil(t, res6.Err)

	res7 := <-ss.System().GetByName(s1.Name)
	assert.NotNil(t, res7.Err)

	res8 := <-ss.System().GetByName(s2.Name)
	assert.NotNil(t, res8.Err)

}