From 0638dfc6168d39b6e2142c9f42729ee9ef49ab3f Mon Sep 17 00:00:00 2001 From: Kirill Boldyrev Date: Wed, 28 Apr 2021 18:15:57 +0500 Subject: [PATCH 1/6] ThingCache tests --- ThingCache/ThingCache.cs | 27 +++++++++++++++++++++++++-- ThingCache/ThingCache.csproj | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ThingCache/ThingCache.cs b/ThingCache/ThingCache.cs index 2d3859d..01ec462 100644 --- a/ThingCache/ThingCache.cs +++ b/ThingCache/ThingCache.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using FakeItEasy; using NUnit.Framework; namespace MockFramework @@ -40,11 +41,17 @@ public class ThingCache_Should private const string thingId2 = "CoolBoots"; private Thing thing2 = new Thing(thingId2); + private const string thingIdNotExisted = "NotExisted"; + private Thing thing3 = null; + // Метод, помеченный атрибутом SetUp, выполняется перед каждым тестов [SetUp] public void SetUp() { - //thingService = A... + thingService = A.Fake(); + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).Returns(true); + A.CallTo(() => thingService.TryRead(thingId2, out thing2)).Returns(true); + A.CallTo(() => thingService.TryRead(thingIdNotExisted, out thing3)).Returns(false); thingCache = new ThingCache(thingService); } @@ -53,8 +60,24 @@ public void SetUp() // Пример теста [Test] - public void GiveMeAGoodNamePlease() + public void ThingCache_Get_ReturnsCorrectValue() + { + var actualValue = thingCache.Get(thingId1); + Assert.That(actualValue, Is.EqualTo(thing1)); + } + + [Test] + public void ThingCache_Get_ReturnsNoValue() + { + var notExistedValue = thingCache.Get(thingIdNotExisted); + Assert.That(notExistedValue, Is.Null); + } + + [Test] + public void ThingCache_Get_MethodCallingHappened() { + thingCache.Get(thingId1); + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); } /** Проверки в тестах diff --git a/ThingCache/ThingCache.csproj b/ThingCache/ThingCache.csproj index 6d6bed7..c915cd3 100644 --- a/ThingCache/ThingCache.csproj +++ b/ThingCache/ThingCache.csproj @@ -12,6 +12,7 @@ + From 4aeaddbc982e05c8fc24d340476e18f58f309923 Mon Sep 17 00:00:00 2001 From: Kirill Boldyrev Date: Thu, 29 Apr 2021 06:31:28 +0500 Subject: [PATCH 2/6] FileSender tests --- FileSender/FileSender.cs | 87 +++++++++++++++++++++++++++--------- FileSender/FileSender.csproj | 1 + 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/FileSender/FileSender.cs b/FileSender/FileSender.cs index 5899292..5cb509e 100644 --- a/FileSender/FileSender.cs +++ b/FileSender/FileSender.cs @@ -74,6 +74,15 @@ public class FileSender_Should private readonly X509Certificate certificate = new X509Certificate(); private File file; private byte[] signedContent; + private Document document; + + private File invalidFile; + private byte[] invalidSignedContent; + private Document invalidDocument; + + private File couldNotSendFile; + private byte[] couldNotSendSignedContent; + private Document couldNotSendDocument; [SetUp] public void SetUp() @@ -89,13 +98,8 @@ public void SetUp() sender = A.Fake(); recognizer = A.Fake(); fileSender = new FileSender(cryptographer, sender, recognizer); - } - [TestCase("4.0")] - [TestCase("3.1")] - public void Send_WhenGoodFormat(string format) - { - var document = new Document(file.Name, file.Content, DateTime.Now, format); + document = new Document(file.Name, file.Content, DateTime.Now, "4.0"); A.CallTo(() => recognizer.TryRecognize(file, out document)) .Returns(true); A.CallTo(() => cryptographer.Sign(document.Content, certificate)) @@ -103,57 +107,98 @@ public void Send_WhenGoodFormat(string format) A.CallTo(() => sender.TrySend(signedContent)) .Returns(true); + + invalidFile = new File("invalidFile", new byte[] { 1, 2, 3 }); + invalidSignedContent = new byte[] { 1, 7 }; + invalidDocument = new Document(invalidFile.Name, invalidFile.Content, DateTime.Now, "111.222"); + + A.CallTo(() => recognizer.TryRecognize(invalidFile, out invalidDocument)) + .Returns(true); + A.CallTo(() => cryptographer.Sign(invalidDocument.Content, certificate)) + .Returns(invalidSignedContent); + A.CallTo(() => sender.TrySend(invalidSignedContent)) + .Returns(true); + + + couldNotSendFile = new File("invalidFile", new byte[] { 1, 2, 3 }); + couldNotSendSignedContent = new byte[] { 1, 7 }; + couldNotSendDocument = new Document(couldNotSendFile.Name, couldNotSendFile.Content, DateTime.Now, "4.0"); + + A.CallTo(() => recognizer.TryRecognize(couldNotSendFile, out couldNotSendDocument)) + .Returns(true); + A.CallTo(() => cryptographer.Sign(couldNotSendDocument.Content, certificate)) + .Returns(couldNotSendSignedContent); + A.CallTo(() => sender.TrySend(couldNotSendSignedContent)) + .Returns(false); + } + + [TestCase("4.0")] + [TestCase("3.1")] + public void Send_WhenGoodFormat(string format) + { + document.Format = format; + fileSender.SendFiles(new[] {file}, certificate) .SkippedFiles.Should().BeEmpty(); } [Test] - [Ignore("Not implemented")] - public void Skip_WhenBadFormat() + [TestCase("111.222")] + public void Skip_WhenBadFormat(string format) { - throw new NotImplementedException(); + document.Format = format; + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().HaveCount(1); } [Test] - [Ignore("Not implemented")] public void Skip_WhenOlderThanAMonth() { - throw new NotImplementedException(); + document.Created = document.Created.AddMonths(-1).AddDays(-1); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().HaveCount(1); } [Test] - [Ignore("Not implemented")] public void Send_WhenYoungerThanAMonth() { - throw new NotImplementedException(); + document.Created = document.Created.AddMonths(-1).AddDays(1); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().BeEmpty(); } [Test] - [Ignore("Not implemented")] public void Skip_WhenSendFails() { - throw new NotImplementedException(); + fileSender.SendFiles(new[] { couldNotSendFile }, certificate) + .SkippedFiles.Should().HaveCount(1); } [Test] - [Ignore("Not implemented")] public void Skip_WhenNotRecognized() { - throw new NotImplementedException(); + A.CallTo(() => recognizer.TryRecognize(file, out document)) + .Returns(false); + + fileSender.SendFiles(new[] { file }, certificate) + .SkippedFiles.Should().HaveCount(1); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeAreInvalid() { - throw new NotImplementedException(); + fileSender.SendFiles(new[] { file, invalidFile }, certificate) + .SkippedFiles.Should().HaveCount(1); } [Test] - [Ignore("Not implemented")] public void IndependentlySend_WhenSeveralFilesAndSomeCouldNotSend() { - throw new NotImplementedException(); + fileSender.SendFiles(new[] { file, couldNotSendFile }, certificate) + .SkippedFiles.Should().HaveCount(1); } } } diff --git a/FileSender/FileSender.csproj b/FileSender/FileSender.csproj index b4c7dfe..b0c413e 100644 --- a/FileSender/FileSender.csproj +++ b/FileSender/FileSender.csproj @@ -11,6 +11,7 @@ + From d99f5f09e5966aad2e9c42084014b16f7c71707b Mon Sep 17 00:00:00 2001 From: Kirill Boldyrev Date: Thu, 29 Apr 2021 17:13:56 +0500 Subject: [PATCH 3/6] ThingCache tests: ThingCache_Get_ReturnsValueFromCache --- ThingCache/ThingCache.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ThingCache/ThingCache.cs b/ThingCache/ThingCache.cs index 01ec462..9c55937 100644 --- a/ThingCache/ThingCache.cs +++ b/ThingCache/ThingCache.cs @@ -80,6 +80,14 @@ public void ThingCache_Get_MethodCallingHappened() A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); } + [Test] + public void ThingCache_Get_ReturnsValueFromCache() + { + thingCache.Get(thingId1); // put value to cache + thingCache.Get(thingId1); // get value via cache + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); + } + /** Проверки в тестах * Assert.AreEqual(expectedValue, actualValue); * actualValue.Should().Be(expectedValue); From b02e6dc7c99ccb1a432643c81065797b81860688 Mon Sep 17 00:00:00 2001 From: Kirill Boldyrev Date: Thu, 29 Apr 2021 17:21:09 +0500 Subject: [PATCH 4/6] ThingCache tests: ThingCache_Get_ReturnsValueFromCacheMany --- ThingCache/ThingCache.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ThingCache/ThingCache.cs b/ThingCache/ThingCache.cs index 9c55937..1283888 100644 --- a/ThingCache/ThingCache.cs +++ b/ThingCache/ThingCache.cs @@ -88,6 +88,17 @@ public void ThingCache_Get_ReturnsValueFromCache() A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); } + [Test] + public void ThingCache_Get_ReturnsValueFromCacheMany() + { + thingCache.Get(thingId1); // put value to cache + thingCache.Get(thingId2); // put value to cache + thingCache.Get(thingId1); // get value via cache + thingCache.Get(thingId2); // get value via cache + A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly(); + A.CallTo(() => thingService.TryRead(thingId2, out thing2)).MustHaveHappenedOnceExactly(); + } + /** Проверки в тестах * Assert.AreEqual(expectedValue, actualValue); * actualValue.Should().Be(expectedValue); From 554902e4d5d0a6bda68641b1b78f2f0d2d4bc832 Mon Sep 17 00:00:00 2001 From: Kirill Boldyrev Date: Thu, 29 Apr 2021 17:26:56 +0500 Subject: [PATCH 5/6] FileSender tests: add tests for TrySend() happened or not --- FileSender/FileSender.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/FileSender/FileSender.cs b/FileSender/FileSender.cs index 5cb509e..7145b18 100644 --- a/FileSender/FileSender.cs +++ b/FileSender/FileSender.cs @@ -150,6 +150,8 @@ public void Skip_WhenBadFormat(string format) fileSender.SendFiles(new[] { file }, certificate) .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } [Test] @@ -159,6 +161,8 @@ public void Skip_WhenOlderThanAMonth() fileSender.SendFiles(new[] { file }, certificate) .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } [Test] @@ -168,6 +172,8 @@ public void Send_WhenYoungerThanAMonth() fileSender.SendFiles(new[] { file }, certificate) .SkippedFiles.Should().BeEmpty(); + + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } [Test] @@ -185,6 +191,8 @@ public void Skip_WhenNotRecognized() fileSender.SendFiles(new[] { file }, certificate) .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustNotHaveHappened(); } [Test] @@ -192,6 +200,8 @@ public void IndependentlySend_WhenSeveralFilesAndSomeAreInvalid() { fileSender.SendFiles(new[] { file, invalidFile }, certificate) .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } [Test] @@ -199,6 +209,8 @@ public void IndependentlySend_WhenSeveralFilesAndSomeCouldNotSend() { fileSender.SendFiles(new[] { file, couldNotSendFile }, certificate) .SkippedFiles.Should().HaveCount(1); + + A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } } } From 629fca14133f01d31c603f17eede0a94ef2e0fcf Mon Sep 17 00:00:00 2001 From: Kirill Boldyrev Date: Thu, 29 Apr 2021 17:32:02 +0500 Subject: [PATCH 6/6] FileSender tests: add tests to check what file were skipped to send --- FileSender/FileSender.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/FileSender/FileSender.cs b/FileSender/FileSender.cs index 7145b18..850dcee 100644 --- a/FileSender/FileSender.cs +++ b/FileSender/FileSender.cs @@ -198,8 +198,10 @@ public void Skip_WhenNotRecognized() [Test] public void IndependentlySend_WhenSeveralFilesAndSomeAreInvalid() { - fileSender.SendFiles(new[] { file, invalidFile }, certificate) - .SkippedFiles.Should().HaveCount(1); + var result = fileSender.SendFiles(new[] { file, invalidFile }, certificate); + + result.SkippedFiles.Should().HaveCount(1); + result.SkippedFiles.Should().Contain(invalidFile); A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); } @@ -207,8 +209,10 @@ public void IndependentlySend_WhenSeveralFilesAndSomeAreInvalid() [Test] public void IndependentlySend_WhenSeveralFilesAndSomeCouldNotSend() { - fileSender.SendFiles(new[] { file, couldNotSendFile }, certificate) - .SkippedFiles.Should().HaveCount(1); + var result = fileSender.SendFiles(new[] { file, couldNotSendFile }, certificate); + + result.SkippedFiles.Should().HaveCount(1); + result.SkippedFiles.Should().Contain(couldNotSendFile); A.CallTo(() => sender.TrySend(signedContent)).MustHaveHappenedOnceExactly(); }