can't get rspec to emulate transactional fixture.

42 views
Skip to first unread message

trung

unread,
Nov 22, 2009, 10:34:42 PM11/22/09
to DataMapper
I put the following code in spec_helper.rb

config.after(:each) do
repository do |r|
adapter = r.adapter
while adapter.current_transaction
adapter.current_transaction.rollback
adapter.pop_transaction
end
end
end


config.before(:each) do
repository do |r|
transaction = DataMapper::Transaction.new(r)
transaction.begin
r.adapter.push_transaction(transaction)
end
end

it does what it's supposed to do and reset the database after every
test.

however it has a side affect.

when i do.

Model.count # is 0
@model.save # will save, but this save is wrapped in a transaction via
dm-validations gem.
Model.count # is still 0!!!

it's really screwing with my test.

SQL log looks something like this.

begin
count

begin
insert
commit

count
....
for some strange reason, it doesn't pick up the change from the nested
transaction with the second count.

please help me over come this last hurdle. I've spent days converting
my app over to datamapper, and I might have to go back to active
record if I can't get the test suite to work.

I supposed I could do away with transactional fixture and truncate my
table before every test...

Ashley Moran

unread,
Nov 24, 2009, 6:23:10 PM11/24/09
to datam...@googlegroups.com

On 23 Nov 2009, at 03:34, trung wrote:

> I supposed I could do away with transactional fixture and truncate my
> table before every test...


I can't what's going wrong with your code, so unfortunately I can't solve you actual problem. But, FWIW, I do what you are considering, and reset the database using `Model.all.destroy!`. Just have to do it in the right order to avoid integrity constraint violations.

Ashley

--
http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran



trung

unread,
Nov 25, 2009, 5:47:59 PM11/25/09
to DataMapper
I gave up on transactional fixtures. Instead, I make sure everytime I
create a model using dm-sweatshop, it always passes in a unique email
address or username for example so it won't violate the unique
constraint. Seems to be working okay.

On Nov 24, 3:23 pm, Ashley Moran <ashley.mo...@patchspace.co.uk>
wrote:

Ashley Moran

unread,
Nov 26, 2009, 6:09:01 AM11/26/09
to datam...@googlegroups.com

On Nov 25, 2009, at 10:47 pm, trung wrote:

> I gave up on transactional fixtures. Instead, I make sure everytime I
> create a model using dm-sweatshop, it always passes in a unique email
> address or username for example so it won't violate the unique
> constraint. Seems to be working okay.

Does that mean you have data from one spec example still in the database for the next?

trung

unread,
Nov 26, 2009, 5:41:26 PM11/26/09
to DataMapper
unfortunately. yes.

i think as long as the data don't collide, it should be safe.

so i make sure every name and email address are unique when i create
them.

having said that, i still rather see transactional support working
correctly.

On Nov 26, 3:09 am, Ashley Moran <ashley.mo...@patchspace.co.uk>
wrote:

Ashley Moran

unread,
Nov 30, 2009, 8:17:16 AM11/30/09
to datam...@googlegroups.com

On Nov 26, 2009, at 10:41 pm, trung wrote:

> i think as long as the data don't collide, it should be safe.

I'd keep an eye on it - if strange things start to happen, it might be worth scripting a regular clearout of the test data.

Ashley
Reply all
Reply to author
Forward
0 new messages