public virtual async Task Polymorphism() { await entityRepo.TruncateCollection(); await collectionEntityRepo.TruncateCollection(); TestExtraEltEntity entity2 = TestHelper.GetEntity2(); await entityRepo.InsertOne(entity2); Assert.IsFalse(string.IsNullOrEmpty(entity2.Id)); var entity2_repo = await entityRepo.GetById(entity2.Id); //entity_repo.LookLikeEachOther(entity); AssertHelper.AreJsonEqual(entity2, entity2_repo, ErrorMsg: "Get of a TestExtraEltEntity instance from a TestEntity repo should return TestExtraEltEntity"); //Assert.AreEqual<TestEntity>(entity1, entity1_repo); var collectionTest = new CollectionTest(); collectionTest.PolymorphCollection.Add(entity2); // TestExtraEltEntity instance collectionTest.PolymorphCollection.Add(TestHelper.GetEntity1()); // TestEntity instance await collectionEntityRepo.InsertOne(collectionTest); var collectionTest_fromRepo = await collectionEntityRepo.GetById(collectionTest.Id); AssertHelper.AreJsonEqual(collectionTest, collectionTest_fromRepo, ErrorMsg: "Check if collection elements has the good type"); }
private void InsertEntities(int nbEntities, int firstId, IAsyncNoSQLRepository <TestEntity> repo) { for (int i = 1; i <= nbEntities; i++) { Console.WriteLine(i); TestEntity e = new TestEntity { Id = (firstId + i).ToString(), PoidsDouble = Faker.RandomNumber.Next(), NumberOfChildenInt = Faker.RandomNumber.Next(), Name = Faker.Name.FullName() }; repo.InsertOne(e); } }
public virtual async Task InsertExtraEltEntity() { await entityExtraEltRepo.TruncateCollection(); var entity = TestHelper.GetEntity2(); await entityExtraEltRepo.InsertOne(entity); Assert.IsFalse(string.IsNullOrEmpty(entity.Id)); var entity_repo = await entityExtraEltRepo.GetById(entity.Id); //entity_repo.LookLikeEachOther(entity); AssertHelper.AreJsonEqual(entity, entity_repo); //Assert.AreEqual<TestEntity>(entity1, entity1_repo); }
public virtual async Task InsertEntity() { await entityRepo.TruncateCollection(); var entity1 = TestHelper.GetEntity1(); await entityRepo.InsertOne(entity1); Assert.IsFalse(string.IsNullOrEmpty(entity1.Id), "DocId has not been set during insert"); var t1 = new DateTime(2016, 01, 01, 0, 0, 0, DateTimeKind.Utc); var t2 = new DateTime(2017, 01, 01, 0, 0, 0, DateTimeKind.Utc); NoSQLRepoHelper.DateTimeUtcNow = (() => t1); // Set the current time to t1 Exception e = null; try { await entityRepo.InsertOne(entity1); } catch (Exception ex) { e = ex; } Assert.IsInstanceOfType(e, typeof(DupplicateKeyNoSQLException), "InsertOne should raise DupplicateKeyException if id already exists"); var insertResult = await entityRepo.InsertOne(entity1, InsertMode.do_nothing_if_key_exists); // Do Nothing Assert.AreEqual(InsertResult.not_affected, insertResult, "Expecting not_affected result"); var entity1_repo = await entityRepo.GetById(entity1.Id); Assert.IsNotNull(entity1_repo); AssertHelper.AreJsonEqual(entity1, entity1_repo); Assert.AreEqual(t1, entity1.SystemCreationDate, "SystemCreationDate should be defined during insert"); Assert.AreEqual(t1, entity1.SystemLastUpdateDate, "SystemLastUpdateDate should be defined during insert"); // Replace first version { NoSQLRepoHelper.DateTimeUtcNow = (() => t2); // Set the current time to t2 var entity1V2 = TestHelper.GetEntity1(); entity1V2.Id = entity1.Id; entity1V2.Name = "Balan2"; await entityRepo.InsertOne(entity1V2, InsertMode.erase_existing); // Erase var entity1V2_fromRepo = await entityRepo.GetById(entity1.Id); Assert.AreEqual(entity1V2_fromRepo.Name, "Balan2", "The insert with erase_existing mode should erase the previous version of the entity"); Assert.AreEqual(t1, entity1V2.SystemCreationDate, "SystemCreationDate should be the date of the erased entity version"); Assert.AreEqual(t2, entity1V2.SystemLastUpdateDate, "SystemLastUpdateDate should the date of the update of the entity version"); AssertHelper.AreJsonEqual(entity1V2, entity1V2_fromRepo); } // Erase while doc not exists { var entity2 = TestHelper.GetEntity2(); await entityRepo.InsertOne(entity2, InsertMode.erase_existing); // Insert var entity2_repo = await entityRepo.GetById(entity2.Id); AssertHelper.AreJsonEqual(entity2, entity2_repo); // ABN: why this modification of unit test ?! // Clone in order to get a new objet of type TestEntity because a cast is not suffisant //var entity2Casted = entity2.CloneToTestEntity(); //AssertHelper.AreJsonEqual(entity2Casted, entity2_repo); } }
public virtual async Task ViewTests() { await entityRepo.TruncateCollection(); // // Add test data // var entity1 = TestHelper.GetEntity1(); entity1.Id = "1"; await entityRepo.InsertOne(entity1); var entity2 = TestHelper.GetEntity2(); entity2.Id = "2"; await entityRepo.InsertOne(entity2); // Add the 3td et 4th entities to en secondary repo to ensure insert are visible throw all repositories var entity3 = TestHelper.GetEntity3(); entity3.Id = "3"; await entityRepo2.InsertOne(entity3); var entity4 = TestHelper.GetEntity4(); entity4.Id = "4"; await entityRepo2.InsertOne(entity4); // // Get data from an "Int" field // // Filter on 1 value var task1 = await entityRepo.GetByField <int>(nameof(TestEntity.NumberOfChildenInt), 0); var res1 = task1.OrderBy(e => e.Id).ToList(); Assert.AreEqual(2, res1.Count); Assert.AreEqual("2", res1[0].Id); Assert.AreEqual("3", res1[1].Id); AssertHelper.AreJsonEqual(entity2, res1[0]); AssertHelper.AreJsonEqual(entity3, res1[1]); var task2 = await entityRepo.GetByField <int>(nameof(TestEntity.NumberOfChildenInt), 0); var res2 = task2.OrderBy(e => e.Id).ToList(); Assert.AreEqual(2, res2.Count, "Check the an error not occured after a 2 call (the object entities are in memory)"); var task3 = await entityRepo.GetKeyByField <int>(nameof(TestEntity.NumberOfChildenInt), 0); var res3 = task3.OrderBy(e => e).ToList(); Assert.AreEqual("2", res3[0]); Assert.AreEqual("3", res3[1]); Exception expectedEx = null; try { var taskResult = await entityRepo.GetByField <int>(nameof(TestEntity.NumberOfChildenLong), 0); taskResult.OrderBy(e => e.Id).ToList(); } catch (Exception ex) { expectedEx = ex; } Assert.IsInstanceOfType(expectedEx, typeof(IndexNotFoundNoSQLException)); // Filter on a set of value var searchedValues = new List <int> { 0, 10 }; var task4 = await entityRepo.GetByField <int>(nameof(TestEntity.NumberOfChildenInt), searchedValues); var res4 = task4.OrderBy(e => e.Id).ToList(); Assert.AreEqual(3, res4.Count); var task5 = await entityRepo.GetKeyByField <int>(nameof(TestEntity.NumberOfChildenInt), searchedValues); var res5 = task5.OrderBy(e => e).ToList(); Assert.AreEqual(3, res5.Count); // // Get data from a "List<string>" field // var task6 = await entityRepo.GetByField <string>(nameof(TestEntity.Cities), "Grenoble"); var res6 = task6.OrderBy(e => e.Id).ToList(); Assert.AreEqual(3, res6.Where(e => e.Cities.Contains("Grenoble")).Count()); var searchList = new List <string> { "Grenoble", "Andernos" }; var task7 = await entityRepo.GetByField <string>(nameof(TestEntity.Cities), searchList); var res7 = task7.OrderBy(e => e.Id).ToList(); Assert.AreEqual(3, res7.Count, "Dupplicate entries should be removed"); Assert.AreEqual(3, res7.Where(e => e.Cities.Contains("Grenoble")).Count()); Assert.AreEqual(1, res7.Where(e => e.Cities.Contains("Andernos")).Count()); var task8 = await entityRepo.GetByField <string>(nameof(TestEntity.Cities), "Grenoble"); var res8 = task8.OrderBy(e => e.Id).ToList(); Assert.AreEqual(3, res8.Count); var task9 = await entityRepo.GetKeyByField <string>(nameof(TestEntity.Cities), searchList); var res9 = task9.OrderBy(e => e).ToList(); Assert.AreEqual(3, res9.Count, "Dupplicate entries should be removed"); var task10 = await entityRepo.GetByField <string>(nameof(TestEntity.Cities), "GrEnObLe"); var res10 = task10.OrderBy(e => e.Id).ToList(); Assert.AreEqual(0, res10.Count, "String comparison should be case sensitive"); var task11 = await entityRepo.GetByField <string>(nameof(TestEntity.Cities), "Grenoblé"); var res11 = task11.OrderBy(e => e.Id).ToList(); Assert.AreEqual(0, res11.Count, "String comparison should be accent sensitive"); }