-
Notifications
You must be signed in to change notification settings - Fork 600
/
Save.cs
61 lines (51 loc) · 1.95 KB
/
Save.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Documentation.Pocos;
using PetaPoco.Tests.Integration.Providers;
using Shouldly;
using Xunit;
namespace PetaPoco.Tests.Integration.Documentation
{
[Collection("Documentation")]
public class SaveTests : BaseDbContext
{
public SaveTests()
: base(new SqlServerSystemDataTestProvider())
{
PocoData.FlushCaches();
}
[Fact]
public void Save_Insert()
{
// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Save(note);
// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);
// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);
// Fetch a new copy of note
var noteFromDb = DB.Single<Note>(note.Id);
// The values in noteFromDb's column-mapped properties should be equal to the original poco's
note.Id.ShouldBe(noteFromDb.Id);
note.Text.ShouldBe(noteFromDb.Text);
note.CreatedOn.Ticks.ShouldBe(noteFromDb.CreatedOn.Ticks);
}
[Fact]
public void Save_Update()
{
// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Save(note);
// Update the note
note.Text += " and this is my update";
DB.Save(note);
// Fetch a new copy of note
var noteFromDb = DB.Single<Note>(note.Id);
// The values in noteFromDb's column-mapped properties should be equal to the original poco's
note.Text.ShouldBe(noteFromDb.Text);
note.Text.ShouldContain(" and this is my update");
}
}
}