<commit_before># frozen_string_literal: true

require("test_helper")

class ObservationReportTest < UnitTestCase
  def do_csv_test(report_type, obs, expect, &block)
    query = Query.lookup(:Observation, :all)
    report = report_type.new(query: query).body
    assert_not_empty(report)
    table = CSV.parse(report, col_sep: report_type.separator)
    assert_equal(query.num_results + 1, table.count)
    idx = query.results.sort_by(&block).index(obs)
    assert_equal(expect, table[idx + 1])
  end

  def do_tsv_test(report_type, obs, expect, &block)
    query = Query.lookup(:Observation, :all)
    report = report_type.new(query: query).body
    assert_not_empty(report)
    rows = report.split("\n")
    assert_equal(query.num_results + 1, rows.length)
    idx = query.results.sort_by(&block).index(obs)
    assert_equal(expect, rows[idx + 1].split("\t"))
  end

  def do_zip_test(report_type, expect)
    query = Query.lookup(:Observation, :all)
    report = report_type.new(query: query).body
    assert_not_empty(report)
    zio = Zip::InputStream.new(StringIO.new(report))
    contents = []
    while (entry = zio.get_next_entry)
      contents << entry.name
    end
    assert_equal(expect, contents)
  end

  # ----------------------------------------------------------------------------

  def test_adolf
    obs = observations(:agaricus_campestris_obs)
    expect = [
      nil,
      nil,
      nil,
      "Agaricus",
      nil,
      "campestris",
      "L.",
      nil,
      nil,
      nil,
      nil,
      "USA",
      "California",
      "Burbank",
      "34.15",
      "-118.37",
      "34.22",
      "-118.29",
      nil,
      nil,
      nil,
      nil,
      "2007-03-19",
      "<NAME>",
      nil,
      nil,
      nil,
      "From the lawn next door",
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      obs.id.to_s,
      nil
    ]
    do_csv_test(ObservationReport::Adolf, obs, expect, &:text_name)
  end

  def test_darwin_observations
    obs = observations(:detailed_unknown_obs)
    expect = [
      obs.id.to_s,
      "#{MO.http_domain}/#{obs.id}",
      "HumanObservation",
      "2006-05-12 17:21:00 UTC",
      "MushroomObserver",
      nil,
      "Fungi",
      nil,
      "Kingdom",
      "Fungi",
      nil,
      nil,
      "<NAME>",
      "2006-05-11",
      "2006",
      "5",
      "11",
      "USA",
      "California",
      nil,
      "Burbank",
      "34.185",
      "-118.33",
      "148",
      "294",
      "Found in a strange place... & with śtrangè characters™"
    ]
    do_csv_test(ObservationReport::Darwin::Observations, obs, expect, &:id)
  end

  def test_dwca
    expect = ["meta.xml", "observations.csv", "taxa.csv"]
    do_zip_test(ObservationReport::Dwca, expect)
  end

  def test_taxa_report
    taxa_report = build_taxa_report
    report_content = taxa_report.body
    assert_not_empty(report_content)
    table = CSV.parse(report_content, col_sep: taxa_report.separator)
    assert_equal(Observation.all.pluck(:name_id).uniq.length + 1, table.count)
    obs = Observation.first
    assert(table.include?([obs.name_id.to_s, obs.text_name]))
  end

  def build_taxa_report
    query = Query.lookup(:Observation, :all)
    observations = ObservationReport::Darwin::Observations.new(query: query)
    return if observations.body.empty?<commit_msg>Update test from taxa.csv to multimedia.csv<commit_after># frozen_string_literal: true

require("test_helper")

class ObservationReportTest < UnitTestCase
  def do_csv_test(report_type, obs, expect, &block)
    query = Query.lookup(:Observation, :all)
    report = report_type.new(query: query).body
    assert_not_empty(report)
    table = CSV.parse(report, col_sep: report_type.separator)
    assert_equal(query.num_results + 1, table.count)
    idx = query.results.sort_by(&block).index(obs)
    assert_equal(expect, table[idx + 1])
  end

  def do_tsv_test(report_type, obs, expect, &block)
    query = Query.lookup(:Observation, :all)
    report = report_type.new(query: query).body
    assert_not_empty(report)
    rows = report.split("\n")
    assert_equal(query.num_results + 1, rows.length)
    idx = query.results.sort_by(&block).index(obs)
    assert_equal(expect, rows[idx + 1].split("\t"))
  end

  def do_zip_test(report_type, expect)
    query = Query.lookup(:Observation, :all)
    report = report_type.new(query: query).body
    assert_not_empty(report)
    zio = Zip::InputStream.new(StringIO.new(report))
    contents = []
    while (entry = zio.get_next_entry)
      contents << entry.name
    end
    assert_equal(expect, contents)
  end

  # ----------------------------------------------------------------------------

  def test_adolf
    obs = observations(:agaricus_campestris_obs)
    expect = [
      nil,
      nil,
      nil,
      "Agaricus",
      nil,
      "campestris",
      "L.",
      nil,
      nil,
      nil,
      nil,
      "USA",
      "California",
      "Burbank",
      "34.15",
      "-118.37",
      "34.22",
      "-118.29",
      nil,
      nil,
      nil,
      nil,
      "2007-03-19",
      "<NAME>",
      nil,
      nil,
      nil,
      "From the lawn next door",
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      nil,
      obs.id.to_s,
      nil
    ]
    do_csv_test(ObservationReport::Adolf, obs, expect, &:text_name)
  end

  def test_darwin_observations
    obs = observations(:detailed_unknown_obs)
    expect = [
      obs.id.to_s,
      "#{MO.http_domain}/#{obs.id}",
      "HumanObservation",
      "2006-05-12 17:21:00 UTC",
      "MushroomObserver",
      nil,
      "Fungi",
      nil,
      "Kingdom",
      "Fungi",
      nil,
      nil,
      "<NAME>",
      "2006-05-11",
      "2006",
      "5",
      "11",
      "USA",
      "California",
      nil,
      "Burbank",
      "34.185",
      "-118.33",
      "148",
      "294",
      "Found in a strange place... & with śtrangè characters™"
    ]
    do_csv_test(ObservationReport::Darwin::Observations, obs, expect, &:id)
  end

  def test_dwca
    expect = ["meta.xml", "observations.csv", "multimedia.csv"]
    do_zip_test(ObservationReport::Dwca, expect)
  end

  def test_taxa_report
    taxa_report = build_taxa_report
    report_content = taxa_report.body
    assert_not_empty(report_content)
    table = CSV.parse(report_content, col_sep: taxa_report.separator)
    assert_equal(Observation.all.pluck(:name_id).uniq.length + 1, table.count)
    obs = Observation.first
    assert(table.include?([obs.name_id.to_s, obs.text_name]))
  end

  def build_taxa_report
    query = Query.lookup(:Observation, :all)
    observations = ObservationReport::Darwin::Observations.new(query: query)
    return if observations.body.empty?
{'fraction_non_alphanumeric': 0.1509433962264151, 'fraction_numerical': 0.02240566037735849, 'mean_word_length': 2.325980392156863, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3854840', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2313, 'n_words': 434}