initial commit

This commit is contained in:
Jens Kraemer
2020-04-22 18:03:56 +08:00
commit 70257cdee0
16 changed files with 331 additions and 0 deletions

5
test/test_helper.rb Normal file
View File

@@ -0,0 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
class ActiveSupport::TestCase
end

View File

@@ -0,0 +1,40 @@
require_relative '../test_helper'
class StartTimerTest < ActiveSupport::TestCase
fixtures :users, :user_preferences, :time_entries, :projects,
:roles, :member_roles, :members, :enumerations
setup do
@user = User.find 1
@time_entry = TimeEntry.last
end
test "should start timer" do
refute User.find(@user.id).timer_running?
assert r = Stopwatch::StartTimer.new(@time_entry, user: @user).call
assert r.success?, r.inspect
assert User.find(@user.id).timer_running?
end
test "should stop and save existing timer" do
hours = @time_entry.hours
r = Stopwatch::StartTimer.new(@time_entry, user: @user).call
assert r.success?
t = Stopwatch::Timer.new(@user)
data = t.send(:data)
data[:started_at] = 1.hour.ago.to_i
t.save
@time_entry.reload
assert_equal hours, @time_entry.hours
another = TimeEntry.new(@time_entry.attributes)
another.user = @user
r = Stopwatch::StartTimer.new(another, user: @user).call
assert r.success?
@time_entry.reload
assert_equal hours+1, @time_entry.hours
end
end

52
test/unit/timer_test.rb Normal file
View File

@@ -0,0 +1,52 @@
require_relative '../test_helper'
class TimerTest < ActiveSupport::TestCase
fixtures :users, :user_preferences, :time_entries
setup do
@user = User.find 1
@timer = Stopwatch::Timer.new @user
@data = @timer.send :data
@time_entry = TimeEntry.last
end
test "should know wether its running" do
refute @timer.running?
@data[:started_at] = 5.minutes.ago
assert @timer.running?
end
test "should start timer" do
assert_nil @data[:started_at]
@timer.start @time_entry
assert @data[:started_at].present?
assert_equal @time_entry.id, @data[:time_entry_id]
end
test "should stop timer and update time entry" do
hours = @time_entry.hours
@timer.start @time_entry
@data[:started_at] = 1.hour.ago.to_i
@timer.stop
@time_entry.reload
assert_equal hours + 1, @time_entry.hours
end
test "should save and restore" do
hours = @time_entry.hours
@timer.start @time_entry
@data[:started_at] = 1.hour.ago.to_i
@timer.save
timer = Stopwatch::Timer.new User.find(@user.id)
assert timer.running?
timer.stop
@time_entry.reload
assert_equal hours + 1, @time_entry.hours
end
end

21
test/unit/user_test.rb Normal file
View File

@@ -0,0 +1,21 @@
require_relative '../test_helper'
class UserTest < ActiveSupport::TestCase
fixtures :users, :user_preferences
setup do
@user = User.find 1
end
test "should get inactive timer state" do
refute @user.timer_running?
end
test "should start timer and get running state" do
t = Stopwatch::Timer.new @user
t.start
t.save
assert @user.timer_running?
end
end