Skip to content

Commit 277e185

Browse files
author
LoneWandererProductions
committed
Fix up some stuff
1 parent be8fe0e commit 277e185

4 files changed

Lines changed: 296 additions & 259 deletions

File tree

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,73 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: CommonExtendedObjectsTests
4-
* FILE: CommonExtendedObjectsTests/Transactions.cs
5-
* PURPOSE: Test the Transaction Log
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
3+
* PROJECT: CommonExtendedObjectsTests
4+
* FILE: CommonExtendedObjectsTests/Transactions.cs
5+
* PURPOSE: Test the Transaction Log
6+
* PROGRAMER: Peter Geinitz (Wayfarer)
77
*/
88

9+
using System.Collections.Generic;
910
using System.Linq;
10-
using CommonControls;
1111
using ExtendedSystemObjects;
1212
using Microsoft.VisualStudio.TestTools.UnitTesting;
1313

1414
namespace CommonExtendedObjectsTests
1515
{
16-
/// <summary>
17-
/// InterOps testing
18-
/// </summary>
1916
[TestClass]
2017
public class Transactions
2118
{
2219
/// <summary>
23-
/// Test Transactions.
20+
/// Dummy data item for testing complex object logging.
2421
/// </summary>
25-
[TestMethod]
26-
public void TransactionLog()
22+
private class DataItem
2723
{
28-
//TODO improve
24+
public int Id { get; set; }
25+
public string Name { get; set; }
26+
public override bool Equals(object obj) => obj is DataItem item && Name == item.Name;
27+
public override int GetHashCode() => Name.GetHashCode();
28+
}
2929

30+
/// <summary>
31+
/// Transactions the log complex workflow.
32+
/// </summary>
33+
[TestMethod]
34+
public void TransactionLog_ComplexWorkflow()
35+
{
3036
var log = new TransactionLogs();
31-
//Generate Test data for stuff to copy
32-
33-
var key = log.GetNewKey();
34-
35-
var data = new DataItem { Id = key, Name = "start" };
36-
37-
//start of the event, nothing was done yet
38-
39-
log.Add(0, data, true);
40-
41-
key = log.GetNewKey();
42-
43-
data = new DataItem { Id = key, Name = "added" };
44-
log.Add(data.Id, data, false);
4537

46-
data = new DataItem { Id = key, Name = "added Change" };
47-
log.Change(data.Id, data);
38+
// 1. Initial State (StartData) -> Key 1
39+
var initialData = new DataItem { Id = 100, Name = "start" };
40+
log.Add(100, initialData, true);
4841

49-
data = new DataItem { Id = key, Name = "another Change" };
50-
log.Change(data.Id, data);
42+
// 2. New Addition -> Key 2
43+
var addedData = new DataItem { Id = 101, Name = "added" };
44+
log.Add(101, addedData, false);
5145

52-
var logEntry = log.Changelog[1];
46+
// 3. First Change for ID 101 -> Key 3
47+
// (Because no Change entry existed yet, only an Add entry)
48+
var change1 = new DataItem { Id = 101, Name = "added Change" };
49+
log.Change(101, change1);
5350

54-
if (logEntry.Data is DataItem obj)
55-
{
56-
Assert.AreNotEqual((object)obj.Name, data.Name, "Overwrite Data");
57-
}
51+
// 4. Second Change for ID 101 -> Still Key 3
52+
// (Our logic overwrites the existing Change entry for that ID)
53+
var change2 = new DataItem { Id = 101, Name = "another Change" };
54+
log.Change(101, change2);
5855

59-
var dct = log.GetNewItems();
56+
// Index 3 is now the "another Change" entry
57+
var logEntry = log.Changelog[3];
58+
Assert.AreEqual("another Change", ((DataItem)logEntry.Data).Name);
6059

61-
Assert.AreNotEqual(dct.Count, 1, "Correct amount");
62-
63-
var item = log.GetPredecessor(1);
64-
65-
Assert.AreNotEqual(item, 0, "Correct id");
66-
67-
log.Remove(0);
60+
// Check GetNewItems (ID 101 Add [Key 2] and ID 101 Change [Key 3])
61+
var newItems = log.GetNewItems();
62+
Assert.AreEqual(2, newItems.Count);
6863

69-
Assert.AreNotEqual(log.Changelog.Count, 0, "Correct amount");
64+
// 5. Remove ID 100 -> Key 4
65+
log.Remove(100);
66+
Assert.AreEqual(4, log.Changelog.Count);
7067
}
7168

7269
/// <summary>
73-
/// Adds the item success.
70+
/// Adds the item success.
7471
/// </summary>
7572
[TestMethod]
7673
public void AddItemSuccess()
@@ -80,11 +77,12 @@ public void AddItemSuccess()
8077

8178
Assert.IsTrue(transactionLogs.Changed);
8279
Assert.AreEqual(1, transactionLogs.Changelog.Count);
83-
Assert.AreEqual("Item1", transactionLogs.Changelog[0].Data);
80+
// In auto-increment, the first key is 1
81+
Assert.AreEqual("Item1", transactionLogs.Changelog[1].Data);
8482
}
8583

8684
/// <summary>
87-
/// Removes the item success.
85+
/// Removes the item success.
8886
/// </summary>
8987
[TestMethod]
9088
public void RemoveItemSuccess()
@@ -95,11 +93,12 @@ public void RemoveItemSuccess()
9593

9694
Assert.IsTrue(transactionLogs.Changed);
9795
Assert.AreEqual(2, transactionLogs.Changelog.Count);
98-
Assert.AreEqual(LogState.Remove, transactionLogs.Changelog[1].State);
96+
// Last entry should be Remove
97+
Assert.AreEqual(LogState.Remove, transactionLogs.Changelog[2].State);
9998
}
10099

101100
/// <summary>
102-
/// Changes the item success.
101+
/// Changes the item success.
103102
/// </summary>
104103
[TestMethod]
105104
public void ChangeItemSuccess()
@@ -109,82 +108,81 @@ public void ChangeItemSuccess()
109108
transactionLogs.Change(1, "Item1_Changed");
110109

111110
Assert.IsTrue(transactionLogs.Changed);
111+
// Change overwrites existing Change entry if found,
112+
// but here we only have an Add, so it creates a new Change entry.
112113
Assert.AreEqual(2, transactionLogs.Changelog.Count);
113-
Assert.AreEqual("Item1_Changed", transactionLogs.Changelog[1].Data);
114+
Assert.AreEqual("Item1_Changed", transactionLogs.Changelog[2].Data);
114115
}
115116

116117
/// <summary>
117-
/// Gets the predecessor success.
118+
/// Gets the predecessor success.
118119
/// </summary>
119120
[TestMethod]
120121
public void GetPredecessorSuccess()
121122
{
122123
var transactionLogs = new TransactionLogs();
123-
transactionLogs.Add(1, "Item1", false);
124-
transactionLogs.Add(2, "Item2", false);
125-
transactionLogs.Change(1, "Item1_Changed");
124+
transactionLogs.Add(1, "Item1", false); // Key 1
125+
transactionLogs.Change(1, "Changed"); // Key 2
126126

127+
// Looking for the Add predecessor of the Change (Key 2)
127128
var predecessor = transactionLogs.GetPredecessor(2);
128129

129-
Assert.AreEqual(0, predecessor);
130+
Assert.AreEqual(1, predecessor);
130131
}
131132

132133
/// <summary>
133-
/// Gets the new items success.
134+
/// Gets the new items success.
134135
/// </summary>
135136
[TestMethod]
136137
public void GetNewItemsSuccess()
137138
{
138139
var transactionLogs = new TransactionLogs();
139-
transactionLogs.Add(1, "Item1", true);
140-
transactionLogs.Add(2, "Item2", false);
141-
transactionLogs.Change(1, "Item1_Changed");
140+
transactionLogs.Add(1, "Item1", true); // StartData
141+
transactionLogs.Add(2, "Item2", false); // New
142+
transactionLogs.Change(1, "Changed"); // New
142143

143144
var newItems = transactionLogs.GetNewItems();
144145

146+
// Item 2 (Add) and Item 1 (Change) are both "New" (StartData is false)
145147
Assert.AreEqual(2, newItems.Count);
146-
Assert.IsFalse(newItems.Values.Any(item => item.StartData));
147148
}
148149

149150
/// <summary>
150-
/// Gets the index of the item exists returns.
151+
/// Gets the index of the item item exists returns.
151152
/// </summary>
152153
[TestMethod]
153154
public void GetItemItemExistsReturnsIndex()
154155
{
155156
var transactionLogs = new TransactionLogs();
156-
transactionLogs.Add(1, "Item1", false);
157+
transactionLogs.Add(55, "Data", false); // Key 1
157158

158-
var index = transactionLogs.GetItem(1, LogState.Add);
159+
var key = transactionLogs.GetItem(55, LogState.Add);
159160

160-
Assert.AreEqual(0, index);
161+
Assert.AreEqual(1, key);
161162
}
162163

163164
/// <summary>
164-
/// Gets the item does not exist returns minus one.
165+
/// Gets the item item does not exist returns minus one.
165166
/// </summary>
166167
[TestMethod]
167168
public void GetItemItemDoesNotExistReturnsMinusOne()
168169
{
169170
var transactionLogs = new TransactionLogs();
170-
171-
var index = transactionLogs.GetItem(1, LogState.Add);
172-
171+
var index = transactionLogs.GetItem(999, LogState.Add);
173172
Assert.AreEqual(-1, index);
174173
}
175174

176175
/// <summary>
177-
/// Gets the new key returns correct key.
176+
/// Gets the new key returns correct key.
178177
/// </summary>
179178
[TestMethod]
180179
public void GetNewKeyReturnsCorrectKey()
181180
{
182181
var transactionLogs = new TransactionLogs();
183-
transactionLogs.Add(1, "Item1", false);
184-
185-
var newKey = transactionLogs.GetNewKey();
182+
Assert.AreEqual(1, transactionLogs.GetNewKey());
186183

187-
Assert.AreEqual(1, newKey);
184+
transactionLogs.Add(1, "Item1", false); // Consumes Key 1
185+
Assert.AreEqual(3, transactionLogs.GetNewKey());
188186
}
189187
}
190188
}

0 commit comments

Comments
 (0)