2013-02-16 04:03:54 +00:00
|
|
|
using System;
|
2013-02-16 03:50:22 +00:00
|
|
|
using System.Collections.Generic;
|
2013-02-05 04:07:07 +00:00
|
|
|
using System.Linq;
|
|
|
|
using FizzWare.NBuilder;
|
|
|
|
using FluentAssertions;
|
|
|
|
using NUnit.Framework;
|
2013-02-13 01:25:32 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-02-05 04:07:07 +00:00
|
|
|
using NzbDrone.Core.Repository;
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Datastore
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class ObjectDatabaseFixture : ObjectDbTest
|
|
|
|
{
|
|
|
|
private Series testSeries;
|
|
|
|
private Episode testEpisode;
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
2013-02-17 01:52:40 +00:00
|
|
|
WithObjectDb(memory:false);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-06 08:40:57 +00:00
|
|
|
testSeries = Builder<Series>
|
|
|
|
.CreateNew()
|
2013-02-17 01:52:40 +00:00
|
|
|
.With(s => s.OID = 0)
|
2013-02-06 08:40:57 +00:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
testEpisode = Builder<Episode>
|
|
|
|
.CreateNew()
|
2013-02-17 01:52:40 +00:00
|
|
|
.With(e => e.OID = 0)
|
2013-02-06 08:40:57 +00:00
|
|
|
.Build();
|
2013-02-05 04:07:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_write_to_database()
|
|
|
|
{
|
|
|
|
Db.Insert(testSeries);
|
|
|
|
Db.AsQueryable<Series>().Should().HaveCount(1);
|
|
|
|
}
|
|
|
|
|
2013-02-16 04:03:54 +00:00
|
|
|
[Test]
|
|
|
|
public void double_insert_should_fail()
|
|
|
|
{
|
|
|
|
Db.Insert(testSeries);
|
|
|
|
Assert.Throws<InvalidOperationException>(() => Db.Insert(testSeries));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void update_item_with_root_index_0_should_faile()
|
|
|
|
{
|
2013-02-17 01:52:40 +00:00
|
|
|
testSeries.OID = 0;
|
2013-02-16 04:03:54 +00:00
|
|
|
Assert.Throws<InvalidOperationException>(() => Db.Update(testSeries));
|
|
|
|
}
|
|
|
|
|
2013-02-16 04:17:23 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_store_empty_list()
|
|
|
|
{
|
|
|
|
var series = new List<Series>();
|
|
|
|
|
|
|
|
Db.InsertMany(series);
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:07:07 +00:00
|
|
|
[Test]
|
|
|
|
public void should_not_store_dirty_data_in_cache()
|
|
|
|
{
|
|
|
|
Db.Insert(testEpisode);
|
|
|
|
|
|
|
|
Db.AsQueryable<Episode>().Single().Series.Should().BeNull();
|
|
|
|
|
|
|
|
testEpisode.Series = Builder<Series>.CreateNew().Build();
|
|
|
|
|
|
|
|
Db.AsQueryable<Episode>().Single().Series.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_store_nested_objects()
|
|
|
|
{
|
|
|
|
testEpisode.Series = testSeries;
|
|
|
|
|
|
|
|
Db.Insert(testEpisode);
|
|
|
|
|
|
|
|
Db.AsQueryable<Episode>().Should().HaveCount(1);
|
|
|
|
Db.AsQueryable<Episode>().Single().Series.Should().NotBeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_update_nested_objects()
|
|
|
|
{
|
2013-02-08 08:05:43 +00:00
|
|
|
testEpisode.Series = Builder<Series>
|
|
|
|
.CreateNew()
|
2013-02-17 01:52:40 +00:00
|
|
|
.With(s => s.OID = 0)
|
2013-02-08 08:05:43 +00:00
|
|
|
.Build();
|
2013-02-05 04:07:07 +00:00
|
|
|
|
|
|
|
Db.Insert(testEpisode);
|
|
|
|
|
|
|
|
testEpisode.Series.Title = "UpdatedTitle";
|
|
|
|
|
|
|
|
Db.Update(testEpisode);
|
|
|
|
|
|
|
|
Db.AsQueryable<Episode>().Should().HaveCount(1);
|
|
|
|
Db.AsQueryable<Episode>().Single().Series.Should().NotBeNull();
|
|
|
|
Db.AsQueryable<Episode>().Single().Series.Title.Should().Be("UpdatedTitle");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void new_objects_should_get_id()
|
|
|
|
{
|
2013-02-17 01:52:40 +00:00
|
|
|
testSeries.OID = 0;
|
2013-02-13 01:25:32 +00:00
|
|
|
Db.Insert(testSeries);
|
2013-02-17 01:52:40 +00:00
|
|
|
testSeries.OID.Should().NotBe(0);
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 03:50:22 +00:00
|
|
|
[Test]
|
2013-02-16 04:03:54 +00:00
|
|
|
public void new_object_should_get_new_id()
|
2013-02-16 03:50:22 +00:00
|
|
|
{
|
2013-02-17 01:52:40 +00:00
|
|
|
testSeries.OID = 0;
|
2013-02-16 03:50:22 +00:00
|
|
|
Db.Insert(testSeries);
|
|
|
|
|
|
|
|
Db.AsQueryable<Series>().Should().HaveCount(1);
|
2013-02-17 01:52:40 +00:00
|
|
|
testSeries.OID.Should().Be(1);
|
2013-02-16 03:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_assign_ids_to_nested_objects()
|
|
|
|
{
|
|
|
|
var nested = new NestedModel();
|
|
|
|
|
|
|
|
nested.List.Add(new NestedModel());
|
|
|
|
|
|
|
|
Db.Insert(nested);
|
|
|
|
|
2013-02-17 01:52:40 +00:00
|
|
|
nested.OID.Should().Be(1);
|
|
|
|
nested.List.Should().OnlyContain(c => c.OID > 0);
|
2013-02-16 03:50:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-05 04:07:07 +00:00
|
|
|
[Test]
|
2013-02-06 08:40:57 +00:00
|
|
|
public void should_have_id_when_returned_from_database()
|
|
|
|
{
|
2013-02-17 01:52:40 +00:00
|
|
|
testSeries.OID = 0;
|
2013-02-13 01:25:32 +00:00
|
|
|
Db.Insert(testSeries);
|
2013-02-06 08:40:57 +00:00
|
|
|
var item = Db.AsQueryable<Series>();
|
|
|
|
|
|
|
|
item.Should().HaveCount(1);
|
2013-02-17 01:52:40 +00:00
|
|
|
item.First().OID.Should().NotBe(0);
|
|
|
|
item.First().OID.Should().BeLessThan(100);
|
|
|
|
item.First().OID.Should().Be(testSeries.OID);
|
2013-02-06 08:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_find_object_by_id()
|
|
|
|
{
|
2013-02-13 01:25:32 +00:00
|
|
|
Db.Insert(testSeries);
|
2013-02-17 01:52:40 +00:00
|
|
|
var item = Db.AsQueryable<Series>().Single(c => c.OID == testSeries.OID);
|
2013-02-06 08:40:57 +00:00
|
|
|
|
2013-02-17 01:52:40 +00:00
|
|
|
item.OID.Should().NotBe(0);
|
|
|
|
item.OID.Should().Be(testSeries.OID);
|
2013-02-06 08:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_read_unknown_type()
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
|
|
|
Db.AsQueryable<UnknownType>().ToList().Should().BeEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 01:25:32 +00:00
|
|
|
public class UnknownType : BaseRepositoryModel
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
|
|
|
public string Field1 { get; set; }
|
|
|
|
}
|
2013-02-16 03:50:22 +00:00
|
|
|
|
|
|
|
public class NestedModel : BaseRepositoryModel
|
|
|
|
{
|
|
|
|
public NestedModel()
|
|
|
|
{
|
|
|
|
List = new List<NestedModel> { this };
|
|
|
|
}
|
|
|
|
|
2013-02-17 01:52:40 +00:00
|
|
|
public List<NestedModel> List { get; set; }
|
2013-02-16 03:50:22 +00:00
|
|
|
}
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|