Skip to content

Commit

Permalink
Created tests for SavedSearch date filter.
Browse files Browse the repository at this point in the history
modified:   app/models/audio_recording.rb
new file:   lib/external/diagram.png
modified:   lib/search.rb
modified:   spec/requests/REST_routes_tests.rb
new file:   spec/requests/saved_search_request_spec.rb
modified:   spec/support/analysis_script_helper.rb
  • Loading branch information
Mark Cottman-Fields committed Feb 8, 2013
1 parent 95901e9 commit cad778d
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 120 deletions.
70 changes: 53 additions & 17 deletions app/models/audio_recording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,31 @@ class AudioRecording < ActiveRecord::Base
validates_as_paranoid

#enums
AVAILABLE_STATUSES = [:new, :to_check, :ready, :corrupt, :ignore].map{ |item| item.to_s }
AVAILABLE_STATUSES = [:new, :to_check, :ready, :corrupt, :ignore].map { |item| item.to_s }
enumerize :status, in: AVAILABLE_STATUSES, predicates: true
validates :status, :inclusion => {in: AVAILABLE_STATUSES}, :presence => true

# validation
validates :uuid, :presence => true, :length => {:is => 36}, :uniqueness => { :case_sensitive => false }
validates :uuid, :presence => true, :length => {:is => 36}, :uniqueness => {:case_sensitive => false}
validates :uploader_id, :presence => true


validates :recorded_date, :presence => true, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date }

validates :recorded_date, :presence => true, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
validates :site, :presence => true
validates :duration_seconds, :presence => true, :numericality => {greater_than_or_equal_to: 0}

validates :sample_rate_hertz, :numericality => {only_integer: true, greater_than_or_equal_to: 0}

# the channels field encodes our special version of a bit flag. 0 (no bits flipped) represents
# a mix down option - we don't store mix downs (if we did they would be stored as single channel / mono (value = 1))
validates :channels, :presence => true, :numericality => { :only_integer => true, :greater_than => 0 }
validates :bit_rate_bps, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
validates :channels, :presence => true, :numericality => {:only_integer => true, :greater_than => 0}
validates :bit_rate_bps, :numericality => {:only_integer => true, :greater_than_or_equal_to => 0}
validates :media_type, :presence => true
validates :data_length_bytes, :presence => true, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
validates :data_length_bytes, :presence => true, :numericality => {:only_integer => true, :greater_than_or_equal_to => 0}

validates :file_hash, :presence => true



# uuid stuff
attr_protected :uuid
include UUIDHelper
Expand All @@ -72,30 +71,41 @@ def status=(new_status)
#scope :recordings_from_projects, lambda { |project_ids| joins(:site) }
#scope :filter_by_branch, lambda{|branch_id| includes(:branches).where(:branches => {:id => branch_id})

# these need ot be left outer joins. includes should do this, ut does not.
# these need to be left outer joins. includes should do this, but does not.
# use joins with the join in sql text :(
# http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-eager-loaded-associations
def self.recording_projects(project_ids)
includes(:site => :projects).where(:projects => { :id => project_ids})
includes(:site => :projects).where(:projects => {:id => project_ids})
end

def self.recording_sites(site_ids)
includes(:site).where(:sites => { :id => site_ids })
includes(:site).where(:sites => {:id => site_ids})
end

def self.recordings(recording_ids)
def self.recording_ids(recording_ids)
where(:id => recording_ids)
end

def self.recording_uuids(recording_ids)
where(:uuid => recording_ids)
end

# only one of these can be included.
def self.recording_within_date(start_date, end_date)
rel_query = scoped
if start_date.is_a?(Time)
rel_query = rel_query.where('recorded_date + duration_seconds >= :start_date', {:start_date => start_date})
if start_date.kind_of?(DateTime)
sqlite_calculation = "datetime(recorded_date, '+' || duration_seconds || ' seconds') >= :start_date"
formatted_start_date = start_date.utc.midnight.strftime('%Y-%m-%d %H:%M:%S')
rel_query = rel_query.where(sqlite_calculation, {:start_date => formatted_start_date})
else
raise ArgumentError, "Expected start_date to be a DateTime, given #{start_date.class} type."
end

if end_date.is_a?(Time)
rel_query = rel_query.where('recorded_date <= :end_date', {:end_date => end_date})
if end_date.kind_of?(DateTime)
formatted_end_date = end_date.utc.advance({days: 1}).midnight.strftime('%Y-%m-%d %H:%M:%S')
rel_query = rel_query.where('recorded_date < :end_date', {:end_date => formatted_end_date})
else
raise ArgumentError, "Expected end_date to be a DateTime, given #{start_date.class} type."
end

rel_query
Expand All @@ -112,7 +122,33 @@ def self.recording_tags(tags)
end

def self.recording_time_ranges(time_ranges)
scoped
raise ArgumentError, "Expected time ranges to be an array, got #{time_ranges.class} instead." unless time_ranges.kind_of?(Array)

rel_query = scoped

time_ranges.each do |range|
range_start = range[0]
range_end = range[1]

formatted_start_time = range_start.utc.strftime('%H:%M:%S')
formatted_end_time = range_end.utc.strftime('%H:%M:%S')

if range_start < range_end
# if start is less than end, the range is within one day
sqlite_calculation = "datetime(recorded_date, '+' || duration_seconds || ' seconds') >= :start_date"
rel_query.where(sqlite_calculation, {range_start: formatted_start_time, range_end: formatted_end_time})

elsif range_start > range_end
# if start is greater than end, the range goes over midnight (2 days)

else
# start and end are equal, ignore this range

end

end

rel_query
end

private
Expand Down
Binary file added lib/external/diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cad778d

Please sign in to comment.