2017-02-01 13:18:59 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* eslint-env es6, node */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-07-10 14:53:00 +00:00
|
|
|
/* global REALM_MODULE_PATH */
|
|
|
|
|
2017-02-01 13:18:59 +00:00
|
|
|
const Realm = require('realm');
|
|
|
|
const TestCase = require('./asserts');
|
2018-02-20 13:43:36 +00:00
|
|
|
let schemas = require('./schemas');
|
2017-05-11 20:22:28 +00:00
|
|
|
|
2017-05-12 12:38:44 +00:00
|
|
|
const isNodeProccess = (typeof process === 'object' && process + '' === '[object process]');
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2017-11-15 10:59:13 +00:00
|
|
|
const require_method = require;
|
2017-05-12 12:38:44 +00:00
|
|
|
function node_require(module) {
|
2017-11-15 10:59:13 +00:00
|
|
|
return require_method(module);
|
2017-09-27 13:59:21 +00:00
|
|
|
}
|
2017-05-12 12:38:44 +00:00
|
|
|
|
|
|
|
let tmp;
|
|
|
|
let fs;
|
|
|
|
let execFile;
|
2017-09-29 18:43:04 +00:00
|
|
|
let path;
|
2017-05-12 12:38:44 +00:00
|
|
|
|
|
|
|
if (isNodeProccess) {
|
|
|
|
tmp = node_require('tmp');
|
|
|
|
fs = node_require('fs');
|
|
|
|
execFile = node_require('child_process').execFile;
|
|
|
|
tmp.setGracefulCleanup();
|
2017-09-29 18:43:04 +00:00
|
|
|
path = node_require("path");
|
2017-05-12 12:38:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 13:18:59 +00:00
|
|
|
function uuid() {
|
2017-05-11 20:22:28 +00:00
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
|
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
2017-02-01 13:18:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 18:43:04 +00:00
|
|
|
function copyFileToTempDir(filename) {
|
|
|
|
let tmpDir = tmp.dirSync();
|
|
|
|
let content = fs.readFileSync(filename);
|
|
|
|
let tmpFile = tmp.fileSync({ dir: tmpDir.name });
|
|
|
|
fs.appendFileSync(tmpFile.fd, content);
|
|
|
|
return tmpFile.name;
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:47:04 +00:00
|
|
|
function runOutOfProcess() {
|
|
|
|
const args = Array.prototype.slice.call(arguments);
|
2017-06-28 10:39:31 +00:00
|
|
|
let tmpDir = tmp.dirSync();
|
2018-03-02 18:47:04 +00:00
|
|
|
console.log(`runOutOfProcess : ${args.join(' ')}`);
|
2017-06-28 10:39:31 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-09-01 15:12:06 +00:00
|
|
|
try {
|
2018-03-02 18:47:04 +00:00
|
|
|
execFile(process.execPath, args, {cwd: tmpDir.name}, (error, stdout, stderr) => {
|
2017-09-01 15:12:06 +00:00
|
|
|
if (error) {
|
2018-03-02 20:06:00 +00:00
|
|
|
console.error("runOutOfProcess failed\n", error, stdout, stderr);
|
|
|
|
reject(new Error(`Running ${args[0]} failed. error: ${error}`));
|
2017-09-01 15:12:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('runOutOfProcess success\n' + stdout);
|
|
|
|
resolve();
|
|
|
|
});
|
2018-03-02 18:47:04 +00:00
|
|
|
}
|
2017-09-01 15:12:06 +00:00
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
2017-09-14 08:35:02 +00:00
|
|
|
}
|
2017-09-01 15:12:06 +00:00
|
|
|
});
|
2017-06-28 10:39:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 20:22:28 +00:00
|
|
|
module.exports = {
|
2017-05-11 20:54:29 +00:00
|
|
|
testLocalRealmHasNoSession() {
|
|
|
|
let realm = new Realm();
|
|
|
|
TestCase.assertNull(realm.syncSession);
|
|
|
|
},
|
|
|
|
|
2018-06-28 08:34:02 +00:00
|
|
|
testCustomHTTPHeaders() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', username, 'password').then(user => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`,
|
|
|
|
fullSynchronization: true,
|
|
|
|
custom_http_headers: {
|
|
|
|
'X-Foo': 'Bar'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
return Realm.open(config).then(realm => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
TestCase.assertDefined(realm.syncSession.config.custom_http_headers);
|
|
|
|
TestCase.assertEqual(realm.syncSession.config.custom_http_headers['X-Foo'], 'Bar');
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-11 20:54:29 +00:00
|
|
|
testProperties() {
|
2017-11-10 20:05:35 +00:00
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
2017-05-11 20:54:29 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
function checkSuccess() {
|
|
|
|
successCounter++;
|
|
|
|
if (successCounter == 2) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function postTokenRefreshChecks(sender, error) {
|
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error, accessTokenRefreshed);
|
|
|
|
TestCase.assertEqual(sender.url, `realm://localhost:9080/${user.identity}/myrealm`);
|
|
|
|
checkSuccess();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e)
|
|
|
|
}
|
2017-07-10 14:53:00 +00:00
|
|
|
}
|
2017-05-11 20:54:29 +00:00
|
|
|
|
|
|
|
// Let the error handler trigger our checks when the access token was refreshed.
|
|
|
|
postTokenRefreshChecks._notifyOnAccessTokenRefreshed = accessTokenRefreshed;
|
|
|
|
|
2018-05-30 10:54:51 +00:00
|
|
|
const config = user.createConfiguration({ sync: { url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks, fullSynchronization: true } });
|
2017-05-11 20:54:29 +00:00
|
|
|
const realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertUndefined(session.url);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
checkSuccess();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-05-11 20:22:28 +00:00
|
|
|
|
|
|
|
testRealmOpen() {
|
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-05-11 20:22:28 +00:00
|
|
|
}
|
2017-05-11 20:54:29 +00:00
|
|
|
|
2017-05-11 20:22:28 +00:00
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
let user, config;
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-11-10 20:05:35 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(u => {
|
|
|
|
user = u;
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
2017-06-28 10:39:31 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
config = {
|
2018-05-30 10:54:51 +00:00
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}`, fullSynchronization: true },
|
2017-11-10 20:05:35 +00:00
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
2017-06-28 10:39:31 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
return Realm.open(config)
|
|
|
|
}).then(realm => {
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
|
|
|
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
2017-12-18 22:46:26 +00:00
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmOpenWithExistingLocalRealm() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
|
|
|
let user, config;
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(u => {
|
|
|
|
user = u;
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
config = {
|
2018-05-30 10:54:51 +00:00
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}`, fullSynchronization: true },
|
2017-12-18 22:46:26 +00:00
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
schemaVersion: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Open the Realm with a schema version of 1, then immediately close it.
|
|
|
|
// This verifies that Realm.open doesn't hit issues when the schema version
|
|
|
|
// of an existing, local Realm is different than the one passed in the configuration.
|
|
|
|
let realm = new Realm(config);
|
|
|
|
realm.close();
|
|
|
|
|
|
|
|
config.schemaVersion = 2;
|
|
|
|
return Realm.open(config)
|
|
|
|
}).then(realm => {
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
|
|
|
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
2017-11-10 20:05:35 +00:00
|
|
|
TestCase.assertEqual(session.state, 'active');
|
2017-02-01 13:18:59 +00:00
|
|
|
});
|
2017-05-11 20:54:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testRealmOpenAsync() {
|
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-05-11 20:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
2017-05-11 20:22:28 +00:00
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-11-10 20:05:35 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(user => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
2017-05-11 20:54:29 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
let config = {
|
2018-05-30 10:54:51 +00:00
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}`, fullSynchronization: true },
|
2017-11-10 20:05:35 +00:00
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
2017-05-11 20:54:29 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
2017-05-11 20:54:29 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
resolve();
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
2017-05-19 05:59:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmOpenAsyncNoSchema() {
|
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-05-19 05:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
2017-09-01 15:12:06 +00:00
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2018-03-02 20:05:09 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(user => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
2017-05-19 05:59:03 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
let config = {
|
2018-05-30 10:54:51 +00:00
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}`, fullSynchronization: true }
|
2018-03-02 20:05:09 +00:00
|
|
|
};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Synced realm does not contain the expected objects count");
|
2017-05-19 05:59:03 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
let firstDog = realm.objects('Dog')[0];
|
|
|
|
TestCase.assertTrue(({}).hasOwnProperty.call(firstDog, 'name'), "Synced realm does not have an inffered schema");
|
|
|
|
TestCase.assertTrue(firstDog.name, "Synced realm object's property should have a value");
|
|
|
|
TestCase.assertTrue(firstDog.name.indexOf('Lassy') !== -1, "Synced realm object's property should contain the actual written value");
|
|
|
|
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.config.url, config.sync.url);
|
|
|
|
TestCase.assertEqual(session.config.user.identity, config.sync.user.identity);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
2017-05-11 20:54:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-19 06:18:48 +00:00
|
|
|
testRealmOpenLocalRealm() {
|
|
|
|
const username = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
2017-05-19 06:18:48 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
let config = {
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
2017-05-19 06:18:48 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
return Realm.open(config).then(realm => {
|
|
|
|
realm.write(() => {
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
realm.create('Dog', { name: `Lassy ${i}` });
|
|
|
|
}
|
|
|
|
});
|
2017-05-19 06:18:48 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Local realm does not contain the expected objects count");
|
2017-05-19 06:18:48 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRealmOpenAsyncLocalRealm() {
|
|
|
|
const username = uuid();
|
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const accessTokenRefreshed = this;
|
|
|
|
let successCounter = 0;
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
try {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
realm.write(() => {
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
realm.create('Dog', { name: `Lassy ${i}` });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let actualObjectsCount = realm.objects('Dog').length;
|
|
|
|
TestCase.assertEqual(actualObjectsCount, expectedObjectsCount, "Local realm does not contain the expected objects count");
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-05-11 20:54:29 +00:00
|
|
|
testErrorHandling() {
|
2017-08-24 18:01:12 +00:00
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
2017-05-11 20:54:29 +00:00
|
|
|
return new Promise((resolve, _reject) => {
|
2018-05-30 10:54:51 +00:00
|
|
|
const config = user.createConfiguration({ sync: { url: 'realm://localhost:9080/~/myrealm' } });
|
2017-05-11 20:54:29 +00:00
|
|
|
config.sync.error = (sender, error) => {
|
|
|
|
try {
|
|
|
|
TestCase.assertEqual(error.message, 'simulated error');
|
|
|
|
TestCase.assertEqual(error.code, 123);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
_reject(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
|
|
|
|
|
|
|
TestCase.assertEqual(session.config.error, config.sync.error);
|
|
|
|
session._simulateError(123, 'simulated error');
|
|
|
|
});
|
|
|
|
});
|
2017-09-01 15:12:06 +00:00
|
|
|
},
|
|
|
|
|
2018-02-20 13:43:36 +00:00
|
|
|
testListNestedSync() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/nested-list-helper.js', __dirname + '/schemas.js', username, realmName, REALM_MODULE_PATH)
|
2018-03-02 20:05:09 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(user => {
|
|
|
|
let config = {
|
|
|
|
schema: [schemas.ParentObject, schemas.NameObject],
|
2018-05-30 10:54:51 +00:00
|
|
|
sync: { user, url: `realm://localhost:9080/~/${realmName}`, fullSynchronization: true }
|
2018-03-02 20:05:09 +00:00
|
|
|
};
|
|
|
|
return Realm.open(config)
|
|
|
|
}).then(realm => {
|
|
|
|
let objects = realm.objects('ParentObject');
|
|
|
|
|
|
|
|
let json = JSON.stringify(objects);
|
|
|
|
TestCase.assertEqual(json, '{"0":{"id":1,"name":{"0":{"family":"Larsen","given":{"0":"Hans","1":"Jørgen"},"prefix":{}},"1":{"family":"Hansen","given":{"0":"Ib"},"prefix":{}}}},"1":{"id":2,"name":{"0":{"family":"Petersen","given":{"0":"Gurli","1":"Margrete"},"prefix":{}}}}}');
|
|
|
|
TestCase.assertEqual(objects.length, 2);
|
|
|
|
TestCase.assertEqual(objects[0].name.length, 2);
|
|
|
|
TestCase.assertEqual(objects[0].name[0].given.length, 2);
|
|
|
|
TestCase.assertEqual(objects[0].name[0].prefix.length, 0);
|
|
|
|
TestCase.assertEqual(objects[0].name[0].given[0], 'Hans');
|
|
|
|
TestCase.assertEqual(objects[0].name[0].given[1], 'Jørgen')
|
|
|
|
TestCase.assertEqual(objects[0].name[1].given.length, 1);
|
|
|
|
TestCase.assertEqual(objects[0].name[1].given[0], 'Ib');
|
|
|
|
TestCase.assertEqual(objects[0].name[1].prefix.length, 0);
|
|
|
|
|
|
|
|
TestCase.assertEqual(objects[1].name.length, 1);
|
|
|
|
TestCase.assertEqual(objects[1].name[0].given.length, 2);
|
|
|
|
TestCase.assertEqual(objects[1].name[0].prefix.length, 0);
|
|
|
|
TestCase.assertEqual(objects[1].name[0].given[0], 'Gurli');
|
|
|
|
TestCase.assertEqual(objects[1].name[0].given[1], 'Margrete');
|
2018-02-20 13:43:36 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-29 18:43:04 +00:00
|
|
|
testIncompatibleSyncedRealmOpen() {
|
|
|
|
let realm = "sync-v1.realm";
|
|
|
|
if (isNodeProccess) {
|
|
|
|
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
|
|
|
|
}
|
2017-09-30 13:43:40 +00:00
|
|
|
else {
|
|
|
|
//copy the bundled RN realm files for the test
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password')
|
|
|
|
.then(user => {
|
2017-09-29 18:43:04 +00:00
|
|
|
const config = {
|
|
|
|
path: realm,
|
|
|
|
sync: {
|
|
|
|
user,
|
2017-10-30 19:55:50 +00:00
|
|
|
error : err => console.log(err),
|
2017-09-29 18:43:04 +00:00
|
|
|
url: 'realm://localhost:9080/~/sync-v1'
|
|
|
|
}
|
|
|
|
};
|
2017-11-10 20:05:35 +00:00
|
|
|
return Realm.open(config)
|
|
|
|
})
|
|
|
|
.then(realm => { throw new Error("Should fail with IncompatibleSyncedRealmError") })
|
|
|
|
.catch(e => {
|
|
|
|
if (e.name == "IncompatibleSyncedRealmError") {
|
|
|
|
const backupRealm = new Realm(e.configuration);
|
|
|
|
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
function printObject(o) {
|
|
|
|
var out = '';
|
|
|
|
for (var p in o) {
|
|
|
|
out += p + ': ' + o[p] + '\n';
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
2017-11-10 20:05:35 +00:00
|
|
|
throw new Error("Failed with unexpected error " + printObject(e));
|
2017-09-29 18:43:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testIncompatibleSyncedRealmOpenAsync() {
|
|
|
|
let realm = "sync-v1.realm";
|
|
|
|
if (isNodeProccess) {
|
|
|
|
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
|
|
|
|
}
|
2017-09-30 13:43:40 +00:00
|
|
|
else {
|
|
|
|
//copy the bundled RN realm files for the test
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
const config = {
|
|
|
|
path: realm,
|
|
|
|
sync: {
|
|
|
|
user,
|
2017-10-30 19:55:50 +00:00
|
|
|
error : err => console.log(err),
|
2017-09-29 18:43:04 +00:00
|
|
|
url: 'realm://localhost:9080/~/sync-v1'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.openAsync(config, (error, realm) => {
|
|
|
|
if (!error) {
|
|
|
|
_reject("Should fail with IncompatibleSyncedRealmError");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.name == "IncompatibleSyncedRealmError") {
|
|
|
|
const backupRealm = new Realm(error.configuration);
|
|
|
|
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
|
2017-09-19 09:31:58 +00:00
|
|
|
resolve();
|
2017-09-29 18:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_reject("Failed with unexpected error" + JSON.stringify(error));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testIncompatibleSyncedRealmConsructor() {
|
|
|
|
let realm = "sync-v1.realm";
|
|
|
|
if (isNodeProccess) {
|
|
|
|
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
|
|
|
|
}
|
2017-09-30 13:43:40 +00:00
|
|
|
else {
|
|
|
|
//copy the bundled RN realm files for the test
|
|
|
|
Realm.copyBundledRealmFiles();
|
|
|
|
}
|
2017-09-29 18:43:04 +00:00
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
const config = {
|
|
|
|
path: realm,
|
|
|
|
sync: {
|
|
|
|
user,
|
2017-10-30 19:55:50 +00:00
|
|
|
error : err => console.log(err),
|
2017-09-29 18:43:04 +00:00
|
|
|
url: 'realm://localhost:9080/~/sync-v1'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const realm = new Realm(config);
|
|
|
|
_reject("Should fail with IncompatibleSyncedRealmError");
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (e.name == "IncompatibleSyncedRealmError") {
|
|
|
|
const backupRealm = new Realm(e.configuration);
|
|
|
|
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_reject("Failed with unexpected error" + JSON.stringify(e));
|
2017-09-19 09:31:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2018-02-20 12:40:54 +00:00
|
|
|
/* testProgressNotificationsForRealmConstructor() {
|
2017-09-01 15:12:06 +00:00
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-09-01 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2017-11-10 20:05:35 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(user => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2018-01-17 11:15:20 +00:00
|
|
|
return Realm.open(config).then((realm) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-01-17 15:47:23 +00:00
|
|
|
realm.syncSession.addProgressNotification('download', 'reportIndefinitely', (transferred, transferable) => {
|
|
|
|
if (transferred === transferable) {
|
2018-01-17 11:15:20 +00:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setTimeout(function() {
|
|
|
|
reject("Progress Notifications API failed to call progress callback for Realm constructor");
|
|
|
|
}, 5000);
|
|
|
|
});
|
2017-09-01 15:12:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-02-20 12:40:54 +00:00
|
|
|
},*/
|
2017-09-01 15:12:06 +00:00
|
|
|
|
|
|
|
testProgressNotificationsUnregisterForRealmConstructor() {
|
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-09-01 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2018-03-02 20:05:09 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(user => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
|
|
|
|
|
|
|
let realm = new Realm(config);
|
|
|
|
let unregisterFunc;
|
|
|
|
|
|
|
|
let writeDataFunc = () => {
|
|
|
|
realm.write(() => {
|
|
|
|
for (let i = 1; i <= 3; i++) {
|
|
|
|
realm.create('Dog', { name: `Lassy ${i}` });
|
2017-09-01 15:12:06 +00:00
|
|
|
}
|
2018-03-02 20:05:09 +00:00
|
|
|
});
|
|
|
|
}
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let syncFinished = false;
|
|
|
|
let failOnCall = false;
|
|
|
|
const progressCallback = (transferred, total) => {
|
|
|
|
if (failOnCall) {
|
|
|
|
reject(new Error("Progress callback should not be called after removeProgressNotification"));
|
|
|
|
}
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
syncFinished = transferred === total;
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
//unregister and write some new data.
|
|
|
|
if (syncFinished) {
|
|
|
|
failOnCall = true;
|
|
|
|
unregisterFunc();
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
//use second callback to wait for sync finished
|
|
|
|
realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', (transferred, transferable) => {
|
|
|
|
if (transferred === transferable) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
writeDataFunc();
|
|
|
|
}
|
|
|
|
};
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
realm.syncSession.addProgressNotification('upload', 'reportIndefinitely', progressCallback);
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
unregisterFunc = () => {
|
|
|
|
realm.syncSession.removeProgressNotification(progressCallback);
|
|
|
|
};
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
writeDataFunc();
|
2017-09-01 15:12:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-10-10 22:41:27 +00:00
|
|
|
testProgressNotificationsForRealmOpen() {
|
2017-09-01 15:12:06 +00:00
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-09-01 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
2018-03-02 20:05:09 +00:00
|
|
|
let progressCalled = false;
|
2017-09-01 15:12:06 +00:00
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2018-03-02 20:05:09 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(user => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
return Promise.race([
|
|
|
|
Realm.open(config).progress((transferred, total) => { progressCalled = true; }),
|
|
|
|
new Promise((_, reject) => setTimeout(() => reject("Progress Notifications API failed to call progress callback for Realm constructor"), 5000))
|
|
|
|
]);
|
|
|
|
}).then(() => TestCase.assertTrue(progressCalled));
|
2017-09-27 13:59:21 +00:00
|
|
|
},
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2017-10-10 22:41:27 +00:00
|
|
|
testProgressNotificationsForRealmOpenAsync() {
|
2017-09-01 15:12:06 +00:00
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-09-01 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const username = uuid();
|
|
|
|
const realmName = uuid();
|
|
|
|
|
|
|
|
return runOutOfProcess(__dirname + '/download-api-helper.js', username, realmName, REALM_MODULE_PATH)
|
2018-03-02 20:05:09 +00:00
|
|
|
.then(() => Realm.Sync.User.login('http://localhost:9080', username, 'password'))
|
|
|
|
.then(user => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user,
|
|
|
|
url: `realm://localhost:9080/~/${realmName}`
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }],
|
|
|
|
};
|
2017-09-27 13:59:21 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
let progressCalled = false;
|
2017-09-01 15:12:06 +00:00
|
|
|
|
2018-03-02 20:05:09 +00:00
|
|
|
Realm.openAsync(config,
|
|
|
|
(error, realm) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestCase.assertTrue(progressCalled);
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
(transferred, total) => {
|
|
|
|
progressCalled = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
reject("Progress Notifications API failed to call progress callback for Realm constructor");
|
|
|
|
}, 5000);
|
2017-09-01 15:12:06 +00:00
|
|
|
});
|
|
|
|
});
|
2017-09-27 13:59:21 +00:00
|
|
|
},
|
2017-10-02 17:44:24 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
|
|
|
|
// All tests releated to partial sync is assemble in one big test.
|
|
|
|
// Since it is the same instance of ROS running, it is virtually impossible
|
|
|
|
// to reset the state between the tests.
|
|
|
|
// In the future we should away from this style of testing.
|
|
|
|
testPartialSync() {
|
2018-03-13 07:27:47 +00:00
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
var user;
|
|
|
|
var realm;
|
|
|
|
|
2018-03-13 07:27:47 +00:00
|
|
|
const username = uuid();
|
2018-03-21 17:18:37 +00:00
|
|
|
const expectedObjectsCount = 3;
|
|
|
|
|
|
|
|
function __partialIsAllowed() {
|
|
|
|
// test: __partial is allowed
|
2018-03-13 07:27:47 +00:00
|
|
|
let config1 = {
|
|
|
|
sync: {
|
|
|
|
user: user,
|
2018-03-21 17:18:37 +00:00
|
|
|
url: `realm://localhost:9080/default/__partial/`,
|
2018-05-30 10:54:51 +00:00
|
|
|
_disableQueryBasedSyncUrlChecks: true,
|
|
|
|
fullSynchronization: false,
|
2018-03-13 07:27:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const realm = new Realm(config1);
|
|
|
|
TestCase.assertFalse(realm.isClosed);
|
2018-03-21 17:18:37 +00:00
|
|
|
realm.close();
|
2018-04-25 06:11:54 +00:00
|
|
|
}
|
2018-03-13 07:27:47 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
function __partialIsNotAllowed() {
|
|
|
|
let config2 = {
|
|
|
|
sync: {
|
|
|
|
user: user,
|
|
|
|
url: `realm://localhost:9080/default/__partial/`, // <--- not allowed URL
|
2018-05-30 10:54:51 +00:00
|
|
|
fullSynchronization: false,
|
2018-03-21 17:18:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
TestCase.assertThrows(() => new Realm(config2));
|
2018-04-25 06:11:54 +00:00
|
|
|
}
|
2018-02-20 12:40:54 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
function shouldFail() {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user: user,
|
|
|
|
url: 'realm://localhost:9080/~/default',
|
2018-05-30 10:54:51 +00:00
|
|
|
fullSynchronization: true, // <---- calling subscribe should fail
|
2018-03-21 17:18:37 +00:00
|
|
|
error: (session, error) => console.log(error)
|
|
|
|
},
|
|
|
|
schema: [{ name: 'Dog', properties: { name: 'string' } }]
|
|
|
|
};
|
2018-02-20 12:40:54 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
Realm.deleteFile(config);
|
|
|
|
const realm = new Realm(config);
|
|
|
|
TestCase.assertEqual(realm.objects('Dog').length, 0);
|
|
|
|
TestCase.assertThrows(function () { var subscription = realm.objects('Dog').filtered("name == 'Lassy 1'").subscribe(); } );
|
|
|
|
realm.close();
|
2018-04-25 06:11:54 +00:00
|
|
|
}
|
2018-02-20 12:40:54 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
function defaultRealmInvalidArguments() {
|
|
|
|
TestCase.assertThrows(() => Realm.automaticSyncConfiguration('foo', 'bar')); // too many arguments
|
2018-04-25 06:11:54 +00:00
|
|
|
}
|
2018-02-20 12:40:54 +00:00
|
|
|
|
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
return runOutOfProcess(__dirname + '/partial-sync-api-helper.js', username, REALM_MODULE_PATH)
|
|
|
|
.then(() => {
|
|
|
|
return Realm.Sync.User.login('http://localhost:9080', username, 'password').then((u) => {
|
|
|
|
user = u;
|
2018-02-20 12:40:54 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
__partialIsAllowed();
|
|
|
|
__partialIsNotAllowed();
|
|
|
|
shouldFail();
|
|
|
|
defaultRealmInvalidArguments();
|
2018-02-20 12:40:54 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-30 10:54:51 +00:00
|
|
|
let config = Realm.Sync.User.current.createConfiguration();
|
2018-03-21 17:18:37 +00:00
|
|
|
config.schema = [{ name: 'Dog', properties: { name: 'string' } }];
|
|
|
|
Realm.deleteFile(config);
|
|
|
|
|
|
|
|
realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
|
|
|
TestCase.assertInstanceOf(session, Realm.Sync.Session);
|
|
|
|
TestCase.assertEqual(session.user.identity, user.identity);
|
|
|
|
TestCase.assertEqual(session.state, 'active');
|
|
|
|
|
|
|
|
var results1 = realm.objects('Dog').filtered("name == 'Lassy 1'");
|
|
|
|
var results2 = realm.objects('Dog').filtered("name == 'Lassy 2'");
|
|
|
|
|
|
|
|
var subscription1 = results1.subscribe();
|
|
|
|
TestCase.assertEqual(subscription1.state, Realm.Sync.SubscriptionState.Creating);
|
|
|
|
|
|
|
|
var subscription2 = results2.subscribe();
|
|
|
|
TestCase.assertEqual(subscription2.state, Realm.Sync.SubscriptionState.Creating);
|
|
|
|
|
|
|
|
let called1 = false;
|
|
|
|
let called2 = false;
|
|
|
|
|
|
|
|
subscription1.addListener((subscription, state) => {
|
|
|
|
if (state === Realm.Sync.SubscriptionState.Complete) {
|
|
|
|
results1.addListener((collection, changeset) => {
|
|
|
|
TestCase.assertEqual(collection.length, 1);
|
|
|
|
TestCase.assertTrue(collection[0].name === 'Lassy 1', "The object is not synced correctly");
|
|
|
|
results1.removeAllListeners();
|
|
|
|
subscription1.unsubscribe();
|
|
|
|
called1 = true;
|
|
|
|
});
|
|
|
|
} else if (state === Realm.Sync.SubscriptionState.Invalidated) {
|
|
|
|
subscription1.removeAllListeners();
|
|
|
|
if (called1 && called2) {
|
|
|
|
realm.close();
|
|
|
|
resolve('Done');
|
|
|
|
}
|
2018-02-20 12:40:54 +00:00
|
|
|
}
|
2018-03-21 17:18:37 +00:00
|
|
|
});
|
2018-02-20 12:40:54 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
subscription2.addListener((subscription, state) => {
|
|
|
|
if (state === Realm.Sync.SubscriptionState.Complete) {
|
|
|
|
results2.addListener((collection, changeset) => {
|
|
|
|
TestCase.assertEqual(collection.length, 1);
|
|
|
|
TestCase.assertTrue(collection[0].name === 'Lassy 2', "The object is not synced correctly");
|
|
|
|
results2.removeAllListeners();
|
|
|
|
subscription2.unsubscribe();
|
|
|
|
called2 = true;
|
|
|
|
});
|
|
|
|
} else if (state === Realm.Sync.SubscriptionState.Invalidated) {
|
|
|
|
subscription2.removeAllListeners();
|
|
|
|
if (called1 && called2) {
|
|
|
|
realm.close();
|
|
|
|
resolve('Done');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-02-20 12:40:54 +00:00
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
reject("listeners never called");
|
|
|
|
}, 15000);
|
2018-02-20 12:40:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-21 17:18:37 +00:00
|
|
|
|
2017-10-02 17:44:24 +00:00
|
|
|
testClientReset() {
|
|
|
|
// FIXME: try to enable for React Native
|
|
|
|
if (!isNodeProccess) {
|
2017-11-10 20:05:35 +00:00
|
|
|
return;
|
2017-10-02 17:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
|
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
var realm;
|
2018-05-30 10:54:51 +00:00
|
|
|
const config = user.createConfiguration({ sync: { url: 'realm://localhost:9080/~/myrealm' } });
|
2017-10-02 17:44:24 +00:00
|
|
|
config.sync.error = (sender, error) => {
|
|
|
|
try {
|
2017-11-06 12:51:33 +00:00
|
|
|
TestCase.assertEqual(error.name, 'ClientReset');
|
2017-10-02 17:44:24 +00:00
|
|
|
TestCase.assertDefined(error.config);
|
|
|
|
TestCase.assertNotEqual(error.config.path, '');
|
2017-11-06 12:51:33 +00:00
|
|
|
const path = realm.path;
|
2017-10-02 17:44:24 +00:00
|
|
|
realm.close();
|
2017-11-06 12:51:33 +00:00
|
|
|
Realm.Sync.initiateClientReset(path);
|
|
|
|
// open Realm with error.config, and copy required objects a Realm at `path`
|
2017-10-02 17:44:24 +00:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
_reject(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
realm = new Realm(config);
|
|
|
|
const session = realm.syncSession;
|
|
|
|
|
|
|
|
TestCase.assertEqual(session.config.error, config.sync.error);
|
|
|
|
session._simulateError(211, 'ClientReset'); // 211 -> divering histories
|
|
|
|
});
|
|
|
|
});
|
2018-08-10 08:39:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
testAddConnectionNotification() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then((u) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user: u,
|
|
|
|
url: `realm://localhost:9080/~/${uuid()}`,
|
|
|
|
fullSynchronization: true,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.open(config).then(realm => {
|
2018-08-15 13:52:37 +00:00
|
|
|
realm.syncSession.addConnectionNotification((newState, oldState) => {
|
2018-08-10 08:39:03 +00:00
|
|
|
if (oldState === Realm.Sync.ConnectionState.Connected && newState === Realm.Sync.ConnectionState.Disconnected) {
|
|
|
|
resolve('Done');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
realm.close()
|
|
|
|
}).catch(error => reject(error));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testRemoveConnectionNotification() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then((u) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user: u,
|
|
|
|
url: `realm://localhost:9080/~/${uuid()}`,
|
|
|
|
fullSynchronization: true,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.open(config).then(realm => {
|
2018-08-15 13:52:37 +00:00
|
|
|
let callback1 = () => {
|
2018-08-10 08:39:03 +00:00
|
|
|
reject("Should not be called");
|
|
|
|
};
|
2018-08-15 13:52:37 +00:00
|
|
|
let callback2 = (newState, oldState) => {
|
2018-08-10 08:39:03 +00:00
|
|
|
if (oldState === Realm.Sync.ConnectionState.Connected && newState === Realm.Sync.ConnectionState.Disconnected) {
|
|
|
|
resolve('Done');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let session = realm.syncSession;
|
|
|
|
session.addConnectionNotification(callback1);
|
|
|
|
session.addConnectionNotification(callback2);
|
|
|
|
session.removeConnectionNotification(callback1);
|
|
|
|
realm.close()
|
|
|
|
}).catch(error => reject(error));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
testConnectionState() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then((u) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let config = {
|
|
|
|
sync: {
|
|
|
|
user: u,
|
|
|
|
url: `realm://localhost:9080/~/${uuid()}`,
|
|
|
|
fullSynchronization: true,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Realm.open(config).then(realm => {
|
|
|
|
let session = realm.syncSession;
|
|
|
|
TestCase.assertEqual(session.connectionState, Realm.Sync.ConnectionState.Disconnected);
|
|
|
|
TestCase.assertFalse(session.isConnected());
|
2018-08-15 13:52:37 +00:00
|
|
|
session.addConnectionNotification((newState, oldState) => {
|
2018-08-10 08:39:03 +00:00
|
|
|
switch (newState) {
|
|
|
|
case Realm.Sync.ConnectionState.Disconnected:
|
|
|
|
TestCase.assertEqual(session.connectionState, Realm.Sync.ConnectionState.Disconnected);
|
|
|
|
TestCase.assertFalse(session.isConnected());
|
|
|
|
break;
|
|
|
|
case Realm.Sync.ConnectionState.Connecting:
|
|
|
|
TestCase.assertEqual(session.connectionState, Realm.Sync.ConnectionState.Connecting);
|
|
|
|
TestCase.assertFalse(session.isConnected());
|
|
|
|
break;
|
|
|
|
case Realm.Sync.ConnectionState.Connected:
|
|
|
|
TestCase.assertEqual(session.connectionState, Realm.Sync.ConnectionState.Connected);
|
|
|
|
TestCase.assertTrue(session.isConnected());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
reject(`unknown connection value: ${newState}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldState === Realm.Sync.ConnectionState.Connecting && newState === Realm.Sync.ConnectionState.Connected) {
|
|
|
|
resolve('Done');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(error => reject(error));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-08-17 08:10:20 +00:00
|
|
|
testOfflinePermissionSchemas() {
|
|
|
|
if (!isNodeProccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then((u) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let realm = new Realm(u.createConfiguration());
|
|
|
|
TestCase.assertEqual(5, realm.objects(Realm.Permissions.Class.schema.name).length);
|
|
|
|
resolve('Done');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|