Martinicity: Tag rake Mike Blake tag:martinicity.net,2005:Typo Typo 2007-05-04T08:12:41+00:00 Mike Blake urn:uuid:ae1917e2-4a47-4fa2-be71-d57109445b46 2007-03-31T13:11:00+00:00 2007-05-04T08:12:41+00:00 Testing 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&#8217;s try duplicting the schema.</p> <code>rake db:test:clone</code> If this works right off the bat, you&#8217;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&#8217;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&lt;=100"</code> </blockquote> Then run <code> rake extract_fixtures </code> <p> <ol> <li>You&#8217;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 = &#8216;http://martinicity.net/articles/2007/03/31/testing-a-non-rails-application-using-rails&#8217;; digg_title = &#8216;Testing a Non-Rails Application Using Rails&#8217;;</script> <script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script> </span></p> Mike Blake urn:uuid:7d6f994d-0845-41c7-a8ea-09a9157c94db 2007-02-07T21:14:00+00:00 2007-02-16T03:30:13+00:00 Rails SafetyNet <h2><b>Curent Mood:</b> Bored with Deleting the Production Database</h2> <p> <p>I thought I was the only clown working on Rails projects and ignoring the <a href="http://dev.rubyonrails.org/svn/rails/applications/plugins/config/database.example.yml">warning in config/database.yml</a> and using rake to wipe out perfectly good databases. But I&#8217;ve seen it happen to others now, and enough&#8217;s enough. So I created the <h3>SafetyNet Plugin</h3>.</p> <p>You can install Safety Net in your app, by running</p> <code> ruby script/plugin install svn://rubyforge.org/var/svn/apptrain/trunk/vendor/plugins/safety_net </code> <p>That&#8217;s it. Now, If the <b>test</b> database points to the same database as <b>development</b> or <b>production</b> running rake will display the following message:</p> <p>!<a href="/images/safety_net.png">safety net</a></p> <pre> rake aborted! The name of your test database matches production or development. </pre> <h3>How it Works</h3> <p> <p>Purging the poor innocent database is prevented in two ways.</p> <p>1. By adding a prerequisite check to the rake script that normaly performs this task. 2. By modifying the fixtures method on ActiveRecord to avoid the same fate when running individual tests with the ruby command.</p>