Permalink
Browse files

initial copy

  • Loading branch information...
Ginny Hendry
Ginny Hendry committed Mar 18, 2010
0 parents commit 420faaf7c0e341c16ec66e2b837825cab184ea38
@@ -0,0 +1,20 @@
+Copyright (c) 2009 EdgeCase
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,149 @@
+=MongoDB Ruby Driver Koans
+
+Written for Chicago Ruby by Ginny Hendry and Nola Stowe
+
+Based on http://github.com/edgecase/ruby_koans
+
+Requires: Ruby 1.8.x or later
+ Rake (any version)
+ MongoDB
+ gem install mongo
+
+Recommended: gem install mongo_ext
+
+
+
+
+=EdgeCase Ruby Koans, Original Readme
+
+The Ruby Koans walk you along the path to enlightenment in order to learn Ruby.
+The goal is to learn the Ruby language, syntax, structure, and some common
+functions and libraries. We also teach you culture. Testing is not just something we
+pay lip service to, but something we live. It is essential in your quest to learn
+and do great things in the language.
+
+== The Structure
+
+The koans are broken out into areas by file, hashes are covered in about_hashes.rb,
+modules are introduced in about_modules.rb, etc. They are presented in order in the
+path_to_enlightenment.rb file.
+
+Each koan builds up your knowledge of Ruby and builds upon itself. It will stop at
+the first place you need to correct.
+
+Some koans simply need to have the correct answer substituted for an incorrect one.
+Some, however, require you to supply your own answer. If you see the method +__+ (a
+double underscore) listed, it is a hint to you to supply your own code in order to
+make it work correctly.
+
+== Installing Ruby
+
+If you do not have Ruby setup, please visit http://ruby-lang.org/en/downloads/ for
+operating specific instructions. In order to run this you need ruby and rake
+installed. To check the installations simply type:
+
+*nix platforms from any terminal window:
+
+ [~] $ ruby --version
+ [~] $ rake --version
+
+Windows from the command prompt (cmd.exe)
+
+ c:\ruby --version
+ c:\rake --version
+
+Any response for Ruby with a version number greater than 1.8 is fine (should be
+around 1.8.6 or more). Any version of rake will do.
+
+== The Path To Enlightenment
+
+You can run the tests through rake or by calling the file itself (rake is the
+recommended way to run them as we might build more functionality into this task).
+
+*nix platforms, from the koans directory
+
+ [ruby_koans] $ rake # runs the default target :walk_the_path
+ [ruby_koans] $ ruby path_to_enlightenment.rb # simply call the file directly
+
+Windows is the same thing
+
+ c:\ruby_koans\rake # runs the default target :walk_the_path
+ c:\ruby_koans\ruby path_to_enlightenment.rb # simply call the file directly
+
+=== Red, Green, Refactor
+
+In test-driven development the mantra has always been, red, green, refactor. Write a
+failing test and run it (red), make the test pass (green), then refactor it (that is
+look at the code and see if you can make it any better. In this case you will need
+to run the koan and see it fail (red), make the test pass (green), then take a
+moment and reflect upon the test to see what it is teaching you and improve the
+code to better communicate its intent (refactor).
+
+The very first time you run it you will see the following output:
+
+ [ ruby_koans ] $ rake
+ (in /Users/person/dev/ruby_koans)
+ cd koans
+
+ Thinking AboutAsserts
+ test_assert_truth has damaged your karma.
+
+ You have not yet reached enlightenment ...
+ <false> is not true.
+
+ Please meditate on the following code:
+ ./about_basics.rb:10:in `test_assert_truth'
+ path_to_enlightenment.rb:27
+
+ mountains are merely mountains
+
+You have come to your first stage. If you notice it is telling you where to look for
+the first solution:
+
+ Please meditate on the following code:
+ ./about_basics.rb:10:in `test_assert_truth'
+ path_to_enlightenment.rb:27
+
+We then open up the about_basics.rb file and look at the first test:
+
+ # We shall contemplate truth by testing reality, via asserts.
+ def test_assert_truth
+ assert false # This should be true
+ end
+
+We then change the +false+ to +true+ and run the test again. After you are
+done, think about what you are learning. In this case, ignore everything except
+the method name (+test_assert_truth+) and the parts inside the method (everything
+before the +end+).
+
+In this case the goal is for you to see that if you pass a value to the +assert+
+method, it will either ensure it is +true+ and continue on, or fail if in fact
+the statement is +false+.
+
+== Inspiration
+
+A special thanks to Mike Clark and Ara Howard for inspiring this project. Mike Clark
+wrote an excellent blog post about learning Ruby through unit testing. This sparked
+an idea that has taken a bit to solidify, that of bringing new rubyists into the
+community through testing. Ara Howard then gave us the idea for the Koans in his
+ruby quiz entry an Meta Koans (a must for any rubyist wanting to improve their skills).
+
+Mike Clark's post :: http://www.clarkware.com/cgi/blosxom/2005/03/18
+Meta Koans :: http://rubyquiz.com/quiz67.html
+
+== Other Resources
+
+The Ruby Language :: http://ruby-lang.org
+Try Ruby in your browser :: http://tryruby.sophrinix.com
+
+Dave Thomas' introduction to Ruby Programming Ruby (the Pick Axe) :: http://pragprog.com/titles/ruby/programming-ruby
+
+Brian Marick's fantastic guide for beginners Everyday Scripting with Ruby :: http://pragprog.com/titles/bmsft/everyday-scripting-with-ruby
+
+= Other stuff
+
+Author:: Jim Weirich <jim@weirichhouse.org>
+Author:: Joe O'Brien <joe@edgecase.com>
+Requires:: Ruby 1.8.x or later and Rake (any version)
+
+
@@ -0,0 +1,14 @@
+require 'rubygems'
+require 'rake/rdoctask'
+
+task :default => :walk_the_path
+
+task :walk_the_path do
+ cd 'koans'
+ ruby 'path_to_enlightenment.rb'
+end
+
+Rake::RDocTask.new do |rd|
+ rd.main = "README.rdoc"
+ rd.rdoc_files.include("README.rdoc", "koans/*.rb")
+end
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+# -*- ruby -*-
+
+require 'edgecase'
+
+class AboutAsserts < EdgeCase::Koan
+
+ # We shall contemplate truth by testing reality, via asserts.
+ def test_assert_truth
+ assert true # This should be true
+ end
+
+ # Enlightenment may be more easily achieved with appropriate
+ # messages.
+ def test_assert_with_message
+ assert true, "This should be true -- Please fix this"
+ end
+
+ # To understand reality, we must compare our expectations against
+ # reality.
+ def test_assert_equality
+ expected_value = 2
+ actual_value = 1 + 1
+
+ assert expected_value == actual_value
+ end
+
+ # Some ways of asserting equality are better than others.
+ def test_a_better_way_of_asserting_equality
+ expected_value = 2
+ actual_value = 1 + 1
+
+ assert_equal expected_value, actual_value
+ end
+
+ # Sometimes we will ask you to fill in the values
+ def test_fill_in_values
+ assert_equal 2, 1 + 1
+ end
+end
@@ -0,0 +1,76 @@
+require 'rubygems'
+require 'mongo'
+require 'edgecase'
+
+class AboutCRUD < EdgeCase::Koan
+ include Mongo
+
+ def setup
+ @mongo = Connection.new
+ @db = @mongo.db('hack0318')
+ @col = @db["stuff"]
+ end
+
+ def teardown
+ @db.collections.each do |collection|
+ @db.drop_collection(collection.name)
+ end
+ end
+
+ def test_create_empty_collection
+ empty_coll = @db.create_collection("empty")
+ assert_equal empty_coll.size, 0, "New collection is not empty"
+ assert_instance_of Mongo::Collection, empty_coll, "empty_coll is not a Mongo Collection"
+ end
+
+ def test_create_and_insert_document
+ doc = {:test => "hi there"}
+ @col.insert(doc)
+ assert_equal 1, @col.size, "Collection size is wrong"
+ assert_equal doc[:test], @col.find_one['test'], "Document in collection is wrong"
+ end
+
+ def test_fetch_document
+ doc = {:test => "hi there"}
+ @col.insert(doc)
+ assert_equal doc[:test], @col.find_one['test'], "Document in collection is wrong"
+ end
+
+ def test_replace_document
+ doc1 = {:category => "sandwich"}
+ @col.insert(doc1)
+ doc2 = @col.find_one
+ doc2["type"] = "peanut butter"
+ @col.save(doc2)
+ assert_equal 1, @col.size, "Collection size is wrong"
+ assert_not_equal doc1, @col.find_one, "Document not updated"
+ assert_equal doc2, @col.find_one, "Document not updated correctly"
+ end
+
+ def test_update_document
+ doc1 = {:category => "sandwich", :type =>"peanut butter"}
+ @col.insert(doc1)
+ @col.update({'category' => 'sandwich'}, {'$set' => {:type => "tuna fish"}})
+ assert_not_equal doc1, @col.find_one, "Document not updated"
+ assert_equal "tuna fish", @col.find_one['type'], "Document not updated correctly"
+ end
+
+ def test_delete_document
+ doc1 = {:category => "sandwich", :type =>"peanut butter"}
+ @col.insert(doc1)
+ doc2 = {:category => "soup", :type =>"minestrone"}
+ @col.insert(doc2)
+ assert_equal 2, @col.size, "Collection size is wrong"
+ @col.remove({'category' => "sandwich"})
+ assert_equal 1, @col.size, "Document not deleted"
+ end
+
+ def test_hash_key
+ doc1 = {:category => "sandwich", :type =>"peanut butter"}
+ @col.insert(doc1)
+ assert_equal doc1[:type], @col.find_one['type']
+ assert_not_nil doc1[:type]
+ assert_nil @col.find_one[:type]
+ #mongo driver only allows symbols as hash keys in some commands
+ end
+end
@@ -0,0 +1,76 @@
+require 'rubygems'
+require 'mongo'
+require 'edgecase'
+require 'date'
+
+class AboutDataTypes < EdgeCase::Koan
+ include Mongo
+
+ def setup
+ @mongo = Connection.new
+ @db = @mongo.db('hack0318')
+ @col = @db["stuff"]
+ @col.remove
+ end
+
+ def teardown
+ @db.collections.each do |collection|
+ @db.drop_collection(collection.name)
+ end
+ end
+
+ def test_int
+ @col.insert({:value => 123})
+ assert_instance_of Fixnum, @col.find_one['value']
+ end
+ def test_float
+ @col.insert({:value => 123.4})
+ assert_instance_of Float, @col.find_one['value']
+ end
+ def test_string
+ @col.insert({:value => 'abc'})
+ assert_instance_of String, @col.find_one['value']
+ end
+ def test_time
+ @col.insert({:value => Time.new})
+ assert_instance_of Time, @col.find_one['value']
+ end
+ def test_date
+ assert_raises(InvalidDocument) { @col.insert({:value => Date.new}) }
+ end
+ def test_datetime
+ assert_raises(InvalidDocument) { @col.insert({:value => DateTime.new}) }
+ end
+ def test_boolean_false
+ @col.insert({:value => false})
+ assert_instance_of FalseClass, @col.find_one['value']
+ end
+ def test_boolean_true
+ @col.insert({:value => true})
+ assert_instance_of TrueClass, @col.find_one['value']
+ end
+ def test_nil
+ @col.insert({:value => nil})
+ assert_instance_of NilClass, @col.find_one['value']
+ end
+ def test_not_attribute
+ @col.insert({:value => true})
+ assert_instance_of NilClass, @col.find_one['xyz']
+ end
+ def test_array
+ @col.insert({:value => [1,2]})
+ assert_instance_of Array, @col.find_one['value']
+ end
+ def test_id
+ @col.insert({:value => 123})
+ assert_instance_of ObjectID, @col.find_one['_id']
+ end
+ def test_regex
+ @col.insert({:value => /^123$/i})
+ assert_equal "(?i-mx:^123$)", @col.find_one['value'].to_s
+ end
+
+ def test_the_rest
+ assert "also data types binary, cstr, code, object"
+ end
+end
Oops, something went wrong.

0 comments on commit 420faaf

Please sign in to comment.