From c0049696bf925e9a62eeab293aa10c508de981b3 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Fri, 12 Feb 2021 19:05:35 -0500 Subject: [PATCH] TwitterUserDal implementation + tests --- .../TwitterUserPostgresDal.cs | 16 ++-- .../TwitterUserPostgresDalTests.cs | 80 ++++++++++++++++++- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs index edee63d..714eb8f 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/TwitterUserPostgresDal.cs @@ -5,9 +5,7 @@ using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base; using BirdsiteLive.DAL.Postgres.Settings; -using BirdsiteLive.DAL.Postgres.Tools; using Dapper; -using Npgsql; namespace BirdsiteLive.DAL.Postgres.DataAccessLayers { @@ -44,7 +42,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers { dbConnection.Open(); - var result = (await dbConnection.QueryAsync(query, new { acct = acct })).FirstOrDefault(); + var result = (await dbConnection.QueryAsync(query, new { acct })).FirstOrDefault(); return result; } } @@ -90,7 +88,15 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers public async Task GetAllTwitterUsersAsync() { - throw new NotImplementedException(); + var query = $"SELECT * FROM {_settings.TwitterUserTableName}"; + + using (var dbConnection = Connection) + { + dbConnection.Open(); + + var result = await dbConnection.QueryAsync(query); + return result.ToArray(); + } } public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, DateTime lastSync) @@ -112,7 +118,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers public async Task DeleteTwitterUserAsync(string acct) { - if (acct == default) throw new ArgumentException("acct"); + if (string.IsNullOrWhiteSpace(acct)) throw new ArgumentException("acct"); acct = acct.ToLowerInvariant(); diff --git a/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/TwitterUserPostgresDalTests.cs b/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/TwitterUserPostgresDalTests.cs index d71842f..b290735 100644 --- a/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/TwitterUserPostgresDalTests.cs +++ b/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/TwitterUserPostgresDalTests.cs @@ -50,6 +50,24 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers Assert.IsTrue(result.Id > 0); } + [TestMethod] + public async Task CreateAndGetUser_byId() + { + var acct = "myid"; + var lastTweetId = 1548L; + + var dal = new TwitterUserPostgresDal(_settings); + + await dal.CreateTwitterUserAsync(acct, lastTweetId); + var result = await dal.GetTwitterUserAsync(acct); + var resultById = await dal.GetTwitterUserAsync(result.Id); + + Assert.AreEqual(acct, resultById.Acct); + Assert.AreEqual(lastTweetId, resultById.LastTweetPostedId); + Assert.AreEqual(lastTweetId, resultById.LastTweetSynchronizedForAllFollowersId); + Assert.AreEqual(result.Id, resultById.Id); + } + [TestMethod] public async Task CreateUpdateAndGetUser() { @@ -75,6 +93,38 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers Assert.IsTrue(Math.Abs((now.ToUniversalTime() - result.LastSync).Milliseconds) < 100); } + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public async Task Update_NoId() + { + var dal = new TwitterUserPostgresDal(_settings); + await dal.UpdateTwitterUserAsync(default, default, default, DateTime.UtcNow); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public async Task Update_NoLastTweetPostedId() + { + var dal = new TwitterUserPostgresDal(_settings); + await dal.UpdateTwitterUserAsync(12, default, default, DateTime.UtcNow); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public async Task Update_NoLastTweetSynchronizedForAllFollowersId() + { + var dal = new TwitterUserPostgresDal(_settings); + await dal.UpdateTwitterUserAsync(12, 9556, default, DateTime.UtcNow); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public async Task Update_NoLastSync() + { + var dal = new TwitterUserPostgresDal(_settings); + await dal.UpdateTwitterUserAsync(12, 9556, 65, default); + } + [TestMethod] public async Task CreateAndDeleteUser() { @@ -93,7 +143,15 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers } [TestMethod] - public async Task GetAllTwitterUsers() + [ExpectedException(typeof(ArgumentException))] + public async Task DeleteUser_NotAcct() + { + var dal = new TwitterUserPostgresDal(_settings); + await dal.DeleteTwitterUserAsync(string.Empty); + } + + [TestMethod] + public async Task GetAllTwitterUsers_Top() { var dal = new TwitterUserPostgresDal(_settings); for (var i = 0; i < 1000; i++) @@ -147,6 +205,26 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers Assert.IsTrue(Math.Abs((acc.LastSync - oldest.ToUniversalTime()).TotalMilliseconds) < 1000); } + [TestMethod] + public async Task GetAllTwitterUsers() + { + var dal = new TwitterUserPostgresDal(_settings); + for (var i = 0; i < 1000; i++) + { + var acct = $"myid{i}"; + var lastTweetId = 1548L; + + await dal.CreateTwitterUserAsync(acct, lastTweetId); + } + + var result = await dal.GetAllTwitterUsersAsync(); + Assert.AreEqual(1000, result.Length); + Assert.IsFalse(result[0].Id == default); + Assert.IsFalse(result[0].Acct == default); + Assert.IsFalse(result[0].LastTweetPostedId == default); + Assert.IsFalse(result[0].LastTweetSynchronizedForAllFollowersId == default); + } + [TestMethod] public async Task CountTwitterUsers() {