Skip to content

Commit

Permalink
Test green
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLarsenNZ committed Jun 14, 2021
1 parent 3cb7779 commit 351eeae
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ obj
.suo
.user
.vs
appsettings.json
16 changes: 15 additions & 1 deletion DataLayer/DataLayer.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Content Include="..\..\..\..\.nuget\packages\microsoft.azure.cosmos\3.19.0\contentFiles\any\netstandard2.0\ThirdPartyNotice.txt" Link="ThirdPartyNotice.txt">
<Private>False</Private>
<NuGetPackageVersion>3.19.0</NuGetPackageVersion>
<NuGetItemType>Content</NuGetItemType>
<NuGetPackageId>Microsoft.Azure.Cosmos</NuGetPackageId>
<Pack>false</Pack>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.19.0" />
</ItemGroup>

</Project>
36 changes: 35 additions & 1 deletion DataLayer/PeopleData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
using System;
using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DataLayer
{
/// <summary>
/// A Data Access Layer (DAL) object for People.
/// </summary>
public class PeopleData
{
private readonly Container _container;
private string ContainerId { get; set; }

public PeopleData(CosmosClient client, string databaseId, string containerId = "People")
{
ContainerId = containerId;
_container = client.GetContainer(databaseId, containerId);
}

/// <summary>
/// Gets all people.
/// </summary>
/// <returns><see cref="IEnumerable{Person}"/></returns>
public async Task<IEnumerable<Person>> GetAll()
{
// Define a Query
var query = new QueryDefinition($"SELECT * FROM {ContainerId}");

// Get an Iterator
var iterator = _container.GetItemQueryIterator<Person>(query);

// Get the first page of results
var response = await iterator.ReadNextAsync();

//TODO: Paging

return response.Resource;
}
}
}
17 changes: 17 additions & 0 deletions DataLayer/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataLayer
{
/// <summary>
/// A Person Model
/// </summary>
/// <remarks>AKA DTO (data transfer object), POCO (plain old c# object)</remarks>
public class Person
{
//TODO: Properties
}
}
30 changes: 30 additions & 0 deletions DataLayerTests/CosmosTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace DataLayerTests
{
/// <summary>
/// Test helper base class
/// </summary>
public abstract class CosmosTest
{
// load configuration from appsettings.json file
protected static readonly IConfiguration _config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false)
.Build();

// Constants for App Settings keys
protected const string CosmosDataConnectionStringKey = "Cosmos_ConnectionString";
protected const string CosmosDataDatabaseIdKey = "Cosmos_DatabaseId";

public CosmosTest()
{
// Ensure app settings exist
if (string.IsNullOrEmpty(_config[CosmosDataConnectionStringKey]))
throw new InvalidOperationException($"{CosmosDataConnectionStringKey} must be set");
if (string.IsNullOrEmpty(_config[CosmosDataDatabaseIdKey]))
throw new InvalidOperationException($"{CosmosDataDatabaseIdKey} must be set"); }
}
}
14 changes: 14 additions & 0 deletions DataLayerTests/DataLayerTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.19.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DataLayer\DataLayer.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.example.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
13 changes: 10 additions & 3 deletions DataLayerTests/PeopleDataTests.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
using DataLayer;
using Microsoft.Azure.Cosmos;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Threading.Tasks;

namespace DataLayerTests
{
[TestClass]
[TestCategory("Integration")]
public class PeopleDataTests
public class PeopleDataTests : CosmosTest
{
[TestMethod]
// Marks this test as an Integration test to that it can be excluded from CI Builds.
[TestCategory("Integration")]
public async Task GetAll()
{
// arrange
var data = new PeopleData();

// Create a CosmosClient
var client = new CosmosClient(_config["Cosmos_ConnectionString"]);

// Inject into PeopleData. IRL we would do this with IoC
var data = new PeopleData(client, _config["Cosmos_DatabaseId"]);

// act
var people = await data.GetAll();
Expand Down
9 changes: 9 additions & 0 deletions DataLayerTests/appsettings.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug"
}
},
"CosmosData_ConnectionString": "",
"CosmosData_DatabaseId": ""
}

0 comments on commit 351eeae

Please sign in to comment.