oleksii
oleksii

Reputation: 35905

RavenDb session management for WCF and integration testing

Put simply, I have a WCF service that manages apples. Apart from other functionality, it has two methods to add and remove apples from storage. I am writing an integration test to check if someone is getting advantage of the job and nicks apples. Raven DB in my WCF service has an audit role, it just records actions and apples. In the methods of WCF service there is some other processing: cleaning, validation, packaging etc.

My audit integration test can be expresses as

As these are two different people (two WCF calls) it make sense to use different instances of session. However, with Raven DB I get

Exception

Apple is not associated with the session, cannot delete unknown entity instance

If I now run similar integration test where two different people just add apples to the storage, the total storage content corresponds to truth. This is confusing bit: adding works across session, removing doesn't work. In this post Ayende says session micro-managing is not the way to go, but it seems natural to me to use different sessions in my integration testing. Hope analogy with apples doesn't put you off.

Question: How do I use sessions in integration testing with RavenDB?

Sample code (from notepad)

public void Remove(Apple apple)
{
    using (var session = Store.OpenSession())
    {
        session.Delete(apple);
        session.SaveChanges();      
    }
}

public void Add(Apple apple)
{
    using (var session = Store.OpenSession())
    {
        session.Store(apple);
        session.SaveChanges();      
    }
}

...

var apples = new apples[10];
//init
MyRavenDB.Add(apples);
MyRavenDB.Remove(apples.Take(4)); //throws here
//verify

Upvotes: 2

Views: 1279

Answers (2)

Ayende Rahien
Ayende Rahien

Reputation: 22956

You are passing entities over the wire, and that is generally a big no-no. Do it like this:

 public void Remove(string appleId)

That would give you much better sematnics.

Upvotes: 3

Arnold Zokas
Arnold Zokas

Reputation: 8560

In RavenDB, "The session manages change tracking for all of the entities that it has either loaded or stored".

I suspect the Apple reference you are passing to Remove() method, did not originate from RavenDB Document Store, hence the error.

Try this:

public void Remove(Apple apple)
{
    using (var session = Store.OpenSession())
    {
        var entity = session.Load<Apple>(apple.Id);

        session.Delete(entity);
        session.SaveChanges();      
    }
}

Upvotes: 3

Related Questions