Save.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using PetaPoco.Core;
  3. using PetaPoco.Tests.Integration.Databases;
  4. using PetaPoco.Tests.Integration.Databases.MSSQL;
  5. using PetaPoco.Tests.Integration.Documentation.Pocos;
  6. using Shouldly;
  7. using Xunit;
  8. namespace PetaPoco.Tests.Integration.Documentation
  9. {
  10. [Collection("MssqlTests")]
  11. public class Save : BaseDatabase
  12. {
  13. public Save()
  14. : base(new MssqlDBTestProvider())
  15. {
  16. PocoData.FlushCaches();
  17. }
  18. [Fact]
  19. public void Save_Insert()
  20. {
  21. // Clear out any notes and reset the ID sequence counter
  22. DB.Execute("TRUNCATE TABLE [Note]");
  23. // Add a note
  24. var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
  25. DB.Save(note);
  26. // As note.id is auto increment, we should have an id of 1
  27. note.Id.ShouldBe(1);
  28. // Obviously, we should find only one matching note in the db
  29. var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
  30. count.ShouldBe(1);
  31. // Fetch a new copy of note
  32. var noteFromDb = DB.Single<Note>(note.Id);
  33. // They are the same
  34. note.Id.ShouldBe(noteFromDb.Id);
  35. note.Text.ShouldBe(noteFromDb.Text);
  36. note.CreatedOn.Ticks.ShouldBe(noteFromDb.CreatedOn.Ticks);
  37. }
  38. [Fact]
  39. public void Save_Update()
  40. {
  41. // Clear out any notes and reset the ID sequence counter
  42. DB.Execute("TRUNCATE TABLE [Note]");
  43. // Add a note
  44. var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
  45. DB.Save(note);
  46. // Update the note
  47. note.Text += " and this is my update";
  48. DB.Save(note);
  49. // Fetch a new copy of note
  50. var noteFromDb = DB.Single<Note>(note.Id);
  51. // The note text is the same
  52. note.Text.ShouldBe(noteFromDb.Text);
  53. note.Text.ShouldContain(" and this is my update");
  54. }
  55. }
  56. }