PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/ncqrs/ncqrs
C# | 85 lines | 72 code | 13 blank | 0 comment | 0 complexity | 744a425d2ceaf747a2be54a81296998f 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.Sourcing;
  4. using Ncqrs.Eventing.Storage;
  5. using Ncqrs.Eventing.Storage.Serialization;
  6. using Newtonsoft.Json.Linq;
  7. using Xunit;
  8. namespace Ncqrs.Tests.Eventing.Storage.Serialization
  9. {
  10. public class JsonEventFormatterTests
  11. {
  12. private IEventTypeResolver _typeResolver;
  13. public JsonEventFormatterTests()
  14. {
  15. var typeResolver = new AttributeEventTypeResolver();
  16. typeResolver.AddEvent(typeof(AnEvent));
  17. _typeResolver = typeResolver;
  18. }
  19. [Fact]
  20. public void Ctor()
  21. {
  22. var formatter = new JsonEventFormatter(_typeResolver);
  23. }
  24. [Fact]
  25. public void Ctor_typeResolver_null()
  26. {
  27. var ex = Assert.Throws<ArgumentNullException>(() => new JsonEventFormatter(null));
  28. ex.ParamName.Should().Be("typeResolver");
  29. }
  30. [Fact]
  31. public void Serialize()
  32. {
  33. var formatter = new JsonEventFormatter(_typeResolver);
  34. var theEvent = new AnEvent {
  35. Day = new DateTime(2000, 01, 01),
  36. Name = "Alice",
  37. Value = 10,
  38. };
  39. string eventName;
  40. var result = formatter.Serialize(theEvent, out eventName);
  41. eventName.Should().Be("bob");
  42. result.Should().NotBeNull();
  43. result.Count.Should().Be(3);
  44. result.Value<string>("Name").Should().Be(theEvent.Name);
  45. result.Value<int>("Value").Should().Be(theEvent.Value);
  46. result.Value<DateTime>("Day").Should().Be(theEvent.Day);
  47. }
  48. [Fact]
  49. public void Deserialize()
  50. {
  51. var formatter = new JsonEventFormatter(_typeResolver);
  52. var obj = new JObject(
  53. new JProperty("Name", "Alice"),
  54. new JProperty("Value", 10),
  55. new JProperty("Day", new DateTime(2000, 01, 01))
  56. );
  57. var rawResult = formatter.Deserialize(obj, "bob");
  58. rawResult.Should().BeOfType<AnEvent>();
  59. var result = (AnEvent) rawResult;
  60. result.Name.Should().Be("Alice");
  61. result.Value.Should().Be(10);
  62. result.Day.Should().Be(new DateTime(2000, 01, 01));
  63. }
  64. [EventName("bob")]
  65. public class AnEvent
  66. {
  67. public string Name { get; set; }
  68. public int Value { get; set; }
  69. public DateTime Day { get; set; }
  70. }
  71. }
  72. }