Exemplo n.º 1
0
        public void temp_controller_get_all_temps()
        {
            var tempController = new TempPlugin.TempController(_dbConnectionString);

            var addEntity = new TempPlugin.TempModel
            {
                Id       = 1,
                Value    = 1f,
                DateTime = new DateTime(2018, 12, 1, 12, 0, 0)
            };
            var addSuccess = tempController.AddTemp(addEntity);

            Assert.IsTrue(addSuccess);

            var addEntity2 = new TempPlugin.TempModel
            {
                Id       = 2,
                Value    = 2f,
                DateTime = new DateTime(2019, 12, 1, 12, 0, 0)
            };

            addSuccess = tempController.AddTemp(addEntity2);
            Assert.IsTrue(addSuccess);

            var temps = tempController.GetTemps().ToArray();

            Assert.IsNotNull(temps);
            Assert.AreEqual(2, temps.Length);

            Assert.AreEqual(addEntity2.Id, temps[0].Id);
            Assert.AreEqual(addEntity2.DateTime, temps[0].DateTime);
            Assert.AreEqual(addEntity2.Value, temps[0].Value);
            Assert.AreEqual(addEntity.Id, temps[1].Id);
            Assert.AreEqual(addEntity.DateTime, temps[1].DateTime);
            Assert.AreEqual(addEntity.Value, temps[1].Value);

            Assert.IsTrue(tempController.RemoveTemp((int)addEntity.Id));
            Assert.IsTrue(tempController.RemoveTemp((int)addEntity2.Id));
        }
Exemplo n.º 2
0
        public IResponse Handle(IRequest req)
        {
            var resp = new Response();

            resp.StatusCode = 400;
            resp.SetContent(resp.Status);
            resp.ContentType = resp.ValidContentTypes["txt"];

            if (req.Url.Segments[1] == "html")
            {
                // html temp plugin is handled via the static file plugin (returns the temperature.html)
                // this is only for the unit test
                resp.StatusCode = 200;
                resp.SetContent(resp.Status);
                resp.ContentType = resp.ValidContentTypes["html"];
            }
            else if (req.Url.Segments[1] == "json")
            {
                int pageIndex, pageSize;

                try
                {
                    pageIndex = Int16.Parse(req.Url.Parameter["pageindex"]);
                }
                catch (Exception)
                {
                    pageIndex = 1;
                }

                try
                {
                    pageSize = Int16.Parse(req.Url.Parameter["pagesize"]);
                }
                catch (Exception)
                {
                    pageSize = 20;
                }

                if (pageIndex < 1)
                {
                    pageIndex = 1;
                }
                if (pageSize < 1)
                {
                    pageSize = 20;
                }

                var tempController = new TempController();
                TempPaginatedList temps;

                if (req.Url.Segments.Length == 3)
                {
                    DateTime date;

                    try
                    {
                        date = DateTime.Parse(req.Url.Segments[2]);
                    }
                    catch (Exception)
                    {
                        date = DateTime.Today;
                    }

                    temps = tempController.GetTempsByDateAsPaginatedList(date, pageIndex, pageSize);
                }
                else
                {
                    temps = tempController.GetTempsAsPaginatedList(pageIndex, pageSize);
                }

                var tempsJson = JsonSerializer.Serialize(temps);

                resp.StatusCode  = 200;
                resp.ContentType = resp.ValidContentTypes["json"];
                resp.SetContent(tempsJson);
            }

            return(resp);
        }
Exemplo n.º 3
0
        public void temp_controller_add_get_remove_count()
        {
            var tempController = new TempPlugin.TempController(_dbConnectionString);

            var addEntity = new TempPlugin.TempModel
            {
                Id       = 1234,
                Value    = 1.12345f,
                DateTime = new DateTime(2019, 12, 1, 12, 0, 0)
            };
            var addSuccess = tempController.AddTemp(addEntity);

            Assert.IsTrue(addSuccess);

            addSuccess = tempController.AddTemp(addEntity);
            Assert.IsFalse(addSuccess);

            var count = tempController.Count();

            Assert.AreEqual(1, count);

            var countDate = tempController.CountByDate(addEntity.DateTime);

            Assert.AreEqual(1, countDate);

            var getEntity = tempController.GetTemp((int)addEntity.Id);

            Assert.IsNotNull(getEntity);
            Assert.AreEqual(addEntity.Id, getEntity.Id);
            Assert.AreEqual(addEntity.DateTime, getEntity.DateTime);
            Assert.AreEqual(addEntity.Value, getEntity.Value);

            var removeSuccess = tempController.RemoveTemp((int)addEntity.Id);

            Assert.IsTrue(removeSuccess);

            removeSuccess = tempController.RemoveTemp((int)addEntity.Id);
            Assert.IsFalse(removeSuccess);

            getEntity = tempController.GetTemp((int)addEntity.Id);
            Assert.IsNull(getEntity);

            count = tempController.Count();
            Assert.AreEqual(0, count);

            countDate = tempController.CountByDate(addEntity.DateTime);
            Assert.AreEqual(0, countDate);

            /*
             * Test without Id
             * however data can not be removed because the id is not known
             *
             * var testEntity = new TempPlugin.TempModel
             * {
             *  Value =  1.12345f,
             *  DateTime = new DateTime(2019, 12, 1, 12, 0, 0)
             * };
             * addSuccess = tempController.AddTemp(testEntity);
             * Assert.IsTrue(addSuccess);
             */
        }