2013-02-16 05:03:54 +01:00
|
|
|
using System;
|
2013-02-16 04:50:22 +01:00
|
|
|
using System.Collections.Generic;
|
2013-02-05 05:07:07 +01:00
|
|
|
using System.Linq;
|
|
|
|
using FizzWare.NBuilder;
|
|
|
|
using FluentAssertions;
|
|
|
|
using NUnit.Framework;
|
2013-02-13 02:25:32 +01:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-25 07:13:53 +01:00
|
|
|
using NzbDrone.Core.Jobs;
|
2013-02-05 05:07:07 +01:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
2013-04-11 06:38:45 +02:00
|
|
|
using NzbDrone.Core.Tv;
|
2013-02-05 05:07:07 +01:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Datastore
|
|
|
|
{
|
2013-05-09 08:38:20 +02:00
|
|
|
|
|
|
|
public class DatabaseFixture : DbTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void SingleOrDefault_should_return_null_on_empty_db()
|
|
|
|
{
|
|
|
|
Mocker.Resolve<IDatabase>()
|
2013-07-16 02:45:49 +02:00
|
|
|
.GetDataMapper().Query<Series>()
|
2013-05-09 08:38:20 +02:00
|
|
|
.SingleOrDefault(c => c.CleanTitle == "SomeTitle")
|
|
|
|
.Should()
|
|
|
|
.BeNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 05:07:07 +01:00
|
|
|
[TestFixture]
|
2013-05-09 08:38:20 +02:00
|
|
|
public class ObjectDatabaseFixture : DbTest<BasicRepository<ScheduledTask>, ScheduledTask>
|
2013-02-05 05:07:07 +01:00
|
|
|
{
|
2013-05-09 08:38:20 +02:00
|
|
|
private ScheduledTask _sampleType;
|
2013-02-05 05:07:07 +01:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
2013-05-09 08:38:20 +02:00
|
|
|
_sampleType = Builder<ScheduledTask>
|
2013-02-06 09:40:57 +01:00
|
|
|
.CreateNew()
|
2013-02-26 04:58:57 +01:00
|
|
|
.With(s => s.Id = 0)
|
2013-02-06 09:40:57 +01:00
|
|
|
.Build();
|
2013-03-24 20:56:51 +01:00
|
|
|
|
2013-02-05 05:07:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_write_to_database()
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
Subject.Insert(_sampleType);
|
2013-05-09 08:38:20 +02:00
|
|
|
Db.All<ScheduledTask>().Should().HaveCount(1);
|
2013-02-05 05:07:07 +01:00
|
|
|
}
|
|
|
|
|
2013-02-16 05:03:54 +01:00
|
|
|
[Test]
|
|
|
|
public void double_insert_should_fail()
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
Subject.Insert(_sampleType);
|
|
|
|
Assert.Throws<InvalidOperationException>(() => Subject.Insert(_sampleType));
|
2013-02-16 05:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void update_item_with_root_index_0_should_faile()
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Assert.Throws<InvalidOperationException>(() => Subject.Update(_sampleType));
|
2013-02-16 05:03:54 +01:00
|
|
|
}
|
|
|
|
|
2013-02-16 05:17:23 +01:00
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_store_empty_list()
|
|
|
|
{
|
2013-05-09 08:38:20 +02:00
|
|
|
var series = new List<ScheduledTask>();
|
2013-02-05 05:07:07 +01:00
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
Subject.InsertMany(series);
|
2013-02-05 05:07:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void new_objects_should_get_id()
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Subject.Insert(_sampleType);
|
|
|
|
_sampleType.Id.Should().NotBe(0);
|
2013-02-05 05:07:07 +01:00
|
|
|
}
|
|
|
|
|
2013-02-16 04:50:22 +01:00
|
|
|
[Test]
|
2013-02-16 05:03:54 +01:00
|
|
|
public void new_object_should_get_new_id()
|
2013-02-16 04:50:22 +01:00
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Subject.Insert(_sampleType);
|
2013-02-16 04:50:22 +01:00
|
|
|
|
2013-05-09 08:38:20 +02:00
|
|
|
Db.All<ScheduledTask>().Should().HaveCount(1);
|
2013-03-24 05:16:00 +01:00
|
|
|
_sampleType.Id.Should().Be(1);
|
2013-02-16 04:50:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-11 07:59:42 +02:00
|
|
|
[Test]
|
|
|
|
public void should_read_and_write_in_utc()
|
|
|
|
{
|
|
|
|
var storedTime = DateTime.UtcNow;
|
2013-02-16 04:50:22 +01:00
|
|
|
|
2013-05-11 07:59:42 +02:00
|
|
|
_sampleType.LastExecution =storedTime;
|
2013-02-16 04:50:22 +01:00
|
|
|
|
2013-05-11 07:59:42 +02:00
|
|
|
Subject.Insert(_sampleType);
|
2013-05-09 08:38:20 +02:00
|
|
|
|
2013-05-11 07:59:42 +02:00
|
|
|
StoredModel.LastExecution.Kind.Should().Be(DateTimeKind.Utc);
|
|
|
|
StoredModel.LastExecution.ToLongTimeString().Should().Be(storedTime.ToLongTimeString());
|
|
|
|
}
|
2013-05-09 08:38:20 +02:00
|
|
|
|
2013-06-09 22:50:57 +02:00
|
|
|
[Test]
|
|
|
|
public void should_convert_all_dates_to_utc()
|
|
|
|
{
|
|
|
|
var storedTime = DateTime.Now;
|
|
|
|
|
|
|
|
_sampleType.LastExecution = storedTime;
|
|
|
|
|
|
|
|
Subject.Insert(_sampleType);
|
|
|
|
|
|
|
|
StoredModel.LastExecution.Kind.Should().Be(DateTimeKind.Utc);
|
|
|
|
StoredModel.LastExecution.ToLongTimeString().Should().Be(storedTime.ToUniversalTime().ToLongTimeString());
|
|
|
|
}
|
|
|
|
|
2013-02-05 05:07:07 +01:00
|
|
|
[Test]
|
2013-02-06 09:40:57 +01:00
|
|
|
public void should_have_id_when_returned_from_database()
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Subject.Insert(_sampleType);
|
2013-05-09 08:38:20 +02:00
|
|
|
var item = Db.All<ScheduledTask>();
|
2013-02-06 09:40:57 +01:00
|
|
|
|
|
|
|
item.Should().HaveCount(1);
|
2013-02-26 04:58:57 +01:00
|
|
|
item.First().Id.Should().NotBe(0);
|
|
|
|
item.First().Id.Should().BeLessThan(100);
|
2013-03-24 05:16:00 +01:00
|
|
|
item.First().Id.Should().Be(_sampleType.Id);
|
2013-02-06 09:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_find_object_by_id()
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
Subject.Insert(_sampleType);
|
2013-05-09 08:38:20 +02:00
|
|
|
var item = Db.All<ScheduledTask>().Single(c => c.Id == _sampleType.Id);
|
2013-02-06 09:40:57 +01:00
|
|
|
|
2013-02-26 04:58:57 +01:00
|
|
|
item.Id.Should().NotBe(0);
|
2013-03-24 05:16:00 +01:00
|
|
|
item.Id.Should().Be(_sampleType.Id);
|
2013-02-27 08:25:56 +01:00
|
|
|
}
|
|
|
|
|
2013-03-01 08:03:41 +01:00
|
|
|
|
|
|
|
[Test]
|
2013-03-27 07:16:55 +01:00
|
|
|
public void set_fields_should_only_update_selected_filed()
|
2013-03-01 08:03:41 +01:00
|
|
|
{
|
2013-05-09 08:38:20 +02:00
|
|
|
var childModel = new ScheduledTask
|
2013-03-01 08:03:41 +01:00
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
TypeName = "Address",
|
2013-03-25 07:13:53 +01:00
|
|
|
Interval = 12
|
2013-03-01 08:03:41 +01:00
|
|
|
};
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
Subject.Insert(childModel);
|
2013-03-01 08:03:41 +01:00
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
childModel.TypeName = "A";
|
2013-03-25 07:13:53 +01:00
|
|
|
childModel.Interval = 0;
|
2013-03-01 08:03:41 +01:00
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
Subject.SetFields(childModel, t => t.TypeName);
|
2013-03-01 08:03:41 +01:00
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
Db.All<ScheduledTask>().Single().TypeName.Should().Be("A");
|
2013-05-09 08:38:20 +02:00
|
|
|
Db.All<ScheduledTask>().Single().Interval.Should().Be(12);
|
2013-03-01 08:03:41 +01:00
|
|
|
}
|
2013-02-17 05:33:56 +01:00
|
|
|
}
|
|
|
|
|
2013-02-05 05:07:07 +01:00
|
|
|
}
|
|
|
|
|