Commits

Tuukka Norri committed 3317ff5

Deprecated methods, bug fix
- Changed deprecated relationship names.
- Also replaced other deprecated methods.
- Changed the test loader so that it actually fails on unexpected exceptions.
- BXDataModelTests now uses BXPGEntityConverter instead of BXPGEntityImporter, since the former doesn't require a database connection.
- Fixed a bug which caused a mock object to be stored into a database object model.

  • Participants
  • Parent commits 24a790d

Comments (0)

Files changed (12)

UnitTests/Sources/BXDataModelTests.h

 #import <SenTestingKit/SenTestingKit.h>
 #import "BXTestCase.h"
 #import <BaseTen/BaseTen.h>
-#import <BaseTen/BXPGEntityImporter.h>
+#import <BaseTen/BXPGEntityConverter.h>
 
 
-@interface BXDataModelTests : BXDatabaseTestCase <BXPGEntityImporterDelegate>
+@interface BXDataModelTests : BXDatabaseTestCase <BXPGEntityConverterDelegate>
 {
-	BXPGEntityImporter* mImporter;
+	BXPGEntityConverter *mConverter;
 }
 @end

UnitTests/Sources/BXDataModelTests.m

 //
 
 #import "BXDataModelTests.h"
+#import <BaseTen/BXDatabaseContextPrivate.h>
 #import <BaseTen/BXDataModelCompiler.h>
-#import <BaseTen/BXPGEntityConverter.h>
+#import <BaseTen/BXPGInterface.h>
+#import <BaseTen/BXPGTransactionHandler.h>
 #import <BaseTen/BXEnumerate.h>
 #import "MKCSenTestCaseAdditions.h"
 
 {
 	[super setUp];
 	
-	mImporter = [[BXPGEntityImporter alloc] init];
-	[mImporter setDelegate: self];
-	[mImporter setDatabaseContext: mContext];
+	mConverter = [[BXPGEntityConverter alloc] init];
+	[mConverter setDelegate: self];
 }
 
+
 - (void) tearDown
 {
-	[mImporter release];
+	[mConverter release];
 	[super tearDown];
 }
 
-- (void) entityImporterAdvanced: (BXPGEntityImporter *) importer
+
+- (BXEntityDescription *) entityConverter: (BXPGEntityConverter *) converter 
+ shouldAddDropStatementFromEntityMatching: (NSEntityDescription *) importedEntity
+								 inSchema: (NSString *) schemaName
+									error: (NSError **) outError
 {
+	return NO;
 }
 
-- (void) entityImporter: (BXPGEntityImporter *) importer finishedImporting: (BOOL) succeeded error: (NSError *) error
+
+- (BOOL) entityConverter: (BXPGEntityConverter *) converter shouldCreateSchema: (NSString *) schemaName
 {
+	return YES;
+}
+
+
+- (PGTSConnection *) connectionForEntityConverter: (BXPGEntityConverter *) converter
+{
+	return [[(BXPGInterface *) [mContext databaseInterface] transactionHandler] connection];
 }
 
 
 	MKCAssertNotNil (model);
 	MKCAssertNotNil (entities);
 	
-	[mImporter setSchemaName: @"test_schema"];
-	[mImporter setEntities: entities];
-	NSArray* statements = [mImporter importStatements];
+	NSArray *enabledRelations = nil;
+	NSArray *errors = nil;
+	NSArray *statements = [mConverter statementsForEntities: entities
+												 schemaName: @"test_schema"
+										   enabledRelations: &enabledRelations 
+													 errors: &errors];
 	MKCAssertNotNil (statements);
 	
 	return statements;
 }
 
+
 - (void) testPeopleDepartments
 {
 	NSArray* statements = [self importStatements: @"people-departments"];
 	MKCAssertEqualObjects (expected, statements);
 }
 
+
 - (void) testPeopleDepartmentsNoInverse
 {
 	NSArray* statements = [self importStatements: @"people-departments-no-inverse"];
 	MKCAssertEqualObjects (expected, statements);
 }	
 
+
 - (void) testOneToOne
 {
 	NSArray* statements = [self importStatements: @"one-to-one"];
 				  statements, expected1, expected2);
 }
 
+
 - (void) testRelationshipOptionality
 {
 	NSArray* statements = [self importStatements: @"relationship-optionality"];

UnitTests/Sources/BXPredicateTests.m

 	
 	mQueryBuilder = [[BXPGQueryBuilder alloc] init];
 	
-	BXDatabaseContext* ctx = [[BXDatabaseContext alloc] initWithDatabaseURI: [self databaseURI]];
+	BXDatabaseObjectModelStorage *storage = [[[BXDatabaseObjectModelStorage alloc] init] autorelease];
+	BXDatabaseContext *ctx = [[[BXDatabaseContext alloc] init] autorelease];
 	[ctx setDelegate: self];
+	[ctx setDatabaseObjectModelStorage: storage];
+	[ctx setDatabaseURI: [self databaseURI]];
 	
 	BXEntityDescription* entity = [[ctx databaseObjectModel] entityForTable: @"test" inSchema: @"public"];
 	MKCAssertNotNil (entity);

UnitTests/Sources/BXTestCase.h

 //
 
 #import <BaseTen/BaseTen.h>
+#import <SenTestingKit/SenTestingKit.h>
 
 
 extern int d_eq (double a, double b);

UnitTests/Sources/ForeignKeyModificationTests.m

     BXDatabaseObject* object = [mContext createObjectForEntity: oneEntity withFieldValues: nil error: &error];
     STAssertNotNil (object, [error description]);
 	//If the set proxy wasn't created earlier, here it will be. This might be useful for debugging.
-    STAssertTrue (0 == [[object valueForKey: [manyEntity name]] count], [[object valueForKey: [manyEntity name]] description]);
+	NSString *relationshipName = [[manyEntity name] stringByAppendingString: @"Set"];
+    STAssertTrue (0 == [[object valueForKey: relationshipName] count], [[object valueForKey: relationshipName] description]);
     MKCAssertTrue ([[object objectID] entity] == oneEntity);
     
     const int count = 2;
     }
     MKCAssertTrue (count == [foreignObjects count]);
     
-	[object setPrimitiveValue: foreignObjects forKey: [manyEntity name]];
+	[object setPrimitiveValue: foreignObjects forKey: relationshipName];
     
-    NSSet* referencedObjects = [NSSet setWithSet: [object primitiveValueForKey: [manyEntity name]]];
+    NSSet* referencedObjects = [NSSet setWithSet: [object primitiveValueForKey: relationshipName]];
     MKCAssertEqualObjects (referencedObjects, foreignObjects);
 
     [mContext rollback];
 - (void) remove1: (BXEntityDescription *) oneEntity
 {
     BXDatabaseObject* object = [self removeRefObject: oneEntity];
-	MKCAssertTrue (0 < [[object primitiveValueForKey: @"test2"] count]);
-    [object setPrimitiveValue: nil forKey: @"test2"];
-	MKCAssertTrue (0 == [[object primitiveValueForKey: @"test2"] count]);
+	MKCAssertTrue (0 < [[object primitiveValueForKey: @"test2Set"] count]);
+    [object setPrimitiveValue: nil forKey: @"test2Set"];
+	MKCAssertTrue (0 == [[object primitiveValueForKey: @"test2Set"] count]);
 
     [mContext rollback];
 }
 - (void) remove2: (BXEntityDescription *) oneEntity
 {
     BXDatabaseObject* object = [self removeRefObject: oneEntity];
-    NSSet* refObjects = [object primitiveValueForKey: @"test2"];
+    NSSet* refObjects = [object primitiveValueForKey: @"test2Set"];
     BXEnumerate (currentObject, e, [[refObjects allObjects] objectEnumerator])
         [currentObject setPrimitiveValue: nil forKey: @"test1"];
     
     MKCAssertTrue (1 == [res count]);
     BXDatabaseObject* object = [res objectAtIndex: 0];
     //Create a self-updating container to see if it interferes with object creation.
-    id collection = [object valueForKey: @"test2"];
+    id collection = [object valueForKey: @"test2Set"];
     
     NSDictionary* values = [NSDictionary dictionaryWithObjectsAndKeys:
 							[object primitiveValueForKey: @"id"], @"fkt1id",

UnitTests/Sources/ForeignKeyTests.m

     BXEnumerate (currentObject, e, [foreignObjects objectEnumerator])
         MKCAssertTrue ([[currentObject objectID] entity] == manyEntity);
 
-    foreignObjects = [object valueForKey: [manyEntity name]];
+    foreignObjects = [object valueForKey: [[manyEntity name] stringByAppendingString: @"Set"]];
     MKCAssertTrue (2 == [foreignObjects count]);
     values = [foreignObjects valueForKey: @"value"];
     MKCAssertTrue ([values containsObject: @"21"]);
     {
         MKCAssertTrue ([[object objectID] entity] == entity1);
         
-        NSSet* foreignObjects = [object primitiveValueForKey: [entity2 name]];
+        NSSet* foreignObjects = [object primitiveValueForKey: [[entity2 name] stringByAppendingString: @"Set"]];
         MKCAssertNotNil (foreignObjects);
         if ([@"d1" isEqualToString: [object valueForKey: @"value1"]])
         {
             MKCAssertTrue ([[foreignObject objectID] entity] == entity2);
 
             MKCAssertEqualObjects ([foreignObject valueForKey: @"value2"], @"d2");
-            NSSet* objects = [foreignObject valueForKey: [entity1 name]];
+            NSSet* objects = [foreignObject valueForKey: [[entity1 name] stringByAppendingString: @"Set"]];
             MKCAssertTrue (1 == [objects count]);
             BXDatabaseObject* backRef = [objects anyObject];
             MKCAssertTrue ([[backRef objectID] entity] == entity1);
             BXEnumerate (foreignObject, e, [foreignObjects objectEnumerator])
             {
                 MKCAssertTrue ([[foreignObject objectID] entity] == entity2);
-                NSArray* objects = [foreignObject valueForKey: [entity1 name]];
+                NSArray* objects = [foreignObject valueForKey: [[entity1 name] stringByAppendingString: @"Set"]];
                 MKCAssertNotNil (objects);
                 MKCAssertTrue (3 == [objects count]);
                 
 											error: &error];
 	STAssertNotNil (res, [error description]);
 	BXDatabaseObject* object = [res objectAtIndex: 0];
-	NSSet* helperObjects = [object primitiveValueForKey: @"mtmrel1"];
+	NSSet* helperObjects = [object primitiveValueForKey: @"mtmrel1Set"];
 	MKCAssertTrue (3 == [helperObjects count]);
 	BXEnumerate (currentObject, e, [helperObjects objectEnumerator])
 	{

UnitTests/Sources/MTMCollectionTest.m

     MKCAssertTrue (4 == [res count]);
     
     //Get an object from the result
+	NSString *relationshipName = [[entity2 name] stringByAppendingString: @"Set"];
     NSPredicate* predicate = [NSPredicate predicateWithFormat: @"value1 = 'a1'"];
     res =  [res filteredArrayUsingPredicate: predicate];
     MKCAssertTrue (1 == [res count]);
     BXDatabaseObject* object = [res objectAtIndex: 0];
-    NSCountedSet* foreignObjects = [object valueForKey: [entity2 name]];
-    NSCountedSet* foreignObjects2 = [object resolveNoncachedRelationshipNamed: [entity2 name]];
+    NSCountedSet* foreignObjects = [object valueForKey: relationshipName];
+    NSCountedSet* foreignObjects2 = [object resolveNoncachedRelationshipNamed: relationshipName];
 	
     MKCAssertNotNil (foreignObjects);
     MKCAssertNotNil (foreignObjects2);

UnitTests/Sources/MTOCollectionTest.m

     
     //Get an object from the result
     //Here it doesn't matter, whether there are any objects in the relationship or not.
+	NSString *relationshipName = [[manyEntity name] stringByAppendingString: @"Set"];
     BXDatabaseObject* object = [res objectAtIndex: 0];
-    NSCountedSet* foreignObjects = [object primitiveValueForKey: [manyEntity name]];
-    NSCountedSet* foreignObjects2 = [object resolveNoncachedRelationshipNamed: [manyEntity name]];
+    NSCountedSet* foreignObjects = [object primitiveValueForKey: relationshipName];
+    NSCountedSet* foreignObjects2 = [object resolveNoncachedRelationshipNamed: relationshipName];
     MKCAssertNotNil (foreignObjects);
     MKCAssertNotNil (foreignObjects2);
     MKCAssertTrue (foreignObjects != foreignObjects2);
     MKCAssertTrue ([foreignObjects isEqualToSet: foreignObjects2]);
 
     //Remove the referenced objects
-    [object setValue: nil forKey: [manyEntity name]];
+    [object setValue: nil forKey: relationshipName];
     MKCAssertTrue (0 == [foreignObjects count]);
     MKCAssertTrue ([foreignObjects isEqualToSet: foreignObjects2]);
     
     MKCAssertTrue (3 == [objects2 count]);
     
     //Set the referenced objects. The self-updating collection should get notified when objects are added.
-    [object setPrimitiveValue: objects2 forKey: [manyEntity name]];
+    [object setPrimitiveValue: objects2 forKey: relationshipName];
     
     MKCAssertTrue (3 == [foreignObjects count]);
     MKCAssertEqualObjects ([NSSet setWithSet: foreignObjects], objects2);
     MKCAssertTrue (2 == [res count]);
     
     //Get an object from the result
+	NSString *relationshipName = [[manyEntity name] stringByAppendingString: @"Set"];
     BXDatabaseObject* object = [res objectAtIndex: 0];
-    NSCountedSet* foreignObjects = [object valueForKey: [manyEntity name]];
-    NSCountedSet* foreignObjects2 = [object resolveNoncachedRelationshipNamed: [manyEntity name]];
+    NSCountedSet* foreignObjects = [object valueForKey: relationshipName];
+    NSCountedSet* foreignObjects2 = [object resolveNoncachedRelationshipNamed: relationshipName];
     MKCAssertNotNil (foreignObjects);
     MKCAssertNotNil (foreignObjects2);
     MKCAssertTrue (foreignObjects != foreignObjects2);

UnitTests/Sources/ObjectIDTests.m

 	BXDatabaseObject* object = [[mContext executeFetchForEntity: entity 
 											 withPredicate: [NSPredicate predicateWithFormat: @"id == 1"]
 													 error: &error] objectAtIndex: 0];
-	MKCAssertNotNil (object);
+	STAssertNotNil (object, [error description]);
 	
 	BXDatabaseObjectID* objectID = [object objectID];
 	NSURL* uri = [objectID URIRepresentation];
 	//Change the URI back to a object id
 	BXDatabaseContext* ctx2 = [BXDatabaseContext contextWithDatabaseURI: [mContext databaseURI]];
 	[ctx2 setDelegate: self];
-	BXDatabaseObjectID* objectID2 = [[[BXDatabaseObjectID alloc] initWithURI: uri context: ctx2 error: &error] autorelease];
-	STAssertNotNil (objectID2, [error description]);
+	BXDatabaseObjectID* objectID2 = [[[BXDatabaseObjectID alloc] initWithURI: uri context: ctx2] autorelease];
+	MKCAssertNotNil (objectID2);
 	MKCAssertEqualObjects (objectID, objectID2);
 	
 	BXDatabaseObject* fault = [[ctx2 faultsWithIDs: [NSArray arrayWithObject: objectID2]] objectAtIndex: 0];
 
 - (void) testInvalidObjectID
 {
+	NSURL* uri = [NSURL URLWithString: @"/public/test?id,n=12345" relativeToURL: [mContext databaseURI]];
+	BXDatabaseObjectID* anId = [[[BXDatabaseObjectID alloc] initWithURI: uri context: mContext] autorelease];
+	MKCAssertNotNil (anId);
+	
 	NSError* error = nil;
-	NSURL* uri = [NSURL URLWithString: @"/public/test?id,n=12345" relativeToURL: [mContext databaseURI]];
-	BXDatabaseObjectID* anId = [[[BXDatabaseObjectID alloc] initWithURI: uri context: mContext error: &error] autorelease];
-	STAssertNotNil (anId, [error description]);
-	
 	STAssertTrue ([mContext connectIfNeeded: &error], [error description]);
 	
 	BXDatabaseObject* object = [mContext objectWithID: anId error: &error];
 
 - (void) testValidObjectID
 {
+	NSURL* uri = [NSURL URLWithString: @"/public/test?id,n=1" relativeToURL: [mContext databaseURI]];
+	BXDatabaseObjectID* anId = [[[BXDatabaseObjectID alloc] initWithURI: uri context: mContext] autorelease];
+	MKCAssertNotNil (anId);
+	
 	NSError* error = nil;
-	NSURL* uri = [NSURL URLWithString: @"/public/test?id,n=1" relativeToURL: [mContext databaseURI]];
-	BXDatabaseObjectID* anId = [[[BXDatabaseObjectID alloc] initWithURI: uri context: mContext error: &error] autorelease];
-	STAssertNotNil (anId, [error description]);
-	
 	STAssertTrue ([mContext connectIfNeeded: &error], [error description]);
 	
 	BXDatabaseObject* object = [mContext objectWithID: anId error: &error];

UnitTests/Sources/TestLoader.h

  * 3. Run otest with like this: `otest -SenTest BXTestLoader your_custom_path/UnitTests.octest`.
  */
 
-#import <SenTestingKit/SenTestingKit.h>
+#import "BXTestCase.h"
 
 
-@interface BXTestLoader : SenTestCase 
+@interface BXTestLoader : BXTestCase 
 {
 }
 

UnitTests/Sources/TestLoader.m

 	
 	for (Class testCaseClass in testClasses)
 	{
+		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 		SenTestSuite *suite = [SenTestSuite testSuiteForTestCaseClass: testCaseClass];
 		SenTestRun *testRun = [suite run];
 		STAssertTrue (0 == [testRun unexpectedExceptionCount], @"Had %u unexpected exceptions.", [testRun unexpectedExceptionCount]);
+		[pool drain];
 	}
 }
 @end

UnitTests/Sources/UndoTests.m

 
     MKCAssertNotNil (object);
     
-    NSMutableSet* foreignObjects = [object primitiveValueForKey: @"test2"];
+    NSMutableSet* foreignObjects = [object primitiveValueForKey: @"test2Set"];
     BXDatabaseObject* foreignObject = [foreignObjects anyObject];
     [foreignObjects removeObject: foreignObject];
     MKCAssertTrue (1 == [foreignObjects count]);
 	//FIXME: this should really be fetched from a different database context since now we get the same object we fetched earlier.
-    NSMutableSet* foreignObjects2 = [[self objectWithId: objectId entity: test1] primitiveValueForKey: @"test2"];
+    NSMutableSet* foreignObjects2 = [[self objectWithId: objectId entity: test1] primitiveValueForKey: @"test2Set"];
     MKCAssertEqualObjects (foreignObjects, foreignObjects2);
     
     [undoManager undo];