Note that there are some explanatory texts on larger screens.

plurals
  1. POFixes for problems with SubSonic 3's TestRepository
    primarykey
    data
    text
    <p>I've been trying to use SubSonic 3.0's test repository support for unit testing but encountered a few issues, so I thought I document them, and the fixes I've come up with:</p> <h2>Auto-Increment Columns Don't Work</h2> <p>Obviously with no DB, auto-increment columns don't work automatically, but if like me you're using simple ints or longs for all identity columns, this fix works well:</p> <p>(This is a copy from <a href="https://stackoverflow.com/questions/1508706/subsonic-3-active-record-testrepository-identity-column-not-icremented/2452170#2452170">here</a>, included for completeness)</p> <p>In ActiveRecord.tt:</p> <p>1: In the top of the function public void Add(IDataProvider provider){</p> <pre><code> public void Add(IDataProvider provider){ &lt;#if(tbl.PK.SysType=="long" || tbl.PK.SysType=="int") {#&gt; if (TestMode) { this.&lt;#=tbl.PK.CleanName#&gt;=++next_test_autoid; } &lt;#}#&gt; </code></pre> <p>2: Under the line public bool TestMode = false, add:</p> <pre><code> public bool TestMode = false; &lt;#if(tbl.PK.SysType=="long" || tbl.PK.SysType=="int") {#&gt; private static &lt;#=tbl.PK.SysType#&gt; next_test_autoid = 0; &lt;#}#&gt; </code></pre> <h2>Object Equality Comparison is Broken</h2> <p>Using the default ActiveRecord template, object equality doesn't work. So removing items from the DB doesn't work since the List&lt;>.Remove() used in the TestRepository fails to match the item being removed. This can be fixed in the tt templates with the following: (ie: replacing "==" with "Equals()")</p> <p>In ActiveRecord.tt:</p> <pre><code> public override bool Equals(object obj){ if(obj.GetType()==typeof(&lt;#=tbl.ClassName#&gt;)){ &lt;#=tbl.ClassName#&gt; compare=(&lt;#=tbl.ClassName#&gt;)obj; return compare.KeyValue().Equals(this.KeyValue()); }else{ return base.Equals(obj); } } </code></pre> <h2>DeleteMany is Not Implemented in the Test Repository</h2> <p>Operations like this <code>records.Delete(x =&gt; x.whatever == whatever)</code> fail against the test repo because DeleteMany is not implemented. Fixing this requires getting the source and building yourself, but here's an implementation that seems to work:</p> <p>In TestRepository.cs:</p> <pre><code> public int DeleteMany(Expression&lt;Func&lt;T, bool&gt;&gt; expression) { foreach (var x in _items.AsQueryable().Where(expression).ToList()) { _items.Remove(x); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload