Commits

Tuukka Norri committed 728189a

Changed some tests so that the exact order of SQL statements isn't significant

Comments (0)

Files changed (1)

UnitTests/Sources/BXDataModelTests.m

 #import "BXDataModelTests.h"
 #import <BaseTen/BXDataModelCompiler.h>
 #import <BaseTen/BXPGEntityConverter.h>
+#import <BaseTen/BXEnumerate.h>
 #import "MKCSenTestCaseAdditions.h"
 
 
 - (void) testRelationshipOptionality
 {
 	NSArray* statements = [self importStatements: @"relationship-optionality"];
-	NSArray* expected = [NSArray arrayWithObjects:
-						 @"CREATE SCHEMA \"test_schema\";",
-						 @"CREATE TABLE \"test_schema\".\"Book\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Book\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"User\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"User\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Department\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Department\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Location\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Location\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Revision\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Revision\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Date\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Date\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Employee\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Employee\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Author\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Author\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Licence\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Licence\" ADD PRIMARY KEY (id);",
-						 @"CREATE TABLE \"test_schema\".\"Person\" (id SERIAL) ;",
-						 @"ALTER TABLE \"test_schema\".\"Person\" ADD PRIMARY KEY (id);",
-						 @"DROP TABLE IF EXISTS \"test_schema\".\"authors_books_rel\" CASCADE;",
-						 @"CREATE TABLE \"test_schema\".\"authors_books_rel\" (\"Book_id\" integer, \"Author_id\" integer);",
-						 @"ALTER TABLE \"test_schema\".\"authors_books_rel\" ADD PRIMARY KEY (\"Book_id\", \"Author_id\")",
-						 @"ALTER TABLE \"test_schema\".\"authors_books_rel\" ADD CONSTRAINT \"authors\"   FOREIGN KEY (\"Book_id\") REFERENCES \"test_schema\".\"Book\" (id)   ON UPDATE CASCADE ON DELETE CASCADE;",
-						 @"ALTER TABLE \"test_schema\".\"authors_books_rel\" ADD CONSTRAINT \"books\"   FOREIGN KEY (\"Author_id\") REFERENCES \"test_schema\".\"Author\" (id)   ON UPDATE CASCADE ON DELETE CASCADE;",
-						 @"ALTER TABLE \"test_schema\".\"User\" ADD COLUMN \"licence_id\" integer;",
-						 @"ALTER TABLE \"test_schema\".\"User\" ADD UNIQUE (\"licence_id\");",
-						 @"ALTER TABLE \"test_schema\".\"User\" ADD CONSTRAINT \"licence__user\"   FOREIGN KEY (\"licence_id\") REFERENCES \"test_schema\".\"Licence\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
-						 @"ALTER TABLE \"test_schema\".\"Employee\" ADD COLUMN \"department_id\" integer;",
-						 @"ALTER TABLE \"test_schema\".\"Employee\" ALTER COLUMN \"department_id\" SET NOT NULL;",
-						 @"ALTER TABLE \"test_schema\".\"Employee\" ADD CONSTRAINT \"department__people\"   FOREIGN KEY (\"department_id\") REFERENCES \"test_schema\".\"Department\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
-						 @"ALTER TABLE \"test_schema\".\"Person\" ADD COLUMN \"address_id\" integer;",
-						 @"ALTER TABLE \"test_schema\".\"Person\" ADD CONSTRAINT \"address__people\"   FOREIGN KEY (\"address_id\") REFERENCES \"test_schema\".\"Location\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
-						 @"ALTER TABLE \"test_schema\".\"Revision\" ADD COLUMN \"date_id\" integer;",
-						 @"ALTER TABLE \"test_schema\".\"Revision\" ALTER COLUMN \"date_id\" SET NOT NULL;",
-						 @"ALTER TABLE \"test_schema\".\"Revision\" ADD UNIQUE (\"date_id\");",
-						 @"ALTER TABLE \"test_schema\".\"Revision\" ADD CONSTRAINT \"date__revision\"   FOREIGN KEY (\"date_id\") REFERENCES \"test_schema\".\"Date\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
-						 nil];
-	MKCAssertEqualObjects (expected, statements);
+	MKCAssertTrue (38 == [statements count]);
+	NSArray* creationStatements = [statements subarrayWithRange: NSMakeRange (1, 20)];
+	NSArray* constraintStatements = [statements subarrayWithRange: NSMakeRange (21, 17)];
+	
+	MKCAssertEqualObjects ([statements objectAtIndex: 0], @"CREATE SCHEMA \"test_schema\";");
+	
+	{
+		NSArray* expectedCreationStatements = [NSArray arrayWithObjects:
+											   @"CREATE TABLE \"test_schema\".\"Author\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Author\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Book\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Book\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Date\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Date\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Department\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Department\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Employee\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Employee\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Licence\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Licence\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Location\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Location\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Person\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Person\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"Revision\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"Revision\" ADD PRIMARY KEY (id);",
+											   @"CREATE TABLE \"test_schema\".\"User\" (id SERIAL) ;",
+											   @"ALTER TABLE \"test_schema\".\"User\" ADD PRIMARY KEY (id);",
+											   nil];
+		STAssertTrue (0 == [expectedCreationStatements count] % 2, 
+					  @"There should be an equal number of expected creation statements in the test.");
+		
+		NSEnumerator* e = [expectedCreationStatements objectEnumerator];
+		NSString* stmt = nil;
+		while ((stmt = [e nextObject]))
+		{
+			NSUInteger i = [creationStatements indexOfObject: stmt];
+			MKCAssertFalse (NSNotFound == i);
+			
+			stmt = [e nextObject];
+			NSUInteger j = [creationStatements indexOfObject: stmt];
+			MKCAssertFalse (NSNotFound == j);
+			MKCAssertTrue (i + 1 == j);
+		}
+	}
+	
+	{
+		NSArray* constraintStmtArrays = [NSArray arrayWithObjects:
+										 [NSArray arrayWithObjects:
+										  @"ALTER TABLE \"test_schema\".\"Employee\" ADD COLUMN \"department_id\" integer;",
+										  @"ALTER TABLE \"test_schema\".\"Employee\" ALTER COLUMN \"department_id\" SET NOT NULL;",
+										  @"ALTER TABLE \"test_schema\".\"Employee\" ADD CONSTRAINT \"department__people\"   FOREIGN KEY (\"department_id\") REFERENCES \"test_schema\".\"Department\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
+										  nil],
+										 [NSArray arrayWithObjects:
+										  @"ALTER TABLE \"test_schema\".\"Person\" ADD COLUMN \"address_id\" integer;",
+										  @"ALTER TABLE \"test_schema\".\"Person\" ADD CONSTRAINT \"address__people\"   FOREIGN KEY (\"address_id\") REFERENCES \"test_schema\".\"Location\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
+										  nil],
+										 [NSArray arrayWithObjects:
+										  @"ALTER TABLE \"test_schema\".\"Revision\" ADD COLUMN \"date_id\" integer;",
+										  @"ALTER TABLE \"test_schema\".\"Revision\" ALTER COLUMN \"date_id\" SET NOT NULL;",
+										  @"ALTER TABLE \"test_schema\".\"Revision\" ADD UNIQUE (\"date_id\");",
+										  @"ALTER TABLE \"test_schema\".\"Revision\" ADD CONSTRAINT \"date__revision\"   FOREIGN KEY (\"date_id\") REFERENCES \"test_schema\".\"Date\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
+										  nil],										 
+										 nil];
+		BXEnumerate (expectedConstraintStmts, e, [constraintStmtArrays objectEnumerator])
+		{
+			NSString* addColStmt = [expectedConstraintStmts objectAtIndex: 0];
+			NSUInteger addColIdx = [constraintStatements indexOfObject: addColStmt];
+			MKCAssertFalse (NSNotFound == addColIdx);
+			
+			NSEnumerator* e = [expectedConstraintStmts objectEnumerator];
+			[e nextObject];
+			NSString* stmt = nil;
+			while ((stmt = [e nextObject]))
+			{
+				NSUInteger idx = [constraintStatements indexOfObject: stmt];
+				MKCAssertFalse (NSNotFound == idx);
+				MKCAssertTrue (addColIdx < idx);
+			}
+		}
+	}
+	
+	{
+		NSArray* uniqueConstraintStmtArrays = [NSArray arrayWithObjects:
+											   [NSArray arrayWithObjects:
+												@"ALTER TABLE \"test_schema\".\"User\" ADD COLUMN \"licence_id\" integer;",
+												@"ALTER TABLE \"test_schema\".\"User\" ADD UNIQUE (\"licence_id\");",
+												@"ALTER TABLE \"test_schema\".\"User\" ADD CONSTRAINT \"licence__user\"   FOREIGN KEY (\"licence_id\") REFERENCES \"test_schema\".\"Licence\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
+												nil],
+											   [NSArray arrayWithObjects:
+												@"ALTER TABLE \"test_schema\".\"Licence\" ADD COLUMN \"user_id\" integer;",
+												@"ALTER TABLE \"test_schema\".\"Licence\" ADD UNIQUE (\"user_id\");",
+												@"ALTER TABLE \"test_schema\".\"Licence\" ADD CONSTRAINT \"user__licence\"   FOREIGN KEY (\"user_id\") REFERENCES \"test_schema\".\"User\" (id)   ON DELETE SET NULL ON UPDATE CASCADE;",
+												nil],
+											   nil];
+		BOOL ok = NO;
+		BXEnumerate (expectedConstraintStmts, e, [uniqueConstraintStmtArrays objectEnumerator])
+		{
+			NSString* addColStmt = [expectedConstraintStmts objectAtIndex: 0];
+			NSUInteger addColIdx = [constraintStatements indexOfObject: addColStmt];
+			if (NSNotFound == addColIdx)
+				continue;
+			
+			NSEnumerator* e = [expectedConstraintStmts objectEnumerator];
+			[e nextObject];
+			NSString* stmt = nil;
+			while ((stmt = [e nextObject]))
+			{
+				NSUInteger idx = [constraintStatements indexOfObject: stmt];
+				if (NSNotFound == idx || ! (addColIdx < idx))
+					goto loopend;
+			}
+			
+			ok = YES;
+			break;
+			
+		loopend:
+			;
+		}
+		MKCAssertTrue (ok);
+	}
+	
+	{
+		NSArray* expectedMTMStmts = [NSArray arrayWithObjects:
+									 @"DROP TABLE IF EXISTS \"test_schema\".\"authors_books_rel\" CASCADE;",
+									 @"CREATE TABLE \"test_schema\".\"authors_books_rel\" (\"Author_id\" integer, \"Book_id\" integer);",
+									 @"ALTER TABLE \"test_schema\".\"authors_books_rel\" ADD PRIMARY KEY (\"Author_id\", \"Book_id\")",
+									 @"ALTER TABLE \"test_schema\".\"authors_books_rel\" ADD CONSTRAINT \"authors\"   FOREIGN KEY (\"Book_id\") REFERENCES \"test_schema\".\"Book\" (id)   ON UPDATE CASCADE ON DELETE CASCADE;",
+									 @"ALTER TABLE \"test_schema\".\"authors_books_rel\" ADD CONSTRAINT \"books\"   FOREIGN KEY (\"Author_id\") REFERENCES \"test_schema\".\"Author\" (id)   ON UPDATE CASCADE ON DELETE CASCADE;",
+									 nil];
+		NSUInteger i = [constraintStatements indexOfObject: [expectedMTMStmts objectAtIndex: 0]];
+		NSUInteger j = [constraintStatements indexOfObject: [expectedMTMStmts objectAtIndex: 1]];
+		MKCAssertFalse (NSNotFound == i);
+		MKCAssertFalse (NSNotFound == j);
+		MKCAssertTrue (i + 1 == j);
+		
+		NSEnumerator* e = [expectedMTMStmts objectEnumerator];
+		[e nextObject];
+		[e nextObject];
+		NSString* stmt = nil;
+		while ((stmt = [e nextObject]))
+		{
+			NSUInteger idx = [constraintStatements indexOfObject: stmt];
+			MKCAssertFalse (NSNotFound == idx);
+			MKCAssertTrue (j < idx);
+		}
+	}
 }
 @end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.