public void Seed(IBiblioRepository repository, int count) { if (repository == null) { throw new ArgumentNullException(nameof(repository)); } DataPage <Author> authorsPage = repository.GetAuthors(new AuthorFilter { PageNumber = 1, PageSize = 20 }); DataPage <Keyword> keywordsPage = repository.GetKeywords(new KeywordFilter { PageNumber = 1, PageSize = 20 }); Faker faker = new Faker(); for (int n = 1; n <= count; n++) { Container container = new Faker <Container>() .RuleFor(c => c.Id, Guid.NewGuid()) .RuleFor(c => c.Type, f => f.PickRandom(WorkTypeSeeder.TypeIds)) .RuleFor(c => c.Title, f => f.Random.Words(3)) .RuleFor(c => c.Language, "eng") .RuleFor(c => c.Edition, f => f.Random.Short(0, 3)) .RuleFor(c => c.Publisher, f => f.Company.CompanyName()) .RuleFor(c => c.YearPub, f => (short)f.Random.Number(1900, DateTime.Now.Year)) .RuleFor(c => c.PlacePub, f => f.Address.City()) .RuleFor(c => c.Location, f => f.Random.Bool()? f.Internet.Url() : null) .RuleFor(c => c.AccessDate, f => f.Random.Bool(0.2f) ? (DateTime?)f.Date.Recent() : null) .RuleFor(c => c.Number, f => $"{f.Random.Number(0, 100)}") .RuleFor(c => c.Note, f => f.Random.Bool(0.2f) ? f.Lorem.Sentence() : null) .Generate(); container.Key = WorkKeyBuilder.Build(container); // authors container.Authors.Add(new WorkAuthor { Id = faker.PickRandom(authorsPage.Items).Id, Role = "editor" }); // keywords for (int k = 1; k <= faker.Random.Number(1, 2); k++) { container.Keywords.Add(faker.PickRandom(keywordsPage.Items)); } repository.AddContainer(container); } }
public void PickKey_NewNullVsNonManual_New() { Work work = GetSampleWork(); string key = WorkKeyBuilder.PickKey("old", work); Assert.Equal("Doe 2020", key); }
public void PickKey_NewNullVsManual_Old() { Work work = GetSampleWork(); string key = WorkKeyBuilder.PickKey("!old", work); Assert.Equal("!old", key); }
public void PickKey_NewManual_New() { Work work = GetSampleWork(); work.Key = "!new"; string key = WorkKeyBuilder.PickKey("old", work); Assert.Equal("!new", key); }
public void Build_SingleAuthor_Ok() { string key = WorkKeyBuilder.Build(new Work { Authors = new List <WorkAuthor>(new[] { new WorkAuthor { First = "John", Last = "Doe" } }), YearPub = 2020 }); Assert.Equal("Doe 2020", key); }
public void Build_SingleContainer_Ok() { string key = WorkKeyBuilder.Build(new Container { Authors = new List <WorkAuthor>(new[] { new WorkAuthor { First = "John", Last = "Doe" } }), Number = "n.s.11", YearPub = 2020 }); Assert.Equal("Doe n.s.11 2020", key); }
public void Build_MoreThan3Authors_Ok() { string key = WorkKeyBuilder.Build(new Work { Authors = new List <WorkAuthor>(new[] { new WorkAuthor { First = "John", Last = "Doe", Ordinal = 1 }, new WorkAuthor { First = "Mike", Last = "Aspen", Ordinal = 2 }, new WorkAuthor { First = "Josh", Last = "Williams", Ordinal = 3 }, new WorkAuthor { First = "Judy", Last = "Perth", Ordinal = 4 }, new WorkAuthor { First = "Zac", Last = "Neuman", Ordinal = 5 }, }), YearPub = 2020 }); Assert.Equal("Doe & Aspen & Williams & al. 2020", key); }
public void Build_MultiAuthorsNoOrdinals_Ok() { string key = WorkKeyBuilder.Build(new Work { Authors = new List <WorkAuthor>(new[] { new WorkAuthor { First = "John", Last = "Doe", }, new WorkAuthor { First = "Mike", Last = "Aspen", } }), YearPub = 2020 }); Assert.Equal("Aspen & Doe 2020", key); }
/// <summary> /// Gets the work entity corresponding to the specified work. /// </summary> /// <param name="work">The work or null.</param> /// <param name="context">The context.</param> /// <returns>The entity or null.</returns> /// <exception cref="ArgumentNullException">context</exception> public static EfWork GetEfWork(Work work, BiblioDbContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (work == null) { return(null); } // find the work unless new EfWork ef = work.Id != Guid.Empty ? context.Works .Include(w => w.AuthorWorks) .Include(w => w.KeywordWorks) .FirstOrDefault(w => w.Id == work.Id) : null; // if new or not found, add it with a new ID if (ef == null) { ef = new EfWork(); context.Works.Add(ef); } // update the work ef.Type = GetOrCreateWorkType(work.Type, null, context); ef.Title = work.Title; ef.Titlex = StandardFilter.Apply(work.Title, true); ef.Language = work.Language; ef.Container = GetEfContainer(work.Container, context); ef.Edition = work.Edition; ef.Publisher = work.Publisher; ef.YearPub = work.YearPub; ef.PlacePub = work.PlacePub; ef.Location = work.Location; ef.AccessDate = work.AccessDate; ef.Number = work.Number; ef.FirstPage = work.FirstPage; ef.LastPage = work.LastPage; ef.Note = work.Note; // authors if (work.Authors?.Count > 0) { AddAuthors(work.Authors, ef, context); } // keywords if (work.Keywords?.Count > 0) { AddKeywords(work.Keywords, ef, context); } // key ef.Key = WorkKeyBuilder.PickKey(ef.Key, work); // add key suffix if required and possible if (ef.Key?.StartsWith(WorkKeyBuilder.MAN_KEY_PREFIX) != true) { char c = GetSuffixForKey(ef.Key, context); if (c != '\0') { ef.Key += c; } } return(ef); }
/// <summary> /// Gets the container entity corresponding to the specified container. /// </summary> /// <param name="container">The container or null.</param> /// <param name="context">The context.</param> /// <returns>The entity or null.</returns> /// <exception cref="ArgumentNullException">context</exception> public static EfContainer GetEfContainer(Container container, BiblioDbContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (container == null) { return(null); } // get the container unless it's new EfContainer ef = container.Id != Guid.Empty ? context.Containers .Include(c => c.AuthorContainers) .Include(c => c.KeywordContainers) .FirstOrDefault(c => c.Id == container.Id) : null; // if new or not found, add it with a new ID if (ef == null) { if (container.Title == null) { return(null); } ef = new EfContainer(); context.Containers.Add(ef); } // update the container unless empty if (container.Title != null) { ef.Type = GetOrCreateWorkType(container.Type, null, context); ef.Title = container.Title; ef.Titlex = StandardFilter.Apply(container.Title, true); ef.Language = container.Language; ef.Edition = container.Edition; ef.Publisher = container.Publisher; ef.YearPub = container.YearPub; ef.PlacePub = container.PlacePub; ef.Location = container.Location; ef.AccessDate = container.AccessDate; ef.Number = container.Number; ef.Note = container.Note; // authors if (container.Authors?.Count > 0) { AddAuthors(container.Authors, ef, context); } // keywords if (container.Keywords?.Count > 0) { AddKeywords(container.Keywords, ef, context); } // key ef.Key = WorkKeyBuilder.PickKey(ef.Key, container); // add key suffix if required and possible if (ef.Key?.StartsWith(WorkKeyBuilder.MAN_KEY_PREFIX) != true) { char c = GetSuffixForKey(ef.Key, context); if (c != '\0') { ef.Key += c; } } } return(ef); }