PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/src/Ncqrs.Tests/Eventing/Storage/Serialization/StringEventTranslatorTests.cs

http://github.com/ncqrs/ncqrs
C# | 101 lines | 82 code | 19 blank | 0 comment | 0 complexity | 54bc0f5c322248456e717fe86abc4b00 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0
  1. using System;
  2. using FluentAssertions;
  3. using Ncqrs.Eventing.Storage;
  4. using Ncqrs.Eventing.Storage.Serialization;
  5. using Newtonsoft.Json.Linq;
  6. using Xunit;
  7. namespace Ncqrs.Tests.Eventing.Storage.Serialization
  8. {
  9. public class StringEventTranslatorTests
  10. {
  11. private StringEventTranslator _translator;
  12. public StringEventTranslatorTests()
  13. {
  14. _translator = new StringEventTranslator();
  15. }
  16. [Fact]
  17. public void TranslateToCommon()
  18. {
  19. var obj = CreateEvent(new JObject(
  20. new JProperty("Name", "Alice"),
  21. new JProperty("Value", 10)));
  22. var result = _translator.TranslateToRaw(obj);
  23. result.EventIdentifier.Should().Be(obj.EventIdentifier);
  24. result.EventSourceId.Should().Be(obj.EventSourceId);
  25. result.EventSequence.Should().Be(obj.EventSequence);
  26. result.EventTimeStamp.Should().Be(obj.EventTimeStamp);
  27. result.EventVersion.Should().Be(obj.EventVersion);
  28. result.Data.Should().Be("{\"Name\":\"Alice\",\"Value\":10}");
  29. }
  30. [Fact]
  31. public void TranslateToCommon_obj_null()
  32. {
  33. var ex = Assert.Throws<ArgumentNullException>(() => _translator.TranslateToCommon(null));
  34. ex.ParamName.Should().Be("obj");
  35. }
  36. [Fact]
  37. public void TranslateToRaw()
  38. {
  39. var obj = CreateEvent("{\"Name\":\"Alice\",\"Value\":10}");
  40. var result = _translator.TranslateToCommon(obj);
  41. result.EventIdentifier.Should().Be(obj.EventIdentifier);
  42. result.EventSourceId.Should().Be(obj.EventSourceId);
  43. result.EventSequence.Should().Be(obj.EventSequence);
  44. result.EventTimeStamp.Should().Be(obj.EventTimeStamp);
  45. result.EventVersion.Should().Be(obj.EventVersion);
  46. result.Data.Should().NotBeNull();
  47. }
  48. [Fact]
  49. public void TranslateToRaw_obj_null()
  50. {
  51. var ex = Assert.Throws<ArgumentNullException>(() => _translator.TranslateToRaw(null));
  52. ex.ParamName.Should().Be("obj");
  53. }
  54. [Fact]
  55. public void Dates_use_iso_format()
  56. {
  57. var obj = CreateEvent(new JObject(new JProperty("Value", new DateTime(2000, 01, 02, 03, 04, 05, 006))));
  58. var result = _translator.TranslateToRaw(obj);
  59. result.Data.Should().Be("{\"Value\":\"2000-01-02T03:04:05.006\"}");
  60. }
  61. [Fact]
  62. public void Dates_respect_timezone()
  63. {
  64. var zone = new TimeSpan(0, 4, 0, 0);
  65. var obj = CreateEvent(new JObject(new JProperty("Value", new DateTimeOffset(2000, 01, 02, 03, 04, 05, 006, zone))));
  66. var result = _translator.TranslateToRaw(obj);
  67. result.Data.Should().Be("{\"Value\":\"2000-01-02T03:04:05.006+04:00\"}");
  68. }
  69. private StoredEvent<T> CreateEvent<T>(T data)
  70. {
  71. return new StoredEvent<T>(
  72. eventName: "bob",
  73. eventVersion: new Version(3, 0) /* different on purpose */,
  74. eventIdentifier: new Guid("402639D5-4106-4AE7-B210-45780C7A08F3"),
  75. eventSourceId: new Guid("697BAB3C-E122-4C70-85B0-76750BC2D878"),
  76. eventSequence: 1,
  77. eventTimeStamp: new DateTime(2001, 4, 12, 12, 52, 21, 425, DateTimeKind.Utc),
  78. data: data
  79. );
  80. }
  81. }
  82. }