Skip to content

Commit

Permalink
chore: better error messages for assertions (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
cprice404 authored Oct 20, 2022
1 parent e7a7501 commit efbf52f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
8 changes: 1 addition & 7 deletions tests/Integration/Momento.Sdk.Tests/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ public SimpleCacheClientFixture()
throw new NullReferenceException("TEST_CACHE_NAME environment variable must be set.");
Client = new(Configurations.Laptop.Latest(), AuthProvider, defaultTtl: DefaultTtl);

try
{
var result = Client.CreateCacheAsync(CacheName).Result;
}
catch (AlreadyExistsException)
{
}
var result = Client.CreateCacheAsync(CacheName).Result;
}

public void Dispose()
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/Momento.Sdk.Tests/SimpleCacheControlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void SimpleCacheClientConstructor_NullJWT_InvalidJwtException()
public async Task DeleteCacheAsync_NullCache_InvalidArgumentError()
{
DeleteCacheResponse deleteResponse = await client.DeleteCacheAsync(null!);
Assert.True(deleteResponse is DeleteCacheResponse.Error);
Assert.True(deleteResponse is DeleteCacheResponse.Error, $"Unexpected response: {deleteResponse}");
DeleteCacheResponse.Error errorResponse = (DeleteCacheResponse.Error)deleteResponse;
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, errorResponse.InnerException.ErrorCode);
}
Expand All @@ -50,7 +50,7 @@ public async Task DeleteCacheAsync_NullCache_InvalidArgumentError()
public async Task DeleteCacheAsync_CacheDoesntExist_NotFoundException()
{
DeleteCacheResponse response = await client.DeleteCacheAsync("non-existent cache");
Assert.True(response is DeleteCacheResponse.Error);
Assert.True(response is DeleteCacheResponse.Error, $"Unexpected response: {response}");
var errorResponse = (DeleteCacheResponse.Error)response;
Assert.Equal(MomentoErrorCode.NOT_FOUND_ERROR, errorResponse.InnerException.ErrorCode);
}
Expand All @@ -59,7 +59,7 @@ public async Task DeleteCacheAsync_CacheDoesntExist_NotFoundException()
public async Task CreateCacheAsync_NullCache_InvalidArgumentError()
{
CreateCacheResponse response = await client.CreateCacheAsync(null!);
Assert.True(response is CreateCacheResponse.Error);
Assert.True(response is CreateCacheResponse.Error, $"Unexpected response: {response}");
CreateCacheResponse.Error errorResponse = (CreateCacheResponse.Error)response;
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, errorResponse.InnerException.ErrorCode);
}
Expand All @@ -74,15 +74,15 @@ public async Task ListCachesAsync_OneCache_HappyPath()

// Test cache exists
ListCachesResponse result = await client.ListCachesAsync();
Assert.True(result is ListCachesResponse.Success);
Assert.True(result is ListCachesResponse.Success, $"Unexpected response: {result}");
var successResult = (ListCachesResponse.Success)result;
List<CacheInfo> caches = successResult.Caches;
Assert.Contains(new CacheInfo(cacheName), caches);

// Test deleting cache
await client.DeleteCacheAsync(cacheName);
result = await client.ListCachesAsync();
Assert.True(result is ListCachesResponse.Success);
Assert.True(result is ListCachesResponse.Success, $"Unexpected response: {result}");
var successResult2 = (ListCachesResponse.Success)result;
var caches2 = successResult2.Caches;
Assert.DoesNotContain(new CacheInfo(cacheName), caches2);
Expand All @@ -107,7 +107,7 @@ public async Task ListCachesAsync_Iteration_HappyPath()
ListCachesResponse result = await client.ListCachesAsync();
while (true)
{
Assert.True(result is ListCachesResponse.Success);
Assert.True(result is ListCachesResponse.Success, $"Unexpected response: {result}");
var successResult = (ListCachesResponse.Success)result;
foreach (CacheInfo cache in successResult.Caches)
{
Expand Down
60 changes: 30 additions & 30 deletions tests/Integration/Momento.Sdk.Tests/SimpleCacheDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public SimpleCacheDataTest(SimpleCacheClientFixture fixture)
public async Task SetAsync_NullChecksByteArrayByteArray_IsError(string cacheName, byte[] key, byte[] value)
{
CacheSetResponse response = await client.SetAsync(cacheName, key, value);
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
var errorResponse = (CacheSetResponse.Error)response;
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, errorResponse.ErrorCode);

response = await client.SetAsync(cacheName, key, value, defaultTtl);
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
errorResponse = (CacheSetResponse.Error)response;
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, errorResponse.ErrorCode);
}
Expand All @@ -43,7 +43,7 @@ public async Task SetAsync_NullChecksByteArrayByteArray_IsError(string cacheName
public async Task SetAsync_InvalidTTLByteArrayByteArray_IsError(int ttlSeconds)
{
CacheSetResponse response = await client.SetAsync(cacheName, new byte[] { 0x00 }, new byte[] { 0x00 }, TimeSpan.FromSeconds(ttlSeconds));
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
var errorResponse = (CacheSetResponse.Error)response;
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, errorResponse.ErrorCode);
}
Expand All @@ -56,7 +56,7 @@ public async Task SetAsync_KeyIsByteArrayValueIsByteArray_HappyPath()
byte[] value = Utils.NewGuidByteArray();
await client.SetAsync(cacheName, key, value);
CacheGetResponse response = await client.GetAsync(cacheName, key);
Assert.True(response is CacheGetResponse.Hit);
Assert.True(response is CacheGetResponse.Hit, $"Unexpected response: {response}");
var goodResponse = (CacheGetResponse.Hit)response;
byte[] setValue = goodResponse.ValueByteArray;
Assert.Equal(value, setValue);
Expand All @@ -65,7 +65,7 @@ public async Task SetAsync_KeyIsByteArrayValueIsByteArray_HappyPath()
value = Utils.NewGuidByteArray();
await client.SetAsync(cacheName, key, value, ttl: TimeSpan.FromSeconds(15));
response = await client.GetAsync(cacheName, key);
Assert.True(response is CacheGetResponse.Hit);
Assert.True(response is CacheGetResponse.Hit, $"Unexpected response: {response}");
goodResponse = (CacheGetResponse.Hit)response;
setValue = goodResponse.ValueByteArray;
Assert.Equal(value, setValue);
Expand All @@ -77,7 +77,7 @@ public async Task SetAsync_KeyIsByteArrayValueIsByteArray_HappyPath()
public async Task GetAsync_NullChecksByteArray_IsError(string cacheName, byte[] key)
{
CacheGetResponse response = await client.GetAsync(cacheName, key);
Assert.True(response is CacheGetResponse.Error);
Assert.True(response is CacheGetResponse.Error, $"Unexpected response: {response}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheGetResponse.Error)response).ErrorCode);
}

Expand All @@ -88,11 +88,11 @@ public async Task GetAsync_NullChecksByteArray_IsError(string cacheName, byte[]
public async Task SetAsync_NullChecksStringString_IsError(string cacheName, string key, string value)
{
CacheSetResponse response = await client.SetAsync(cacheName, key, value);
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheSetResponse.Error)response).ErrorCode);

response = await client.SetAsync(cacheName, key, value, defaultTtl);
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheSetResponse.Error)response).ErrorCode);
}

Expand All @@ -102,7 +102,7 @@ public async Task SetAsync_NullChecksStringString_IsError(string cacheName, stri
public async Task SetAsync_InvalidTTLStringString_IsError(int ttlSeconds)
{
CacheSetResponse response = await client.SetAsync(cacheName, "hello", "world", TimeSpan.FromSeconds(ttlSeconds));
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
var errorResponse = (CacheSetResponse.Error)response;
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, errorResponse.ErrorCode);
}
Expand All @@ -115,10 +115,10 @@ public async Task SetAsync_KeyIsStringValueIsString_HappyPath()
string key = Utils.NewGuidString();
string value = Utils.NewGuidString();
var setResponse = await client.SetAsync(cacheName, key, value);
Assert.True(setResponse is CacheSetResponse.Success);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");

CacheGetResponse response = await client.GetAsync(cacheName, key);
Assert.True(response is CacheGetResponse.Hit);
Assert.True(response is CacheGetResponse.Hit, $"Unexpected response: {response}");
var goodResponse = (CacheGetResponse.Hit)response;
string setValue = goodResponse.ValueString;
Assert.Equal(value, setValue);
Expand All @@ -127,10 +127,10 @@ public async Task SetAsync_KeyIsStringValueIsString_HappyPath()
key = Utils.NewGuidString();
value = Utils.NewGuidString();
setResponse = await client.SetAsync(cacheName, key, value, ttl: TimeSpan.FromSeconds(15));
Assert.True(setResponse is CacheSetResponse.Success);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");

response = await client.GetAsync(cacheName, key);
Assert.True(response is CacheGetResponse.Hit);
Assert.True(response is CacheGetResponse.Hit, $"Unexpected response: {response}");
goodResponse = (CacheGetResponse.Hit)response;
setValue = goodResponse.ValueString;
Assert.Equal(value, setValue);
Expand All @@ -142,7 +142,7 @@ public async Task SetAsync_KeyIsStringValueIsString_HappyPath()
public async Task GetAsync_NullChecksString_IsError(string cacheName, string key)
{
CacheGetResponse response = await client.GetAsync(cacheName, key);
Assert.True(response is CacheGetResponse.Error);
Assert.True(response is CacheGetResponse.Error, $"Unexpected response: {response}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheGetResponse.Error)response).ErrorCode);
}

Expand All @@ -153,11 +153,11 @@ public async Task GetAsync_NullChecksString_IsError(string cacheName, string key
public async Task SetAsync_NullChecksStringByteArray_IsError(string cacheName, string key, byte[] value)
{
CacheSetResponse response = await client.SetAsync(cacheName, key, value);
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheSetResponse.Error)response).ErrorCode);

response = await client.SetAsync(cacheName, key, value, defaultTtl);
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheSetResponse.Error)response).ErrorCode);
}

Expand All @@ -167,7 +167,7 @@ public async Task SetAsync_NullChecksStringByteArray_IsError(string cacheName, s
public async Task SetAsync_InvalidTTLStringByteArray_IsError(int ttlSeconds)
{
CacheSetResponse response = await client.SetAsync(cacheName, "hello", new byte[] { 0x00 }, TimeSpan.FromSeconds(ttlSeconds));
Assert.True(response is CacheSetResponse.Error);
Assert.True(response is CacheSetResponse.Error, $"Unexpected response: {response}");
var errorResponse = (CacheSetResponse.Error)response;
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, errorResponse.ErrorCode);
}
Expand All @@ -180,7 +180,7 @@ public async Task SetAsync_KeyIsStringValueIsByteArray_HappyPath()
string key = Utils.NewGuidString();
byte[] value = Utils.NewGuidByteArray();
var setResponse = await client.SetAsync(cacheName, key, value);
Assert.True(setResponse is CacheSetResponse.Success);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");

CacheGetResponse response = await client.GetAsync(cacheName, key);
var goodResponse = (CacheGetResponse.Hit)response;
Expand All @@ -191,7 +191,7 @@ public async Task SetAsync_KeyIsStringValueIsByteArray_HappyPath()
key = Utils.NewGuidString();
value = Utils.NewGuidByteArray();
setResponse = await client.SetAsync(cacheName, key, value, ttl: TimeSpan.FromSeconds(15));
Assert.True(setResponse is CacheSetResponse.Success);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");

response = await client.GetAsync(cacheName, key);
var anotherGoodResponse = (CacheGetResponse.Hit)response;
Expand All @@ -205,10 +205,10 @@ public async Task GetAsync_ExpiredTtl_HappyPath()
string key = Utils.NewGuidString();
string value = Utils.NewGuidString();
var setResponse = await client.SetAsync(cacheName, key, value, TimeSpan.FromSeconds(1));
Assert.True(setResponse is CacheSetResponse.Success);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");
await Task.Delay(3000);
CacheGetResponse result = await client.GetAsync(cacheName, key);
Assert.True(result is CacheGetResponse.Miss);
Assert.True(result is CacheGetResponse.Miss, $"Unexpected response: {result}");
}

[Theory]
Expand All @@ -217,7 +217,7 @@ public async Task GetAsync_ExpiredTtl_HappyPath()
public async Task DeleteAsync_NullChecksByte_IsError(string cacheName, byte[] key)
{
CacheDeleteResponse result = await client.DeleteAsync(cacheName, key);
Assert.True(result is CacheDeleteResponse.Error);
Assert.True(result is CacheDeleteResponse.Error, $"Unexpected response: {result}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheDeleteResponse.Error)result).ErrorCode);
}

Expand All @@ -228,17 +228,17 @@ public async Task DeleteAsync_KeyIsByteArray_HappyPath()
byte[] key = new byte[] { 0x01, 0x02, 0x03, 0x04 };
byte[] value = new byte[] { 0x05, 0x06, 0x07, 0x08 };
var setResponse = await client.SetAsync(cacheName, key, value, ttl: TimeSpan.FromMinutes(1));
Assert.True(setResponse is CacheSetResponse.Success);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");
CacheGetResponse getResponse = await client.GetAsync(cacheName, key);
Assert.True(getResponse is CacheGetResponse.Hit);
Assert.True(getResponse is CacheGetResponse.Hit, $"Unexpected response: {getResponse}");

// Delete
var deleteResponse = await client.DeleteAsync(cacheName, key);
Assert.True(deleteResponse is CacheDeleteResponse.Success);
Assert.True(deleteResponse is CacheDeleteResponse.Success, $"Unexpected response: {deleteResponse}");

// Check deleted
getResponse = await client.GetAsync(cacheName, key);
Assert.True(getResponse is CacheGetResponse.Miss);
Assert.True(getResponse is CacheGetResponse.Miss, $"Unexpected response: {getResponse}");
}

[Theory]
Expand All @@ -247,7 +247,7 @@ public async Task DeleteAsync_KeyIsByteArray_HappyPath()
public async Task DeleteAsync_NullChecksString_IsError(string cacheName, string key)
{
CacheDeleteResponse response = await client.DeleteAsync(cacheName, key);
Assert.True(response is CacheDeleteResponse.Error);
Assert.True(response is CacheDeleteResponse.Error, $"Unexpected response: {response}");
Assert.Equal(MomentoErrorCode.INVALID_ARGUMENT_ERROR, ((CacheDeleteResponse.Error)response).ErrorCode);
}

Expand All @@ -258,15 +258,15 @@ public async Task DeleteAsync_KeyIsString_HappyPath()
string key = Utils.NewGuidString();
string value = Utils.NewGuidString();
var setResponse = await client.SetAsync(cacheName, key, value, ttl: TimeSpan.FromMinutes(1));
Assert.True(setResponse is CacheSetResponse.Success);
Assert.True(setResponse is CacheSetResponse.Success, $"Unexpected response: {setResponse}");
CacheGetResponse getResponse = await client.GetAsync(cacheName, key);
Assert.True(getResponse is CacheGetResponse.Hit);
Assert.True(getResponse is CacheGetResponse.Hit, $"Unexpected response: {getResponse}");

// Delete
await client.DeleteAsync(cacheName, key);

// Check deleted
getResponse = await client.GetAsync(cacheName, key);
Assert.True(getResponse is CacheGetResponse.Miss);
Assert.True(getResponse is CacheGetResponse.Miss, $"Unexpected response: {getResponse}");
}
}

0 comments on commit efbf52f

Please sign in to comment.