Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Added more unit tests and fixed two issues with cosmos id
Browse files Browse the repository at this point in the history
  • Loading branch information
Elfocrash committed Mar 30, 2018
1 parent 6ab8a91 commit c66c708
Show file tree
Hide file tree
Showing 14 changed files with 506 additions and 75 deletions.
4 changes: 3 additions & 1 deletion Cosmonaut.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ static void Main(string[] args)
var provider = serviceCollection.BuildServiceProvider();

var cosmoStore = provider.GetService<ICosmosStore<Book>>();
cosmoStore.RemoveAsync(x => true).GetAwaiter().GetResult();

var books = new List<Book>();
for (int i = 0; i < 50; i++)
{
books.Add(new Book
{
CosmosId = Guid.NewGuid().ToString(),
Id = Guid.NewGuid().ToString(),
Name = "Test " + i
});
}
Expand Down
5 changes: 3 additions & 2 deletions Cosmonaut.Models/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
namespace Cosmonaut.Models
{
[CosmosCollection(Throughput = 1000)]
public class Book : ICosmosEntity
public class Book
{
public string Name { get; set; }

public TestUser Author { get; set; }

public string CosmosId { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
}
2 changes: 1 addition & 1 deletion Cosmonaut.Tests/Cosmonaut.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.2" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
Expand Down
72 changes: 72 additions & 0 deletions Cosmonaut.Tests/CosmosAddTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Threading.Tasks;
using Cosmonaut.Exceptions;
using Cosmonaut.Response;
using Xunit;

namespace Cosmonaut.Tests
{
public class CosmosAddTests
{
private readonly ICosmosStore<Dummy> _dummyStore;

public CosmosAddTests()
{
_dummyStore = new InMemoryCosmosStore<Dummy>();
}

[Fact]
public async Task AddValidObjectSuccess()
{
// Arrange
var id = Guid.NewGuid().ToString();
var dummy = new Dummy
{
Id = id,
Name = "Nick"
};

// Act
var expectedResponse = new CosmosResponse<Dummy>(dummy, CosmosOperationStatus.Success);
var result = await _dummyStore.AddAsync(dummy);

//Assert
Assert.Equal(expectedResponse.Entity, result.Entity);
Assert.Equal(expectedResponse.IsSuccess, result.IsSuccess);
}

[Fact]
public async Task AddEntityWithoutIdEmptyGeneratedId()
{
// Arrange
var dummy = new Dummy
{
Name = "Nick"
};

// Act
var result = await _dummyStore.AddAsync(dummy);

//Assert
var isGuid = Guid.TryParse(result.Entity.Id, out var guid);
Assert.True(isGuid);
Assert.NotEqual(Guid.Empty, guid);
}

[Fact]
public async Task AddingEntityWithoutIdThrowsException()
{
// Arrange
var dummy = new
{
Name = "Name"
};

// Act
var addTask = new InMemoryCosmosStore<object>().AddAsync(dummy);

//Assert
await Assert.ThrowsAsync<CosmosEntityWithoutIdException<object>>(() => addTask);
}
}
}
37 changes: 0 additions & 37 deletions Cosmonaut.Tests/CosmosCrudTests.cs

This file was deleted.

107 changes: 107 additions & 0 deletions Cosmonaut.Tests/CosmosRemoveTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cosmonaut.Response;
using Xunit;

namespace Cosmonaut.Tests
{
public class CosmosRemoveTests
{
private readonly ICosmosStore<Dummy> _dummyStore;

public CosmosRemoveTests()
{
_dummyStore = new InMemoryCosmosStore<Dummy>();
}

[Fact]
public async Task RemoveEntityRemoves()
{
// Assign
var id = Guid.NewGuid().ToString();
var addedDummy = new Dummy
{
Id = id,
Name = "Test"
};
await _dummyStore.AddAsync(addedDummy);

// Act
var result = await _dummyStore.RemoveAsync(addedDummy);

// Assert
Assert.True(result.IsSuccess);
Assert.Equal(CosmosOperationStatus.Success, result.CosmosOperationStatus);
}

[Fact]
public async Task RemoveByIdRemoves()
{
// Assign
var id = Guid.NewGuid().ToString();
var addedDummy = new Dummy
{
Id = id,
Name = "Test"
};
await _dummyStore.AddAsync(addedDummy);

// Act
var result = await _dummyStore.RemoveByIdAsync(id);

// Assert
Assert.True(result.IsSuccess);
Assert.Equal(CosmosOperationStatus.Success, result.CosmosOperationStatus);
}

[Fact]
public async Task RemoveByExpressionRemoves()
{
// Assign
foreach (var i in Enumerable.Range(0, 10))
{
var id = Guid.NewGuid().ToString();
var addedDummy = new Dummy
{
Id = id,
Name = "Test " + i
};
await _dummyStore.AddAsync(addedDummy);
}

// Act
var result = await _dummyStore.RemoveAsync(x => x.Name.Contains("Test"));

// Assert
Assert.True(result.IsSuccess);
Assert.Empty(result.FailedEntities);
}

[Fact]
public async Task RemoveRangeRemoves()
{
// Assign
var addedList = new List<Dummy>();
foreach (var i in Enumerable.Range(0, 10))
{
var id = Guid.NewGuid().ToString();
var addedDummy = new Dummy
{
Id = id,
Name = "Test " + i
};
await _dummyStore.AddAsync(addedDummy);
addedList.Add(addedDummy);
}

// Act
var result = await _dummyStore.RemoveRangeAsync(addedList);

// Assert
Assert.True(result.IsSuccess);
Assert.Empty(result.FailedEntities);
}
}
}
85 changes: 85 additions & 0 deletions Cosmonaut.Tests/CosmosUpdateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cosmonaut.Response;
using Xunit;

namespace Cosmonaut.Tests
{
public class CosmosUpdateTests
{
private readonly ICosmosStore<Dummy> _dummyStore;

public CosmosUpdateTests()
{
_dummyStore = new InMemoryCosmosStore<Dummy>();
}

[Fact]
public async Task UpdateEntityUpdates()
{
// Arrange
var id = Guid.NewGuid().ToString();
var addedDummy = new Dummy
{
Id = id,
Name = "Test"
};
var expectedName = "NewTest";
await _dummyStore.AddAsync(addedDummy);

// Act
addedDummy.Name = expectedName;
var result = await _dummyStore.UpdateAsync(addedDummy);

// Assert
Assert.Equal(expectedName, result.Entity.Name);
}

[Fact]
public async Task UpdateRangeUpdatesEntities()
{
// Arrange
var addedEntities = new List<Dummy>();
for (int i = 0; i < 10; i++)
{
var id = Guid.NewGuid().ToString();
var addedDummy = new Dummy
{
Id = id,
Name = "UpdateMe"
};
var added = await _dummyStore.AddAsync(addedDummy);
addedEntities.Add(added.Entity);
}

// Act
var result = await _dummyStore.UpdateRangeAsync();

// Assert
Assert.True(result.IsSuccess);
Assert.Empty(result.FailedEntities);
}

[Fact]
public async Task UpdateEntityThatHasIdChangedFails()
{
// Arrange
var id = Guid.NewGuid().ToString();
var addedDummy = new Dummy
{
Id = id,
Name = "Test"
};
await _dummyStore.AddAsync(addedDummy);

// Act
addedDummy.Id = Guid.NewGuid().ToString();
var result = await _dummyStore.UpdateAsync(addedDummy);

// Assert
Assert.False(result.IsSuccess);
Assert.Equal(CosmosOperationStatus.ResourceNotFound, result.CosmosOperationStatus);
}
}
}
Loading

0 comments on commit c66c708

Please sign in to comment.