Martinicity: Tag unit testsMike Blaketag:www.martinicity.net,2005:TypoTypo2007-05-04T08:12:41+00:00Mike Blakeurn:uuid:ae1917e2-4a47-4fa2-be71-d57109445b462007-03-31T13:11:00+00:002007-05-04T08:12:41+00:00Testing a Non-Rails Application Using Rails<p>Rails developers working on enterprise software projects are suprised to discover the lack of automated tests in many mature web applications. As frustrating as this can be, A lack of automated tests is also a tremendous opportunity to </p>
<pre><code>1. <b>Learn your non-rails applications underlying database structure.</b>
2. <b>Demonstrate to a devlopment team the power of The Rails Framework.</b>
2. <b>Encourage automated testing.</b></code></pre>
<p>Rails may be the quickest path to automate some basic test of your non-rails applications data model. These steps will get you all set up to write your automated tests in Ruby.</p>
<p>1. <a href="#connect">Connect to Your Enterprise Database</a>
2. <a href="#conventions">Set DB Conventions</a>
3. <a href="#safety">Safety Net</a>
4. <a href="#schema">Duplicate the Schema</a>
4. <a href="#extract">Extract Development Data</a></p>
<h4 id='connect'>I. Connect to Your Enterprise Database from Rails.</h4>
<p>
<p>Download and install any os driver, ruby gems, and rails adapters needed to connect to your database:</p>
<blockquote>
<p><a href="http://wiki.rubyonrails.com/rails/pages/MySQL">MySQL</a>
<br><a href="http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0606dumbill/?ca=dgr-lnxw01DB24RubyonRails"><span class="caps">DB2</span></a>
<br><a href="http://www.oracle.com/technology/pub/articles/saternos-ror-faq.html">Oracle</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/HowToSetupSybaseAdapterOnRails">Sybase</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/HowtoConnectToMicrosoftSQLServer">SQLServer</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/PostgreSQL">PostgreSQL</a>
<br><a href="http://wiki.rubyonrails.org/rails/pages/Firebird">Firebird</a></p>
</blockquote>
<h4 id='conventions'>II. Set Your Existing Database Conventions </h4>
<p>
<p>, Recipie #16 can walk you through this very quickly.</p>
Identify any conventions used by your legacy database. Application wide conventions can be set in the <code>config/environment.rb</code> file.
They are specified by calling the appropriate class methods available on <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">ActiveRecord::Base</a>. The methods you need to call depend on how your database is configured:
<pre>
ActiveRecord::Base.table_name_prefix 'myapp_'
ActiveRecord::Base.table_name_suffix = '_def'
ActiveRecord::Base.sequence_name = 'dev_company'
ActiveRecord::Base.pluralize_table_names = false
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore # or :table_name
</pre>
<h4 id='safety'><span class="caps">III</span>. Safety Net</h4>
<p>
<p>Load the <a href="http://agilewebdevelopment.com/plugins/safety_net">SafetyNet Plugin</a> into your rails app to prevent from destroying your development database.</p>
<h4 id='schema'>IV. Duplicate Your Database Schema</h4>
<p>
<p>OK , with your dev and test development databases set correctly, let’s try duplicting the schema.</p>
<code>rake db:test:clone</code>
If this works right off the bat, you’re one of the lucky ones. Skip to <a href="#extract">Extracting Development Data</a>
<p>
<p>If you recieved errors from the clone command, do steps A through C .</p>
<p>A. You can now correct any errors you may have had by manually modifying schema.rb . See <a href="/articles/2007/03/29/rails-on-oracle">Oracle Errors</a> for some problems I had with the Oracle Database. Since schema.rb is generated, it’s a good idea to rename it when you modify it manually.</p>
B. The clone command above may have started loading data; you need may to purge the test db.
<code>
rake db:test:purge
</code>
<p>C. Tell rake to load your new schema file by passing the new name, relative to <span class="caps">RAILS</span>_ROOT in the <span class="caps">SCHEMA</span> environment variable:</p>
<code>
rake db:schema:load RAILS_ENV=test SCHEMA=db/oracle\_schema.rb
</code>
<h4 id='extract'>V. Extracting Development Data</h4>
<p>
<p>Copy the code from
Rails Recipe #42: <a href="http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake">extract_fixtures.rake</a> to your <span class="caps">RAILS</span>\_ROOT/lib/tasks directory.</p>
<blockquote>
<p><b><span class="caps">TIP</span>: If your develoment database has lots of data, Modify the <span class="caps">SQL</span> Select in this code to limit the amount of data you copy using the <span class="caps">SQL</span> limit statement:</b></p>
</blockquote>
<blockquote>
<code>sql = "SELECT * FROM %s limit = 100"</code>
</blockquote>
<blockquote>
<p>Or for a proprietary database, the equivalent command:</p>
</blockquote>
<blockquote>
<code>sql = "SELECT * FROM %s WHERE ROWNUM<=100"</code>
</blockquote>
Then run
<code>
rake extract_fixtures
</code>
<p>
<ol>
<li>You’ve made your testbed so you can lie in it.</li>
</ol>
<p>You now have and exact copy of your development database, and a collection of sample data in Yaml format, and are ready to begin writing some Unit Tests. Stay tuned for some examples. Rail on!
<span>
<script type="text/javascript">
digg_url = ‘http://martinicity.net/articles/2007/03/31/testing-a-non-rails-application-using-rails’;
digg_title = ‘Testing a Non-Rails Application Using Rails’;</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>
</span></p>Mike Blakeurn:uuid:d3a23de4-6d31-4890-aaf1-7e6e04e61f322007-02-14T21:07:00+00:002007-02-14T21:09:18+00:00The Unselfish Act of Writing Tests<p>
####ORUG
At this months <a href="http://www.orug.org">Orlando Ruby User’s Group</a> meeting, I did my <a href="http://www.apptrain.net/testing/img0.html">Testing on Rails</a> presentation. In it I touched on just some of the benefits of having an automated suite of Unit, Functional, and Integration tests.
<p>
<p>!<a href="http://www.apptrain.net/testing/img4.jpg">Benefits of Testing</a></p>
<p>
Later I thought of another major benefit. Tests help other people on the project, in addition to the author of the test.
<ol>
<li>Good Habits are Contagious</li>
</ol>
<p>Recently I had the opportunity to work on a project with <a href="http://blog.harrisreynolds.net/">Harris Reynolds</a>. Harris did something very important that sadly, you don’t see on every Rails Project. He wrote a unit test. Lot’s of us wrote tests , but this particular test stood out for two reasons. The first was when it was written.</p>
<ol>
<li>Writing Tests First</li>
</ol>
<p>Harris had found a bug in a model object that I had created. To notify me, he checked in an assertion that failed, then emailed me. This saved me time searching thought and remembering code I had written. I simply typed <code>rake test:recent</code> which runs all recently changed tests in a Rails application . The error took me right to the problem, and I immediately fixed the bug.</p>
<ol>
<li>Testing a Deployment</li>
</ol>
<p>The second reason I remember this test happened at least a month later. The application was being deployed to a new machine, and the test failed. The particular model object being tested pointed to a different database that the rest of the application, and it turned out that that database hadn’t been installed correctly on the new machine. Harris’s test told the problem straight away, and saved time once again.</p>
<p><b>Unit Testing: the gift that keeps on giving.</b></p>
<p>Thanks Harris!</p>