private static IEnumerable <CompositeKey <TPrimaryKey, TSecondaryKey> > SecondaryKeysOf(TPrimaryKey primaryKey, IReadOnlyDictionary <TSecondaryKey, TValue> seondaryKeyDictionary) { var result = ImmutableHashSet.CreateBuilder <CompositeKey <TPrimaryKey, TSecondaryKey> >(); foreach (var secondaryKey in seondaryKeyDictionary.Keys) { result.Add(CompositeKeys.Of(primaryKey, secondaryKey)); } return(result.ToImmutable()); }
public void ShouldReturnNullForNotExitingKey(CompositeDictionaryCreator creator) { // given var compositeDictionary = creator.Create <int, int, string>(); // when var value1 = compositeDictionary.Get(1, 1); var value2 = compositeDictionary[CompositeKeys.Of(1, 1)]; // then Check.That(value1).IsNull(); Check.That(value2).IsNull(); Check.That(compositeDictionary.ContainsKey(1, 1)).IsFalse(); Check.That(compositeDictionary.ContainsKey(CompositeKeys.Of(1, 1))).IsFalse(); }
public async Task Deletes_entity_with_composite_keys(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var id = new { Key1 = 5, Key2 = 20 }; var entity = new CompositeKeys { Key1 = 5, Key2 = 20, Name = "Some Name" }; database.Insert(entity); // Act await database.DeleteAsync <CompositeKeys>(id); } }
public void Does_not_allow_part_of_composite_key_to_be_null(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var entity = new CompositeKeys { Key1 = null, Key2 = 5, Name = "Some Name" }; // Act Action act = () => database.Insert(entity); // Assert act.Should().Throw <Exception>(); } }
public void Inserts_entities_with_composite_keys(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var entity = new CompositeKeys { Key1 = 2, Key2 = 3, Name = "Some Name" }; // Act database.Insert(entity); // Assert database.Count <CompositeKeys>().Should().Be(1); } }
public void Deletes_entity_with_composite_keys(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var id = new { Key1 = 5, Key2 = 20 }; var entity = new CompositeKeys { Key1 = 5, Key2 = 20, Name = "Some Name" }; database.Insert(entity); // Act database.Delete(entity); // Assert database.Find <CompositeKeys>(id).Should().BeNull(); } }
public void Updates_entities_with_composite_keys(IDialect dialect) { using (var database = BlankDatabaseFactory.MakeDatabase(dialect)) { // Arrange var entity = new CompositeKeys { Key1 = 5, Key2 = 20, Name = "Some name" }; database.Insert(entity); // Act entity.Name = "Other name"; database.Update(entity); // Assert var id = new { Key1 = 5, Key2 = 20 }; var updatedEntity = database.Find <CompositeKeys>(id); updatedEntity.Name.Should().Be("Other name"); } }
public void ShouldPutValuesIntoCompositeDictionary(CompositeDictionaryCreator creator) { // given var compositeDictionary = creator.Create <string, int, string>(); // when compositeDictionary.Put("A", 1, "A1"); compositeDictionary.Put("A", 2, "A2"); compositeDictionary.Put("A", 3, "A3"); compositeDictionary.Put("B", 1, "B1"); compositeDictionary.PutAll(Dictionaries.Create( "C", Dictionaries.Create(1, "C1", 2, "C2"), "D", Dictionaries.Create(1, "D1", 2, "D2"))); // then Check.That(compositeDictionary.Count).IsEqualTo(8); Check.That(compositeDictionary.PrimaryKeysCount).IsEqualTo(4); Check.That(compositeDictionary.IsEmpty).IsFalse(); Check.That(compositeDictionary.Get("A", 1)).IsEqualTo("A1"); Check.That(compositeDictionary[CompositeKeys.Of("A", 2)]).IsEqualTo("A2"); Check.That(compositeDictionary.Get(CompositeKeys.Of("A", 3))).IsEqualTo("A3"); Check.That(compositeDictionary.Get("B", 1)).IsEqualTo("B1"); Check.That(compositeDictionary.Get("C", 1)).IsEqualTo("C1"); Check.That(compositeDictionary.Get("C", 2)).IsEqualTo("C2"); Check.That(compositeDictionary.Get("D", 1)).IsEqualTo("D1"); Check.That(compositeDictionary.Get("D", 2)).IsEqualTo("D2"); Check.That(compositeDictionary.Values).HasSize(8).And .Contains("A1", "A2", "A3", "B1", "C1", "C2", "D1", "D2"); Check.That(compositeDictionary.Entries).HasSize(8).And.Contains( Dictionaries.Entry("A", Dictionaries.Entry(1, "A1")), Dictionaries.Entry("A", Dictionaries.Entry(2, "A2")), Dictionaries.Entry("A", Dictionaries.Entry(3, "A3")), Dictionaries.Entry("B", Dictionaries.Entry(1, "B1")), Dictionaries.Entry("C", Dictionaries.Entry(1, "C1")), Dictionaries.Entry("C", Dictionaries.Entry(2, "C2")), Dictionaries.Entry("D", Dictionaries.Entry(1, "D1")), Dictionaries.Entry("D", Dictionaries.Entry(2, "D2"))); Check.That(compositeDictionary.PrimaryKeys).HasSize(4).And .Contains("A", "B", "C", "D"); Check.That(compositeDictionary.SecondaryKeys).HasSize(3).And .Contains(1, 2, 3); Check.That(compositeDictionary.PrimaryKeyEntries("A")).HasSize(3).And .Contains(Dictionaries.Entry(1, "A1"), Dictionaries.Entry(2, "A2"), Dictionaries.Entry(3, "A3")); Check.That(compositeDictionary.PrimaryKeyEntries("B")).HasSize(1).And .Contains(Dictionaries.Entry(1, "B1")); Check.That(compositeDictionary.PrimaryKeyEntries("C")).HasSize(2).And .Contains(Dictionaries.Entry(1, "C1"), Dictionaries.Entry(2, "C2")); Check.That(compositeDictionary.PrimaryKeyEntries("D")).HasSize(2).And .Contains(Dictionaries.Entry(1, "D1"), Dictionaries.Entry(2, "D2")); Check.That(compositeDictionary.PrimaryKeyValues("A")).HasSize(3).And.Contains("A1", "A2", "A3"); Check.That(compositeDictionary.PrimaryKeyValues("B")).HasSize(1).And.Contains("B1"); Check.That(compositeDictionary.PrimaryKeyValues("C")).HasSize(2).And.Contains("C1", "C2"); Check.That(compositeDictionary.PrimaryKeyValues("D")).HasSize(2).And.Contains("D1", "D2"); Check.That(compositeDictionary.SecondaryKeyEntries(1)).HasSize(4).And .Contains(Dictionaries.Entry("A", "A1"), Dictionaries.Entry("B", "B1"), Dictionaries.Entry("C", "C1"), Dictionaries.Entry("D", "D1")); Check.That(compositeDictionary.SecondaryKeyEntries(2)).HasSize(3).And .Contains(Dictionaries.Entry("A", "A2"), Dictionaries.Entry("C", "C2"), Dictionaries.Entry("D", "D2")); Check.That(compositeDictionary.SecondaryKeyEntries(3)).HasSize(1).And .Contains(Dictionaries.Entry("A", "A3")); Check.That(compositeDictionary.SecondaryKeyValues(1)).HasSize(4).And.Contains("A1", "B1", "C1", "D1"); Check.That(compositeDictionary.SecondaryKeyValues(2)).HasSize(3).And.Contains("A2", "C2", "D2"); Check.That(compositeDictionary.SecondaryKeyValues(3)).HasSize(1).And.Contains("A3"); }