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.Test.Framework;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Datastore
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class ObjectDatabaseFixture : ObjectDbTest
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
private ChildModel _childModel;
|
|
|
|
private ParentModel _parentModel;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
_childModel = Builder<ChildModel>
|
2013-02-06 08:40:57 +00:00
|
|
|
.CreateNew()
|
2013-02-26 03:58:57 +00:00
|
|
|
.With(s => s.Id = 0)
|
2013-02-06 08:40:57 +00:00
|
|
|
.Build();
|
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
_parentModel = Builder<ParentModel>
|
2013-02-06 08:40:57 +00:00
|
|
|
.CreateNew()
|
2013-02-26 03:58:57 +00:00
|
|
|
.With(e => e.Id = 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()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Insert(_childModel);
|
2013-02-17 04:33:56 +00:00
|
|
|
Db.AsQueryable<ChildModel>().Should().HaveCount(1);
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 04:03:54 +00:00
|
|
|
[Test]
|
|
|
|
public void double_insert_should_fail()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Insert(_childModel);
|
|
|
|
Assert.Throws<InvalidOperationException>(() => Db.Insert(_childModel));
|
2013-02-16 04:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void update_item_with_root_index_0_should_faile()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
_childModel.Id = 0;
|
|
|
|
Assert.Throws<InvalidOperationException>(() => Db.Update(_childModel));
|
2013-02-16 04:03:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 04:17:23 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_store_empty_list()
|
|
|
|
{
|
2013-02-17 04:33:56 +00:00
|
|
|
var series = new List<ParentModel>();
|
2013-02-16 04:17:23 +00:00
|
|
|
|
|
|
|
Db.InsertMany(series);
|
|
|
|
}
|
|
|
|
|
2013-02-05 04:07:07 +00:00
|
|
|
[Test]
|
|
|
|
public void should_not_store_dirty_data_in_cache()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Insert(_parentModel);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-17 04:33:56 +00:00
|
|
|
Db.AsQueryable<ParentModel>().Single().Child.Should().BeNull();
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
_parentModel.Child = Builder<ChildModel>.CreateNew().Build();
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-17 04:33:56 +00:00
|
|
|
Db.AsQueryable<ParentModel>().Single().Child.Should().BeNull();
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_store_nested_objects()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
_parentModel.Child = _childModel;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Insert(_parentModel);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-17 04:33:56 +00:00
|
|
|
Db.AsQueryable<ParentModel>().Should().HaveCount(1);
|
|
|
|
Db.AsQueryable<ParentModel>().Single().Child.Should().NotBeNull();
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_update_nested_objects()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
_parentModel.Child = Builder<ChildModel>
|
2013-02-08 08:05:43 +00:00
|
|
|
.CreateNew()
|
2013-02-26 03:58:57 +00:00
|
|
|
.With(s => s.Id = 0)
|
2013-02-08 08:05:43 +00:00
|
|
|
.Build();
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Insert(_parentModel);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
_parentModel.Child.A = "UpdatedTitle";
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Update(_parentModel);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
2013-02-17 04:33:56 +00: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 04:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void new_objects_should_get_id()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
_childModel.Id = 0;
|
|
|
|
Db.Insert(_childModel);
|
|
|
|
_childModel.Id.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-27 07:25:56 +00:00
|
|
|
_childModel.Id = 0;
|
|
|
|
Db.Insert(_childModel);
|
2013-02-16 03:50:22 +00:00
|
|
|
|
2013-02-17 04:33:56 +00:00
|
|
|
Db.AsQueryable<ChildModel>().Should().HaveCount(1);
|
2013-02-27 07:25:56 +00:00
|
|
|
_childModel.Id.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-26 03:58:57 +00:00
|
|
|
nested.Id.Should().Be(1);
|
|
|
|
nested.List.Should().OnlyContain(c => c.Id > 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-27 07:25:56 +00:00
|
|
|
_childModel.Id = 0;
|
|
|
|
Db.Insert(_childModel);
|
2013-02-17 04:33:56 +00:00
|
|
|
var item = Db.AsQueryable<ChildModel>();
|
2013-02-06 08:40:57 +00:00
|
|
|
|
|
|
|
item.Should().HaveCount(1);
|
2013-02-26 03:58:57 +00:00
|
|
|
item.First().Id.Should().NotBe(0);
|
|
|
|
item.First().Id.Should().BeLessThan(100);
|
2013-02-27 07:25:56 +00:00
|
|
|
item.First().Id.Should().Be(_childModel.Id);
|
2013-02-06 08:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_be_able_to_find_object_by_id()
|
|
|
|
{
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Insert(_childModel);
|
|
|
|
var item = Db.AsQueryable<ChildModel>().Single(c => c.Id == _childModel.Id);
|
2013-02-06 08:40:57 +00:00
|
|
|
|
2013-02-26 03:58:57 +00:00
|
|
|
item.Id.Should().NotBe(0);
|
2013-02-27 07:25:56 +00:00
|
|
|
item.Id.Should().Be(_childModel.Id);
|
2013-02-06 08:40:57 +00:00
|
|
|
}
|
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
[Test]
|
|
|
|
public void deleting_child_model_directly_should_set_link_to_null()
|
|
|
|
{
|
2013-03-01 07:03:41 +00:00
|
|
|
_parentModel.Child = _childModel;
|
|
|
|
|
2013-02-27 07:25:56 +00:00
|
|
|
Db.Insert(_childModel);
|
|
|
|
Db.Insert(_parentModel);
|
|
|
|
|
|
|
|
Db.AsQueryable<ParentModel>().Single().Child.Should().NotBeNull();
|
|
|
|
|
|
|
|
Db.Delete(_childModel);
|
|
|
|
|
|
|
|
Db.AsQueryable<ParentModel>().Single().Child.Should().BeNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void deleting_child_model_directly_should_remove_item_from_child_list()
|
|
|
|
{
|
|
|
|
|
|
|
|
var children = Builder<ChildModel>.CreateListOfSize(5)
|
|
|
|
.All()
|
|
|
|
.With(c => c.Id = 0)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
_parentModel.ChildList = children.ToList();
|
|
|
|
|
|
|
|
Db.Insert(_parentModel);
|
|
|
|
|
|
|
|
Db.AsQueryable<ParentModel>().Single().ChildList.Should().HaveSameCount(children);
|
|
|
|
|
|
|
|
Db.Delete(children[1]);
|
|
|
|
|
|
|
|
Db.AsQueryable<ParentModel>().Single().ChildList.Should().HaveCount(4);
|
|
|
|
}
|
2013-03-01 07:03:41 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void update_field_should_only_update_that_filed()
|
|
|
|
{
|
|
|
|
var childModel = new ChildModel
|
|
|
|
{
|
|
|
|
A = "A_Original",
|
|
|
|
B = 1,
|
|
|
|
C = 1
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Db.Insert(childModel);
|
|
|
|
|
2013-03-05 01:47:51 +00:00
|
|
|
childModel.A = "A_New";
|
|
|
|
childModel.B = 2;
|
|
|
|
childModel.C = 2;
|
2013-03-01 07:03:41 +00:00
|
|
|
|
|
|
|
Db.UpdateField(childModel, "B");
|
|
|
|
|
|
|
|
Db.AsQueryable<ChildModel>().Single().A.Should().Be("A_Original");
|
|
|
|
Db.AsQueryable<ChildModel>().Single().B.Should().Be(2);
|
|
|
|
Db.AsQueryable<ChildModel>().Single().C.Should().Be(1);
|
|
|
|
}
|
2013-02-27 07:25:56 +00:00
|
|
|
|
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-18 03:18:25 +00:00
|
|
|
public class UnknownType : ModelBase
|
2013-02-05 04:07:07 +00:00
|
|
|
{
|
|
|
|
public string Field1 { get; set; }
|
|
|
|
}
|
2013-02-16 03:50:22 +00:00
|
|
|
|
2013-02-18 03:18:25 +00:00
|
|
|
public class NestedModel : ModelBase
|
2013-02-16 03:50:22 +00:00
|
|
|
{
|
|
|
|
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-17 04:33:56 +00:00
|
|
|
|
2013-02-18 03:18:25 +00:00
|
|
|
public class ParentModel : ModelBase
|
2013-02-17 04:33:56 +00:00
|
|
|
{
|
|
|
|
public ChildModel Child { get; set; }
|
2013-02-27 07:25:56 +00:00
|
|
|
public List<ChildModel> ChildList { get; set; }
|
2013-02-17 04:33:56 +00:00
|
|
|
}
|
|
|
|
|
2013-02-18 03:18:25 +00:00
|
|
|
public class ChildModel : ModelBase
|
2013-02-17 04:33:56 +00:00
|
|
|
{
|
|
|
|
public String A { get; set; }
|
|
|
|
public int B { get; set; }
|
|
|
|
public int C { get; set; }
|
|
|
|
}
|
2013-02-05 04:07:07 +00:00
|
|
|
}
|
|
|
|
|