/// <summary> /// Creates and seeds a new part. /// </summary> /// <param name="item">The item this part should belong to.</param> /// <param name="roleId">The optional part role ID.</param> /// <param name="factory">The part seeder factory. This is used /// for layer parts, which need to seed a set of fragments.</param> /// <returns>A new part.</returns> /// <exception cref="ArgumentNullException">item or factory</exception> public override IPart GetPart(IItem item, string roleId, PartSeederFactory factory) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (factory == null) { throw new ArgumentNullException(nameof(factory)); } if (_options?.Categories == null || _options.Categories.Length == 0 || _options.MaxCategoriesPerItem < 1) { return(null); } CategoriesPart part = new CategoriesPart(); SetPartMetadata(part, roleId, item); // pick from 1 to 3 categories, all different int count = Randomizer.Seed.Next(1, _options.MaxCategoriesPerItem + 1); foreach (string category in SeedHelper.RandomPickOf( _options.Categories, count)) { part.Categories.Add(category); } return(part); }
public void Seed_ValidOptions_Ok() { CategoriesPartSeeder seeder = new CategoriesPartSeeder(); seeder.SetSeedOptions(_seedOptions); seeder.Configure(new CategoriesPartSeederOptions { MaxCategoriesPerItem = 3, Categories = new[] { "alpha", "beta", "gamma" } }); IPart part = seeder.GetPart(_item, null, _factory); Assert.NotNull(part); CategoriesPart cp = part as CategoriesPart; Assert.NotNull(cp); TestHelper.AssertPartMetadata(cp); Assert.NotEmpty(cp.Categories); }
private void SeedParts(IMongoDatabase db) { var collection = db.GetCollection <MongoPart>(MongoPart.COLLECTION); // categories CategoriesPart categoriesPart = new CategoriesPart { Id = "part-001", ItemId = "item-001", RoleId = "categories", CreatorId = "Odd", UserId = "Odd", Categories = { "alpha", "beta" } }; collection.InsertOne(CreateMongoPart(categoriesPart)); // note NotePart notePart = new NotePart { Id = "part-002", ItemId = "item-001", RoleId = "note", CreatorId = "Odd", UserId = "Odd", Text = "Some notes." }; collection.InsertOne(CreateMongoPart(notePart)); // layer: comments TokenTextLayerPart <CommentLayerFragment> commentLayerPart = new TokenTextLayerPart <CommentLayerFragment> { Id = "part-003", ItemId = "item-001", CreatorId = "Odd", UserId = "Odd" }; commentLayerPart.Fragments.AddRange(new [] { new CommentLayerFragment { Location = "1.2", Text = "The comment to 1.2" }, new CommentLayerFragment { Location = "1.3", Text = "The comment to 1.3" } }); collection.InsertOne(CreateMongoPart(commentLayerPart)); }
public void Part_Is_Serializable() { BibliographyPart part = GetPart(2); string json = TestHelper.SerializePart(part); CategoriesPart part2 = TestHelper.DeserializePart <CategoriesPart>(json); Assert.Equal(part.Id, part2.Id); Assert.Equal(part.TypeId, part2.TypeId); Assert.Equal(part.ItemId, part2.ItemId); Assert.Equal(part.RoleId, part2.RoleId); Assert.Equal(part.CreatorId, part2.CreatorId); Assert.Equal(part.UserId, part2.UserId); Assert.Equal(2, part.Entries.Count); }
private static CategoriesPart GetPart(params string[] categories) { CategoriesPart part = new CategoriesPart { ItemId = Guid.NewGuid().ToString(), RoleId = "some-role", CreatorId = "zeus", UserId = "another" }; foreach (string category in categories) { part.Categories.Add(category); } return(part); }
public void Part_Is_Serializable() { CategoriesPart part = GetPart("alpha", "beta"); string json = TestHelper.SerializePart(part); CategoriesPart part2 = TestHelper.DeserializePart <CategoriesPart>(json); Assert.Equal(part.Id, part2.Id); Assert.Equal(part.TypeId, part2.TypeId); Assert.Equal(part.ItemId, part2.ItemId); Assert.Equal(part.RoleId, part2.RoleId); Assert.Equal(part.CreatorId, part2.CreatorId); Assert.Equal(part.UserId, part2.UserId); Assert.Equal(2, part.Categories.Count); Assert.Contains("alpha", part.Categories); Assert.Contains("beta", part.Categories); }
public void GetDataPins_TwoCategories_2() { CategoriesPart part = GetPart("alpha", "beta"); List <DataPin> pins = part.GetDataPins().ToList(); Assert.Equal(2, pins.Count); DataPin pin = pins[0]; Assert.Equal(part.ItemId, pin.ItemId); Assert.Equal(part.Id, pin.PartId); Assert.Equal(part.RoleId, pin.RoleId); Assert.Equal("category", pin.Name); Assert.Equal("alpha", pin.Value); pin = pins[1]; Assert.Equal(part.ItemId, pin.ItemId); Assert.Equal(part.Id, pin.PartId); Assert.Equal(part.RoleId, pin.RoleId); Assert.Equal("category", pin.Name); Assert.Equal("beta", pin.Value); }
public void GetDataPins_NoCategories_Empty() { CategoriesPart part = GetPart(); Assert.Empty(part.GetDataPins()); }