Skip to content

Commit

Permalink
Wrote *a lot* of unit tests.
Browse files Browse the repository at this point in the history
modified:   Gemfile
modified:   Gemfile.lock
	-- added rspec gem explicitly to fix weird dependency issue

modified:   app/models/bookmark.rb
modified:   app/models/permission.rb
modified:   app/models/progress.rb
modified:   app/models/saved_search.rb
	-- various bug fixes to to testing

modified:   db/development_seeds.rb
	-- updated seeds so that they are valid

new file:   db/migrate/20130119095800_bookmark_change_offset_name.rb
modified:   db/schema.rb
	-- added migration for bookmark model to make naming more consistent with the rest of the app

modified:   db/seeds.rb
	-- added warning console message to make it blatently obvious when it runs

new file:   lib/modules/JSON_patch.rb
	-- added handy method for testing if JSON is valid

modified:   spec/controllers/analysis_items_controller_spec.rb
modified:   spec/controllers/analysis_jobs_controller_spec.rb
modified:   spec/controllers/analysis_scripts_controller_spec.rb
modified:   spec/controllers/audio_events_controller_spec.rb
modified:   spec/controllers/audio_recordings_controller_spec.rb
modified:   spec/controllers/bookmarks_controller_spec.rb
modified:   spec/controllers/permissions_controller_spec.rb
modified:   spec/controllers/photos_controller_spec.rb
modified:   spec/controllers/progresses_controller_spec.rb
modified:   spec/controllers/projects_controller_spec.rb
modified:   spec/controllers/saved_searches_controller_spec.rb
modified:   spec/controllers/sites_controller_spec.rb
modified:   spec/controllers/tags_controller_spec.rb
modified:   spec/controllers/users_controller_spec.rb
	-- added 5 default tests to every GET #index action test for every controller

new file:   spec/factories/bookmark_factory.rb
new file:   spec/factories/permission_factory.rb
new file:   spec/factories/photo_factory.rb
new file:   spec/factories/progress_factory.rb
	-- new factories to coincide with the completion of the rest of the model tests

modified:   spec/factories/project_factory.rb
	-- fixed urn generation

modified:   spec/factories/saved_search_factory.rb
	-- fixed bug with default factory

modified:   spec/models/authorization_spec.rb
modified:   spec/models/bookmark_spec.rb
modified:   spec/models/permission_spec.rb
modified:   spec/models/photo_spec.rb
modified:   spec/models/progress_spec.rb
	-- wrote and completed model tests for the above models

modified:   spec/models/project_spec.rb
	-- fixed urn validation

modified:   spec/models/saved_search_spec.rb
	-- finished spec / fixed specs

modified:   spec/spec_helper.rb
	-- moved the seeds.db runner into controller tests only (they are unecessary for model tests and slow them down)

new file:   spec/support/shared_api_examples.rb
	-- made a set of reuseable specs which where applied to all GET#index controller tests
  • Loading branch information
atruskie committed Jan 20, 2013
1 parent a183e61 commit 829d366
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app/models/bookmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ class Bookmark < ActiveRecord::Base
belongs_to :audio_recording

# attr
attr_accessible :name, :notes, :offset, :audio_recording_id
attr_accessible :name, :notes,
:offset_seconds, # offset since start of audio recording
:audio_recording_id

# userstamp
stampable
belongs_to :user, :class_name => 'User', :foreign_key => :creator_id

# validation
validates :offset, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validates :offset_seconds, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validates :audio_recording_id, :presence => true


Expand Down
2 changes: 1 addition & 1 deletion app/models/permission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Permission < ActiveRecord::Base

# custom validation methods
def anonymous_permission_can_only_be_read_or_none
return unless self.user_id.nil?
return unless self.user.nil?

return if self.reader? || self.none?

Expand Down
16 changes: 10 additions & 6 deletions app/models/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class Progress < ActiveRecord::Base
belongs_to :saved_search
belongs_to :audio_recording

# attr
# attr # JSON encoded array or offsets (in seconds)
attr_accessible :offset_list, # <- this is the actual data packet

# ↓ these are just keys ↓
:activity, :saved_search_id, :audio_recording_id,
:start_offset_seconds, :end_offset_seconds
Expand All @@ -19,18 +20,21 @@ class Progress < ActiveRecord::Base

# validation
validates_uniqueness_of :activity,
:scope => [:saved_search_id, :audio_recording_id,
:start_offset_seconds, :end_offset_seconds, :creator_id ]
case_sensitive: false,
scope: [:saved_search_id, :audio_recording_id,
:start_offset_seconds, :end_offset_seconds, :creator_id]

validates_presence_of :activity, :saved_search_id, :audio_recording_id,
validates_presence_of :offset_list,
:activity, :saved_search_id, :audio_recording_id,
:start_offset_seconds, :end_offset_seconds, :creator_id

validates :start_offset_seconds, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validates :end_offset_seconds, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validates :start_offset_seconds, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validates :end_offset_seconds, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validate :start_time_must_be_lte_end_time

# custom validation methods
def start_time_must_be_lte_end_time
return if start_offset_seconds.nil? || end_offset_seconds.nil?

if start_offset_seconds > end_offset_seconds then
errors.add(:start_time_seconds, " start offset must be lower than end offset")
Expand Down
17 changes: 15 additions & 2 deletions app/models/saved_search.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative '../../lib/modules/JSON_patch'

class SavedSearch < ActiveRecord::Base

# relations
Expand All @@ -16,8 +18,11 @@ class SavedSearch < ActiveRecord::Base

# validation
validates :search_object, :presence => true
validates_uniqueness_of :search_object,
:scope => [:creator_id, :name]
#validates_uniqueness_of :search_object, :scope => [:creator_id, :name]
validates_uniqueness_of :name, :scope => [:owner_id]

validate :json_format


# custom methods
def implicit_global?
Expand All @@ -35,4 +40,12 @@ def implicit_personal?
def explicit_personal?
!name.blank? && !owner_id.blank?
end

protected

def json_format

errors.add(:search_object, 'search_object not in json format') unless JSON.is_json?(search_object)
end

end
12 changes: 12 additions & 0 deletions lib/modules/JSON_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://stackoverflow.com/a/9361331/224512

module JSON
def self.is_json?(foo)
begin
return false unless foo.is_a?(String)
JSON.parse(foo).all?
rescue JSON::ParserError
false
end
end
end

0 comments on commit 829d366

Please sign in to comment.