1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs

197 lines
5.0 KiB
C#
Raw Normal View History

2013-02-16 07:03:54 +03:00
using System;
using System.Collections.Generic;
2013-02-05 07:07:07 +03:00
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
2013-02-13 04:25:32 +03:00
using NzbDrone.Core.Datastore;
2013-02-05 07:07:07 +03:00
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore
{
[TestFixture]
public class ObjectDatabaseFixture : ObjectDbTest
{
private ChildModel childModel;
private ParentModel ParentModel;
2013-02-05 07:07:07 +03:00
[SetUp]
public void SetUp()
{
childModel = Builder<ChildModel>
.CreateNew()
.With(s => s.Id = 0)
.Build();
ParentModel = Builder<ParentModel>
.CreateNew()
.With(e => e.Id = 0)
.Build();
2013-02-05 07:07:07 +03:00
}
[Test]
public void should_be_able_to_write_to_database()
{
Db.Insert(childModel);
Db.AsQueryable<ChildModel>().Should().HaveCount(1);
2013-02-05 07:07:07 +03:00
}
2013-02-16 07:03:54 +03:00
[Test]
public void double_insert_should_fail()
{
Db.Insert(childModel);
Assert.Throws<InvalidOperationException>(() => Db.Insert(childModel));
2013-02-16 07:03:54 +03:00
}
[Test]
public void update_item_with_root_index_0_should_faile()
{
childModel.Id = 0;
Assert.Throws<InvalidOperationException>(() => Db.Update(childModel));
2013-02-16 07:03:54 +03:00
}
2013-02-16 07:17:23 +03:00
[Test]
public void should_be_able_to_store_empty_list()
{
var series = new List<ParentModel>();
2013-02-16 07:17:23 +03:00
Db.InsertMany(series);
}
2013-02-05 07:07:07 +03:00
[Test]
public void should_not_store_dirty_data_in_cache()
{
Db.Insert(ParentModel);
2013-02-05 07:07:07 +03:00
Db.AsQueryable<ParentModel>().Single().Child.Should().BeNull();
2013-02-05 07:07:07 +03:00
ParentModel.Child = Builder<ChildModel>.CreateNew().Build();
2013-02-05 07:07:07 +03:00
Db.AsQueryable<ParentModel>().Single().Child.Should().BeNull();
2013-02-05 07:07:07 +03:00
}
[Test]
public void should_store_nested_objects()
{
ParentModel.Child = childModel;
2013-02-05 07:07:07 +03:00
Db.Insert(ParentModel);
2013-02-05 07:07:07 +03:00
Db.AsQueryable<ParentModel>().Should().HaveCount(1);
Db.AsQueryable<ParentModel>().Single().Child.Should().NotBeNull();
2013-02-05 07:07:07 +03:00
}
[Test]
public void should_update_nested_objects()
{
ParentModel.Child = Builder<ChildModel>
.CreateNew()
.With(s => s.Id = 0)
.Build();
2013-02-05 07:07:07 +03:00
Db.Insert(ParentModel);
2013-02-05 07:07:07 +03:00
ParentModel.Child.A = "UpdatedTitle";
2013-02-05 07:07:07 +03:00
Db.Update(ParentModel);
2013-02-05 07:07:07 +03:00
Db.AsQueryable<ParentModel>().Should().HaveCount(1);
Db.AsQueryable<ParentModel>().Single().Child.Should().NotBeNull();
Db.AsQueryable<ParentModel>().Single().Child.A.Should().Be("UpdatedTitle");
2013-02-05 07:07:07 +03:00
}
[Test]
public void new_objects_should_get_id()
{
childModel.Id = 0;
Db.Insert(childModel);
childModel.Id.Should().NotBe(0);
2013-02-05 07:07:07 +03:00
}
[Test]
2013-02-16 07:03:54 +03:00
public void new_object_should_get_new_id()
{
childModel.Id = 0;
Db.Insert(childModel);
Db.AsQueryable<ChildModel>().Should().HaveCount(1);
childModel.Id.Should().Be(1);
}
[Test]
public void should_be_able_to_assign_ids_to_nested_objects()
{
var nested = new NestedModel();
nested.List.Add(new NestedModel());
Db.Insert(nested);
nested.Id.Should().Be(1);
nested.List.Should().OnlyContain(c => c.Id > 0);
}
2013-02-05 07:07:07 +03:00
[Test]
public void should_have_id_when_returned_from_database()
{
childModel.Id = 0;
Db.Insert(childModel);
var item = Db.AsQueryable<ChildModel>();
item.Should().HaveCount(1);
item.First().Id.Should().NotBe(0);
item.First().Id.Should().BeLessThan(100);
item.First().Id.Should().Be(childModel.Id);
}
[Test]
public void should_be_able_to_find_object_by_id()
{
Db.Insert(childModel);
var item = Db.AsQueryable<ChildModel>().Single(c => c.Id == childModel.Id);
item.Id.Should().NotBe(0);
item.Id.Should().Be(childModel.Id);
}
[Test]
public void should_be_able_to_read_unknown_type()
2013-02-05 07:07:07 +03:00
{
Db.AsQueryable<UnknownType>().ToList().Should().BeEmpty();
}
}
public class UnknownType : ModelBase
2013-02-05 07:07:07 +03:00
{
public string Field1 { get; set; }
}
public class NestedModel : ModelBase
{
public NestedModel()
{
List = new List<NestedModel> { this };
}
2013-02-17 04:52:40 +03:00
public List<NestedModel> List { get; set; }
}
public class ParentModel : ModelBase
{
public ChildModel Child { get; set; }
}
public class ChildModel : ModelBase
{
public String A { get; set; }
public int B { get; set; }
public int C { get; set; }
}
2013-02-05 07:07:07 +03:00
}