public void CanListEntitiesByPage() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity1 = new Post(0, 1, "Title1", "Description1", 1, "Snippet1", DateTime.Now.AddDays(1), true); var entity2 = new Post(0, 2, "Title2", "Description2", 2, "SnippeTKey", DateTime.Now.AddDays(2), false); var entity3 = new Post(0, 1, "Title3", "Description3", 1, "Snippet3", DateTime.Now.AddDays(3), true); var entity4 = new Post(0, 2, "Title4", "Description4", 2, "Snippet4", DateTime.Now.AddDays(4), false); var entity5 = new Post(0, 2, "Title5", "Description5", 2, "Snippet5", DateTime.Now.AddDays(5), false); repo.AddOrUpdate(entity1); var savedEntity1 = repo.AddOrUpdate(entity2); var savedEntity2 = repo.AddOrUpdate(entity3); repo.AddOrUpdate(entity4); repo.AddOrUpdate(entity5); // Act var listEntities = repo.ListEntities(take: 2, skip: 1) ; // Assert Assert.IsTrue(listEntities.IsValid); Assert.IsTrue(listEntities.Value.Count == 2); Assert.IsTrue(savedEntity1.Value.Equals(listEntities.Value[0])); Assert.IsTrue(savedEntity2.Value.Equals(listEntities.Value[1])); }
public void CanUpdateEntity() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity = new Post(0, 1, "Title", "Description", 1, "Snippet1", DateTime.Now, true); ; var savedEntity = repo.AddOrUpdate(entity); // Act var updatedEntity = new Post(savedEntity.Value, author: 2, title: "newTitle", description: "newDesciprtion", language: 2, snippet: "newSnippet", published: DateTime.Now.AddDays(1), isActive: false); var newUpdatedEntity = repo.AddOrUpdate(updatedEntity); // Assert Assert.IsTrue(newUpdatedEntity.IsValid); Assert.IsTrue(newUpdatedEntity.Value.Equals(updatedEntity)); Assert.IsTrue((newUpdatedEntity.Value as Entity <uint>).Equals(savedEntity.Value)); }
public void CanListEntitiesBetweenDates() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var date2 = DateTime.Now.AddDays(2); var date4 = DateTime.Now.AddDays(4); var entity1 = new Post(0, 1, "Title1", "Description1", 1, "Snippet1", DateTime.Now.AddDays(1), true); var entity2 = new Post(0, 2, "Title2", "Description2", 2, "SnippeTKey", date2, false); var entity3 = new Post(0, 1, "Title3", "Description3", 1, "Snippet3", DateTime.Now.AddDays(3), true); var entity4 = new Post(0, 2, "Title4", "Description4", 2, "Snippet4", date4, false); var entity5 = new Post(0, 2, "Title5", "Description5", 2, "Snippet5", DateTime.Now.AddDays(5), false); repo.AddOrUpdate(entity1); var savedEntity1 = repo.AddOrUpdate(entity2); var savedEntity2 = repo.AddOrUpdate(entity3); var savedEntity3 = repo.AddOrUpdate(entity4); repo.AddOrUpdate(entity5); var filters = ImmutableList.Create(new Filter <Post>("BetweenPublished", (date2, date4))); // Act var listEntities = repo.ListEntities(filters, 50) ; // Assert Assert.IsTrue(listEntities.IsValid); Assert.IsTrue(listEntities.Value.Count == 3); Assert.IsTrue(savedEntity1.Value.Equals(listEntities.Value[0])); Assert.IsTrue(savedEntity2.Value.Equals(listEntities.Value[1])); Assert.IsTrue(savedEntity3.Value.Equals(listEntities.Value[2])); }
public static void ListPostInThread(SqlitePostRepository repository, int threadId) { var thread = repository.GetThreadWithId(threadId); var posts = repository.GetPostsFromThread(thread); Console.WriteLine($"Topic: {thread.Topic}\nText: {thread.Text}\n"); foreach (var post in posts) { Console.WriteLine($"PostId: {post.PostId} {post.User.FirstName} (UserId: {post.UserId}): {post.Text}"); } Console.WriteLine(); }
public void ListEntitiesIsSafe() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); // Act _conn.Dispose(); var listEntities = repo.ListEntities(); // Assert Assert.IsFalse(listEntities.IsValid); }
public void AddOrUpdateEntityIsRequired() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); // Act var result = repo.AddOrUpdate(null); // Assert Assert.IsFalse(result.IsValid); Assert.IsTrue(result.Exception is ArgumentNullException); }
public void CanCreateEntity() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity = new Post(0, 0, "Title", "Description", 0, "Snippet"); // Act var newEntity = repo.AddOrUpdate(entity); // Assert Assert.IsTrue(newEntity.IsValid); Assert.IsTrue(newEntity.Value.Id == 1); }
public void AddOrUpdateIsSafe() { // Arrange var conn = GetConnectionString(); var entity = new Post(0, 0, "Title", "Description", 0, "Snippet"); var repo = new SqlitePostRepository(conn); // Act _conn.Dispose(); var newEntity = repo.AddOrUpdate(entity); // Assert Assert.IsFalse(newEntity.IsValid); }
public void CanGetEntityById() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity = new Post(0, 0, "Title", "Description", 0, "Snippet"); var savedEntity = repo.AddOrUpdate(entity); // Act var result = repo.GetById(savedEntity.Value.Id); // Assert Assert.IsTrue(result.IsValid); Assert.IsTrue(result.Value.Equals(savedEntity.Value)); }
public void CanListEntities() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity = new Post(0, 0, "Title", "Description", 0, "Snippet"); var savedEntity = repo.AddOrUpdate(entity); // Act var listEntities = repo.ListEntities(); // Assert Assert.IsTrue(listEntities.IsValid); Assert.IsTrue(listEntities.Value.Any()); Assert.IsTrue(savedEntity.Value.Equals(listEntities.Value.First())); }
public void CanListEntitiesByIdAndTitleNotFound() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity1 = new Post(0, 1, "Title1", "Description1", 1, "Snippet1", DateTime.Now.AddDays(1), true); var entity2 = new Post(0, 2, "Title2", "Description2", 2, "SnippeTKey", DateTime.Now.AddDays(2), false); repo.AddOrUpdate(entity1); repo.AddOrUpdate(entity2); var filters = ImmutableList.Create(new[] { new Filter <Post>("Id", (uint)1), new Filter <Post>("Title", "Title2") }); // Act var listEntities = repo.ListEntities(filters); // Assert Assert.IsTrue(listEntities.IsValid); Assert.IsFalse(listEntities.Value.Any()); }
public void CanListEntitiesInactive() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity1 = new Post(0, 0, "Title1", "Description1", 0, "Snippet1", isActive: true); _ = repo.AddOrUpdate(entity1); var entity2 = new Post(0, 0, "Title2", "Description2", 0, "SnippeTKey", isActive: false); var savedEntity2 = repo.AddOrUpdate(entity2); // Act var listEntities = repo.ListEntities(status: Status.Inactive); // Assert Assert.IsTrue(listEntities.IsValid); Assert.IsTrue(listEntities.Value.Count == 1); Assert.IsTrue(savedEntity2.Value.Equals(listEntities.Value.First())); }
public void CanListEntitiesDesc() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity1 = new Post(0, 1, "Title1", "Description1", 1, "Snippet1", DateTime.Now.AddDays(1), false); var entity2 = new Post(0, 2, "Title2", "Description2", 2, "SnippeTKey", DateTime.Now.AddDays(2), true); repo.AddOrUpdate(entity1); var savedEntity2 = repo.AddOrUpdate(entity2); // Act var listEntities = repo.ListEntities(desc: true); // Assert Assert.IsTrue(listEntities.IsValid); Assert.IsTrue(listEntities.Value.Any()); Assert.IsTrue(savedEntity2.Value.Equals(listEntities.Value.First())); }
public void CanListEntitiesActiveAndByTitle() { // Arrange var conn = GetConnectionString(); var repo = new SqlitePostRepository(conn); var entity1 = new Post(0, 0, "Title1", "Description1", 0, "Snippet1", isActive: true); var savedEntity1 = repo.AddOrUpdate(entity1); var entity2 = new Post(0, 0, "Title2", "Description2", 0, "SnippeTKey", isActive: false); _ = repo.AddOrUpdate(entity2); var filters = ImmutableList.Create(new Filter <Post>("Title", "Title1")); // Act var listEntities = repo.ListEntities(filters: filters, status: Status.Active); // Assert Assert.IsTrue(listEntities.IsValid); Assert.IsTrue(listEntities.Value.Count == 1); Assert.IsTrue(savedEntity1.Value.Equals(listEntities.Value.First())); }
static void Main(string[] args) { var repo = new SqliteInitialization(); repo.PrintVersion(); var postRepo = new SqlitePostRepository(); var threadRepo = new SqliteThreadRepository(); var userRepo = new SqliteUserRepository(); while (true) { Console.WriteLine("Write 1 to show all threads. \n" + "Write 2 to show all users. \n"); string x = ""; x = Console.ReadLine(); if (x == "1") { Console.Clear(); threadRepo.GetThreads(); while (true) { PrintThreads(threadRepo); var people = threadRepo.GetThreads(); Console.WriteLine("Write the id of the thread you want to view." + "\nWrite c to create a new thread." + "\nWrite x to go back."); string input = Console.ReadLine(); if (input == "x") { Console.Clear(); break; } if (input == "c") { Console.Clear(); Thread newThread = new Thread(); Console.WriteLine("Write a topic:"); newThread.Topic = Console.ReadLine(); Console.WriteLine("Write a text:"); newThread.Text = Console.ReadLine(); newThread.PostCount = 100; threadRepo.AddThread(newThread); Console.WriteLine("Thread added!"); continue; } int threadId = int.Parse(input); Console.Clear(); var threadCreator = userRepo.GetPersonWithIdWhoCreatedThread(threadId); Console.WriteLine("User: "******"Write 1 to add a post"); Console.WriteLine("Write 2 to edit a post"); Console.WriteLine("Write 3 to delete a post"); Console.WriteLine("Write x to go back."); string threadInput = Console.ReadLine(); if (threadInput == "x") { Console.Clear(); break; } if (threadInput == "1") { Console.WriteLine(threadId); Post newPost = new Post(); Console.WriteLine("Write a text:"); newPost.Text = Console.ReadLine(); Console.WriteLine("Write the owners ID:"); newPost.UserId = Console.ReadLine(); newPost.ThreadId = threadId.ToString(); postRepo.AddPost(newPost); var obj = threadRepo.GetThreadWithId(threadId); obj.PostCount = obj.PostCount + 1; threadRepo.UpdateThreadPostCount(obj); Console.Clear(); break; } if (threadInput == "2") { Console.WriteLine("What is the ID of the post you want to edit?"); string editPostId = Console.ReadLine(); int editPostInt = int.Parse(editPostId); Post postObject = new Post(); postObject = postRepo.GetPostWithId(editPostInt); Console.WriteLine("Write the new text."); string newText = Console.ReadLine(); postObject.Text = newText; postRepo.UpdatePost(postObject); } if (threadInput == "3") { Console.WriteLine("What is the ID of the post you want to delete?"); string deletePostId = Console.ReadLine(); int editPostInt = int.Parse(deletePostId); Post postObject = new Post(); postObject = postRepo.GetPostWithId(editPostInt); postRepo.DeletePost(postObject); var obj = threadRepo.GetThreadWithId(threadId); obj.PostCount = obj.PostCount - 1; threadRepo.UpdateThreadPostCount(obj); Console.Clear(); break; } } } continue; } if (x == "2") { Console.Clear(); userRepo.GetUsers(); PrintUsers(userRepo); continue; } x = ""; } }