require File.dirname(__FILE__) + '/../test_helper'
require 'user_system'

class WorkTest < Test::Unit::TestCase
  include UserSystem
  
  main_scenario

  def teardown
    Thread.current[:user] = nil
  end
  
  def test_work_totals_for_week
    Thread.current[:user] = users(:tesla)
    # Week 24 is from 
    work_totals = Work.work_totals_for_week(2007, 24, users(:tesla))
    assert_equal 1, work_totals.size
    assert_equal Array, work_totals[1].class
    assert_equal 2, work_totals[1].size
    assert_equal Array, work_totals[1][0].class
    assert_equal Array, work_totals[1][1].class
    assert_equal  0, work_totals[1][1][0]
    assert_equal 40, work_totals[1][1][1]
    assert_equal  8, work_totals[1][1][2]
    assert_equal  0, work_totals[1][1][3]
    assert_equal  0, work_totals[1][1][4]
    assert_equal  0, work_totals[1][1][5]
    assert_equal  0, work_totals[1][1][6]
  end
  
  def test_work_totals_for_week_anonymously
    # Week 24 is from 
    work_totals = Work.work_totals_for_week(2007, 24, nil)
    assert_equal 1, work_totals.size
    assert_equal Array, work_totals[1].class
    assert_equal 2, work_totals[1].size
    assert_equal Array, work_totals[1][0].class
    assert_equal Array, work_totals[1][1].class
    assert_equal  0, work_totals[1][1][0]
    assert_equal 30, work_totals[1][1][1]
    assert_equal  0, work_totals[1][1][2]
    assert_equal  0, work_totals[1][1][3]
    assert_equal  0, work_totals[1][1][4]
    assert_equal  0, work_totals[1][1][5]
    assert_equal  0, work_totals[1][1][6]
  end
  
  def test_find_work_for_day
    Thread.current[:user] = users(:tesla)
    assert_equal 1, Work.find_work_for_day(Date.parse('2007-06-12')).size
  end

  def test_works_for_week_by_work_account
    Thread.current[:user] = users(:tesla)
    work_totals = Work.works_for_week_by_work_account(2007, 24, users(:tesla))
    assert_equal({work_accounts(:light_control) => [nil, BigDecimal('40'), BigDecimal('8'), nil, nil, nil, nil]}, work_totals)
  end

  def test_works_for_week_by_work_account_anonymous
    work_totals = Work.works_for_week_by_work_account(2007, 24, nil)
    assert_equal({work_accounts(:light_control) => [nil, BigDecimal('30'), nil, nil, nil, nil, nil]}, work_totals)
  end

  def test_legal_to_register_work_if_the_same_date_is_marked_as_absent
    Thread.current[:user] = users(:tesla)
    Work.create!({:started_on => '2007-06-10', :user_id => 1000001, :work_account => work_accounts(:light_control)})
  end

  def test_calculate_hours
    assert_equal 0.083, works(:short).calculate_hours
  end
  
  private

  # TODO (uwe): This method should be removed
  # It is here only because ClassTableInheritanceInRails broke reading fixtures by name
  def users(login)
    super(login)
  end
  
end