Skip to content

Commit

Permalink
[#7] fixed tests to work with route changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjburka committed Jan 27, 2009
1 parent ef89592 commit faf905a
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 56 deletions.
105 changes: 55 additions & 50 deletions Classes/DogTest.m
Expand Up @@ -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 {
Expand All @@ -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");

}

Expand Down
3 changes: 3 additions & 0 deletions Classes/ItemIds.h
@@ -0,0 +1,3 @@
#define DOG_OWNER 1
#define PERSON 2
#define PERSON_DESTROY 3
17 changes: 17 additions & 0 deletions 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
68 changes: 68 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions objective_resource.xcodeproj/project.pbxproj
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -130,6 +132,9 @@
2314FF6F0F2F8089003B13AB /* Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = "<group>"; };
2314FF740F2F8094003B13AB /* ViewPersonController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ViewPersonController.h; sourceTree = "<group>"; };
2314FF750F2F8094003B13AB /* ViewPersonController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewPersonController.m; sourceTree = "<group>"; };
2322CD630F2F968000EDF562 /* PersonTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersonTest.h; sourceTree = "<group>"; };
2322CD640F2F968000EDF562 /* PersonTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PersonTest.m; sourceTree = "<group>"; };
2322CD890F2F9F3D00EDF562 /* ItemIds.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ItemIds.h; sourceTree = "<group>"; };
2377C4F30F019E67006E155F /* NSHTTPURLResponse+Error.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSHTTPURLResponse+Error.h"; path = "lib/NSHTTPURLResponse+Error.h"; sourceTree = "<group>"; };
2377C4F40F019E67006E155F /* NSHTTPURLResponse+Error.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSHTTPURLResponse+Error.m"; path = "lib/NSHTTPURLResponse+Error.m"; sourceTree = "<group>"; };
23829E0A0EA393700070F0BF /* EditDogViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EditDogViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion sample_rails_app/app/controllers/dogs_controller.rb
Expand Up @@ -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" }
Expand Down
15 changes: 10 additions & 5 deletions sample_rails_app/lib/tasks/populate.rake
Expand Up @@ -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

0 comments on commit faf905a

Please sign in to comment.