aboutsummaryrefslogtreecommitdiff
blob: 647649d4058e839b844c4f1d8051a70b1c25a4bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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));
        }
    }
}