<commit_before>require 'test_helper'
require 'ostruct'
require 'utility/performance_report_generator'

class GroupOfferCategoryTest < ActiveSupport::TestCase
  include PerformanceReportGenerator

  def setup
    DatabaseCleaner.clean
    DatabaseCleaner.start

    really_destroy_with_deleted(Hour, Feedback, TrialFeedback, Assignment, Client, GroupAssignment,
      GroupOffer, Department, User)
    @today = Time.zone.today
    @this_periods = [@today.beginning_of_year, @today.end_of_year]
    @this_dates = @this_periods.map(&:to_date)
    @year_ago = 1.year.ago.to_date
    @last_periods = [@year_ago.beginning_of_year, @year_ago.end_of_year]
    @last_dates = @last_periods.map(&:to_date)
    @user = create :user

    @this_year = PerformanceReport.create!(user: @user, period_start: @this_periods.first,
      period_end: @this_periods.last)
    @last_year = PerformanceReport.create!(user: @user, period_start: @last_periods.first,
      period_end: @last_periods.last)
  end

  def refresh_reports
    @this_year.generate_report
    @last_year.generate_report
  end

  def type_of_values(content)
    content.values.flat_map(&:values).flat_map(&:values).uniq
  end

  test 'no_nothing_reports_have_all_zero_values' do
    assert_equal([0, 0.0], type_of_values(@this_year.report_content))
    assert_equal([0, 0.0], type_of_values(@last_year.report_content))
  end

  VOLUNTEER_ZERO = {
    total: 0,
    active_assignment: 0,
    active_group_assignment: 0,
    active_both: 0,
    active_total: 0,
    only_assignment_active: 0,
    only_group_active: 0,
    created: 0,
    resigned: 0,
    inactive: 0,
    assignment_hour_records: 0,
    assignment_hours: 0,
    group_offer_hour_records: 0,
    group_offer_hours: 0,
    total_hour_records: 0,
    total_hours: 0,
    assignment_feedbacks: 0,
    group_offer_feedbacks: 0,
    total_feedbacks: 0,
    assignment_trial_feedbacks: 0,
    group_offer_trial_feedbacks: 0,
    total_trial_feedbacks: 0
  }.stringify_keys.freeze

  test 'volunteer_zurich_internal_external' do
    volunteer_zurich_this_year = create :volunteer_with_user, :zuerich
    volunteer_zurich_this_year.update(created_at: @this_periods.first)

    # Last year still all zero
    refresh_reports
    assert_equal([0, 0.0], type_of_values(@last_year.report_content))

    # this year 0 and 1
    refresh_reports
    assert_equal([0, 0.0, 1], type_of_values(@this_year.report_content).sort)

    expected_influenced = VOLUNTEER_ZERO.merge(
      total: 1, created: 1, inactive: 1
    ).stringify_keys

    assert_equal(expected_influenced, @this_year.report_content['volunteers']['all'])
    assert_equal(expected_influenced, @this_year.report_content['volunteers']['zurich'])
    assert_equal(expected_influenced, @this_year.report_content['volunteers']['internal'])
<commit_msg>fix hours to float in model test<commit_after>require 'test_helper'
require 'ostruct'
require 'utility/performance_report_generator'

class GroupOfferCategoryTest < ActiveSupport::TestCase
  include PerformanceReportGenerator

  def setup
    DatabaseCleaner.clean
    DatabaseCleaner.start

    really_destroy_with_deleted(Hour, Feedback, TrialFeedback, Assignment, Client, GroupAssignment,
      GroupOffer, Department, User)
    @today = Time.zone.today
    @this_periods = [@today.beginning_of_year, @today.end_of_year]
    @this_dates = @this_periods.map(&:to_date)
    @year_ago = 1.year.ago.to_date
    @last_periods = [@year_ago.beginning_of_year, @year_ago.end_of_year]
    @last_dates = @last_periods.map(&:to_date)
    @user = create :user

    @this_year = PerformanceReport.create!(user: @user, period_start: @this_periods.first,
      period_end: @this_periods.last)
    @last_year = PerformanceReport.create!(user: @user, period_start: @last_periods.first,
      period_end: @last_periods.last)
  end

  def refresh_reports
    @this_year.generate_report
    @last_year.generate_report
  end

  def type_of_values(content)
    content.values.flat_map(&:values).flat_map(&:values).uniq
  end

  test 'no_nothing_reports_have_all_zero_values' do
    assert_equal([0, 0.0], type_of_values(@this_year.report_content))
    assert_equal([0, 0.0], type_of_values(@last_year.report_content))
  end

  VOLUNTEER_ZERO = {
    total: 0,
    active_assignment: 0,
    active_group_assignment: 0,
    active_both: 0,
    active_total: 0,
    only_assignment_active: 0,
    only_group_active: 0,
    created: 0,
    resigned: 0,
    inactive: 0,
    assignment_hour_records: 0,
    assignment_hours: 0.0,
    group_offer_hour_records: 0,
    group_offer_hours: 0.0,
    total_hour_records: 0,
    total_hours: 0.0,
    assignment_feedbacks: 0,
    group_offer_feedbacks: 0,
    total_feedbacks: 0,
    assignment_trial_feedbacks: 0,
    group_offer_trial_feedbacks: 0,
    total_trial_feedbacks: 0
  }.stringify_keys.freeze

  test 'volunteer_zurich_internal_external' do
    volunteer_zurich_this_year = create :volunteer_with_user, :zuerich
    volunteer_zurich_this_year.update(created_at: @this_periods.first)

    # Last year still all zero
    refresh_reports
    assert_equal([0, 0.0], type_of_values(@last_year.report_content))

    # this year 0 and 1
    refresh_reports
    assert_equal([0, 0.0, 1], type_of_values(@this_year.report_content).sort)

    expected_influenced = VOLUNTEER_ZERO.merge(
      total: 1, created: 1, inactive: 1
    ).stringify_keys

    assert_equal(expected_influenced, @this_year.report_content['volunteers']['all'])
    assert_equal(expected_influenced, @this_year.report_content['volunteers']['zurich'])
    assert_equal(expected_influenced, @this_year.report_content['volunteers']['internal'])

{'fraction_non_alphanumeric': 0.09966365728447513, 'fraction_numerical': 0.015046910957691628, 'mean_word_length': 4.672690763052209, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4724995', 'n_tokens_mistral': 2079, 'n_tokens_neox': 1960, 'n_words': 334}