Creating A Sample Test

Let's create a simple test case to check if the cascade on update setting is working as expected.

We will use a simple linked list class:

 

 

CascadeOnUpdate.cs: Atom
01namespace Db4oUnit.Extensions.Tests 02{ 03 public class CascadeOnUpdate : AbstractDb4oTestCase 04 { 05 public class Atom 06 { 07 public Atom child; 08 09 public string name; 10 11 public Atom() 12 { 13 } 14 15 public Atom(Atom child) 16 { 17 this.child = child; 18 } 19 20 public Atom(string name) 21 { 22 this.name = name; 23 } 24 25 public Atom(Atom child, string name) 26 : this(child) 27 { 28 this.name = name; 29 } 30 }

 

Custom configuration should be added to configure method:

 

CascadeOnUpdate.cs: Configure
1protected override void Configure(Db4objects.Db4o.Config.IConfiguration conf) 2 { 3 conf.ObjectClass(typeof(Atom)).CascadeOnUpdate(true); 4 }

 

To prepare the database we will need to store some Atom objects:

 

CascadeOnUpdate.cs: Store
1protected override void Store() 2 { 3 Atom atom = new Atom(new Atom(new Atom("StoredGrandChildChild"), "StoredChild"), "Parent"); 4 Db().Set(atom); 5 }

 

Now we are ready to test the expected behavior:

 

CascadeOnUpdate.cs: Test
01public virtual void Test() 02 { 03 IQuery q = NewQuery(typeof(Atom)); 04 q.Descend("name").Constrain("Parent"); 05 IObjectSet objectSet = q.Execute(); 06 Atom atom = null; 07 while (objectSet.HasNext()) 08 { 09 // update child objects 10 atom = (Atom)objectSet.Next(); 11 ((Atom)atom.child).name = "Updated"; 12 ((Atom)atom.child).child.name = "NotUpdated"; 13 // store the parent object 14 Db().Set(atom); 15 } 16 17 // commit and refresh to make sure that the changes are saved 18 // and reference cash is refreshed 19 Db().Commit(); 20 Db().Refresh(atom, Int32.MaxValue); 21 22 23 q = NewQuery(typeof(Atom)); 24 q.Descend("name").Constrain("Parent"); 25 objectSet = q.Execute(); 26 while (objectSet.HasNext()) 27 { 28 atom = (Atom)objectSet.Next(); 29 // check if the child objects were updated 30 Atom child = (Atom)atom.child; 31 Assert.AreEqual("Updated", child.name); 32 Assert.AreNotEqual("Updated", child.child.name); 33 } 34 }