From d5deafeb0f02e40c298740483526beb778d89a78 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Mon, 13 May 2024 12:20:47 +0500 Subject: [PATCH] Fix Test for TelemetryController.PostMessagesAsync(..) Time to utc. --- .../Controllers/TelemetryControllerTest.cs | 126 +++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/TelemetryControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/TelemetryControllerTest.cs index e8abd06f..8622f11b 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/TelemetryControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/TelemetryControllerTest.cs @@ -24,6 +24,12 @@ public class TelemetryControllerTest : BaseIntegrationTest BlockPosition = 5, BlockSpeed = 5, }]; + private readonly TelemetryInfoDto telemetryInfoDto = new() + { + TimeZoneId = timezone.TimezoneId, + TimeZoneOffsetTotalHours = timezone.Hours, + Cluster = "cluster1", + }; public TelemetryControllerTest(WebAppFactoryFixture factory) : base(factory) @@ -48,12 +54,69 @@ public class TelemetryControllerTest : BaseIntegrationTest } [Fact] - public async Task PostUsersAsync() + public async Task PostInfoAsync() { // arrange dbContext.CleanupDbSet(); // act + var response = await client.PostInfoAsync(uid, telemetryInfoDto, CancellationToken.None); + + // Assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var telemetriesCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + } + + [Fact] + public async Task PostInfoAsync_twice_should_be_ok() + { + // arrange + dbContext.CleanupDbSet(); + + // act + _ = await client.PostInfoAsync(uid, telemetryInfoDto, CancellationToken.None); + var response = await client.PostInfoAsync(uid, telemetryInfoDto, CancellationToken.None); + + // Assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var telemetriesCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + } + + [Fact] + public async Task PostUsersAsync() + { + // arrange + dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); + + // act + var response = await client.PostUsersAsync(uid, users, CancellationToken.None); + + // Assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var telemetriesCount = dbContext.Set().Count(); + var telemetryUserCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + Assert.Equal(1, telemetryUserCount); + } + + [Fact] + public async Task PostUsers_twice_should_be_ok() + { + // arrange + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); + + // act + _ = await client.PostUsersAsync(uid, users, CancellationToken.None); var response = await client.PostUsersAsync(uid, users, CancellationToken.None); // Assert @@ -69,7 +132,10 @@ public class TelemetryControllerTest : BaseIntegrationTest public async Task PostEventsAsync() { // arrange + dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); // act var response = await client.PostEventsAsync(uid, events, CancellationToken.None); @@ -83,12 +149,40 @@ public class TelemetryControllerTest : BaseIntegrationTest Assert.Equal(1, telemetryEventCount); } + [Fact] + public async Task PostEventsAsync_twice_should_be_ok() + { + // arrange + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); + + // act + _ = await client.PostEventsAsync(uid, events, CancellationToken.None); + var response = await client.PostEventsAsync(uid, events, CancellationToken.None); + + // Assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var telemetriesCount = dbContext.Set().Count(); + var telemetryEventCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + Assert.Equal(1, telemetryEventCount); + } + [Fact] public async Task PostMessagesAsync() { // arrange + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); + // act _ = await client.PostEventsAsync(uid, events, CancellationToken.None); _ = await client.PostUsersAsync(uid, users, CancellationToken.None); @@ -106,4 +200,34 @@ public class TelemetryControllerTest : BaseIntegrationTest Assert.Equal(1, telemetryUserCount); Assert.Equal(1, telemetryMessageCount); } + + [Fact] + public async Task PostMessagesAsync_twice_should_be_ok() + { + // arrange + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); + + // act + _ = await client.PostEventsAsync(uid, events, CancellationToken.None); + _ = await client.PostUsersAsync(uid, users, CancellationToken.None); + _ = await client.PostMessagesAsync(uid, messages, CancellationToken.None); + var response = await client.PostMessagesAsync(uid, messages, CancellationToken.None); + + // Assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var telemetriesCount = dbContext.Set().Count(); + var telemetryEventCount = dbContext.Set().Count(); + var telemetryUserCount = dbContext.Set().Count(); + var telemetryMessageCount = dbContext.Set().Count(); + + Assert.Equal(1, telemetriesCount); + Assert.Equal(1, telemetryEventCount); + Assert.Equal(1, telemetryUserCount); + Assert.Equal(2, telemetryMessageCount); + } }