test

Report 3 Downloads 137 Views
Relational Databases and Web Integration Dr. Carl Pulley [email protected]

Monday, 17 August 2009

Revision: development migration views model controller scaffold script/generate

controller model migration

Monday, 17 August 2009

+ fixtures + unit tests + functional tests

Revision: development SQL Data Definitions create tables drop tables insert into tables migration db:migrate rake

db:migrate:drop db:fixtures:load

Monday, 17 August 2009

Revision: test test rake

test:unit

test/* test/unit/*_unit.rb

test:functional

test/functional/*_controller_test.rb

Test fixtures

test/fixtures/*.yml

Monday, 17 August 2009

Example Tag

Blog

title 1 * * value created_at entry blogs_tags

Feedback

blog_id

created_at * comment 1

author_id

1

Author name email

Monday, 17 August 2009

author_id

Basic Implementation rails blog .. cd blog .. script/generate .. script/generate .. script/generate .. script/generate .. script/generate ..

Monday, 17 August 2009

model blog title:string created_at:datetime entry:text author_id:integer model feedback created_at:datetime comment:text author_id:integer model author name:string email:string model tag value:string migration blogs_tags

Basic Model Edits class Blog < ActiveRecord::Base has_and_belongs_to_many :tags has_many :feedbacks, :dependent => :destroy belongs_to :author end

class Feedback < ActiveRecord::Base belongs_to :blog belongs_to :author end

Monday, 17 August 2009

class Tag < ActiveRecord::Base has_and_belongs_to_many :blogs end

class Author < ActiveRecord::Base end

Migration Edits class CreateBlogs < ActiveRecord::Migration def self.up create_table :blogs do |t| t.string :title t.datetime :created_at t.text :entry t.integer :author_id, :null => false class CreateFeedbacks < ActiveRecord::Migration t.timestamps def self.up create_table :feedbacks do |t| end end t.datetime :created_at t.text :comment def self.down t.integer :blog_id, :null => false t.integer :author_id, :null => false drop_table :blogs end t.timestamps end end end def self.down drop_table :feedbacks end end Monday, 17 August 2009

Migration Edits class BlogsTags < ActiveRecord::Migration def self.up create_table :blogs_tags, :id => false do |t| t.integer :blog_id t.integer :tag_id end end def self.down drop_table :blogs_tags end end

Monday, 17 August 2009

Validations class Blog < ActiveRecord::Base has_and_belongs_to_many :tags has_many :feedbacks, :dependent => :destroy belongs_to :author validates_presence_of :title validates_presence_of :entry validates_associated :author end class Feedback < ActiveRecord::Base belongs_to :blog belongs_to :author validates_presence_of :comment validates_associated :blog validates_associated :author end

Monday, 17 August 2009

class Tag < ActiveRecord::Base has_and_belongs_to_many :blogs validates_presence_of :value validates_uniqueness_of :value validates_format_of :value, :with => /^[a-z]+$/ end

class Author < ActiveRecord::Base validates_presence_of :name validates_presence_of :email validates_uniqueness_of :email validates_format_of :email, :with => /^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\.\-_]+$/ end

Tag Filtering class Blog < ActiveRecord::Base has_and_belongs_to_many :tags has_many :feedbacks, :dependent => :destroy belongs_to :author validates_presence_of :title validates_presence_of :entry named_scope :tagged, lambda { |tg| {:joins => :tags, :conditions => ['value = ?', tg]} } end

Monday, 17 August 2009

Fixtures: blog

cis2360-feedback: title: CIS2360 Feedback Wanted created_at: 2008-11-30 12:00:55 entry: I would be grateful if you could reply to this blog with course feedback please. Many thanks, Carl. author: carl tags: fixture-problem: title: Problem with using fixtures created_at: 2008-11-30 12:00:55 entry: | Any references that I could use to understand test fixtures? Thanks, Random. author: u1234567 tags: testing, fixtures named-scope-question: title: Some help required with named scopes created_at: 2008-11-30 12:00:55 entry: Named scopes confuse me, any pointers appreciated! author: u0000000 tags: rails Monday, 17 August 2009

Fixtures: feedback feedback1: created_at: 2008-11-30 12:01:24 comment: Ramble, ramble, ramble,... blog: cis2360-feedback author: u1234567 feedback2: created_at: 2008-11-30 12:01:24 comment: Blar, blar, blar,... blog: cis2360-feedback author: u7654321 feedback3: created_at: 2008-11-30 12:01:24 comment: Wibble, wibble, wibble,... blog: cis2360-feedback author: u0000000 reply: created_at: 2008-11-30 12:00:55 comment: Try looking at Railscast #81. Hope that helps, Carl. blog: fixture-problem author: carl Monday, 17 August 2009

Fixtures: author and tag carl: name: Carl Pulley email: [email protected] u1234567: name: Random Student email: [email protected] u7654321: name: Another Student email: [email protected] u0000000: name: Yet Another Student email: [email protected]

Monday, 17 August 2009

testing: value: testing fixtures: value: fixtures rails: value: rails

What about testing? With test-driven development (TDD): no application code should be written unless a test exists to shows a deficiency wrt our requirements specification We have obviously developed our Rails application the wrong way!! Next week we’ll look at how to drive our code development using testing

Monday, 17 August 2009

References Some useful Railscasts (http://railscasts.com) #81 - test fixtures #108 - named scope #126 - generating random data for databases #47 - many-to-many relationships #2 - dynamic find methods

Monday, 17 August 2009