Expose global cleanupTestRealms function

This handles clearing out all of the test files between each test case.
This commit is contained in:
Scott Kyle 2015-10-14 15:25:58 -07:00
parent d0787e36bb
commit c8c1910910
8 changed files with 73 additions and 17 deletions

View File

@ -63,6 +63,7 @@
F64E1EF11BC3510E00E0E150 /* util.js in Resources */ = {isa = PBXBuildFile; fileRef = F64E1EF01BC3510E00E0E150 /* util.js */; settings = {ASSET_TAGS = (); }; }; F64E1EF11BC3510E00E0E150 /* util.js in Resources */ = {isa = PBXBuildFile; fileRef = F64E1EF01BC3510E00E0E150 /* util.js */; settings = {ASSET_TAGS = (); }; };
F68A278C1BC2722A0063D40A /* RJSModuleLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */; settings = {ASSET_TAGS = (); }; }; F68A278C1BC2722A0063D40A /* RJSModuleLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */; settings = {ASSET_TAGS = (); }; };
F68A278E1BC30F0A0063D40A /* index.js in Resources */ = {isa = PBXBuildFile; fileRef = F68A278D1BC30F0A0063D40A /* index.js */; settings = {ASSET_TAGS = (); }; }; F68A278E1BC30F0A0063D40A /* index.js in Resources */ = {isa = PBXBuildFile; fileRef = F68A278D1BC30F0A0063D40A /* index.js */; settings = {ASSET_TAGS = (); }; };
F6F405F81BCF0C1A00A1E24F /* base-test.js in Resources */ = {isa = PBXBuildFile; fileRef = F6F405F71BCF0C1A00A1E24F /* base-test.js */; settings = {ASSET_TAGS = (); }; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -199,6 +200,7 @@
F68A278A1BC2722A0063D40A /* RJSModuleLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RJSModuleLoader.h; path = tests/RJSModuleLoader.h; sourceTree = SOURCE_ROOT; }; F68A278A1BC2722A0063D40A /* RJSModuleLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RJSModuleLoader.h; path = tests/RJSModuleLoader.h; sourceTree = SOURCE_ROOT; };
F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RJSModuleLoader.m; path = tests/RJSModuleLoader.m; sourceTree = SOURCE_ROOT; }; F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RJSModuleLoader.m; path = tests/RJSModuleLoader.m; sourceTree = SOURCE_ROOT; };
F68A278D1BC30F0A0063D40A /* index.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = index.js; path = tests/index.js; sourceTree = SOURCE_ROOT; }; F68A278D1BC30F0A0063D40A /* index.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = index.js; path = tests/index.js; sourceTree = SOURCE_ROOT; };
F6F405F71BCF0C1A00A1E24F /* base-test.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = "base-test.js"; path = "tests/base-test.js"; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -325,6 +327,7 @@
0270BC781B7D020100010E03 /* Info.plist */, 0270BC781B7D020100010E03 /* Info.plist */,
F68A278D1BC30F0A0063D40A /* index.js */, F68A278D1BC30F0A0063D40A /* index.js */,
0270BC7E1B7D020100010E03 /* asserts.js */, 0270BC7E1B7D020100010E03 /* asserts.js */,
F6F405F71BCF0C1A00A1E24F /* base-test.js */,
0270BC7F1B7D020100010E03 /* schemas.js */, 0270BC7F1B7D020100010E03 /* schemas.js */,
F64E1EF01BC3510E00E0E150 /* util.js */, F64E1EF01BC3510E00E0E150 /* util.js */,
02D456D91B7E59A500EE1299 /* ArrayTests.js */, 02D456D91B7E59A500EE1299 /* ArrayTests.js */,
@ -546,6 +549,7 @@
0270BC811B7D020100010E03 /* ObjectTests.js in Resources */, 0270BC811B7D020100010E03 /* ObjectTests.js in Resources */,
02D456DA1B7E59A500EE1299 /* ArrayTests.js in Resources */, 02D456DA1B7E59A500EE1299 /* ArrayTests.js in Resources */,
0270BC861B7D020100010E03 /* schemas.js in Resources */, 0270BC861B7D020100010E03 /* schemas.js in Resources */,
F6F405F81BCF0C1A00A1E24F /* base-test.js in Resources */,
F68A278E1BC30F0A0063D40A /* index.js in Resources */, F68A278E1BC30F0A0063D40A /* index.js in Resources */,
0270BC831B7D020100010E03 /* RealmTests.js in Resources */, 0270BC831B7D020100010E03 /* RealmTests.js in Resources */,
0270BC841B7D020100010E03 /* ResultsTests.js in Resources */, 0270BC841B7D020100010E03 /* ResultsTests.js in Resources */,

View File

@ -19,10 +19,11 @@
'use strict'; 'use strict';
var Realm = require('realm'); var Realm = require('realm');
var BaseTest = require('./base-test');
var TestCase = require('./asserts'); var TestCase = require('./asserts');
var schemas = require('./schemas'); var schemas = require('./schemas');
module.exports = { module.exports = BaseTest.extend({
testArrayLength: function() { testArrayLength: function() {
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]}); var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
realm.write(function() { realm.write(function() {
@ -282,4 +283,4 @@ module.exports = {
obj.arrayCol.splice(0, 0, obj.objectCol); obj.arrayCol.splice(0, 0, obj.objectCol);
}, 'can only splice in a write transaction'); }, 'can only splice in a write transaction');
}, },
}; });

View File

@ -19,10 +19,11 @@
'use strict'; 'use strict';
var Realm = require('realm'); var Realm = require('realm');
var BaseTest = require('./base-test');
var TestCase = require('./asserts'); var TestCase = require('./asserts');
var schemas = require('./schemas'); var schemas = require('./schemas');
module.exports = { module.exports = BaseTest.extend({
testBasicTypesPropertyGetters: function() { testBasicTypesPropertyGetters: function() {
var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA']; var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA'];
var realm = new Realm({schema: [schemas.BasicTypes]}); var realm = new Realm({schema: [schemas.BasicTypes]});
@ -147,4 +148,4 @@ module.exports = {
TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1); TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1);
TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2); TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2);
}, },
}; });

View File

@ -92,15 +92,6 @@ static void DeleteRealmFilesAtPath(NSString *path) {
- (void)tearDown { - (void)tearDown {
[self invokeMethod:@"afterEach"]; [self invokeMethod:@"afterEach"];
realm::Realm::s_global_cache.invalidate_all();
realm::Realm::s_global_cache.clear();
// FIXME - find all realm files in the docs dir and delete them rather than hardcoding these
DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm"));
DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str()));
[super tearDown]; [super tearDown];
} }
@ -116,6 +107,10 @@ static void DeleteRealmFilesAtPath(NSString *path) {
RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context]; RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context];
NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js"]; NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js"];
context[@"cleanupTestRealms"] = ^{
[self cleanupTestRealms];
};
[RealmJS initializeContext:context.JSGlobalContextRef]; [RealmJS initializeContext:context.JSGlobalContextRef];
// Expose the global Realm object as a global 'realm' CommonJS module. // Expose the global Realm object as a global 'realm' CommonJS module.
@ -155,6 +150,17 @@ static void DeleteRealmFilesAtPath(NSString *path) {
return suite; return suite;
} }
+ (void)cleanupTestRealms {
realm::Realm::s_global_cache.invalidate_all();
realm::Realm::s_global_cache.clear();
// FIXME - find all realm files in the docs dir and delete them rather than hardcoding these
DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm"));
DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str()));
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *sig = [super methodSignatureForSelector:aSelector]; NSMethodSignature *sig = [super methodSignatureForSelector:aSelector];
return sig ?: [NSMethodSignature signatureWithObjCTypes:"v@:"]; return sig ?: [NSMethodSignature signatureWithObjCTypes:"v@:"];

View File

@ -19,11 +19,12 @@
'use strict'; 'use strict';
var Realm = require('realm'); var Realm = require('realm');
var BaseTest = require('./base-test');
var TestCase = require('./asserts'); var TestCase = require('./asserts');
var schemas = require('./schemas'); var schemas = require('./schemas');
var util = require('./util'); var util = require('./util');
module.exports = { module.exports = BaseTest.extend({
testRealmConstructorPath: function() { testRealmConstructorPath: function() {
TestCase.assertThrows(function() { new Realm('/invalidpath'); }); TestCase.assertThrows(function() { new Realm('/invalidpath'); });
TestCase.assertThrows(function() { new Realm(util.realmPathForFile('test1.realm'), 'invalidArgument'); }); TestCase.assertThrows(function() { new Realm(util.realmPathForFile('test1.realm'), 'invalidArgument'); });
@ -291,4 +292,4 @@ module.exports = {
TestCase.assertEqual(notificationCount, 1); TestCase.assertEqual(notificationCount, 1);
TestCase.assertEqual(notificationName, 'DidChangeNotification'); TestCase.assertEqual(notificationName, 'DidChangeNotification');
}, },
}; });

View File

@ -19,10 +19,11 @@
'use strict'; 'use strict';
var Realm = require('realm'); var Realm = require('realm');
var BaseTest = require('./base-test');
var TestCase = require('./asserts'); var TestCase = require('./asserts');
var schemas = require('./schemas'); var schemas = require('./schemas');
module.exports = { module.exports = BaseTest.extend({
testResultsLength: function() { testResultsLength: function() {
var realm = new Realm({schema: [schemas.TestObject]}); var realm = new Realm({schema: [schemas.TestObject]});
var objects = realm.objects('TestObject'); var objects = realm.objects('TestObject');
@ -103,4 +104,4 @@ module.exports = {
TestCase.assertEqual(objects[3].doubleCol, 1); TestCase.assertEqual(objects[3].doubleCol, 1);
TestCase.assertEqual(objects[4].doubleCol, 0); TestCase.assertEqual(objects[4].doubleCol, 0);
}, },
}; });

36
tests/base-test.js Normal file
View File

@ -0,0 +1,36 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 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.
//
////////////////////////////////////////////////////////////////////////////
'use strict';
var util = require('./util');
var prototype = exports.prototype = {};
exports.extend = function(object) {
object.__proto__ = prototype;
return object;
};
Object.defineProperties(prototype, {
afterEach: {
value: function() {
util.cleanupTestRealms();
}
}
});

View File

@ -20,7 +20,13 @@
var Realm = require('realm'); var Realm = require('realm');
var global = (typeof global != 'undefined') ? global : this;
exports.realmPathForFile = function(str) { exports.realmPathForFile = function(str) {
var path = Realm.defaultPath; var path = Realm.defaultPath;
return path.substring(0, path.lastIndexOf("/") + 1) + str; return path.substring(0, path.lastIndexOf("/") + 1) + str;
}; };
exports.cleanupTestRealms = function() {
global.cleanupTestRealms();
};