Skip to content

chicken and egg

I can’t believe this still hapens. Unzip software that is zipped. I was trying to unzip a file off of the web to install on my palm and they had zipped the 15k zip prc file to save those few extra bytes on the web making it impossible to actually download and use the software without a full computer.

Ugh.

generator_missing plugin release

I just posted my initial cut of my generator_missing plugin. This plugin is meant to be a repository of additional generators to help workflow. The current ones are:

  • Library: Generates a non active record ruby object and a test class.
    • lib/library_name.rb
    • test/unit/library_name_test.rb
  • Module: Generates a basic module under lib and its tests.
    • lib/module_name.rb
    • test/unit/module_name_test.rb, with a mock class that mixes in the library
  • Base Generator: Generates a generator (mmm meta) with a template, usage and manifest.
    • lib/generators/generator_name/generator_name_generator.rb
    • lib/generators/generator_name/USAGE
    • lib/generators/generator_name/templates

The plugin is at: http://github.com/gaffo/generator_missing / git://github.com/gaffo/generator_missing.git
Lighthouse at http://gaffo.lighthouseapp.com/projects/21321-generator_missing/overview.

I am currently taking both suggestions and pull requests for generators that should be added.

RDocul.us launch

I was tired of not having up to date docs for the rails gems and plugins that I use, so I created RDocul.us. Bascially, anyone is free to submit any github based rails plugin or gem and it will be kept up to date on RDocul.us. Once it’s approved it will be automatically updated within four hours of any commit to github.

Currently there is a delay for as I am manually vetting and configuring the libraries. Unfortunately not everyone is creating the .documentation file or using the same rake task (rdoc, doc, docs) or doc directory (rdoc, doc, docs) so I have to give it a quick once over.

Please let check it out and let me know what you think.

testing named scopes

Named scopes are a nice feature that came out in rails 2.1, however, testing them is not very obvious.

Say we have a named scope in our member object which looks like this:

class Member < ActiveRecord::Base
  named_scope :active, {:conditions => {:status => Member::STATUS_ACTIVE}}
end

There are 2 things we need to do to test them.

First would be to make sure that the conditions are the same as we think they are. This one is open for debate a bit as it is somewhat tying the implementation directly to the test.

Second, we actually have to USE the scope. Just calling Member.active does not actually use the scope. This is because named scopes can be cascaded. If I had recent scope on a member that only showed me those that signed up recently, I could actually do Member.active.recent, and it would not execute against database, it would just nest the conditions.

So how do I use the scope? Use any collection method on them. I like size, but I’m completely open to suggestions. So below is how I am testing named scopes.

class MemberTest < ActiveRecord::TestCase
  context 'named scope active' do
    setup do
      @scoped_find = Member.active
    end

    should "have condition active" do
      expected_conditions  = {:conditions => ["member.status == #{Member::STATUS_ACTIVE}"]}
      assert_equal expected_conditions, @scoped_find.proxy_options
    end

    should "have results" do
      assert_not_nil(scoped.active.size)
    end
  end
end

The first test does part 1, the 2nd, part 2.

Mysql issue with rails and antivirus on windows


abstract_adapter.rb:150:in log': Mysql::Error: Can't create/write to file 'C:\MySQL5\tmp\#sql_190_0.MYI' (Errcode: 13)

I had been getting this issue quite a bit recently. The cause actually turned out to be a conflict between McAfee and MySQL. What was happening is that McAfee scans any file that is recently written to, especially those in tmp directories. McAfee reading the file causes the above issue to MySQL. The fix is 2 fold. First, if you have not already, move the location of the MySQL tmp file. You can do this by editing my.ini in your MySQL directory. For Example:

tmpdir="C:/Program Files/MySQL/tmp/"

You may also have to add an exception to your antivirus so that it will no longer scan this file. I had to do this because I was using corporate antivirus. You may have to get your Sysadmin to do this.

Here is a post on mysql’s forums describing this issue as well

testing protected and private methods in ruby

When I was looking for how to test protected an private methods in ruby on the net, I found many sites arguing whether you should, and several methods for doing so. I am of the opinion that if your method contains any logic at all, it should have a test. Some examples of what I consider logic:

Object:

class User
  validates_presence_of :first_name, :last_name, :company_id
  belongs_to :company
  def full_name
    "#{last_name}, #{first_name}"
  end
  def company_name
    company.name
  end
end

Test:

class UserTest < Test::Unit
  def test_full_name
    assert_equal("Gaffney, Mike", User.new(:first_name => "Mike", :last_name => "Gaffney"))
  end
  def test_company_name
    company = Company.new(:name => "CompanyName")
    user = User.new(:company => company)
    assert_equal("CompanyName", user.company_name)
  end
end

This may seem like overkill but when many people are looking at the code it greatly helps communicate intention to others.

Testing:

I also found several methods to test protected and private methods in ruby, so I will cover the pluses and minuses the ones I know of.

Lets say we have a User class and an unassociated comments class. Comments are only attributed to a poster’s full name. There is no direct ID link between comments and users.

class User
  validates_presence_of :first_name, :last_name
  def find_comments
    Comments.find(:all, :conditions => {:poster => full_name})
  end
  protected
  def full_name
    "#{last_name}, #{first_name}"
  end
end

Here are the approaches I’m going to use to test this:

  • Using a Mock to open access restrictions
  • Make single (protected/private) methods public on the tested class.
  • Make all (protected/private) methods public on the tested class.
  • Hybrid Mock Approach
  • Using instance_eval and send
  • send

Using Mocks:
Mocks should be pretty familiar to most developers. Basically you create a class or subclass that the test will use. In our case we are simply opening the access restrictions with a subclass. Our test class looks like this:

class MockUser < User
  def full_name
    super
  end
end
class UserTest < Test::Unit
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

Plusses:

  • Simple and familiar to most developers from many languages

Minuses:

  • Can’t test private methods (subclass doesn’t have access).
  • With complicated classes you end up with quite a few mocks for all of the different cases. Managing the mocks becomes tedious.

Make single methods public:
Next we will make use of the runtime nature of ruby to change a function from protected to public. This also works for private methods:

class UserTest < Test::Unit
  def test_full_name
    User.send(:public, :first_name)
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

or

class User
    public :first_name
end
class UserTest < Test::Unit
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

Plusses:

  • Lets us directly access the private or protected function.

Minuses:

  • This pollutes the default namespace for all of the other tests. Remember that tests can (and should be able to) run in random order. Take for example:
    1. Full Name is a public function that some other objects use.
    2. We make this function into a protected one and use this method to make it public for testing.
    3. If this new test runs before the tests for the objects using the old public full_name, full_name will still be public even though it is actually protected.
    4. The other tests do not fail but the code will fail during runtime.

Make all methods public:
To save us some time on a big class, we can make all private and protected methods public:

class User
  public *protected_methods.collect(&amp;amp;amp;:to_sym)
  public *private_methods.collect(&amp;amp;amp;:to_sym)
end
class UserTest 
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

or

class UserTest < Test::Unit
  def test_full_name
    User.send(:public, *MyClass.protected_instance_methods)  
    User.send(:public, *MyClass.private_instance_methods)
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

This has the same issues as above but can be done one to make large classes easy to test, especially using the first method.

Hybrid Approach:
This works like the mock example and the previous example combined:

class AllAccessUser
  public *protected_methods.collect(&amp;amp;amp:to_sym)
end
class UserTest < Test::Unit
  def test_full_name
    user = AllAccessUser.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.full_name)
  end
end

This is easy like the previous but does not pollute the namespace. However it cannot be used for private methods.

Using send
Now we will use the built in reflection method to call the protected/private method on the class. This makes use of the fact that protected and private in ruby aren’t really like protected and private in other languages.

class UserTest < Test::Unit
  def test_full_name
    user = User.new(:first_name => "Mike", :last_name => "Gaffney")
    assert_equal("Gaffney, Mike" user.send(:full_name))
  end
end

Plusses:

  • Doesn’t pollute the namespace.
  • All code is right in the test.
  • Works for protected and private.

Minuses:

  • Some people don’t like using send

Summary:
Testing protected and private methods should be done, and ruby makes it much easier than some other languages. My preferred technique is the final one of using send. It is slightly less readable but keeps everything contained in one location.

hiding production database info from source control with capistrano

Whether you are working on an open source app or an enterprise app, it is a good idea to keep the production database connection information out of source control. To do so, add the following to your deploy.rb file:

task :overwrite_database_yml_file do
  run "[ -f /var/rails/#{application}/database.yml ] &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; cp -f /var/rails/#{application}/database.yml #{latest_release}/config/ || echo 'database.yml was not overwritten'"
end
after "deploy:update_code", :overwrite_database_yml_file

And place a database.yml file containing only your production connection information in the same directory as current and shared (eg the deploy_to directory). In fact I typically take the production information out of the config/database.yml entirely.

When deploy runs, if it finds a database.yml file in the deploy_to directory, it will copy it into config before migrating or starting the application. You can use this task to copy other configuration files that you want to keep out of source control as well, but I think its a good idea to only have the database connect info and host names change between deployments.

new has_a block

Pretty simple post, but any ruby object can be post conifged in a line with somthing like:

User.new(:name => “tony”){|u| u.save}

which is great for active record tests.

Or in a bigger example:

@trip = Trip.new{|t| t.save!}
@invite1 = Invitation.new(:user => users(:aaron),
                                   :status => Invitation::STATUS_MAYBE){|i| i.save}
@invite1 = Invitation.new(:user => users(:aaron),
                                   :status => Invitation::STATUS_MAYBE){|i| i.save}

rcov and rails

rcov and Rails
I had the good fortune of attending Rails Conf recently on behalf of the consulting company I work for, Asynchrony Solutions. One of the topics at the great Refactotem tutorial (hosted by the guys from Relevance) was rcov, a code coverage tool for Ruby. One of the things I missed in, or was neglected from, the talk was how to actually run coverage tasks against Rails. So I thought I would share the code to make the test:coverage task that I found with some digging.

Add the following to the Rakefile in the root of your rails app:

require 'rcov/rcovtask'

namespace :test do
namespace :coverage do
desc "Delete aggregate coverage data."
task(:clean) { rm_f "coverage.data" }
end
desc 'Aggregate code coverage for unit, functional and integration tests'
  task :coverage => "test:coverage:clean"
    %w[unit functional integration].each do |target|
    namespace :coverage do
      Rcov::RcovTask.new(target) do |t|
      t.libs << "test"
      t.test_files = FileList["test/#{target}/*_test.rb"]
      t.output_dir = "test/coverage/#{target}"
      t.verbose = true
      t.rcov_opts << '--rails --aggregate coverage.data'
    end
  end
  task :coverage => "test:coverage:#{target}"
  end
end

You can then run rcov on your rails app by running:

rake test:coverage

After this completes, it will output index.html files in RAILS_ROOT/tests/coverage/[units, functional, integration]. There is no overall index file, as, if you read the rake task, it is running rcov separately for each of [units, functional, integration].

Rcov has a few things to work out like showing untested code in modules sometimes, but otherwise it gives you a good idea of sections and branches of your code that are not tested. It’s very easy to set up so why not give it a try. But remember, metrics can only help point out problem spots in your code, they are not a replacement for good coding techniques.

ruby on rails (2.0.1, 2.0.2) and oracle on FC8

Get the following rpm’s from oracle

  • oracle-instantclient-basic-10.2.0.3-1.i386.rpm
  • oracle-instantclient-devel-10.2.0.3-1.i386.rpm

(Note: this should work fine with the newer versions as well)

As root

cd PATH_TO_RPMS
rpm -ivh oracle-instantclient-basic-10.2.0.3-1.i386.rpm oracle-instantclient-devel-10.2.0.3-1.i386.rpm
echo /usr/lib/oracle/10.2.0.3/client/lib/ > /etc/ld.so.conf.d/oracle-instant-client-i386.conf

This last step, the echo, is a bit of magic. The conf files in /etc/ld.so.conf.d are run before each command is invoked to set your library path. The enviroment variable $LD_LIBRARY_PATH overrides these. You won’t see the contents of these files in your $LD_LIBRARY_PATH, but the application will. If your distribution doesn’t have an /etc/ld.so.conf.d directory, you can just add /usr/lib/oracle/10.2.0.3/client/lib to your LD_LIBRARY_PATH inthe /etc/profile, ~/.bash_profile, or any of the other more common places.

Now you need to find the latest ruby-oci8, check http://ar.rubyonrails.com/ and put it in /usr/local/src/

wget http://rubyforge.org/frs/download.php/36134/ruby-oci8-1.0.1.tar.gz
tar -xzvf ruby-oci8-1.0.1.tar.gz
cd ruby-oci8-1.0.1
ruby setup.rb config
make
make install

The previous versions of oci8 and ora instant clients required that you make several symlinks, and was a pain to set up. With the current oci8 and the instant clients on your LD_LIBRARY_PATH, the compile and install should work just fine.

With rails 2.1 out, the activerecord-oracle-adapter is no longer hosted on gems.rubyonrails.org. You can see this with a “gem list -r –source http://gems.rubyonrails.org”. So you have to do the ugly method of directly grabbing the adapter yourself. It is very easy though:

wget -P /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/  http://svn.rubyonrails.org/rails/adapters/oracle/lib/active_record/connection_adapters/oracle_adapter.rb

That’s it. Just remember to make sure that you grab the right versions of everything.