From e4e7dbf553bfa17a39830e67d14d3715946beab3 Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Tue, 12 Dec 2017 14:24:47 +0100 Subject: Added unit tests and fixed some bugs. --- .../ReadingsControllerTests.cs | 116 +++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 PollutometerWebApi.Tests/ReadingsControllerTests.cs (limited to 'PollutometerWebApi.Tests/ReadingsControllerTests.cs') diff --git a/PollutometerWebApi.Tests/ReadingsControllerTests.cs b/PollutometerWebApi.Tests/ReadingsControllerTests.cs new file mode 100644 index 0000000..647649d --- /dev/null +++ b/PollutometerWebApi.Tests/ReadingsControllerTests.cs @@ -0,0 +1,116 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PollutometerWebApi.Models; +using System; +using System.Collections.Generic; +using System.Web.Http.Results; + +namespace PollutometerWebApi.Controllers.Tests +{ + [TestClass()] + public class ReadingsControllerTests + { + ReadingsController controller = new ReadingsController(); + + [TestMethod()] + public void GetAllReadingsTest() + { + var actionResult = controller.GetAllReadings(); + var content = ((OkNegotiatedContentResult>)actionResult).Content; + Assert.IsTrue(content.Count >= 1); + } + + + [TestMethod()] + public void GetReadingTest() + { + var actionResult = controller.GetReading(0); + Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult)); + } + + [TestMethod()] + public void GetLatestReadingTest() + { + var actionResult = controller.GetLatestReading(); + var content = ((OkNegotiatedContentResult)actionResult).Content; + Assert.IsNotNull(content); + } + + [TestMethod()] + public void GetReadingsFromLastWeekTest() + { + var actionResult = controller.GetReadingsFromLastWeek(); + var content = ((OkNegotiatedContentResult>)actionResult).Content; + foreach(var reading in content) + { + var now = DateTimeOffset.Now.ToUnixTimeSeconds(); + Assert.IsTrue(reading.TimeStamp < now && reading.TimeStamp > now - 7*24*3600); + } + } + + [TestMethod()] + public void PostReadingPassTest() + { + var newReading = new Reading + { + TimeStamp = (int)DateTimeOffset.Now.ToUnixTimeSeconds(), + Co = 1.0, + No = 1.0, + So = 1.0 + }; + var actionResult = controller.PostReading(newReading); + var reading = controller.GetLatestReading(); + var content = ((OkNegotiatedContentResult)reading).Content; + Assert.AreEqual(newReading.TimeStamp, content.TimeStamp); + } + + [TestMethod()] + public void PostReadingFailTest() + { + var actionResult = controller.PostReading(null); + Assert.IsInstanceOfType(actionResult, typeof(BadRequestResult)); + } + + [TestMethod()] + public void PutReadingPassTest() + { + var latest = controller.GetLatestReading(); + var content = ((OkNegotiatedContentResult)latest).Content; + var actionResult = controller.PutReading(content.Id, + new Reading + { + TimeStamp = (int)DateTimeOffset.Now.ToUnixTimeSeconds(), + Co = 2.0, + No = 2.0, + So = 2.0 + } + ); + Assert.AreEqual(typeof(OkResult), actionResult.GetType()); + } + + [TestMethod()] + public void PutReadingFailTest() + { + var latest = controller.GetLatestReading(); + var content = ((OkNegotiatedContentResult)latest).Content; + var actionResult = controller.PutReading(content.Id, null); + Assert.AreEqual(typeof(BadRequestResult), actionResult.GetType()); + } + + [TestMethod()] + public void DeleteReadingPassTest() + { + var latest = controller.GetLatestReading(); + var content = ((OkNegotiatedContentResult)latest).Content; + var actionResult = controller.DeleteReading(content.Id); + var reading = controller.GetReading(content.Id); + Assert.IsInstanceOfType(reading, typeof(NotFoundResult)); + } + + [TestMethod()] + public void DeleteReadingFailTest() + { + var actionResult = controller.DeleteReading(1337); + Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult)); + } + } +} \ No newline at end of file -- cgit v1.2.3