Exemplo n.º 1
0
        public async Task When_AddAsync_ThenToDoAdded()
        {
            // Arrange

            var entityDataStoreOptions =
                new EntityDataStoreOptions
            {
                ConnectionString = _configuration["AzureSqlOptions:ConnectionString"]
            };

            var azureTokenProviderOptions =
                new AzureTokenProviderOptions
            {
                Authority    = _configuration["TokenProviderOptions:Authority"],
                ClientId     = _configuration["TokenProviderOptions:ClientId"],
                ClientSecret = _configuration["TokenProviderOptions:ClientSecret"],
                ResourceId   = "https://database.windows.net/",
                TenantId     = _configuration["TokenProviderOptions:TenantId"]
            };

            var azureTokenProvider =
                new AzureTokenProvider(
                    azureTokenProviderOptions);

            var toDoEntityDataStore =
                new ToDoEntityDataStore(
                    azureTokenProvider,
                    entityDataStoreOptions);

            var toDoEntity =
                _faker.GenerateToDoEntity();

            // Action

            await toDoEntityDataStore.AddAsync(
                toDoEntity);

            // Assert

            var query =
                "SELECT * FROM todos WHERE Id = @Id";

            using var cn  = new SqlConnection(_configuration["AzureSqlOptions:ConnectionString"]);
            using var cmd = new SqlCommand(query, cn);

            cn.AccessToken =
                await azureTokenProvider.GetTokenAsync();

            cn.Open();

            cmd.Parameters.AddWithValue("@Id", toDoEntity.Id);

            var reader = await cmd.ExecuteReaderAsync();

            ToDoEntity toDoEntityFetched = null;

            while (reader.Read())
            {
                toDoEntityFetched = new ToDoEntity
                {
                    Id          = reader.GetGuid(0),
                    Status      = reader.GetString(1),
                    Description = reader.GetString(2),
                    CreatedOn   = reader.GetDateTime(3)
                };
            }

            toDoEntityFetched.Should().NotBeNull();
            toDoEntityFetched.Id.Should().Be(toDoEntity.Id);
            toDoEntityFetched.Status.Should().Be(toDoEntity.Status);
            toDoEntityFetched.Description.Should().Be(toDoEntity.Description);
            toDoEntityFetched.CreatedOn.Should().BeCloseTo(toDoEntity.CreatedOn);
        }