/src/NzbDrone.Core.Test/JobTests/JobRepositoryFixture.cs

https://github.com/NzbDrone/NzbDrone · C# · 182 lines · 0 code · 0 blank · 182 comment · 0 complexity · cbff0eb5efb0771e19cb85b77dd0988d MD5 · raw file

  1. /*
  2. using System;
  3. using System.Collections.Generic;
  4. using FizzWare.NBuilder;
  5. using FluentAssertions;
  6. using NUnit.Framework;
  7. using NzbDrone.Core.Jobs;
  8. using NzbDrone.Core.Lifecycle;
  9. using NzbDrone.Core.Test.Framework;
  10. namespace NzbDrone.Core.Test.JobTests
  11. {
  12. [TestFixture]
  13. public class JobRepositoryFixture : DbTest<JobRepository, JobDefinition>
  14. {
  15. FakeJob _fakeJob;
  16. DisabledJob _disabledJob;
  17. [SetUp]
  18. public void Setup()
  19. {
  20. _fakeJob = new FakeJob();
  21. _disabledJob = new DisabledJob();
  22. }
  23. private void Initialize()
  24. {
  25. Subject.Handle(new ApplicationStartedEvent());
  26. }
  27. [Test]
  28. public void Init_should_add_defintaions()
  29. {
  30. IEnumerable<IJob> baseFakeJobs = new List<IJob> { _fakeJob };
  31. Mocker.SetConstant(baseFakeJobs);
  32. Initialize();
  33. Storage.All().Should().HaveCount(1);
  34. StoredModel.Interval.Should().Be((Int32)_fakeJob.DefaultInterval.TotalMinutes);
  35. StoredModel.Name.Should().Be(_fakeJob.Name);
  36. StoredModel.Name.Should().Be(_fakeJob.GetType().ToString());
  37. StoredModel.LastExecution.Should().HaveYear(DateTime.Now.Year);
  38. StoredModel.LastExecution.Should().HaveMonth(DateTime.Now.Month);
  39. StoredModel.LastExecution.Should().HaveDay(DateTime.Today.Day);
  40. }
  41. [Test]
  42. public void inti_should_removed_jobs_that_no_longer_exist()
  43. {
  44. IEnumerable<IJob> fakeJobs = new List<IJob> { _fakeJob };
  45. Mocker.SetConstant(fakeJobs);
  46. var deletedJob = Builder<JobDefinition>.CreateNew()
  47. .With(c => c.Id = 0)
  48. .Build();
  49. Db.Insert(deletedJob);
  50. //Make sure deleted job is stored
  51. AllStoredModels.Should().HaveCount(1);
  52. AllStoredModels.Should().Contain(c => c.Name == deletedJob.Name);
  53. Initialize();
  54. //Make sure init has cleaned up the deleted job
  55. AllStoredModels.Should().HaveCount(1);
  56. AllStoredModels.Should().NotContain(c => c.Name == deletedJob.Name);
  57. }
  58. [Test]
  59. public void init_should_removed_jobs_that_no_longer_exist_even_with_same_name()
  60. {
  61. IEnumerable<IJob> fakeJobs = new List<IJob> { _fakeJob };
  62. Mocker.SetConstant(fakeJobs);
  63. var deletedJob = Builder<JobDefinition>.CreateNew()
  64. .With(c => c.Name = _fakeJob.Name)
  65. .With(c => c.Id = 0)
  66. .Build();
  67. Db.Insert(deletedJob);
  68. //Make sure deleted job is stored
  69. AllStoredModels.Should().HaveCount(1);
  70. AllStoredModels.Should().Contain(c => c.Name == deletedJob.Name);
  71. Initialize();
  72. //Make sure init has cleaned up the deleted job
  73. AllStoredModels.Should().HaveCount(1);
  74. AllStoredModels.Should().NotContain(c => c.Name == deletedJob.Name);
  75. }
  76. [Test]
  77. public void init_should_update_existing_job()
  78. {
  79. var oldJob = Builder<JobDefinition>.CreateNew()
  80. .With(c => c.Id = 0)
  81. .With(c => c.Name = "OldName")
  82. .With(c => c.Name = typeof(FakeJob).ToString())
  83. .With(c => c.Interval = 0)
  84. .With(c => c.Success = true)
  85. .With(c => c.LastExecution = DateTime.Now.AddDays(-7).Date)
  86. .Build();
  87. Storage.Insert(oldJob);
  88. var newJob = new FakeJob();
  89. IEnumerable<IJob> fakeJobs = new List<IJob> { newJob };
  90. Mocker.SetConstant(fakeJobs);
  91. Initialize();
  92. AllStoredModels.Should().HaveCount(1);
  93. StoredModel.Name.Should().Be(newJob.GetType().FullName);
  94. StoredModel.Name.Should().Be(newJob.Name);
  95. StoredModel.Interval.Should().Be((int)newJob.DefaultInterval.TotalMinutes);
  96. StoredModel.Success.Should().Be(oldJob.Success);
  97. StoredModel.LastExecution.Should().Be(oldJob.LastExecution);
  98. }
  99. [Test]
  100. public void pending_job_should_get_jobs_that_have_matured()
  101. {
  102. var oldJob = Builder<JobDefinition>.CreateNew()
  103. .With(c => c.Id = 0)
  104. .With(c => c.Interval = 1)
  105. .With(c => c.Success = true)
  106. .With(c => c.LastExecution = DateTime.Now.AddMinutes(-5))
  107. .Build();
  108. Storage.Insert(oldJob);
  109. Subject.GetPendingJobs().Should().HaveCount(1);
  110. }
  111. [Test]
  112. public void pending_job_should_not_get_jobs_that_havent_matured()
  113. {
  114. var recent = Builder<JobDefinition>.CreateNew()
  115. .With(c => c.Id = 0)
  116. .With(c => c.Interval = 60)
  117. .With(c => c.Success = true)
  118. .With(c => c.LastExecution = DateTime.Now.AddMinutes(-5))
  119. .Build();
  120. Storage.Insert(recent);
  121. Subject.GetPendingJobs().Should().BeEmpty();
  122. }
  123. /* [Test]
  124. public void disabled_jobs_arent_run_by_scheduler()
  125. {
  126. IEnumerable<IJob> BaseFakeJobs = new List<IJob> { disabledJob };
  127. Mocker.SetConstant(BaseFakeJobs);
  128. var jobProvider = Mocker.Resolve<JobController>();
  129. jobProvider.QueueScheduled();
  130. WaitForQueue();
  131. disabledJob.ExecutionCount.Should().Be(0);
  132. }#1#
  133. }
  134. }
  135. */