Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transaction failures for predefined temp_id of new entities #44

Merged
merged 4 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/Todoist.Net.Tests/Services/TransactionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

using Todoist.Net.Models;
using Todoist.Net.Tests.Extensions;
Expand Down Expand Up @@ -44,5 +45,83 @@ public void CreateProjectAndCreateNote_Success()

deleteTransaction.CommitAsync().Wait();
}

[Fact]
[Trait(Constants.TraitName, Constants.IntegrationFreeTraitValue)]
public async Task CreateProjectAndCreateItem_Success()
{
var client = TodoistClientFactory.Create(_outputHelper);

var transaction = client.CreateTransaction();

var project = new Project("Shopping List");
var projectId = await transaction.Project.AddAsync(project);

var item = new Item("Buy milk")
{
ProjectId = projectId
};
await transaction.Items.AddAsync(item);

await transaction.CommitAsync();

Assert.False(string.IsNullOrEmpty(project.Id.PersistentId));
Assert.False(string.IsNullOrEmpty(item.Id.PersistentId));


var itemInfo = await client.Items.GetAsync(item.Id);

Assert.Equal(itemInfo.Item.Content, item.Content);
Assert.Equal(itemInfo.Project.Name, project.Name);
Assert.Equal(itemInfo.Project.Id.PersistentId, project.Id.PersistentId);


await client.Projects.DeleteAsync(project.Id);

var projects = await client.Projects.GetAsync();

Assert.DoesNotContain(projects, p => p.Id.PersistentId == project.Id.PersistentId);
}

[Fact]
[Trait(Constants.TraitName, Constants.IntegrationFreeTraitValue)]
public async Task CreateProjectAndCreateItemWithPredefinedTempId_Success()
{
var client = TodoistClientFactory.Create(_outputHelper);

var project = new Project("Shopping List")
{
Id = new ComplexId(Guid.NewGuid()) // predefined temp id
};
var item = new Item("Buy milk")
{
ProjectId = project.Id // predefined temp id
};

var transaction = client.CreateTransaction();

await transaction.Project.AddAsync(project);
await transaction.Items.AddAsync(item);

await transaction.CommitAsync();

Assert.False(string.IsNullOrEmpty(project.Id.PersistentId));
Assert.False(string.IsNullOrEmpty(item.Id.PersistentId));


var itemInfo = await client.Items.GetAsync(item.Id);

Assert.Equal(itemInfo.Item.Content, item.Content);
Assert.Equal(itemInfo.Project.Name, project.Name);
Assert.Equal(itemInfo.Project.Id.PersistentId, project.Id.PersistentId);


await client.Projects.DeleteAsync(project.Id);

var projects = await client.Projects.GetAsync();

Assert.DoesNotContain(projects, p => p.Id.PersistentId == project.Id.PersistentId);
}

}
}
6 changes: 5 additions & 1 deletion src/Todoist.Net/Services/CommandServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ protected CommandServiceBase(ICollection<Command> queue)

internal Command CreateAddCommand<T>(CommandType commandType, T entity) where T : BaseEntity
{
var tempId = Guid.NewGuid();
var tempId = entity.Id.TempId;
if (tempId == Guid.Empty)
{
tempId = Guid.NewGuid();
}
entity.Id = tempId;

return new Command(commandType, entity, tempId);
Expand Down