aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Zelent <zelent.marcin@protonmail.com>2017-12-12 14:24:47 +0100
committerMarcin Zelent <zelent.marcin@protonmail.com>2017-12-12 14:24:47 +0100
commite4e7dbf553bfa17a39830e67d14d3715946beab3 (patch)
treebf6c34aaeecb938f9ea11612d214a887ceb5d7ca /PollutometerWebApi.Tests/ReadingsControllerTests.cs
parent54d1019413958e0ba24b85007c1f5f97ca19a0b3 (diff)
Added unit tests and fixed some bugs.
Diffstat (limited to 'PollutometerWebApi.Tests/ReadingsControllerTests.cs')
-rw-r--r--PollutometerWebApi.Tests/ReadingsControllerTests.cs116
1 files changed, 116 insertions, 0 deletions
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<List<Reading>>)actionResult).Content;
+ Assert.IsTrue(content.Count >= 1);
+ }
+
+
+ [TestMethod()]
+ public void GetReadingTest()
+ {
+ var actionResult = controller.GetReading(0);
+ Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult<Reading>));
+ }
+
+ [TestMethod()]
+ public void GetLatestReadingTest()
+ {
+ var actionResult = controller.GetLatestReading();
+ var content = ((OkNegotiatedContentResult<Reading>)actionResult).Content;
+ Assert.IsNotNull(content);
+ }
+
+ [TestMethod()]
+ public void GetReadingsFromLastWeekTest()
+ {
+ var actionResult = controller.GetReadingsFromLastWeek();
+ var content = ((OkNegotiatedContentResult<List<Reading>>)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>)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<Reading>)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<Reading>)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<Reading>)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