From 2566a7761c02c4f19129f236e8afdec38b23df42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ko=CC=88tte?= Date: Mon, 18 Jan 2016 13:50:53 +0100 Subject: [PATCH] add tests for next build time --- spec/v3/models/cron_spec.rb | 159 ++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 spec/v3/models/cron_spec.rb diff --git a/spec/v3/models/cron_spec.rb b/spec/v3/models/cron_spec.rb new file mode 100644 index 00000000..21a800db --- /dev/null +++ b/spec/v3/models/cron_spec.rb @@ -0,0 +1,159 @@ +require 'spec_helper' +require 'timecop' + +describe Travis::API::V3::Models::Cron do + let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first } + let(:branch) { Travis::API::V3::Models::Branch.create(repository: repo, name: 'cron test') } + + describe "next build time is calculated correctly on year changes" do + + before do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + end + + after do + Timecop.return + end + + it "for daily builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: false) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 1, 16) + build.destroy + cron.destroy + end + + it "for weekly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: false) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 7, 16) + build.destroy + cron.destroy + end + + it "for monthly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: false) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 31, 16) + build.destroy + cron.destroy + end + + end + + describe "push build is ignored if disable by build is false" do + + before do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + end + + after do + Timecop.return + end + + it "for daily builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: false) + cron_build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + push_build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'push') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 1, 16) + cron_build.destroy + push_build.destroy + cron.destroy + end + + it "for weekly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: false) + cron_build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + push_build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'push') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 7, 16) + cron_build.destroy + push_build.destroy + cron.destroy + end + + it "for monthly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: false) + cron_build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + push_build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'push') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 31, 16) + cron_build.destroy + push_build.destroy + cron.destroy + end + + end + + describe "disable by build works with build" do + + before do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + end + + after do + Timecop.return + end + + it "for daily builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'push') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 2, 16) + build.destroy + cron.destroy + end + + it "for weekly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'push') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 14, 16) + build.destroy + cron.destroy + end + + it "for monthly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'push') + expect(cron.next_build_time).to be == DateTime.new(2016, 2, 29, 16) # it's a leap year :-D + build.destroy + cron.destroy + end + + end + + describe "disable by build works without build" do + + before do + Timecop.travel(DateTime.new(2015, 12, 31, 16)) + end + + after do + Timecop.return + end + + it "for daily builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'daily', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 1, 16) + build.destroy + cron.destroy + end + + it "for weekly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'weekly', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 7, 16) + build.destroy + cron.destroy + end + + it "for monthly builds" do + cron = Travis::API::V3::Models::Cron.create(branch_id: branch.id, interval: 'monthly', disable_by_build: true) + build = Travis::API::V3::Models::Build.create(:repository_id => repo.id, :branch_name => branch.name, :event_type => 'cron') + expect(cron.next_build_time).to be == DateTime.new(2016, 1, 31, 16) + build.destroy + cron.destroy + end + + end + + +end