Skip to content

Commit

Permalink
update started_at and finished_at to be nil if started_at or finished…
Browse files Browse the repository at this point in the history
…_at string is unparseable by Time (#79)
  • Loading branch information
yuenmichelle1 authored Nov 25, 2024
1 parent 131b04e commit 94ad8e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/stream_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def self.user_group_ids?(event_data)

def self.started_at(event_data)
started_at = event_data.fetch('metadata')&.fetch('started_at', nil)
Time.parse(started_at) if started_at
Time.parse(started_at) rescue nil if started_at
end

def self.finished_at(event_data)
finished_at = event_data.fetch('metadata')&.fetch('finished_at', nil)
Time.parse(finished_at) if finished_at
Time.parse(finished_at) rescue nil if finished_at
end

def self.session_time(event_data)
Expand Down
11 changes: 11 additions & 0 deletions spec/models/stream_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,22 @@
expect(StreamEvents.started_at(event_data)).to be_nil
end

it 'returns started_at to nil if started_at cannot be parsed' do
# set started_at to some unparseable time string
event_metadata['started_at'] = '-1 OR 2+742-742-1=0+0+0+1 --'
expect(StreamEvents.started_at(event_data)).to be_nil
end

it 'returns finished_at to nil if no finished_at in metadata' do
event_metadata.delete('finished_at')
expect(StreamEvents.finished_at(event_data)).to be_nil
end

it 'returns finished_at to nil if started_at cannot be parsed' do
event_metadata['finished_at'] = '-1 OR 2+742-742-1=0+0+0+1 --'
expect(StreamEvents.finished_at(event_data)).to be_nil
end

context 'user_group_ids?' do
it 'returns true if classification done by user that belongs to multiple user groups' do
expect(StreamEvents.user_group_ids?(event_data)).to be true
Expand Down

0 comments on commit 94ad8e4

Please sign in to comment.