2020-01-03 12:49:24 +00:00
|
|
|
using System;
|
2017-09-04 02:20:56 +00:00
|
|
|
using System.Linq;
|
2013-03-26 05:51:56 +00:00
|
|
|
using FizzWare.NBuilder;
|
|
|
|
using FluentAssertions;
|
2022-01-26 00:08:27 +00:00
|
|
|
using FluentAssertions.Equivalency;
|
2013-03-26 05:51:56 +00:00
|
|
|
using NUnit.Framework;
|
2022-01-26 00:08:27 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2021-12-25 01:55:32 +00:00
|
|
|
using NzbDrone.Core.History;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.Music;
|
2013-03-26 07:17:46 +00:00
|
|
|
using NzbDrone.Core.Qualities;
|
2013-03-26 05:51:56 +00:00
|
|
|
using NzbDrone.Core.Test.Framework;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Datastore
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2013-03-26 07:17:46 +00:00
|
|
|
public class DatabaseRelationshipFixture : DbTest
|
2013-03-26 05:51:56 +00:00
|
|
|
{
|
2022-01-26 00:08:27 +00:00
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
|
|
|
AssertionOptions.AssertEquivalencyUsing(options =>
|
|
|
|
{
|
|
|
|
options.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation.ToUniversalTime())).WhenTypeIs<DateTime>();
|
|
|
|
options.Using<DateTime?>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation.Value.ToUniversalTime())).WhenTypeIs<DateTime?>();
|
|
|
|
return options;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-05-22 05:30:54 +00:00
|
|
|
[Test]
|
|
|
|
public void one_to_one()
|
|
|
|
{
|
2019-02-16 14:49:24 +00:00
|
|
|
var album = Builder<Album>.CreateNew()
|
2018-12-15 00:02:43 +00:00
|
|
|
.With(c => c.Id = 0)
|
2019-07-09 20:46:10 +00:00
|
|
|
.With(x => x.ReleaseDate = DateTime.UtcNow)
|
|
|
|
.With(x => x.LastInfoSync = DateTime.UtcNow)
|
|
|
|
.With(x => x.Added = DateTime.UtcNow)
|
2018-12-15 00:02:43 +00:00
|
|
|
.BuildNew();
|
2019-02-16 14:49:24 +00:00
|
|
|
Db.Insert(album);
|
2013-05-22 05:30:54 +00:00
|
|
|
|
2019-02-16 14:49:24 +00:00
|
|
|
var albumRelease = Builder<AlbumRelease>.CreateNew()
|
2018-12-15 00:02:43 +00:00
|
|
|
.With(c => c.Id = 0)
|
2019-02-16 14:49:24 +00:00
|
|
|
.With(c => c.AlbumId = album.Id)
|
2013-05-22 05:30:54 +00:00
|
|
|
.BuildNew();
|
2019-02-16 14:49:24 +00:00
|
|
|
Db.Insert(albumRelease);
|
|
|
|
|
|
|
|
var loadedAlbum = Db.Single<AlbumRelease>().Album.Value;
|
|
|
|
|
|
|
|
loadedAlbum.Should().NotBeNull();
|
2022-01-26 00:08:27 +00:00
|
|
|
loadedAlbum.Should().BeEquivalentTo(album, AlbumComparerOptions);
|
2013-05-22 05:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void one_to_one_should_not_query_db_if_foreign_key_is_zero()
|
|
|
|
{
|
2017-10-18 02:55:02 +00:00
|
|
|
var track = Builder<Track>.CreateNew()
|
|
|
|
.With(c => c.TrackFileId = 0)
|
2013-05-22 05:30:54 +00:00
|
|
|
.BuildNew();
|
|
|
|
|
2017-10-18 02:55:02 +00:00
|
|
|
Db.Insert(track);
|
2013-05-22 05:30:54 +00:00
|
|
|
|
2017-10-18 02:55:02 +00:00
|
|
|
Db.Single<Track>().TrackFile.Value.Should().BeNull();
|
2013-05-22 05:30:54 +00:00
|
|
|
}
|
2013-03-26 07:17:46 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void embedded_document_as_json()
|
|
|
|
{
|
2020-01-03 12:49:24 +00:00
|
|
|
var quality = new QualityModel { Quality = Quality.MP3_320, Revision = new Revision(version: 2) };
|
2013-03-26 07:17:46 +00:00
|
|
|
|
2021-12-25 01:55:32 +00:00
|
|
|
var history = Builder<EntityHistory>.CreateNew()
|
2013-03-26 07:17:46 +00:00
|
|
|
.With(c => c.Id = 0)
|
|
|
|
.With(c => c.Quality = quality)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
Db.Insert(history);
|
|
|
|
|
2021-12-25 01:55:32 +00:00
|
|
|
var loadedQuality = Db.Single<EntityHistory>().Quality;
|
2013-03-26 07:17:46 +00:00
|
|
|
loadedQuality.Should().Be(quality);
|
2013-03-26 05:51:56 +00:00
|
|
|
}
|
2013-03-27 00:51:37 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void embedded_list_of_document_with_json()
|
|
|
|
{
|
2021-12-25 01:55:32 +00:00
|
|
|
var history = Builder<EntityHistory>.CreateListOfSize(2)
|
2013-03-27 00:51:37 +00:00
|
|
|
.All().With(c => c.Id = 0)
|
|
|
|
.Build().ToList();
|
|
|
|
|
2017-11-21 00:20:21 +00:00
|
|
|
history[0].Quality = new QualityModel(Quality.MP3_320, new Revision(version: 2));
|
|
|
|
history[1].Quality = new QualityModel(Quality.MP3_256, new Revision(version: 2));
|
2013-03-27 00:51:37 +00:00
|
|
|
|
|
|
|
Db.InsertMany(history);
|
|
|
|
|
2021-12-25 01:55:32 +00:00
|
|
|
var returnedHistory = Db.All<EntityHistory>();
|
2013-03-27 00:51:37 +00:00
|
|
|
|
2017-11-21 00:20:21 +00:00
|
|
|
returnedHistory[0].Quality.Quality.Should().Be(Quality.MP3_320);
|
2013-03-27 00:51:37 +00:00
|
|
|
}
|
2022-01-26 00:08:27 +00:00
|
|
|
|
|
|
|
private EquivalencyAssertionOptions<Album> AlbumComparerOptions(EquivalencyAssertionOptions<Album> opts) => opts.ComparingByMembers<Album>()
|
|
|
|
.Excluding(ctx => ctx.SelectedMemberInfo.MemberType.IsGenericType && ctx.SelectedMemberInfo.MemberType.GetGenericTypeDefinition() == typeof(LazyLoaded<>))
|
|
|
|
.Excluding(x => x.ArtistId);
|
2013-03-26 05:51:56 +00:00
|
|
|
}
|
|
|
|
}
|