public async Task Can_Upsert_When_No_Expire_Given() { var guid = Guid.NewGuid(); var guidString = guid.ToString("N").ToUpper(); var raw = new { user = "******" }; var defaultExpire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds(); _repoMock.UpsertGuidData(Arg.Any <GuidData>()).Returns((new GuidData() { Guid = guid, User = raw.user, Expire = defaultExpire }, false)); var req = GetHttpRequestMessage(raw, $"guid/{guidString}", HttpMethod.Post); var resp = await _client.SendAsync(req); Assert.Equal(HttpStatusCode.Created, resp.StatusCode); var actual = JsonConvert.DeserializeObject <GuidDataResponse>(await resp.Content.ReadAsStringAsync()); Assert.NotNull(actual); Assert.Equal(guidString, actual.Guid); Assert.Equal(raw.user, actual.User); Assert.Equal(defaultExpire.ToString(), actual.Expire); await _repoMock.Received(1).UpsertGuidData(Arg.Any <GuidData>()); }
public async Task Can_Upsert_Returns_GuidData_Using_30_Days_Expire_When_No_Expire_Given() { var guid = Guid.NewGuid(); var guidString = guid.ToString("N").ToUpper(); var request = new GuidDataRequest() { User = "******", Expire = null }; long autoGeneratedExpire = 0; await _repoMock.UpsertGuidData(Arg.Do <GuidData>(x => autoGeneratedExpire = x.Expire)); var actual = await _provider.UpsertGuidData(guidString, request); Assert.True(autoGeneratedExpire > 0); await _repoMock.Received(1).UpsertGuidData(Arg.Any <GuidData>()); }
/// <summary> /// Set cache after data created or updated /// </summary> /// <param name="guidData">The guidData</param> /// <returns>The affected data and: <c>true</c> if updating was performed vs creating; otherwise, <c>false</c></returns> public async Task <(GuidData, bool)> UpsertGuidData(GuidData guidData) { var(upserted, isUpdated) = await _decorated.UpsertGuidData(guidData); if (upserted != null) { await _cache.SetStringAsync(_cacheKey(upserted.Guid), JsonConvert.SerializeObject(upserted), _expire(upserted.Expire)); } return(upserted, isUpdated); }
public async void Can_Set_To_Cache_When_Upserted() { var guid = Guid.NewGuid(); var guidString = guid.ToString("N").ToUpper(); var expected = new GuidData() { Guid = guid, User = "******", Expire = DateTimeOffset.UtcNow.AddDays(30).ToUnixTimeSeconds() }; _dbMock.UpsertGuidData(Arg.Is <GuidData>(x => x.Guid == expected.Guid)).Returns((expected, true)); var(actual, isUpdated) = await _repo.UpsertGuidData(expected); Assert.NotNull(actual); Assert.Equal(expected.Guid, actual.Guid); Assert.Equal(expected.User, actual.User); Assert.Equal(expected.Expire, actual.Expire); await _dbMock.Received(1).UpsertGuidData(Arg.Any <GuidData>()); await _cacheMock.Received(1).SetAsync(Arg.Any <string>(), Arg.Any <byte[]>(), Arg.Any <DistributedCacheEntryOptions>()); }