From faf905a6ce9f8ffef1fed1ef1134f2c6004aec01 Mon Sep 17 00:00:00 2001 From: James Burka Date: Tue, 27 Jan 2009 15:14:17 -0500 Subject: [PATCH] [#7] fixed tests to work with route changes. --- Classes/DogTest.m | 105 +++++++++--------- Classes/ItemIds.h | 3 + Classes/PersonTest.h | 17 +++ Classes/PersonTest.m | 68 ++++++++++++ objective_resource.xcodeproj/project.pbxproj | 10 ++ .../app/controllers/dogs_controller.rb | 2 +- sample_rails_app/lib/tasks/populate.rake | 15 ++- 7 files changed, 164 insertions(+), 56 deletions(-) create mode 100644 Classes/ItemIds.h create mode 100644 Classes/PersonTest.h create mode 100644 Classes/PersonTest.m diff --git a/Classes/DogTest.m b/Classes/DogTest.m index d275a7f..98b0716 100644 --- a/Classes/DogTest.m +++ b/Classes/DogTest.m @@ -8,16 +8,20 @@ #import "DogTest.h" #import "Dog.h" +#import "Person.h" #import "NSString+XMLSerializableSupport.h" +#import "ItemIds.h" @implementation DogTest -NSUInteger shouldBe = 0; +static Person *owner; -(void) setUp { [ObjectiveResource setSite:@"http://localhost:3000/"]; - [ObjectiveResource setResponseType:JSONResponse]; - //[ObjectiveResource setResponseType:XmlResponse]; + //[ObjectiveResource setResponseType:JSONResponse]; + [ObjectiveResource setResponseType:XmlResponse]; + + owner = [Person find:[NSString stringWithFormat:@"%i",DOG_OWNER]]; } -(void) testDogProperties { @@ -30,84 +34,85 @@ -(void) testDogProperties { -(void) testDogCount { - if(shouldBe == 0) { - shouldBe = 100; - } - NSArray * dogs = [Dog findAll]; + NSArray * dogs = [owner findAllDogs]; - STAssertEquals(shouldBe , [dogs count], @"Should have %d dogs , %d found" , - shouldBe, [dogs count]); + STAssertTrue(20 == [dogs count], @"Should have %i dogs , %d found" , + 20 ,[dogs count]); } - (void) testDogWithEscapableChars { + int shouldBe = [[owner findAllDogs] count] + 1; + Dog* aDog = [[Dog alloc] init]; - aDog.name = @"Helio's Coffee & friends"; - if(shouldBe == 0) { - - shouldBe = 100; - - } - shouldBe += 1; + aDog.personId = owner.personId; + aDog.name = @"Helio's Coffee & Chairs"; [aDog save]; - NSArray * dogs = [Dog findAll]; - STAssertEquals(shouldBe , [dogs count], @"Should have %d dogs , %d found" , - shouldBe, [dogs count]); - STAssertTrue([[aDog.name toXMLValue] isEqualToString: @"Helio's Coffee & friends"],@"Should be Helio's Coffee & friends , got %@" , [aDog.name toXMLValue]); - STAssertTrue([[NSString fromXmlString:aDog.name] isEqualToString: @"Helio's Coffee & friends"],@"Should be Helio's Coffee & friends , got %@" , [aDog.name toXMLValue]); + NSArray * dogs = [owner findAllDogs]; + + + STAssertTrue(shouldBe == [dogs count], @"Should have %i dogs , %d found" , + shouldBe ,[dogs count]); + STAssertTrue([[aDog.name toXMLValue] isEqualToString: @"Helio's Coffee & Chairs"],@"Should be Helio's Coffee & Chairs , got %@" , [aDog.name toXMLValue]); + STAssertTrue([[NSString fromXmlString:aDog.name] isEqualToString: @"Helio's Coffee & Chairs"],@"Should be Helio's Coffee & Chairs , got %@" , [aDog.name toXMLValue]); } -(void) testDogSave{ + int shouldBe = [[owner findAllDogs]count] + 1; Dog * aDog = [[Dog alloc] init]; - + aDog.personId = owner.personId; aDog.name = @"Judge"; - - if(shouldBe == 0) { - - shouldBe = 100; - - } - shouldBe += 1; + [aDog save]; - NSArray * dogs = [Dog findAll]; - STAssertEquals(shouldBe , [dogs count], @"Should have %d dogs , %d found" , - shouldBe, [dogs count]); + NSArray * dogs = [owner findAllDogs]; + STAssertTrue(shouldBe == [dogs count], @"Should have %i dogs , %d found" , + shouldBe ,[dogs count]); [aDog release]; } -(void) testDogUpdate{ - - NSArray * dogs = [Dog findAll]; - - Dog * aDog = [dogs objectAtIndex:0]; - + NSArray * dogs = [owner findAllDogs]; + BOOL found = NO; + Dog *aDog = [dogs objectAtIndex:0]; aDog.name = @"Judge"; [aDog update]; - if(shouldBe == 0) { - - shouldBe = 100; - - } - STAssertEquals(shouldBe , [dogs count], @"Should have %d dogs , %d found" , - shouldBe, [dogs count]); + + aDog = [dogs objectAtIndex:0]; + + for(Dog *dog in dogs) { + if([dog isEqual:aDog]) { + STAssertTrue([dog.name isEqualToString: aDog.name],@"Should be Judge , got %@" , aDog.name); + found = YES; + } + } + STAssertTrue(found,@"Should found the record"); + } -(void) testDogDelete { - NSArray * dogs = [Dog findAll]; - - shouldBe = [dogs count] - 1; + BOOL found = NO; + NSArray * dogs = [owner findAllDogs]; - [(Dog *)[dogs objectAtIndex:0] destroy]; + int shouldBe = [dogs count] - 1; + Dog *toDelete = (Dog *)[dogs objectAtIndex:0]; + [toDelete destroy]; - dogs = [Dog findAll]; - STAssertEquals(shouldBe , [dogs count], @"Should have %d dogs , %d found" , + dogs = [owner findAllDogs]; + STAssertTrue(shouldBe == [dogs count], @"Should have %i dogs , %d found" , shouldBe ,[dogs count]); + + for(Dog *dog in dogs) { + if([dog isEqual:toDelete]) { + found = YES; + } + } + STAssertFalse(found,@"Should not found the record"); } diff --git a/Classes/ItemIds.h b/Classes/ItemIds.h new file mode 100644 index 0000000..ddc3a9b --- /dev/null +++ b/Classes/ItemIds.h @@ -0,0 +1,3 @@ +#define DOG_OWNER 1 +#define PERSON 2 +#define PERSON_DESTROY 3 \ No newline at end of file diff --git a/Classes/PersonTest.h b/Classes/PersonTest.h new file mode 100644 index 0000000..e337a89 --- /dev/null +++ b/Classes/PersonTest.h @@ -0,0 +1,17 @@ +// +// PersonTest.h +// objective_resource +// +// Created by James Burka on 1/27/09. +// Copyright 2009 Burkaprojects. All rights reserved. +// + +#import "GTMSenTestCase.h" + + + +@interface PersonTest : SenTestCase { + +} + +@end diff --git a/Classes/PersonTest.m b/Classes/PersonTest.m new file mode 100644 index 0000000..8d0dec2 --- /dev/null +++ b/Classes/PersonTest.m @@ -0,0 +1,68 @@ +// +// PersonTest.m +// objective_resource +// +// Created by James Burka on 1/27/09. +// Copyright 2009 Burkaprojects. All rights reserved. +// + +#import "PersonTest.h" +#import "Person.h" +#import "ItemIds.h" + + +@implementation PersonTest + +-(void) setUp { + [ObjectiveResource setSite:@"http://localhost:3000/"]; + [ObjectiveResource setResponseType:JSONResponse]; + //[ObjectiveResource setResponseType:XmlResponse]; +} + +-(void) testPersonDelete { + int count = [[Person findAll]count]; + Person *person = [Person find:[NSString stringWithFormat:@"%i",PERSON_DESTROY]]; + STAssertTrue([person destroy], @"Should have been true"); + NSArray *people = [Person findAll]; + STAssertTrue((count-1) == [people count], @"Should have %i people , %d found" ,count ,[people count]); + +} + +-(void) testPersonCreate { + BOOL found = NO; + Person *toCreate = [[Person alloc] init]; + toCreate.name = @"Daniel Waterhouse"; + STAssertTrue( [toCreate create], @"Should have been true"); + NSArray *people = [Person findAll]; + + for(Person *person in people) { + if([toCreate isEqual:person]) { + found = YES; + } + } + STAssertTrue(found, @"Did not find the new person : %@",toCreate.name); +} + +-(void) testPersonUpdate { + BOOL found = NO; + Person *toUpdate = [Person find:[NSString stringWithFormat:@"%i",PERSON]]; + toUpdate.name = @"America Shaftoe"; + STAssertTrue( [toUpdate save], @"Should have been true"); + NSArray *people = [Person findAll]; + for(Person *person in people) { + if([toUpdate isEqual:person] && [toUpdate.name isEqualToString:person.name]) { + found = YES; + } + } + STAssertTrue(found, @""); +} + +-(void) testFindPerson { + NSArray * people = [Person findAll]; + Person *toFind = (Person *)[people objectAtIndex:0]; + STAssertTrue([toFind isEqual:[Person find:toFind.personId]], @"Should of returned %@",toFind.name); +} + + + +@end diff --git a/objective_resource.xcodeproj/project.pbxproj b/objective_resource.xcodeproj/project.pbxproj index 242cc04..d7bae6f 100644 --- a/objective_resource.xcodeproj/project.pbxproj +++ b/objective_resource.xcodeproj/project.pbxproj @@ -27,6 +27,8 @@ 2314FF730F2F8089003B13AB /* Person.m in Sources */ = {isa = PBXBuildFile; fileRef = 2314FF6F0F2F8089003B13AB /* Person.m */; }; 2314FF760F2F8094003B13AB /* ViewPersonController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2314FF750F2F8094003B13AB /* ViewPersonController.m */; }; 2314FF770F2F8094003B13AB /* ViewPersonController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2314FF750F2F8094003B13AB /* ViewPersonController.m */; }; + 2322CD650F2F968000EDF562 /* PersonTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 2322CD640F2F968000EDF562 /* PersonTest.m */; }; + 2322CD660F2F968000EDF562 /* PersonTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 2322CD640F2F968000EDF562 /* PersonTest.m */; }; 2377C4F50F019E67006E155F /* NSHTTPURLResponse+Error.m in Sources */ = {isa = PBXBuildFile; fileRef = 2377C4F40F019E67006E155F /* NSHTTPURLResponse+Error.m */; }; 2377C4F60F019E67006E155F /* NSHTTPURLResponse+Error.m in Sources */ = {isa = PBXBuildFile; fileRef = 2377C4F40F019E67006E155F /* NSHTTPURLResponse+Error.m */; }; 23829E0C0EA393700070F0BF /* EditDogViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 23829E0B0EA393700070F0BF /* EditDogViewController.m */; }; @@ -130,6 +132,9 @@ 2314FF6F0F2F8089003B13AB /* Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = ""; }; 2314FF740F2F8094003B13AB /* ViewPersonController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewPersonController.h; sourceTree = ""; }; 2314FF750F2F8094003B13AB /* ViewPersonController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewPersonController.m; sourceTree = ""; }; + 2322CD630F2F968000EDF562 /* PersonTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersonTest.h; sourceTree = ""; }; + 2322CD640F2F968000EDF562 /* PersonTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PersonTest.m; sourceTree = ""; }; + 2322CD890F2F9F3D00EDF562 /* ItemIds.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ItemIds.h; sourceTree = ""; }; 2377C4F30F019E67006E155F /* NSHTTPURLResponse+Error.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSHTTPURLResponse+Error.h"; path = "lib/NSHTTPURLResponse+Error.h"; sourceTree = ""; }; 2377C4F40F019E67006E155F /* NSHTTPURLResponse+Error.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSHTTPURLResponse+Error.m"; path = "lib/NSHTTPURLResponse+Error.m"; sourceTree = ""; }; 23829E0A0EA393700070F0BF /* EditDogViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EditDogViewController.h; sourceTree = ""; }; @@ -293,6 +298,9 @@ 239D50D90EA6921800318802 /* DogTest.m */, 23B4A6400F092B620021AB9D /* DogErrorTest.h */, 23B4A6410F092B620021AB9D /* DogErrorTest.m */, + 2322CD630F2F968000EDF562 /* PersonTest.h */, + 2322CD640F2F968000EDF562 /* PersonTest.m */, + 2322CD890F2F9F3D00EDF562 /* ItemIds.h */, ); name = UnitTests; sourceTree = ""; @@ -649,6 +657,7 @@ 2314FF700F2F8089003B13AB /* PeopleViewController.m in Sources */, 2314FF710F2F8089003B13AB /* Person.m in Sources */, 2314FF760F2F8094003B13AB /* ViewPersonController.m in Sources */, + 2322CD660F2F968000EDF562 /* PersonTest.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -700,6 +709,7 @@ 2314FF720F2F8089003B13AB /* PeopleViewController.m in Sources */, 2314FF730F2F8089003B13AB /* Person.m in Sources */, 2314FF770F2F8094003B13AB /* ViewPersonController.m in Sources */, + 2322CD650F2F968000EDF562 /* PersonTest.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/sample_rails_app/app/controllers/dogs_controller.rb b/sample_rails_app/app/controllers/dogs_controller.rb index 42687e7..c2f0968 100644 --- a/sample_rails_app/app/controllers/dogs_controller.rb +++ b/sample_rails_app/app/controllers/dogs_controller.rb @@ -52,7 +52,7 @@ def create if @dog.save flash[:notice] = 'Dog was successfully created.' format.html { redirect_to(@dog) } - format.xml { render :xml => @dog, :status => :created, :location => @dog } + format.xml { render :xml => @dog, :status => :created, :location => [@person,@dog] } format.json { render :json => @dog.to_json } else format.html { render :action => "new" } diff --git a/sample_rails_app/lib/tasks/populate.rake b/sample_rails_app/lib/tasks/populate.rake index fdc9029..3b44144 100644 --- a/sample_rails_app/lib/tasks/populate.rake +++ b/sample_rails_app/lib/tasks/populate.rake @@ -5,11 +5,16 @@ namespace :db do task :populate => :environment do require 'populator' require 'faker' - - [Dog].each(&:delete_all) - - Dog.populate 100 do |dog| - dog.name = Faker::Name.name + [Person,Dog].each(&:delete_all) + index = 1 + Person.populate 3 do |person| + person.id = index + person.name = Faker::Name.name + Dog.populate 20 do |dog| + dog.name = Faker::Name.name + dog.person_id = person.id + end + index += 1 end end end \ No newline at end of file