Skip to content

Commit f696c9e

Browse files
committed
refactor unit tests of sync function dry run
1 parent feace27 commit f696c9e

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

rest/diagnostic_doc_api_test.go

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,28 @@ func TestSyncFuncDryRun(t *testing.T) {
10041004
base.SkipImportTestsIfNotEnabled(t)
10051005
base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
10061006

1007+
rt := NewRestTester(t, &RestTesterConfig{
1008+
PersistentConfig: true,
1009+
})
1010+
defer rt.Close()
1011+
1012+
dbConfig := rt.NewDbConfig()
1013+
rt.CreateDatabase("db", dbConfig)
1014+
1015+
// When the tests run with named scopes and collections, then the default
1016+
// sync function is:
1017+
// function(doc){channel("<collection_name>");}
1018+
// when the tests run in default scope and collection then the default
1019+
// sync function is:
1020+
// function(doc){channel(doc.channels);}
1021+
var defaultChannelName string
1022+
dbc, _ := rt.GetSingleTestDatabaseCollection()
1023+
if dbc.IsDefaultCollection() {
1024+
defaultChannelName = "chanNew"
1025+
} else {
1026+
defaultChannelName = dbc.Name
1027+
}
1028+
10071029
tests := []struct {
10081030
name string
10091031
dbSyncFunction string
@@ -1077,7 +1099,7 @@ func TestSyncFuncDryRun(t *testing.T) {
10771099
syncFunction: "function(doc) {channel(doc.channel); access(doc.accessUser, doc.accessChannel); role(doc.accessUser, doc.role); expiry(doc.expiry);}",
10781100
document: map[string]any{"accessChannel": []string{"dynamicChan5412"}, "accessUser": "user", "channel": []string{"dynamicChan222"}, "expiry": 10},
10791101
existingDoc: true,
1080-
existingDocID: "doc1",
1102+
existingDocID: "custom_sync_func-db_sync_func-doc_body-existing_doc-no_doc_id",
10811103
existingDocBody: `{"accessChannel": ["dynamicChan5412"],"accessUser": "user","channel": ["dynamicChan222"],"expiry":10}`,
10821104
expectedOutput: SyncFnDryRun{
10831105
Channels: base.SetFromArray([]string{"dynamicChan222"}),
@@ -1106,9 +1128,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11061128
dbSyncFunction: "function(doc,oldDoc){if (doc.user.num >= 100) {channel(doc.channel);} else {throw({forbidden: 'user num too low'});}if (oldDoc){ console.log(oldDoc); if (oldDoc.user.num > doc.user.num) { access(oldDoc.user.name, doc.channel);} else {access(doc.user.name[0], doc.channel);}}}",
11071129
syncFunction: "",
11081130
document: map[string]any{"user": map[string]any{"num": 150}, "channel": "abc"},
1109-
docID: "doc",
1131+
docID: "no_custom_sync_func-db_sync_func-doc_body-no_existing_doc-doc_id-sync_func_exception_typeError",
11101132
existingDoc: true,
1111-
existingDocID: "doc",
1133+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-no_existing_doc-doc_id-sync_func_exception_typeError",
11121134
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11131135
expectedOutput: SyncFnDryRun{
11141136
Exception: "Error returned from Sync Function: TypeError: Cannot access member '0' of undefined",
@@ -1120,9 +1142,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11201142
dbSyncFunction: "function(doc,oldDoc){if (doc.user.num >= 100) {channel(doc.channel);} else {throw({forbidden: 'user num too low'});}if (oldDoc){ console.log(oldDoc); if (oldDoc.user.num > doc.user.num) { access(oldDoc.user.name, doc.channel);} else {access(doc.user.name[0], doc.channel);}}}",
11211143
syncFunction: "",
11221144
document: map[string]any{"user": map[string]any{"num": 120, "name": []string{"user2"}}, "channel": "channel2"},
1123-
docID: "doc",
1145+
docID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id",
11241146
existingDoc: true,
1125-
existingDocID: "doc",
1147+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id",
11261148
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11271149
expectedOutput: SyncFnDryRun{
11281150
Channels: base.SetFromArray([]string{"channel2"}),
@@ -1136,9 +1158,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11361158
name: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-doc_id",
11371159
dbSyncFunction: "function(doc,oldDoc){if (doc.user.num >= 100) {channel(doc.channel);} else {throw({forbidden: 'user num too low'});}if (oldDoc){ console.log(oldDoc); if (oldDoc.user.num > doc.user.num) { access(oldDoc.user.name, doc.channel);} else {access(doc.user.name[0], doc.channel);}}}",
11381160
syncFunction: "",
1139-
docID: "doc",
1161+
docID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-doc_id",
11401162
existingDoc: true,
1141-
existingDocID: "doc",
1163+
existingDocID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-doc_id",
11421164
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11431165
expectedOutput: SyncFnDryRun{
11441166
Channels: base.SetFromArray([]string{"channel1"}),
@@ -1154,7 +1176,7 @@ func TestSyncFuncDryRun(t *testing.T) {
11541176
syncFunction: "",
11551177
docID: "doc404",
11561178
existingDoc: true,
1157-
existingDocID: "doc",
1179+
existingDocID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-invalid_doc_id",
11581180
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11591181
expectedOutput: SyncFnDryRun{},
11601182
expectedStatus: http.StatusNotFound,
@@ -1165,7 +1187,7 @@ func TestSyncFuncDryRun(t *testing.T) {
11651187
syncFunction: "",
11661188
docID: "",
11671189
existingDoc: true,
1168-
existingDocID: "doc",
1190+
existingDocID: "no_custom_sync_func-db_sync_func-no_doc_body-existing_doc-no_doc_id",
11691191
existingDocBody: `{"user":{"num":123, "name":["user1"]}, "channel":"channel1"}`,
11701192
expectedOutput: SyncFnDryRun{},
11711193
expectedStatus: http.StatusBadRequest,
@@ -1177,7 +1199,7 @@ func TestSyncFuncDryRun(t *testing.T) {
11771199
document: map[string]any{"channel": "channel2"},
11781200
docID: "",
11791201
existingDoc: true,
1180-
existingDocID: "doc22",
1202+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-no_doc_id-new_doc_channel",
11811203
existingDocBody: `{"chan1":"channel1", "channel":"chanOld"}`,
11821204
expectedOutput: SyncFnDryRun{
11831205
Channels: base.SetFromArray([]string{"channel2"}),
@@ -1191,9 +1213,9 @@ func TestSyncFuncDryRun(t *testing.T) {
11911213
dbSyncFunction: "function(doc,oldDoc){if(oldDoc){ channel(oldDoc.channel)} else {channel(doc.channel)} }",
11921214
syncFunction: "",
11931215
document: map[string]any{"channel": "chanNew"},
1194-
docID: "doc22",
1216+
docID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id-old_doc_channel",
11951217
existingDoc: true,
1196-
existingDocID: "doc22",
1218+
existingDocID: "no_custom_sync_func-db_sync_func-doc_body-existing_doc-doc_id-old_doc_channel",
11971219
existingDocBody: `{"chan1":"channel1", "channel":"chanOld"}`,
11981220
expectedOutput: SyncFnDryRun{
11991221
Channels: base.SetFromArray([]string{"chanOld"}),
@@ -1207,36 +1229,32 @@ func TestSyncFuncDryRun(t *testing.T) {
12071229
dbSyncFunction: "function(doc,oldDoc){if(oldDoc){ channel(oldDoc.channel)} else {channel(doc.channel)} }",
12081230
syncFunction: "",
12091231
document: `{"channel": "chanNew", "oldchannel":}`,
1210-
docID: "doc22",
1232+
docID: "no_custom_sync_func-db_sync_func-invalid_doc_body-existing_doc-doc_id",
12111233
existingDoc: true,
1212-
existingDocID: "doc22",
1234+
existingDocID: "no_custom_sync_func-db_sync_func-invalid_doc_body-existing_doc-doc_id",
12131235
existingDocBody: `{"chan1":"channel1", "channel":"chanOld"}`,
12141236
expectedOutput: SyncFnDryRun{},
12151237
expectedStatus: http.StatusBadRequest,
12161238
},
1217-
// Since the tests run in named Scopes and Collections therefore the
1218-
// default sync function is:
1219-
// function(doc){channel("<collection_name>");}
1220-
// therefore the channels returned will be named collections
12211239
{
12221240
name: "no_custom_sync_func-default_db_sync_func-doc_body-no_existing_doc-no_doc_id",
12231241
document: map[string]any{"channels": "chanNew"},
12241242
existingDoc: false,
12251243
expectedOutput: SyncFnDryRun{
1226-
Channels: base.SetFromArray([]string{"sg_test_0"}),
1244+
Channels: base.SetFromArray([]string{defaultChannelName}),
12271245
Access: channels.AccessMap{},
12281246
Roles: channels.AccessMap{},
12291247
},
12301248
expectedStatus: http.StatusOK,
12311249
},
12321250
{
12331251
name: "no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id",
1234-
docID: "doc22",
1252+
docID: "no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id",
12351253
existingDoc: true,
1236-
existingDocID: "doc22",
1254+
existingDocID: "no_custom_sync_func-default_db_sync_func-no_doc_body-existing_doc-doc_id",
12371255
existingDocBody: `{"channels": "chanNew"}`,
12381256
expectedOutput: SyncFnDryRun{
1239-
Channels: base.SetFromArray([]string{"sg_test_0"}),
1257+
Channels: base.SetFromArray([]string{defaultChannelName}),
12401258
Access: channels.AccessMap{},
12411259
Roles: channels.AccessMap{},
12421260
},
@@ -1246,13 +1264,9 @@ func TestSyncFuncDryRun(t *testing.T) {
12461264

12471265
for _, test := range tests {
12481266
t.Run(test.name, func(t *testing.T) {
1249-
rt := NewRestTester(t, &RestTesterConfig{
1250-
PersistentConfig: true,
1251-
SyncFn: test.dbSyncFunction,
1252-
})
1253-
defer rt.Close()
1254-
1255-
RequireStatus(t, rt.CreateDatabase("db", rt.NewDbConfig()), http.StatusCreated)
1267+
dbConfig := rt.NewDbConfig()
1268+
dbConfig.Sync = &test.dbSyncFunction
1269+
RequireStatus(t, rt.SendAdminRequest("PUT", "/{{.keyspace}}/_config/sync", test.dbSyncFunction), http.StatusOK)
12561270

12571271
url := "/{{.keyspace}}/_sync"
12581272
if test.existingDoc {

0 commit comments

Comments
 (0)