Skip to content

Commit 39411f7

Browse files
authored
Fix transaction failures for predefined temp_id of new entities (#44)
* Added transaction tests for predefined temp_id as discussed in #43 * Added a check in command service for existing temp_id before creating a new one. Which should fix failing tests for predefined temp_id transactions. * Used `Guid.Empty` instead of `default` for better readability. * Updated transaction tests with `async/await`.
1 parent a90eb4f commit 39411f7

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/Todoist.Net.Tests/Services/TransactionTests.cs

+79
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23

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

4546
deleteTransaction.CommitAsync().Wait();
4647
}
48+
49+
[Fact]
50+
[Trait(Constants.TraitName, Constants.IntegrationFreeTraitValue)]
51+
public async Task CreateProjectAndCreateItem_Success()
52+
{
53+
var client = TodoistClientFactory.Create(_outputHelper);
54+
55+
var transaction = client.CreateTransaction();
56+
57+
var project = new Project("Shopping List");
58+
var projectId = await transaction.Project.AddAsync(project);
59+
60+
var item = new Item("Buy milk")
61+
{
62+
ProjectId = projectId
63+
};
64+
await transaction.Items.AddAsync(item);
65+
66+
await transaction.CommitAsync();
67+
68+
Assert.False(string.IsNullOrEmpty(project.Id.PersistentId));
69+
Assert.False(string.IsNullOrEmpty(item.Id.PersistentId));
70+
71+
72+
var itemInfo = await client.Items.GetAsync(item.Id);
73+
74+
Assert.Equal(itemInfo.Item.Content, item.Content);
75+
Assert.Equal(itemInfo.Project.Name, project.Name);
76+
Assert.Equal(itemInfo.Project.Id.PersistentId, project.Id.PersistentId);
77+
78+
79+
await client.Projects.DeleteAsync(project.Id);
80+
81+
var projects = await client.Projects.GetAsync();
82+
83+
Assert.DoesNotContain(projects, p => p.Id.PersistentId == project.Id.PersistentId);
84+
}
85+
86+
[Fact]
87+
[Trait(Constants.TraitName, Constants.IntegrationFreeTraitValue)]
88+
public async Task CreateProjectAndCreateItemWithPredefinedTempId_Success()
89+
{
90+
var client = TodoistClientFactory.Create(_outputHelper);
91+
92+
var project = new Project("Shopping List")
93+
{
94+
Id = new ComplexId(Guid.NewGuid()) // predefined temp id
95+
};
96+
var item = new Item("Buy milk")
97+
{
98+
ProjectId = project.Id // predefined temp id
99+
};
100+
101+
var transaction = client.CreateTransaction();
102+
103+
await transaction.Project.AddAsync(project);
104+
await transaction.Items.AddAsync(item);
105+
106+
await transaction.CommitAsync();
107+
108+
Assert.False(string.IsNullOrEmpty(project.Id.PersistentId));
109+
Assert.False(string.IsNullOrEmpty(item.Id.PersistentId));
110+
111+
112+
var itemInfo = await client.Items.GetAsync(item.Id);
113+
114+
Assert.Equal(itemInfo.Item.Content, item.Content);
115+
Assert.Equal(itemInfo.Project.Name, project.Name);
116+
Assert.Equal(itemInfo.Project.Id.PersistentId, project.Id.PersistentId);
117+
118+
119+
await client.Projects.DeleteAsync(project.Id);
120+
121+
var projects = await client.Projects.GetAsync();
122+
123+
Assert.DoesNotContain(projects, p => p.Id.PersistentId == project.Id.PersistentId);
124+
}
125+
47126
}
48127
}

src/Todoist.Net/Services/CommandServiceBase.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ protected CommandServiceBase(ICollection<Command> queue)
2626

2727
internal Command CreateAddCommand<T>(CommandType commandType, T entity) where T : BaseEntity
2828
{
29-
var tempId = Guid.NewGuid();
29+
var tempId = entity.Id.TempId;
30+
if (tempId == Guid.Empty)
31+
{
32+
tempId = Guid.NewGuid();
33+
}
3034
entity.Id = tempId;
3135

3236
return new Command(commandType, entity, tempId);

0 commit comments

Comments
 (0)