Skip to content

Commit

Permalink
Merge pull request #938 from DaanVanVugt/feature/set_default_times_at…
Browse files Browse the repository at this point in the history
…_save

always run set_default_times
  • Loading branch information
fbacall authored Aug 29, 2024
2 parents ae15d3f + 11d7381 commit 227e02b
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 30 deletions.
9 changes: 5 additions & 4 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Event < ApplicationRecord

before_validation :fix_keywords, on: :create, if: :scraper_record
before_validation :presence_default
before_save :check_country_name # :set_default_times
before_save :check_country_name
before_save :set_default_times
before_save :geocoding_cache_lookup, if: :address_will_change?
after_save :enqueue_geocoding_worker, if: :address_changed?

Expand Down Expand Up @@ -265,9 +266,9 @@ def set_default_times

self.start = start + 9.hours if start.hour == 0 # hour set to 0 if not otherwise defined...

return if self.end

if online?
if self.end
self.end = self.end + 17.hours if self.end.hour == 0 # hour set to 0 if not otherwise defined...
elsif online?
self.end = start + 1.hour
else
diff = 17 - start.hour
Expand Down
2 changes: 1 addition & 1 deletion test/models/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class EventTest < ActiveSupport::TestCase
assert_nil e.start
e.start = '2016-11-22'
e.save
assert_equal 0, e.start.hour
assert_equal 9, e.start.hour
end

test 'lower precedence content provider does not overwrite' do
Expand Down
9 changes: 6 additions & 3 deletions test/unit/ingestors/bioschemas_ingestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class BioschemasIngestorTest < ActiveSupport::TestCase
assert sample.persisted?
assert_equal 'https://uppsala.instructure.com/courses/75565', sample.url
assert_includes sample.description, 'This course will give an introduction to the concept of Neural Networks'
assert_equal '2023-03-20T00:00:00Z', sample.start.utc.iso8601
assert_equal '2023-03-24T00:00:00Z', sample.end.utc.iso8601
assert_equal '2023-03-20T09:00:00Z', sample.start.utc.iso8601
assert_equal '2023-03-24T17:00:00Z', sample.end.utc.iso8601
assert_equal 'SciLifeLab Uppsala - Navet, Husargatan 3', sample.venue
assert_equal 'Uppsala', sample.city
assert_equal 'Sweden', sample.country
Expand Down Expand Up @@ -157,7 +157,10 @@ class BioschemasIngestorTest < ActiveSupport::TestCase

test 'filters unrecognized fields when scraping' do
mock_bioschemas('https://website.org/courseinstances.json', 'sib_course.json')
@ingestor.stub(:convert_params, -> (p) { p[:blabla_123] = 'woowoo'; p }) do
@ingestor.stub(:convert_params, lambda { |p|
p[:blabla_123] = 'woowoo'
p
}) do
@ingestor.read('https://website.org/courseinstances.json')
assert_difference('Event.count', 2) do
@ingestor.write(@user, @content_provider)
Expand Down
8 changes: 4 additions & 4 deletions test/unit/ingestors/carpentries_ingestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CarpentriesIngestorTest < ActiveSupport::TestCase
# run task
assert_difference 'Event.count', 2 do
freeze_time(2024) do
VCR.use_cassette("ingestors/carpentries_swc") do
VCR.use_cassette('ingestors/carpentries_swc') do
ingestor.read(source.url)
ingestor.write(@user, @content_provider)
end
Expand All @@ -40,12 +40,12 @@ class CarpentriesIngestorTest < ActiveSupport::TestCase
event = Event.where(url: 'https://dexwel.github.io/2024-08-12-NiTheCS-Online').first
assert event
assert_equal 'Carpentries Mix & Match', event.title
assert_equal -26.19162, event.latitude.to_f.round(5)
assert_equal(-26.19162, event.latitude.to_f.round(5))
assert_equal 28.03107, event.longitude.to_f.round(5)
assert_equal 'South Africa', event.country
assert_equal 'The National Institute for Theoretical and Computational Sciences - NITheCS', event.venue
assert_equal Time.zone.parse('12 Aug 2024 00:00:00.000000000 UTC +00:00'), event.start
assert_equal Time.zone.parse('16 Aug 2024 00:00:00.000000000 UTC +00:00'), event.end
assert_equal Time.zone.parse('12 Aug 2024 09:00:00.000000000 UTC +00:00'), event.start
assert_equal Time.zone.parse('16 Aug 2024 17:00:00.000000000 UTC +00:00'), event.end
assert_equal ['National Institute for Theoretical and Computational Sciences'], event.host_institutions
assert event.online?
end
Expand Down
8 changes: 4 additions & 4 deletions test/unit/ingestors/dans_ingestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DansIngestorTest < ActiveSupport::TestCase
# run task
assert_difference 'Event.count', 6 do
freeze_time(2019) do
VCR.use_cassette("ingestors/dans") do
VCR.use_cassette('ingestors/dans') do
ingestor.read(source.url)
ingestor.write(@user, @content_provider)
end
Expand All @@ -51,8 +51,8 @@ class DansIngestorTest < ActiveSupport::TestCase
# check other fields
assert_equal 'DANS', event.source
assert_equal 'Amsterdam', event.timezone
assert_equal ["Social Sciences and Humanities", "Training &amp; Outreach", "Consultancy"], event.keywords
assert_equal Time.zone.parse('Mon, 13 Feb 2023 00:00:00.000000000 UTC +00:00'), event.start
assert_nil event.end
assert_equal ['Social Sciences and Humanities', 'Training &amp; Outreach', 'Consultancy'], event.keywords
assert_equal Time.zone.parse('Mon, 13 Feb 2023 09:00:00.000000000 UTC +00:00'), event.start
assert_equal Time.zone.parse('Mon, 13 Feb 2023 17:00:00.000000000 UTC +00:00'), event.end
end
end
6 changes: 3 additions & 3 deletions test/unit/ingestors/dtls_ingestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DtlsIngestorTest < ActiveSupport::TestCase
# run task
assert_difference 'Event.count', 4 do
freeze_time(2019) do
VCR.use_cassette("ingestors/dtls") do
VCR.use_cassette('ingestors/dtls') do
ingestor.read(source.url)
ingestor.write(@user, @content_provider)
end
Expand All @@ -53,7 +53,7 @@ class DtlsIngestorTest < ActiveSupport::TestCase
assert_equal 'Amsterdam', event.timezone
assert_equal 'Wageningen Campus', event.city
assert_equal 'Netherlands', event.country
assert_equal Time.zone.parse('Mon, 13 Feb 2023 00:00:00.000000000 UTC +00:00'), event.start
assert_equal Time.zone.parse('Fri, 17 Feb 2023 00:00:00.000000000 UTC +00:00'), event.end
assert_equal Time.zone.parse('Mon, 13 Feb 2023 09:00:00.000000000 UTC +00:00'), event.start
assert_equal Time.zone.parse('Fri, 17 Feb 2023 17:00:00.000000000 UTC +00:00'), event.end
end
end
6 changes: 3 additions & 3 deletions test/unit/ingestors/nwo_ingestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class NwoIngestorTest < ActiveSupport::TestCase
# run task
assert_difference 'Event.count', 24 do
freeze_time(2019) do
VCR.use_cassette("ingestors/nwo") do
VCR.use_cassette('ingestors/nwo') do
ingestor.read(source.url)
ingestor.write(@user, @content_provider)
end
Expand All @@ -52,7 +52,7 @@ class NwoIngestorTest < ActiveSupport::TestCase
assert_equal 'NWO Biophysics', event.title
assert_equal 'Amsterdam', event.timezone
assert_equal 'NWO', event.source
assert_equal Time.zone.parse('Mon, 09 Oct 2023 00:00:00.000000000 UTC +00:00'), event.start
assert_equal Time.zone.parse('Tue, 10 Oct 2023 00:00:00.000000000 UTC +00:00'), event.end
assert_equal Time.zone.parse('Mon, 09 Oct 2023 09:00:00.000000000 UTC +00:00'), event.start
assert_equal Time.zone.parse('Tue, 10 Oct 2023 17:00:00.000000000 UTC +00:00'), event.end
end
end
10 changes: 5 additions & 5 deletions test/unit/ingestors/osci_ingestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class OsciIngestorTest < ActiveSupport::TestCase
ingestor = Ingestors::OsciIngestor.new

# check event doesn't
new_title = "14:00: Open Science Coffee: Assessing robustness through multiverse analysis – Applications in research and education"
new_title = '14:00: Open Science Coffee: Assessing robustness through multiverse analysis – Applications in research and education'
new_url = 'https://osc-international.com/my-calendar/?format=calendar&month=9&yr=2023#mc_calendar_03_2-calendar-details-my-calendar'

refute Event.where(title: new_title, url: new_url).any?

# run task
assert_difference 'Event.count', 12 do
assert_difference 'Event.count', 17 do
freeze_time(2023) do
VCR.use_cassette("ingestors/osci") do
VCR.use_cassette('ingestors/osci') do
ingestor.read(source.url)
ingestor.write(@user, @content_provider)
end
Expand All @@ -39,8 +39,8 @@ class OsciIngestorTest < ActiveSupport::TestCase

assert_equal 18, ingestor.events.count
assert ingestor.materials.empty?
assert_equal 12, ingestor.stats[:events][:added]
assert_equal 6, ingestor.stats[:events][:updated]
assert_equal 17, ingestor.stats[:events][:added]
assert_equal 1, ingestor.stats[:events][:updated]
assert_equal 0, ingestor.stats[:events][:rejected]

# check event does exist
Expand Down
6 changes: 3 additions & 3 deletions test/unit/ingestors/utwente_ingestor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UtwenteIngestorTest < ActiveSupport::TestCase
# run task
assert_difference 'Event.count', 1 do
freeze_time(2019) do
VCR.use_cassette("ingestors/utwente") do
VCR.use_cassette('ingestors/utwente') do
ingestor.read(source.url)
ingestor.write(@user, @content_provider)
end
Expand All @@ -51,8 +51,8 @@ class UtwenteIngestorTest < ActiveSupport::TestCase
# check other fields
assert_equal 'Amsterdam', event.timezone
assert_equal 'University of Twente', event.organizer
assert_equal Time.zone.parse('Thu, 09 Nov 2023 00:00:00 +0000'), event.start
assert_equal Time.zone.parse('Thu, 09 Nov 2023 00:00:00 +0000'), event.end
assert_equal Time.zone.parse('Thu, 09 Nov 2023 09:00:00 +0000'), event.start
assert_equal Time.zone.parse('Thu, 09 Nov 2023 17:00:00 +0000'), event.end
assert_equal 'Waaier', event.venue
end
end

0 comments on commit 227e02b

Please sign in to comment.