Monday, February 11, 2013

Distinct dumb-mongodb-name

You are forbidden to use some characters in a mongodb database name, e.g., ?, !, #, @, %, ^, *, .,   or starts with a $.  Any other UTF8 characters are valid.

There are other characters that you can use but can cause difficulties, e.g., the dash "-" character.

If you want to find the distinct values of a certain field, e.g., "location_type", in a database that has a dash in its name, you can get what you want using a mongo query like this:

> db.getSiblingDB("dumb-mongodbname").runCommand ({ distinct: 'location_type', key: 'destinationType' })
"values" : [
"stats" : {
"n" : 999,
"nscanned" : 999,
"nscannedObjects" : 999,
"timems" : 3,
"cursor" : "BasicCursor"
"ok" : 1

If you try to reference the dumb-mongodb-name, with the dashes in the name, you'll get an error like this:

> db.dumb-mongodbname.location.find().skip(0).limit(30)
Mon Feb 11 10:13:51 ReferenceError: mongodbname is not defined (shell):1


Sponsor Ads

Friday, February 8, 2013

I Get Paid for Code That Works

I was recently enamored with using a test framework to test the application I  am developing.

I looked into using the Rack::Test framework to test my RESTful APIs.

It looked clean and minimal.  Looks are sometimes deceiving.

I created a very simple proof of concept.  

A service that returns the names of my kids.

The following scaffold command created the Rails app:

bundle exec rails generate scaffold Kid name:string

I entered  the names in the Rails Console:

Kid.create!(:name => 'Emma')
Kid.create!(:name => 'Kelly May')
Kid.create!(:name => 'Marty')

I tested the index action using my web browser

I created the Rack Test Test Case

require File.join(File.dirname(__FILE__), *%w[.. test_helper])

class KidsTest < ActiveSupport::TestCase
  include Rack::Test::Methods

  def app

  def test_get
    get "/kids"
    assert_equal "Hello world", last_response.body

# This would include the application's middleware,
# which you probably want as it includes sessions
# and cookies
class TestApplication < Test::Unit::TestCase
  include Rack::Test::Methods

  def app

  def test_get
    get "/kids"
    assert_equal "Hello world", last_response.body

I ran the test

/Volumes/HoneyBadger1TB/Users/lex/.rbenv/versions/1.9.3-p194/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -Itest /Volumes/HoneyBadger1TB/Users/lex/PROJECTS/racktest/test/rack/kids_test.rb
Testing started at 4:55 PM ...
Rack::File headers parameter replaces cache_control after Rack 1.5.

MiniTest framework was detected. It is a lightweight version of original Test::Unit framework.
RubyMine/IDEA Ruby plugin test runner requires 'minitest-reporters' (>= 0.5.0) for integration
with MiniTest framework (see
Or you can use full-featured Test::Unit framework version, provided by
'test-unit' gem, otherwise default console tests reporter will be used instead.

Run options: 

# Running tests:


Finished tests in 0.031349s, 31.8989 tests/s, 31.8989 assertions/s.

  1) Failure:
test_get(KidsTest) [/Volumes/HoneyBadger1TB/Users/lex/PROJECTS/racktest/test/rack/kids_test.rb:12]:
<"[{\"created_at\":\"2013-02-08T14:23:57Z\",\"id\":1,\"name\":\"Emma\",\"updated_at\":\"2013-02-08T14:24:17Z\"},{\"created_at\":\"2013-02-08T14:25:40Z\",\"id\":2,\"name\":\"Kelly May\",\"updated_at\":\"2013-02-08T14:25:40Z\"},{\"created_at\":\"2013-02-08T14:25:57Z\",\"id\":3,\"name\":\"Marty\",\"updated_at\":\"2013-02-08T14:25:57Z\"}]"> expected but was

1 tests, 1 assertions, 1 failures, 0 errors, 0 skips

Process finished with exit code 1


Perhaps I'm missing something here, but it looks like Rack Test is expecting me to mock out the data for its get command to retrieve the RESTful response.

I'm not interested in writing a lot of dummy code to test my real code.

There is great benefit to writing comprehensive and modular test code, e.g., code that tests the model layer (unit tests -- very valuable), code that tests APIs (api tests -- very valuable), but then when you start getting into functional, integration and regression testing, it's best to be selective.

With a team of developers, continuous integration testing is a must.

The more developers you have, the more comprehensive and modular your test suite should be.  As a developer, I don't want some other developer to be able to deliver code that breaks _my_ code.  I want the build to break and point at that other developer--not me--when their code change makes it look like _my_ code is broken.

So, with a team of developers, each developer is in a race to push their code that could possibly be affected by other developers _before_ the other developers have a chance to checkin their code.  The last guy to push their code must take time to perform the requisite code merge.

If you test too much or the tests are brittle, it'll be more of a cost than a benefit.

Tests are code that don't have test coverage.

All tests have costs.  Some tests have value.

Next Steps

Choosing the right tools for testing can save a lot of time.

For API testing, using simple rest-client to test in a test environment against real data makes a lot of sense to me.

Adding a sample_data method to return mock response, coded directly in the model, which can be exercised for time sensitive testing also makes a lot of sense to me.


Sponsor Ads

Tuesday, February 5, 2013

System Restore from Networked Time Capsule

The main trick to getting a system restore to work from a networked Time Capsule (TC) is to set the password for the sparsebundle (backup) file.

Open the Airport Utility

Select "Disk Password", as opposed to "Device Password" as the password type for your TC.

Chose a password that you will remember and update the TC.

Connect to the TC via the finder

Open the Finder  |  Go  |  Network

Choose your TC device from the list

Click "Connect as..." button

It will prompt with a dialog that says: Enter your password for the server <TC Name>.

Open Terminal Window

Go to /Volumes/TCDirectory

Change sparsebundle File Password

Run the following command to set the password for the sparsebundle file:

sudo hdiutil chpass /path/to/filename.sparsebundle

Restore from Time Machine Backup

Restart your Mac

Press command + R when you hear the chime.

This will bring up the Mac OSX Utilities dialog window

Choose Restore from Time Machine Backup

Choose your TC | Choose TC disk backup name 

It will ask for a password.

Enter the password you entered in the hdiutil command

Now, you will presented with a list of backups you can restore from.

It will take a long time (perhaps 4+ hours)  to perform the restore.


Sponsor Ads

Monday, February 4, 2013

My Work Clock and Functional Programming

Programming in Scala has made me a better Ruby programmer.  Thank you Scala!

Functional Iterators

I appreciate Functional Iterators much better now.

Given a list of items:

  • If you want to return a single value, after iterating through the list, use reduce.
  • If you want to return a subset of the values, use filter.
  • If you want to return a new list, where each item as likely been transformed, use map.

Example of Using Reduce

Below, is an example of iterating through my list of billable hours in a week to arrive at a single value -- then total number of hours I worked that week for that client.

I use a simple app, My Work Clock, on my phone to track my billable hours.

I simply "Punch In" when I start working and "Punch Out" when I'm done.

It produces a report that I can email to myself in CSV format, which I then open in Libre Office (like MS Excel).

I copy/paste the hours into a Ruby console and run in through a functional programming one-liner to determine my weekly billable hours:

~ $ irb
irb(main):001:0> s =<<TEXT
irb(main):002:0" 8.02
irb(main):003:0" 13.72
irb(main):004:0" 15.87
irb(main):005:0" 10.87
irb(main):006:0" 8.13
irb(main):007:0" TEXT
=> "8.02\n13.72\n15.87\n10.87\n8.13\n"

irb(main):008:0> s.split.reduce(0){|sum, value| sum.to_f + value.to_f}

=> 56.61

Example of Using a Filter

The select function takes two arguments:

  • The list:  ["8.02", "13.72", "15.87", "10.87", "8.13"]
  • Function to apply on each item value; Return only those greater than 10:  number.to_f > 10

# Daily work hours greater than 10 hours

["8.02", "13.72", "15.87", "10.87", "8.13"].select {|number| number.to_f > 10}

=> ["13.72", "15.87", "10.87"]

Example of Using a Map

The map function will return an Array composed of results from the block operation.

The map function takes two arguments:
  • The list of days:  days = %w{ 2013-01-18 2013-01-17 2013-01-16 2013-01-15 2013-01-14 }
  • Function to apply on each item value:  day_hours_hash[d]
Here's the hash:
day_hours_hash = {"2013-01-18" => "8.02", "2013-01-17" => "13.72", "2013-01-16" => "15.87", "2013-01-15" => "10.87","2013-01-14" =>"8.13"}

Return the number of hours worked for each day {|d| day_hours_hash[d]}

=> ["8.02", "13.72", "15.87", "10.87", "8.13"]


Sponsor Ads