~evanj/cms

ref: dfe8c02818cc433c9250cae940d3ab7455b9d54f cms/internal/s/db/db_test.go -rw-r--r-- 5.0 KiB
dfe8c028Evan M Jones Feat(go.mod): Dependency cleanup. Removed some. 10 months ago
                                                                                
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package db_test

import (
	"fmt"
	"log"
	"os"
	"strings"
	"testing"

	"git.sr.ht/~evanj/cms/internal/m/content"
	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/valuetype"
	"git.sr.ht/~evanj/cms/internal/s/db"
	"git.sr.ht/~evanj/security"
	"github.com/bmizerany/assert"
)

var conn, dberr = setup()

func setup() (*db.DB, error) {
	dbname := os.Getenv("TEST_DB_NAME")
	conn, dberr := db.New(
		log.New(os.Stdout, "", 0),
		os.Getenv("TEST_DBTYPE"),
		os.Getenv("TEST_DB"),
		security.Default(os.Getenv("TEST_SECRET")),
	)

	// Get over it. It's a test databse.
	conn.Exec(fmt.Sprintf(`DROP DATABASE %s`, dbname))
	conn.Exec(fmt.Sprintf(`CREATE DATABASE %s`, dbname))
	conn.Exec(fmt.Sprintf(`USE %s`, dbname))
	return conn, dberr
}

func TestBasic(t *testing.T) {
	t.Parallel()

	// Create tables
	assert.Equal(t, nil, dberr)
	assert.Equal(t, nil, conn.EnsureSetup())

	// Create user
	user, err := conn.UserNew("tester", "passer", "passer")
	assert.Equal(t, nil, err)
	assert.Equal(t, "tester", user.Name())

	// Create space
	space, err := conn.SpaceNew(user, "spacer", "desc")
	assert.Equal(t, nil, err)
	assert.Equal(t, "spacer", space.Name())
	assert.Equal(t, "desc", space.Desc())

	// Create contenttype
	ct1, err := conn.ContentTypeNew(space, "blogger", []db.ContentTypeNewParam{
		db.ContentTypeNewParam{"name", valuetype.StringSmall},
		db.ContentTypeNewParam{"slug", valuetype.StringSmall},
		db.ContentTypeNewParam{"desc", valuetype.StringBig},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, "blogger", ct1.Name())

	// Create content of "blogger"
	c1, err := conn.ContentNew(space, ct1, []db.ContentNewParam{
		db.ContentNewParam{valuetype.StringSmall, "name", "content1"},
		db.ContentNewParam{valuetype.StringSmall, "slug", "content-1"},
		db.ContentNewParam{valuetype.StringBig, "desc", "long-desc"},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, ct1.ID(), c1.Type())
	assert.Equal(t, 3, len(c1.Values()))
	assert.Equal(t, "content1", c1.MustValueByName("name").Value())
	assert.Equal(t, "content-1", c1.MustValueByName("slug").Value())
	assert.Equal(t, "long-desc", c1.MustValueByName("desc").Value())

	// Create content of "blogger"
	c2, err := conn.ContentNew(space, ct1, []db.ContentNewParam{
		db.ContentNewParam{valuetype.StringSmall, "name", "content2"},
		db.ContentNewParam{valuetype.StringSmall, "slug", "content-2"},
		db.ContentNewParam{valuetype.StringBig, "desc", "long-desc-2"},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, ct1.ID(), c2.Type())
	assert.Equal(t, 3, len(c2.Values()))
	assert.Equal(t, "content2", c2.MustValueByName("name").Value())
	assert.Equal(t, "content-2", c2.MustValueByName("slug").Value())
	assert.Equal(t, "long-desc-2", c2.MustValueByName("desc").Value())

	// Create content type "category" with ref to "blogger"
	ct2, err := conn.ContentTypeNew(space, "category", []db.ContentTypeNewParam{
		db.ContentTypeNewParam{"name", valuetype.StringSmall},
		db.ContentTypeNewParam{"blog list", valuetype.ReferenceList},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, "category", ct2.Name())

	// Create content of "category"
	c3, err := conn.ContentNew(space, ct2, []db.ContentNewParam{
		db.ContentNewParam{valuetype.StringSmall, "name", "category1"},
		// A string of content IDs seperated by "-" (dash).
		db.ContentNewParam{valuetype.ReferenceList, "blog list", strings.Join([]string{c1.ID(), c2.ID()}, "-")},
	})
	assert.Equal(t, nil, err)
	assert.Equal(t, ct2.ID(), c3.Type())
	assert.Equal(t, 2, len(c3.Values()))
	assert.Equal(t, "category1", c3.MustValueByName("name").Value())
	assert.Equal(t, 2, len(c3.MustValueByName("blog list").RefListIDs()))

	// Delete one of the referenced types.
	err = conn.ContentDelete(space, ct1, c1)
	assert.Equal(t, nil, err)
	isdeleted(t, space, ct1, c1)

	// Fetch the content with references.
	c4, err := conn.ContentGet(space, ct2, c3.ID())
	assert.Equal(t, nil, err)
	assert.Equal(t, ct2.ID(), c4.Type())
	assert.Equal(t, 2, len(c4.Values()))
	assert.Equal(t, "category1", c4.MustValueByName("name").Value())
	assert.Equal(t, 1, len(c4.MustValueByName("blog list").RefListIDs()))

	// Delete the content with references
	err = conn.ContentDelete(space, ct2, c4)
	assert.Equal(t, nil, err)
	isdeleted(t, space, ct2, c4)

	// Fetch a content that still exists and was referenced.
	c5, err := conn.ContentGet(space, ct1, c2.ID())
	assert.Equal(t, nil, err)
	assert.Equal(t, ct1.ID(), c5.Type())
	assert.Equal(t, 3, len(c5.Values()))
	assert.Equal(t, "content2", c5.MustValueByName("name").Value())
	assert.Equal(t, "content-2", c5.MustValueByName("slug").Value())
	assert.Equal(t, "long-desc-2", c5.MustValueByName("desc").Value())

	err = conn.ContentTypeDelete(space, ct1)
	assert.Equal(t, nil, err)

	err = conn.SpaceDelete(space)
	assert.Equal(t, nil, err)

	// Now, make sure we space's CTs are deleted
	_, err = conn.ContentTypeGet(space, ct2.ID())
	assert.NotEqual(t, nil, err)
}

func isdeleted(t *testing.T, s space.Space, ct contenttype.ContentType, c content.Content) {
	c, err := conn.ContentGet(s, ct, c.ID())
	assert.NotEqual(t, nil, err)
	assert.Equal(t, nil, c)
}