From 3419a559e1796a6738d56ca315075e5c7bd43df4 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Wed, 3 Oct 2012 20:38:39 +0100 Subject: [PATCH] Require a system name for crops. --- ...003190731_require_system_name_for_crops.rb | 15 +++++++++++ db/schema.rb | 6 +++-- spec/controllers/crops_controller_spec.rb | 2 +- spec/models/crop_spec.rb | 26 ++++++++++++++++++- spec/{model => models}/user_spec.rb | 0 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20121003190731_require_system_name_for_crops.rb rename spec/{model => models}/user_spec.rb (100%) diff --git a/db/migrate/20121003190731_require_system_name_for_crops.rb b/db/migrate/20121003190731_require_system_name_for_crops.rb new file mode 100644 index 000000000..47a46ec26 --- /dev/null +++ b/db/migrate/20121003190731_require_system_name_for_crops.rb @@ -0,0 +1,15 @@ +class RequireSystemNameForCrops < ActiveRecord::Migration + def up + change_table :crops do |t| + t.index :system_name + t.change :system_name, :string, :null => false + end + end + + def down + change_table :crops do |t| + t.change :system_name, :string, :null => true + t.remove_index :system_name + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 274ee70f6..ec04d917e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,15 +11,17 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20121001212604) do +ActiveRecord::Schema.define(:version => 20121003190731) do create_table "crops", :force => true do |t| - t.string "system_name" + t.string "system_name", :null => false t.string "en_wikipedia_url" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end + add_index "crops", ["system_name"], :name => "index_crops_on_system_name" + create_table "users", :force => true do |t| t.string "email", :default => "", :null => false t.string "encrypted_password", :default => "", :null => false diff --git a/spec/controllers/crops_controller_spec.rb b/spec/controllers/crops_controller_spec.rb index 7ea1db910..05630c3f1 100644 --- a/spec/controllers/crops_controller_spec.rb +++ b/spec/controllers/crops_controller_spec.rb @@ -24,7 +24,7 @@ describe CropsController do # Crop. As you add validations to Crop, be sure to # update the return value of this method accordingly. def valid_attributes - {} + { :system_name => "Tomato" } end # This should return the minimal set of values that should be in the session diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 662efde07..aa7f4a1b0 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -1,5 +1,29 @@ require 'spec_helper' describe Crop do - pending "add some examples to (or delete) #{__FILE__}" + context 'all fields present' do + + before(:each) do + @crop = Crop.new + @crop.system_name = "Tomato" + @crop.en_wikipedia_url = "http://en.wikipedia.org/wiki/Tomato" + end + + it 'should save a basic crop' do + @crop.save.should be_true + end + + it 'should be fetchable from the database' do + @crop.save + @crop2 = Crop.find_by_system_name('Tomato') + @crop2.en_wikipedia_url.should == "http://en.wikipedia.org/wiki/Tomato" + end + end + + context 'invalid data' do + it 'should not save a crop without a system name' do + @crop = Crop.new + expect { @crop.save }.to raise_error ActiveRecord::StatementInvalid + end + end end diff --git a/spec/model/user_spec.rb b/spec/models/user_spec.rb similarity index 100% rename from spec/model/user_spec.rb rename to spec/models/user_spec.rb