Skip to content

Commit ee78c32

Browse files
committed
.
1 parent 25e7b1c commit ee78c32

File tree

3 files changed

+63
-125
lines changed

3 files changed

+63
-125
lines changed

readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ var application = webApplicationBuilder.Build();
320320
application.UseDelta(
321321
getConnection: httpContext => httpContext.RequestServices.GetRequiredService<SqlConnection>());
322322
```
323-
<sup><a href='/src/DeltaTests/Usage.cs#L329-L335' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnection' title='Start of snippet'>anchor</a></sup>
323+
<sup><a href='/src/DeltaTests/Usage.cs#L293-L299' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnection' title='Start of snippet'>anchor</a></sup>
324324
<!-- endSnippet -->
325325

326326
To use custom connection and transaction discovery:
@@ -338,7 +338,7 @@ webApplication.UseDelta(
338338
return new(connection, transaction);
339339
});
340340
```
341-
<sup><a href='/src/DeltaTests/Usage.cs#L340-L352' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnectionAndTransaction' title='Start of snippet'>anchor</a></sup>
341+
<sup><a href='/src/DeltaTests/Usage.cs#L304-L316' title='Snippet source file'>snippet source</a> | <a href='#snippet-CustomDiscoveryConnectionAndTransaction' title='Start of snippet'>anchor</a></sup>
342342
<!-- endSnippet -->
343343

344344

@@ -525,7 +525,7 @@ Nuget: [Delta.SqlServer](https://www.nuget.org/packages/Delta.SqlServer)
525525
```cs
526526
var timeStamp = await sqlConnection.GetLastTimeStamp();
527527
```
528-
<sup><a href='/src/DeltaTests/Usage.cs#L165-L169' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetLastTimeStampSqlConnection' title='Start of snippet'>anchor</a></sup>
528+
<sup><a href='/src/DeltaTests/Usage.cs#L155-L159' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetLastTimeStampSqlConnection' title='Start of snippet'>anchor</a></sup>
529529
<!-- endSnippet -->
530530

531531

@@ -553,7 +553,7 @@ foreach (var db in trackedDatabases)
553553
Trace.WriteLine(db);
554554
}
555555
```
556-
<sup><a href='/src/DeltaTests/Usage.cs#L227-L235' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetDatabasesWithTracking' title='Start of snippet'>anchor</a></sup>
556+
<sup><a href='/src/DeltaTests/Usage.cs#L191-L199' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetDatabasesWithTracking' title='Start of snippet'>anchor</a></sup>
557557
<!-- endSnippet -->
558558

559559
Uses the following SQL:
@@ -583,7 +583,7 @@ foreach (var db in trackedTables)
583583
Trace.WriteLine(db);
584584
}
585585
```
586-
<sup><a href='/src/DeltaTests/Usage.cs#L253-L261' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetTrackedTables' title='Start of snippet'>anchor</a></sup>
586+
<sup><a href='/src/DeltaTests/Usage.cs#L217-L225' title='Snippet source file'>snippet source</a> | <a href='#snippet-GetTrackedTables' title='Start of snippet'>anchor</a></sup>
587587
<!-- endSnippet -->
588588

589589
Uses the following SQL:
@@ -608,7 +608,7 @@ Determine if change tracking is enabled for a database.
608608
```cs
609609
var isTrackingEnabled = await sqlConnection.IsTrackingEnabled();
610610
```
611-
<sup><a href='/src/DeltaTests/Usage.cs#L318-L322' title='Snippet source file'>snippet source</a> | <a href='#snippet-IsTrackingEnabled' title='Start of snippet'>anchor</a></sup>
611+
<sup><a href='/src/DeltaTests/Usage.cs#L282-L286' title='Snippet source file'>snippet source</a> | <a href='#snippet-IsTrackingEnabled' title='Start of snippet'>anchor</a></sup>
612612
<!-- endSnippet -->
613613

614614
Uses the following SQL:
@@ -635,7 +635,7 @@ Enable change tracking for a database.
635635
```cs
636636
await sqlConnection.EnableTracking();
637637
```
638-
<sup><a href='/src/DeltaTests/Usage.cs#L312-L316' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableTracking' title='Start of snippet'>anchor</a></sup>
638+
<sup><a href='/src/DeltaTests/Usage.cs#L276-L280' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableTracking' title='Start of snippet'>anchor</a></sup>
639639
<!-- endSnippet -->
640640

641641
Uses the following SQL:
@@ -663,7 +663,7 @@ Disable change tracking for a database and all tables within that database.
663663
```cs
664664
await sqlConnection.DisableTracking();
665665
```
666-
<sup><a href='/src/DeltaTests/Usage.cs#L297-L301' title='Snippet source file'>snippet source</a> | <a href='#snippet-DisableTracking' title='Start of snippet'>anchor</a></sup>
666+
<sup><a href='/src/DeltaTests/Usage.cs#L261-L265' title='Snippet source file'>snippet source</a> | <a href='#snippet-DisableTracking' title='Start of snippet'>anchor</a></sup>
667667
<!-- endSnippet -->
668668

669669
Uses the following SQL:
@@ -700,7 +700,7 @@ Enables change tracking for all tables listed, and disables change tracking for
700700
```cs
701701
await sqlConnection.SetTrackedTables(["Companies"]);
702702
```
703-
<sup><a href='/src/DeltaTests/Usage.cs#L247-L251' title='Snippet source file'>snippet source</a> | <a href='#snippet-SetTrackedTables' title='Start of snippet'>anchor</a></sup>
703+
<sup><a href='/src/DeltaTests/Usage.cs#L211-L215' title='Snippet source file'>snippet source</a> | <a href='#snippet-SetTrackedTables' title='Start of snippet'>anchor</a></sup>
704704
<!-- endSnippet -->
705705

706706
Uses the following SQL:

src/Delta.EFTests/Usage.cs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,16 @@ public async Task GetLastTimeStamp()
4343
}
4444

4545
[Test]
46-
public async Task LastTimeStampRowVersion()
46+
public async Task LastTimeStamp()
4747
{
4848
await using var database = await LocalDb();
49-
50-
var context = database.Context;
51-
var emptyTimeStamp = await context.GetLastTimeStamp();
52-
IsNotEmpty(emptyTimeStamp);
53-
IsNotNull(emptyTimeStamp);
54-
55-
var entity = new Company
56-
{
57-
Content = "The company"
58-
};
59-
await database.AddData(entity);
60-
var addTimeStamp = await context.GetLastTimeStamp();
61-
IsNotEmpty(addTimeStamp);
62-
IsNotNull(addTimeStamp);
63-
AreNotEqual(addTimeStamp, emptyTimeStamp);
64-
65-
entity.Content = "The company2";
66-
await context.SaveChangesAsync();
67-
var updateTimeStamp = await context.GetLastTimeStamp();
68-
IsNotEmpty(updateTimeStamp);
69-
IsNotNull(updateTimeStamp);
70-
AreNotEqual(updateTimeStamp, addTimeStamp);
71-
AreNotEqual(updateTimeStamp, emptyTimeStamp);
72-
}
73-
74-
[Test]
75-
public async Task LastTimeStampRowVersionAndTracking()
76-
{
77-
await using var database = await LocalDb();
78-
79-
await database.Connection.EnableTracking();
8049
var context = database.Context;
50+
//seed with an entity so there is something in transaction log
51+
await database.AddData(
52+
new Company
53+
{
54+
Content = "The company"
55+
});
8156
var emptyTimeStamp = await context.GetLastTimeStamp();
8257
IsNotEmpty(emptyTimeStamp);
8358
IsNotNull(emptyTimeStamp);
@@ -99,6 +74,5 @@ public async Task LastTimeStampRowVersionAndTracking()
9974
IsNotNull(updateTimeStamp);
10075
AreNotEqual(updateTimeStamp, addTimeStamp);
10176
AreNotEqual(updateTimeStamp, emptyTimeStamp);
102-
10377
}
10478
}

src/DeltaTests/Usage.cs

Lines changed: 47 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,18 @@ public static void ShouldExecute(WebApplicationBuilder builder)
2929
}
3030

3131
[Test]
32-
public async Task LastTimeStampRowVersion()
32+
public async Task LastTimeStamp()
3333
{
3434
await using var database = await LocalDb();
3535

3636
var timeStamp = await DeltaExtensions.GetLastTimeStamp(database.Connection, null);
37+
//seed with an entity so there is something in transaction log
38+
await AddEntity(database.Connection);
3739
IsNotEmpty(timeStamp);
3840
IsNotNull(timeStamp);
3941
Recording.Start();
40-
await using (var command = database.Connection.CreateCommand())
41-
{
42-
command.CommandText =
43-
$"""
44-
insert into [Companies] (Id, Content)
45-
values ('{Guid.NewGuid()}', 'The company')
46-
""";
47-
await command.ExecuteNonQueryAsync();
48-
}
42+
43+
await AddEntity(database.Connection);
4944

5045
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(database.Connection, null);
5146
IsNotEmpty(newTimeStamp);
@@ -54,90 +49,85 @@ insert into [Companies] (Id, Content)
5449
}
5550

5651
[Test]
57-
public async Task LastTimeStampRowVersionOnUpdate()
52+
public async Task LastTimeStampOnUpdate()
5853
{
5954
await using var database = await LocalDb();
6055
await using var connection = database.Connection;
61-
await connection.EnableTracking();
6256

63-
var companyGuid = Guid.NewGuid();
64-
await using var addDataCommand = connection.CreateCommand();
65-
addDataCommand.CommandText =
66-
$"""
67-
insert into [Companies] (Id, Content)
68-
values ('{companyGuid}', 'The company')
69-
""";
70-
await addDataCommand.ExecuteNonQueryAsync();
57+
var companyGuid = await AddEntity(connection);
7158

7259
var timeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
7360
IsNotEmpty(timeStamp);
7461
IsNotNull(timeStamp);
7562

76-
await using (var updateCommand = connection.CreateCommand())
77-
{
78-
updateCommand.CommandText =
79-
$"""
80-
UPDATE [Companies]
81-
SET [Content] = 'New Content Value'
82-
WHERE [Id] = @Id;
83-
""";
84-
updateCommand.Parameters.AddWithValue("@Id", companyGuid);
85-
await updateCommand.ExecuteNonQueryAsync();
86-
}
63+
await UpdateEntity(connection, companyGuid);
8764

8865
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
8966
IsNotEmpty(newTimeStamp);
9067
IsNotNull(newTimeStamp);
9168
AreNotEqual(newTimeStamp, timeStamp);
9269
}
9370

71+
static async Task UpdateEntity(SqlConnection connection, Guid id)
72+
{
73+
await using var command = connection.CreateCommand();
74+
command.CommandText =
75+
$"""
76+
update Companies
77+
set Content = 'New Content Value'
78+
where Id = @Id;
79+
""";
80+
command.Parameters.AddWithValue("@Id", id);
81+
await command.ExecuteNonQueryAsync();
82+
}
83+
9484
[Test]
95-
public async Task LastTimeStampRowVersionOnDelete()
85+
public async Task LastTimeStampOnDelete()
9686
{
9787
await using var database = await LocalDb();
9888
await using var connection = database.Connection;
99-
await connection.EnableTracking();
10089

101-
var companyGuid = Guid.NewGuid();
102-
await using var addDataCommand = connection.CreateCommand();
103-
addDataCommand.CommandText =
104-
$"""
105-
insert into [Companies] (Id, Content)
106-
values ('{companyGuid}', 'The company')
107-
""";
108-
await addDataCommand.ExecuteNonQueryAsync();
90+
var companyGuid = await AddEntity(connection);
10991

11092
var timeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
11193
IsNotEmpty(timeStamp);
11294
IsNotNull(timeStamp);
11395

114-
await using (var deleteCommand = connection.CreateCommand())
115-
{
116-
deleteCommand.CommandText = $"delete From Companies where Id=@Id";
117-
deleteCommand.Parameters.AddWithValue("@Id", companyGuid);
118-
await deleteCommand.ExecuteNonQueryAsync();
119-
}
96+
await DeleteEntity(connection, companyGuid);
12097

12198
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
12299
IsNotEmpty(newTimeStamp);
123100
IsNotNull(newTimeStamp);
124101
AreNotEqual(newTimeStamp, timeStamp);
125102
}
126103

127-
[Test]
128-
public async Task LastTimeStampRowVersionOnTruncate()
104+
static async Task<Guid> AddEntity(SqlConnection connection)
129105
{
130-
await using var database = await LocalDb();
131-
await using var connection = database.Connection;
132-
await connection.EnableTracking();
133-
134-
await using var addDataCommand = connection.CreateCommand();
135-
addDataCommand.CommandText =
106+
var id = Guid.NewGuid();
107+
await using var command = connection.CreateCommand();
108+
command.CommandText =
136109
$"""
137110
insert into [Companies] (Id, Content)
138-
values ('{Guid.NewGuid()}', 'The company')
111+
values ('{id}', 'The company')
139112
""";
140-
await addDataCommand.ExecuteNonQueryAsync();
113+
await command.ExecuteNonQueryAsync();
114+
return id;
115+
}
116+
117+
static async Task DeleteEntity(SqlConnection connection, Guid id)
118+
{
119+
await using var command = connection.CreateCommand();
120+
command.CommandText = $"delete From Companies where Id=@Id";
121+
command.Parameters.AddWithValue("@Id", id);
122+
await command.ExecuteNonQueryAsync();
123+
}
124+
125+
[Test]
126+
public async Task LastTimeStampOnTruncate()
127+
{
128+
await using var database = await LocalDb();
129+
await using var connection = database.Connection;
130+
await AddEntity(connection);
141131

142132
var timeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
143133
IsNotEmpty(timeStamp);
@@ -191,32 +181,6 @@ public async Task GetLastTimeStampPostgres()
191181
IsNotEmpty(timeStamp);
192182
}
193183

194-
[Test]
195-
public async Task LastTimeStampRowVersionAndTracking()
196-
{
197-
await using var database = await LocalDb();
198-
199-
var connection = database.Connection;
200-
await connection.EnableTracking();
201-
var timeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
202-
IsNotEmpty(timeStamp);
203-
IsNotNull(timeStamp);
204-
await using (var command = connection.CreateCommand())
205-
{
206-
command.CommandText =
207-
$"""
208-
insert into [Companies] (Id, Content)
209-
values ('{Guid.NewGuid()}', 'The company')
210-
""";
211-
await command.ExecuteNonQueryAsync();
212-
}
213-
214-
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
215-
IsNotEmpty(newTimeStamp);
216-
IsNotNull(newTimeStamp);
217-
AreNotEqual(timeStamp, newTimeStamp);
218-
}
219-
220184
[Test]
221185
public async Task GetDatabasesWithTracking()
222186
{

0 commit comments

Comments
 (0)