Example #1
0
        public void SaveManyMList()
        {
            using (var tr = new Transaction())
                using (OperationLogic.AllowSave <LabelEntity>())
                    using (OperationLogic.AllowSave <CountryEntity>())
                        using (OperationLogic.AllowSave <AlbumEntity>())
                            using (OperationLogic.AllowSave <ArtistEntity>())
                            {
                                var prev = Database.MListQuery((AlbumEntity a) => a.Songs).Count();

                                var authors =
                                    Database.Query <BandEntity>().Take(6).ToList().Concat <IAuthorEntity>(
                                        Database.Query <ArtistEntity>().Take(8).ToList()).ToList();

                                var label = new LabelEntity {
                                    Name = "Four Music", Country = new CountryEntity {
                                        Name = "Germany"
                                    }, Node = MusicLoader.NextLabelNode()
                                };

                                List <AlbumEntity> albums = 0.To(16).Select(i => new AlbumEntity()
                                {
                                    Name   = "System Greatest hits {0}".FormatWith(i),
                                    Author = i < authors.Count ? authors[i] : new ArtistEntity {
                                        Name = ".Net Framework"
                                    },
                                    Year  = 2001,
                                    Songs = { new SongEmbedded {
                                                  Name = "Compilation {0}".FormatWith(i)
                                              } },
                                    State = AlbumState.Saved,
                                    Label = label,
                                }).ToList();

                                albums.SaveList();

                                Assert.All(GraphExplorer.FromRoots(albums), a => Assert.False(a.IsGraphModified));

                                Assert.Equal(prev + 16, Database.MListQuery((AlbumEntity a) => a.Songs).Count());

                                albums.ForEach(a => a.Name += "Updated");

                                albums.SaveList();

                                albums.ForEach(a => a.Songs.ForEach(s => s.Name = "Updated"));

                                albums.SaveList();

                                //tr.Commit();
                            }
        }
Example #2
0
        public void SaveMList()
        {
            using (var tr = new Transaction())
                using (OperationLogic.AllowSave <LabelEntity>())
                    using (OperationLogic.AllowSave <CountryEntity>())
                        using (OperationLogic.AllowSave <AlbumEntity>())
                            using (OperationLogic.AllowSave <ArtistEntity>())
                            {
                                var prev = Database.MListQuery((AlbumEntity a) => a.Songs).Count();

                                Type[] types = typeof(int).Assembly.GetTypes().Where(a => a.Name.Length > 3 && a.Name.StartsWith("A")).ToArray();

                                AlbumEntity album = new AlbumEntity()
                                {
                                    Name   = "System Greatest hits",
                                    Author = new ArtistEntity {
                                        Name = ".Net Framework"
                                    },
                                    Year  = 2001,
                                    Songs = types.Select(t => new SongEmbedded()
                                    {
                                        Name = t.Name
                                    }).ToMList(),
                                    State = AlbumState.Saved,
                                    Label = new LabelEntity {
                                        Name = "Four Music", Country = new CountryEntity {
                                            Name = "Germany"
                                        }, Node = MusicLoader.NextLabelNode()
                                    },
                                }.Save();

                                Assert.All(GraphExplorer.FromRoot(album), a => Assert.False(a.IsGraphModified));

                                Assert.Equal(prev + types.Length, Database.MListQuery((AlbumEntity a) => a.Songs).Count());

                                album.Name += "Updated";

                                album.Save();

                                album.Songs.ForEach(a => a.Name = "Updated");

                                album.Save();

                                //tr.Commit();
                            }
        }
Example #3
0
        public void SmartSaveMListOrder()
        {
            using (var tr = new Transaction())
                using (OperationLogic.AllowSave <LabelEntity>())
                    using (OperationLogic.AllowSave <CountryEntity>())
                        using (OperationLogic.AllowSave <AlbumEntity>())
                            using (OperationLogic.AllowSave <ArtistEntity>())
                            {
                                var artist = Database.Query <ArtistEntity>().First();

                                var album = new AlbumEntity
                                {
                                    Name   = "Test album",
                                    Author = artist,
                                    Year   = 2000,
                                    Songs  = { new SongEmbedded {
                                                   Name = "Song 0"
                                               }, new SongEmbedded {
                                                   Name = "Song 1"
                                               }, new SongEmbedded {
                                                   Name = "Song 2"
                                               }, },
                                    State = AlbumState.Saved,
                                    Label = new LabelEntity {
                                        Name = "Four Music", Country = new CountryEntity {
                                            Name = "Germany"
                                        }, Node = MusicLoader.NextLabelNode()
                                    },
                                };

                                album.Save();

                                AssertSequenceEquals(album.MListElements(a => a.Songs).OrderBy(a => a.Order).Select(mle => KeyValuePair.Create(mle.Order, mle.Element.Name)),
                                                     new Dictionary <int, string> {
                                    { 0, "Song 0" }, { 1, "Song 1" }, { 2, "Song 2" }
                                });

                                var ids = album.MListElements(a => a.Songs).Select(a => a.RowId).ToHashSet();

                                album.Songs.SortDescending(a => a.Name);

                                album.Save();

                                var ids2 = album.MListElements(a => a.Songs).Select(a => a.RowId).ToHashSet();

                                AssertSequenceEquals(ids.OrderBy(), ids2.OrderBy());


                                AssertSequenceEquals(album.MListElements(a => a.Songs).OrderBy(a => a.Order).Select(mle => KeyValuePair.Create(mle.Order, mle.Element.Name)),
                                                     new Dictionary <int, string> {
                                    { 0, "Song 2" }, { 1, "Song 1" }, { 2, "Song 0" }
                                });


                                var s3 = album.Songs[0];

                                album.Songs.RemoveAt(0);

                                album.Songs.Insert(1, s3);

                                album.Save();

                                AssertSequenceEquals(album.MListElements(a => a.Songs).OrderBy(a => a.Order).Select(mle => KeyValuePair.Create(mle.Order, mle.Element.Name)),
                                                     new Dictionary <int, string> {
                                    { 0, "Song 1" }, { 1, "Song 2" }, { 2, "Song 0" }
                                });

                                AssertSequenceEquals(album.ToLite().RetrieveAndRemember().Songs.Select(a => a.Name), new[] { "Song 1", "Song 2", "Song 0" });

                                //tr.Commit();
                            }
        }
Example #4
0
        public void SmartSaveMList()
        {
            using (var tr = new Transaction())
                using (OperationLogic.AllowSave <LabelEntity>())
                    using (OperationLogic.AllowSave <CountryEntity>())
                        using (OperationLogic.AllowSave <AlbumEntity>())
                            using (OperationLogic.AllowSave <ArtistEntity>())
                            {
                                var maxRowId = Database.MListQuery((AlbumEntity a) => a.Songs).Max(a => a.RowId);

                                var artist = Database.Query <ArtistEntity>().First();

                                var album = new AlbumEntity
                                {
                                    Name   = "Test album",
                                    Author = artist,
                                    Year   = 2000,
                                    Songs  = { new SongEmbedded {
                                                   Name = "Song 1"
                                               } },
                                    State = AlbumState.Saved,
                                    Label = new LabelEntity {
                                        Name = "Four Music", Country = new CountryEntity {
                                            Name = "Germany"
                                        }, Node = MusicLoader.NextLabelNode()
                                    },
                                };

                                var innerList = ((IMListPrivate <SongEmbedded>)album.Songs).InnerList;

                                Assert.Null(innerList[0].RowId);
                                //Insert and row-id is set
                                album.Save();
                                Assert.NotNull(innerList[0].RowId);
                                Assert.True(innerList[0].RowId > maxRowId);


                                album.Songs.Add(new SongEmbedded {
                                    Name = "Song 2"
                                });

                                Assert.Null(innerList[1].RowId);

                                album.Save();
                                //Insert and row-id is set
                                Assert.NotNull(innerList[1].RowId);

                                var song = innerList[0];

                                album.Songs.Remove(song.Element);
                                //Delete
                                album.Save();

                                {
                                    var album2 = album.ToLite().RetrieveAndRemember();

                                    Assert.True(album.Songs.Count == album2.Songs.Count);
                                    Assert.True(innerList[0].RowId == ((IMListPrivate <SongEmbedded>)album2.Songs).InnerList[0].RowId);
                                    Assert.True(!album.MListElements(a => a.Songs).Any(mle => mle.RowId == song.RowId));
                                }

                                album.Songs[0].Name += "*";
                                //Update
                                album.Save();

                                {
                                    var album2 = album.ToLite().RetrieveAndRemember();

                                    Assert.True(album.Songs.Count == album2.Songs.Count);
                                    Assert.True(innerList[0].RowId == ((IMListPrivate <SongEmbedded>)album2.Songs).InnerList[0].RowId);
                                    Assert.True(album.Songs[0].Name == album2.Songs[0].Name);
                                    Assert.True(!album.MListElements(a => a.Songs).Any(mle => mle.RowId == song.RowId));
                                }

                                //tr.Commit();
                            }
        }