From cd6f0fc55cd5da85e19c248039afd9621c27354f Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 24 Mar 2013 12:56:51 -0700 Subject: [PATCH] fixed more tests. --- .../Datastore/BasicRepositoryFixture.cs | 3 +++ .../Datastore/ObjectDatabaseFixture.cs | 13 ++++++++---- NzbDrone.Core/Configuration/Config.cs | 5 +++-- NzbDrone.Core/Datastore/BasicRepository.cs | 20 ++++++++++++++++++- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs b/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs index 5970c9b46..a4e89b8fa 100644 --- a/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs +++ b/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs @@ -1,10 +1,12 @@ using System; +using System.Data; using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.Datastore; using NzbDrone.Core.Test.Framework; +using ServiceStack.OrmLite; namespace NzbDrone.Core.Test.Datastore { @@ -29,6 +31,7 @@ namespace NzbDrone.Core.Test.Datastore .With(c => c.Id = 0) .Build(); + Mocker.Resolve().CreateTable(); } [Test] diff --git a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index e0084131b..42ca25dac 100644 --- a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using NUnit.Framework; using NzbDrone.Core.Datastore; using NzbDrone.Core.Test.Framework; +using ServiceStack.OrmLite; namespace NzbDrone.Core.Test.Datastore { @@ -21,6 +23,9 @@ namespace NzbDrone.Core.Test.Datastore .CreateNew() .With(s => s.Id = 0) .Build(); + + Mocker.Resolve().CreateTable(); + } [Test] @@ -102,9 +107,9 @@ namespace NzbDrone.Core.Test.Datastore { var childModel = new BaiscType { - Address = "Address", - Name = "Name", - Tilte = "Title" + Address = "Address", + Name = "Name", + Tilte = "Title" }; @@ -114,7 +119,7 @@ namespace NzbDrone.Core.Test.Datastore childModel.Name = "B"; childModel.Tilte = "C"; - Subject.UpdateFields(childModel, t=>t.Name); + Subject.UpdateFields(childModel, t => t.Name); Db.All().Single().Address.Should().Be("Address"); Db.All().Single().Name.Should().Be("B"); diff --git a/NzbDrone.Core/Configuration/Config.cs b/NzbDrone.Core/Configuration/Config.cs index c8e569fb0..bda68cb76 100644 --- a/NzbDrone.Core/Configuration/Config.cs +++ b/NzbDrone.Core/Configuration/Config.cs @@ -1,12 +1,13 @@ -using System.Linq; using NzbDrone.Core.Datastore; +using ServiceStack.DataAnnotations; namespace NzbDrone.Core.Configuration { public class Config : ModelBase { + [Index(Unique = true)] public string Key { get; set; } - + public string Value { get; set; } } } \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index d002587ef..968b59920 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -57,7 +57,15 @@ namespace NzbDrone.Core.Datastore public TModel Get(int id) { - return _database.GetById(id); + try + { + return _database.GetById(id); + } + catch (ArgumentNullException e) + { + throw new InvalidOperationException(e.Message); + } + } public TModel Single(Expression> predicate) @@ -87,6 +95,11 @@ namespace NzbDrone.Core.Datastore public TModel Insert(TModel model) { + if (model.Id != 0) + { + throw new InvalidOperationException("Can't insert model with existing ID"); + } + _database.Insert(model); model.Id = (int)_database.GetLastInsertId(); return model; @@ -94,6 +107,11 @@ namespace NzbDrone.Core.Datastore public TModel Update(TModel model) { + if (model.Id == 0) + { + throw new InvalidOperationException("Can't update model with ID 0"); + } + _database.Update(model); return model; }