@@ -39,6 +39,10 @@ class GithubIngestorTest < ActiveSupport::TestCase # rubocop:disable Metrics/Cla
3939 webmock ( 'https://api.github.com/repos/hsf-training/cpluspluscourse/releases' , 'github/releases.json' )
4040 webmock ( 'https://api.github.com/repos/swcarpentry/python-novice-inflammation/releases' , 'github/releases.json' )
4141 webmock ( 'https://api.github.com/repos/hsf-training/hsf-training-scikit-hep-webpage/releases' , 'github/releases.json' )
42+
43+ # HTML
44+ html = File . read ( Rails . root . join ( 'test/fixtures/files/ingestion/github/mock.html' ) )
45+ @doc = Nokogiri ::HTML ( html )
4246 end
4347
4448 teardown do
@@ -235,6 +239,102 @@ class GithubIngestorTest < ActiveSupport::TestCase # rubocop:disable Metrics/Cla
235239 assert_nil sample_after_ttl_and_change . description
236240 end
237241
242+ test 'std errors when exception is raised' do
243+ @ingestor . stub ( :get_sources , -> ( _url ) { raise StandardError , 'test failure' } ) do
244+ @ingestor . send ( :read , 'https://github.com/example' )
245+ end
246+ assert_includes @ingestor . instance_variable_get ( :@messages ) . last ,
247+ 'Ingestors::GithubIngestor read failed, test failure'
248+
249+ @ingestor . stub ( :open_url , -> ( _url ) { raise StandardError , 'test failure' } ) do
250+ @ingestor . send ( :get_or_set_cache , 'key' , 'https://github.com/example' )
251+ end
252+ assert_includes @ingestor . instance_variable_get ( :@messages ) . last ,
253+ 'Ingestors::GithubIngestor get_or_set_cache failed for https://github.com/example, test failure'
254+
255+ @ingestor . stub ( :get_or_set_cache , -> ( _key , _url ) { raise StandardError , 'test failure' } ) do
256+ @ingestor . send ( :fetch_doi , 'full_name' )
257+ end
258+ assert_includes @ingestor . instance_variable_get ( :@messages ) . last ,
259+ 'Ingestors::GithubIngestor fetch_doi failed for https://api.github.com/repos/full_name/contents/README.md, test failure'
260+
261+ @ingestor . stub ( :get_or_set_cache , -> ( _key , _url ) { raise StandardError , 'test failure' } ) do
262+ @ingestor . send ( :fetch_latest_release , 'full_name' )
263+ end
264+ assert_includes @ingestor . instance_variable_get ( :@messages ) . last ,
265+ 'Ingestors::GithubIngestor fetch_latest_release failed for https://api.github.com/repos/full_name/releases, test failure'
266+
267+ @ingestor . stub ( :get_or_set_cache , -> ( _key , _url ) { raise StandardError , 'test failure' } ) do
268+ @ingestor . send ( :fetch_contributors , 'my.url' , 'full_name' )
269+ end
270+ assert_includes @ingestor . instance_variable_get ( :@messages ) . last ,
271+ 'Ingestors::GithubIngestor fetch_contributors failed for my.url, test failure'
272+ end
273+
274+ test 'prereq_node? returns true when id includes prereq' do
275+ node = Nokogiri ::XML ::Node . new ( 'div' , @doc )
276+ node [ 'id' ] = 'prerequisites'
277+ assert @ingestor . send ( :prereq_node? , node )
278+ end
279+
280+ test 'prereq_node? returns true when class includes prereq' do
281+ node = Nokogiri ::XML ::Node . new ( 'div' , @doc )
282+ node [ 'class' ] = 'has_prereq_section'
283+ assert @ingestor . send ( :prereq_node? , node )
284+ end
285+
286+ test 'prereq_node? returns false for unrelated id/class' do
287+ node = Nokogiri ::XML ::Node . new ( 'div' , @doc )
288+ node [ 'id' ] = 'requirements'
289+ refute @ingestor . send ( :prereq_node? , node )
290+ end
291+
292+ test 'extract_following_paragraphs finds immediate sibling paragraphs' do
293+ doc = Nokogiri ::HTML ( <<~HTML )
294+ < div id ="prereq "> </ div >
295+ < p > First paragraph</ p >
296+ < ul > < li > List item</ li > </ ul >
297+ < h2 > Stop here</ h2 >
298+ HTML
299+
300+ node = doc . at_xpath ( '//*[@id="prereq"]' )
301+ paragraphs = [ ]
302+ @ingestor . send ( :extract_following_paragraphs , node , paragraphs )
303+
304+ assert_equal 2 , paragraphs . size
305+ assert_equal %w[ p ul ] , paragraphs . map ( &:name )
306+ end
307+
308+ test 'extract_nested_paragraphs finds paragraphs inside the node' do
309+ doc = Nokogiri ::HTML ( <<~HTML )
310+ < div id ="prereq ">
311+ < p > Nested paragraph</ p >
312+ < ul > < li > Nested list</ li > </ ul >
313+ </ div >
314+ HTML
315+
316+ node = doc . at_xpath ( '//*[@id="prereq"]' )
317+ paragraphs = [ ]
318+ @ingestor . send ( :extract_nested_paragraphs , node , paragraphs )
319+
320+ assert_equal 2 , paragraphs . size
321+ assert_equal %w[ p ul ] , paragraphs . map ( &:name )
322+ end
323+
324+ test 'fetch_prerequisites_from_id_or_class collects prerequisites correctly' do
325+ doc = Nokogiri ::HTML ( <<~HTML )
326+ < div id ="prereq "> </ div >
327+ < p > Be kind</ p >
328+ < p > Have Ruby installed</ p >
329+ HTML
330+
331+ paragraphs = [ ]
332+ result = @ingestor . send ( :fetch_prerequisites_from_id_or_class , doc , paragraphs )
333+
334+ assert_equal 2 , result . size
335+ assert_equal 'Be kind' , result . first . text
336+ end
337+
238338 private
239339
240340 def webmock ( url , filename )
0 commit comments