2013-02-15 20:03:54 -08:00
|
|
|
using System;
|
2013-02-15 19:50:22 -08:00
|
|
|
using System.Collections.Generic;
|
2013-03-24 12:56:51 -07:00
|
|
|
using System.Data;
|
2013-02-04 20:07:07 -08:00
|
|
|
using System.Linq;
|
|
|
|
using FizzWare.NBuilder;
|
|
|
|
using FluentAssertions;
|
|
|
|
using NUnit.Framework;
|
2013-02-12 17:25:32 -08:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-03-24 23:13:53 -07:00
|
|
|
using NzbDrone.Core.Jobs;
|
2013-02-04 20:07:07 -08:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Datastore
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2013-03-24 23:13:53 -07:00
|
|
|
public class ObjectDatabaseFixture : DbTest<BasicRepository<JobDefinition>, JobDefinition>
|
2013-02-04 20:07:07 -08:00
|
|
|
{
|
2013-03-24 23:13:53 -07:00
|
|
|
private JobDefinition _sampleType;
|
2013-02-04 20:07:07 -08:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
2013-03-24 23:13:53 -07:00
|
|
|
_sampleType = Builder<JobDefinition>
|
2013-02-06 00:40:57 -08:00
|
|
|
.CreateNew()
|
2013-02-25 19:58:57 -08:00
|
|
|
.With(s => s.Id = 0)
|
2013-02-06 00:40:57 -08:00
|
|
|
.Build();
|
2013-03-24 12:56:51 -07:00
|
|
|
|
2013-02-04 20:07:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_write_to_database()
|
|
|
|
{
|
2013-03-23 21:16:00 -07:00
|
|
|
Subject.Insert(_sampleType);
|
2013-03-24 23:13:53 -07:00
|
|
|
Db.All<JobDefinition>().Should().HaveCount(1);
|
2013-02-04 20:07:07 -08:00
|
|
|
}
|
|
|
|
|
2013-02-15 20:03:54 -08:00
|
|
|
[Test]
|
|
|
|
public void double_insert_should_fail()
|
|
|
|
{
|
2013-03-23 21:16:00 -07:00
|
|
|
Subject.Insert(_sampleType);
|
|
|
|
Assert.Throws<InvalidOperationException>(() => Subject.Insert(_sampleType));
|
2013-02-15 20:03:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void update_item_with_root_index_0_should_faile()
|
|
|
|
{
|
2013-03-23 21:16:00 -07:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Assert.Throws<InvalidOperationException>(() => Subject.Update(_sampleType));
|
2013-02-15 20:03:54 -08:00
|
|
|
}
|
|
|
|
|
2013-02-15 20:17:23 -08:00
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_store_empty_list()
|
|
|
|
{
|
2013-03-24 23:13:53 -07:00
|
|
|
var series = new List<JobDefinition>();
|
2013-02-04 20:07:07 -08:00
|
|
|
|
2013-03-23 21:16:00 -07:00
|
|
|
Subject.InsertMany(series);
|
2013-02-04 20:07:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void new_objects_should_get_id()
|
|
|
|
{
|
2013-03-23 21:16:00 -07:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Subject.Insert(_sampleType);
|
|
|
|
_sampleType.Id.Should().NotBe(0);
|
2013-02-04 20:07:07 -08:00
|
|
|
}
|
|
|
|
|
2013-02-15 19:50:22 -08:00
|
|
|
[Test]
|
2013-02-15 20:03:54 -08:00
|
|
|
public void new_object_should_get_new_id()
|
2013-02-15 19:50:22 -08:00
|
|
|
{
|
2013-03-23 21:16:00 -07:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Subject.Insert(_sampleType);
|
2013-02-15 19:50:22 -08:00
|
|
|
|
2013-03-24 23:13:53 -07:00
|
|
|
Db.All<JobDefinition>().Should().HaveCount(1);
|
2013-03-23 21:16:00 -07:00
|
|
|
_sampleType.Id.Should().Be(1);
|
2013-02-15 19:50:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-04 20:07:07 -08:00
|
|
|
[Test]
|
2013-02-06 00:40:57 -08:00
|
|
|
public void should_have_id_when_returned_from_database()
|
|
|
|
{
|
2013-03-23 21:16:00 -07:00
|
|
|
_sampleType.Id = 0;
|
|
|
|
Subject.Insert(_sampleType);
|
2013-03-24 23:13:53 -07:00
|
|
|
var item = Db.All<JobDefinition>();
|
2013-02-06 00:40:57 -08:00
|
|
|
|
|
|
|
item.Should().HaveCount(1);
|
2013-02-25 19:58:57 -08:00
|
|
|
item.First().Id.Should().NotBe(0);
|
|
|
|
item.First().Id.Should().BeLessThan(100);
|
2013-03-23 21:16:00 -07:00
|
|
|
item.First().Id.Should().Be(_sampleType.Id);
|
2013-02-06 00:40:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_find_object_by_id()
|
|
|
|
{
|
2013-03-23 21:16:00 -07:00
|
|
|
Subject.Insert(_sampleType);
|
2013-03-24 23:13:53 -07:00
|
|
|
var item = Db.All<JobDefinition>().Single(c => c.Id == _sampleType.Id);
|
2013-02-06 00:40:57 -08:00
|
|
|
|
2013-02-25 19:58:57 -08:00
|
|
|
item.Id.Should().NotBe(0);
|
2013-03-23 21:16:00 -07:00
|
|
|
item.Id.Should().Be(_sampleType.Id);
|
2013-02-26 23:25:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-28 23:03:41 -08:00
|
|
|
|
|
|
|
[Test]
|
2013-03-26 23:16:55 -07:00
|
|
|
public void set_fields_should_only_update_selected_filed()
|
2013-02-28 23:03:41 -08:00
|
|
|
{
|
2013-03-24 23:13:53 -07:00
|
|
|
var childModel = new JobDefinition
|
2013-02-28 23:03:41 -08:00
|
|
|
{
|
2013-03-24 23:13:53 -07:00
|
|
|
Type = "Address",
|
2013-03-24 12:56:51 -07:00
|
|
|
Name = "Name",
|
2013-03-24 23:13:53 -07:00
|
|
|
Interval = 12
|
2013-02-28 23:03:41 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-03-23 21:16:00 -07:00
|
|
|
Subject.Insert(childModel);
|
2013-02-28 23:03:41 -08:00
|
|
|
|
2013-03-24 23:13:53 -07:00
|
|
|
childModel.Type = "A";
|
2013-03-23 21:16:00 -07:00
|
|
|
childModel.Name = "B";
|
2013-03-24 23:13:53 -07:00
|
|
|
childModel.Interval = 0;
|
2013-02-28 23:03:41 -08:00
|
|
|
|
2013-03-26 23:16:55 -07:00
|
|
|
Subject.SetFields(childModel, t => t.Name);
|
2013-02-28 23:03:41 -08:00
|
|
|
|
2013-03-24 23:13:53 -07:00
|
|
|
Db.All<JobDefinition>().Single().Type.Should().Be("Address");
|
|
|
|
Db.All<JobDefinition>().Single().Name.Should().Be("B");
|
|
|
|
Db.All<JobDefinition>().Single().Interval.Should().Be(12);
|
2013-02-28 23:03:41 -08:00
|
|
|
}
|
2013-02-16 20:33:56 -08:00
|
|
|
}
|
|
|
|
|
2013-02-04 20:07:07 -08:00
|
|
|
}
|
|
|
|
|