Exemplo n.º 1
0
        public async Task <IActionResult> UpsertTestAnalysis([FromRoute] Guid testId, [FromBody] ReplaceTestAnalysisDto testAnalysisDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var testExists = await _context.LimpingTests.AnyAsync(test => test.Id == testId);

            if (!testExists)
            {
                return(NotFound());
            }

            // Convert it to db model
            var testAnalysis = new TestAnalysis
            {
                Description     = testAnalysisDto.Description,
                EndValue        = testAnalysisDto.EndValue.GetValueOrDefault(),
                LimpingSeverity = testAnalysisDto.LimpingSeverity.GetValueOrDefault(),
                LimpingTestId   = testId,
            };

            // Replace it
            await _testAnalysesService.ReplaceTestAnalysis(testId, testAnalysis);

            // Convert it to HAL
            var response = new GetTestAnalysisResponse(testAnalysis, LinkGenerator.Analysis.Edit(testAnalysis.Id.ToString(), "self"));

            return(Ok(response));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] CreateLimpingTestDto createDto)
        {
            if (!ModelState.IsValid || string.IsNullOrWhiteSpace(createDto.TestData))
            {
                return(BadRequest(ModelState));
            }

            var userExists = await _context.AppUsers.AnyAsync(user => user.Id == createDto.AppUserId);

            if (!userExists)
            {
                return(NotFound("The user was not found"));
            }

            var testAnalysisDto = createDto.TestAnalysis;

            // Convert it to database model
            var testAnalysis = new TestAnalysis
            {
                Description     = testAnalysisDto.Description,
                EndValue        = testAnalysisDto.EndValue.GetValueOrDefault(),
                LimpingSeverity = testAnalysisDto.LimpingSeverity.GetValueOrDefault(),
            };
            // Save it
            var createdTest = await _limpingTestsService.InsertTest(createDto.AppUserId, createDto.TestData, testAnalysis);

            // Create the HAL response
            var response = new GetLimpingTestResponse(createdTest, selfLink: LinkGenerator.LimpingTests.Create("self"));

            return(Ok(response));
        }
Exemplo n.º 3
0
        private async Task <LimpingTest> AddDefaultDataForAnalysis(LimpingDbContext context)
        {
            var analysis = new TestAnalysis
            {
                Id              = Guid.NewGuid(),
                Description     = "Something",
                EndValue        = 1.5,
                LimpingSeverity = LimpingSeverityEnum.Medium,
            };
            var appUser = context.AppUsers.Add(new AppUser
            {
                Id           = "1",
                UserName     = "******",
                LimpingTests = new List <LimpingTest>(),
                Email        = "f",
            }).Entity;
            var limpingTest = context.LimpingTests.Add(new LimpingTest
            {
                AppUserId    = appUser.Id,
                Date         = DateTime.Now,
                TestAnalysis = analysis,
                TestData     = "{numbers: [1, 2, 3]}"
            }).Entity;
            await context.LimpingTests.AddAsync(limpingTest);

            await context.SaveChangesAsync();

            context.Entry(limpingTest).State = EntityState.Detached;
            context.Entry(appUser).State     = EntityState.Detached;
            context.Entry(analysis).State    = EntityState.Detached;
            return(limpingTest);
        }
Exemplo n.º 4
0
 public TestAnalysisDto(TestAnalysis analysis)
 {
     Id              = analysis.Id;
     EndValue        = analysis.EndValue;
     Description     = analysis.Description;
     LimpingSeverity = analysis.LimpingSeverity;
     LimpingTestId   = analysis.LimpingTestId;
 }
Exemplo n.º 5
0
        public async Task EditTestAnalysis(TestAnalysis editedTest)
        {
            var found = await _context.TestAnalyses.FindAsync(editedTest.Id);

            _context.Entry(found).CurrentValues.SetValues(editedTest);
            _context.Entry(found).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }
Exemplo n.º 6
0
        public async Task TestAnalysesTesting()
        {
            using (var scope = _fixture.Server.Host.Services.CreateScope())
            {
                // Setup
                var databaseFixture = scope.ServiceProvider.GetRequiredService <DatabaseFixture>();
                var context         = databaseFixture.Context;
                var limpingTest     = await AddDefaultDataForAnalysis(context);

                var service = scope.ServiceProvider.GetRequiredService <ITestAnalysesService>();

                // Edit an analysis
                var editAnalysis = new TestAnalysis
                {
                    Id              = limpingTest.TestAnalysis.Id,
                    Description     = "ok",
                    EndValue        = 1.9,
                    LimpingSeverity = LimpingSeverityEnum.High,
                    LimpingTestId   = limpingTest.Id,
                };
                await service.EditTestAnalysis(editAnalysis);

                context.Entry(editAnalysis).State = EntityState.Detached;
                var found = await context.TestAnalyses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == editAnalysis.Id);

                // Test that it was correctly edited
                Assert.Equal(editAnalysis.Description, found.Description);
                Assert.Equal(editAnalysis.EndValue, found.EndValue);
                Assert.Equal(editAnalysis.LimpingSeverity, found.LimpingSeverity);

                // You cannot edit something that doesn't exist
                limpingTest.TestAnalysis.Id = Guid.NewGuid();
                await Assert.ThrowsAnyAsync <Exception>(() => service.EditTestAnalysis(limpingTest.TestAnalysis));

                // Test replacing
                var newAnalysis = new TestAnalysis
                {
                    Id              = Guid.NewGuid(),
                    Description     = "ko",
                    EndValue        = 1.5,
                    LimpingSeverity = LimpingSeverityEnum.High,
                    LimpingTestId   = limpingTest.Id,
                };
                await service.ReplaceTestAnalysis(editAnalysis.LimpingTestId, newAnalysis);

                context.Entry(newAnalysis).State = EntityState.Detached;
                var replacedAnalysis = await context.TestAnalyses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == newAnalysis.Id);

                var nullAnalysis = await context.TestAnalyses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == editAnalysis.Id);

                // The replaced test is there
                Assert.NotNull(replacedAnalysis);

                // The old one is not
                Assert.Null(nullAnalysis);
            }
        }
Exemplo n.º 7
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var testClass = new TestAnalysis();

            MyAssert.RunTests();
            //testClass.TestPassedMethod();
            //testClass.TestFailedMethod();

            //_results.Add(testClass.TestFailed());
            //var testInfo = testClass.TestPassed();
            //MessageBox.Show($"{testInfo.DidTestPass} - {testInfo.TestFailureMessage ?? "Test Passed" }");
        }
Exemplo n.º 8
0
        public async Task ReplaceTestAnalysis(Guid testId, TestAnalysis newTest)
        {
            var test = await _context.LimpingTests.Include(x => x.TestAnalysis).SingleAsync(x => x.Id == testId);

            if (test.TestAnalysis != null)
            {
                _context.TestAnalyses.Remove(test.TestAnalysis);
            }

            test.TestAnalysis          = newTest;
            _context.Entry(test).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }
Exemplo n.º 9
0
        public async Task <LimpingTest> InsertTest(string userId, string testData, TestAnalysis testAnalysis)
        {
            var limpingTest = new LimpingTest
            {
                AppUserId    = userId,
                TestData     = testData,
                Date         = DateTime.Now,
                TestAnalysis = testAnalysis
            };

            _context.LimpingTests.Add(limpingTest);
            await _context.SaveChangesAsync();

            return(limpingTest);
        }
Exemplo n.º 10
0
 public GetTestAnalysisResponse(TestAnalysis analysis, Link selfLink = null) : base(new TestAnalysisDto(analysis))
 {
     if (selfLink == null)
     {
         this.AddLinks(
             LinkGenerator.Analysis.GetSingle(analysis.Id.ToString(), "self")
             );
     }
     else
     {
         this.AddLinks(selfLink);
     }
     this.AddLinks(
         LinkGenerator.Analysis.Edit(analysis.Id.ToString()),
         LinkGenerator.LimpingTests.GetSingle(analysis.LimpingTestId.ToString())
         );
 }
Exemplo n.º 11
0
        public async Task <LimpingTest> EditTest(Guid testId, string testData, TestAnalysis testAnalysis)
        {
            var limpingTest = await _context.LimpingTests.FindAsync(testId);

            _context.Entry(limpingTest).State = EntityState.Modified;
            limpingTest.TestData = testData;
            if (testAnalysis != null)
            {
                _context.Entry(limpingTest).Reference(x => x.TestAnalysis).Load();
                var oldAnalysis = limpingTest.TestAnalysis;
                _context.Entry(oldAnalysis).State = EntityState.Modified;
                oldAnalysis.Description           = testAnalysis.Description;
                oldAnalysis.EndValue        = testAnalysis.EndValue;
                oldAnalysis.LimpingSeverity = testAnalysis.LimpingSeverity;
            }
            limpingTest.Date = DateTime.Now;
            await _context.SaveChangesAsync();

            return(limpingTest);
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Edit([FromRoute] Guid testId, [FromBody] EditLimpingTestDto editTestDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var exists = await _context.LimpingTests.AnyAsync(lt => lt.Id == testId);

            if (!exists)
            {
                return(NotFound());
            }

            var testAnalysisDto = editTestDto.TestAnalysis;
            // Convert it to database model
            TestAnalysis testAnalysis = null;

            if (testAnalysisDto != null)
            {
                testAnalysis = new TestAnalysis
                {
                    Description     = testAnalysisDto.Description,
                    EndValue        = testAnalysisDto.EndValue.GetValueOrDefault(),
                    LimpingSeverity = testAnalysisDto.LimpingSeverity.GetValueOrDefault(),
                    LimpingTestId   = testId
                };
            }

            // Save it
            var edited = await _limpingTestsService.EditTest(testId, editTestDto.TestData, testAnalysis);

            // Create HAL response
            var response = new GetLimpingTestResponse(edited, selfLink: LinkGenerator.LimpingTests.Edit(edited.Id.ToString(), "self"));

            return(Ok(response));
        }
        /// <summary>
        /// Determines if a test analysis is normal (within normal limits) based on this normative analysis.
        /// </summary>
        /// <param name="analysis">The analysis results to check.</param>
        /// <returns>True if the test analysis is within normal limits, otherwise false.</returns>
        public bool IsNormal(TestAnalysis analysis)
        {
            foreach(TestAnalysisComponent c in analysis.Components.Values)
                if(!IsNormal(c))
                    return false;

            //no analysis was abnormal, so the result is normal
            return true;
        }