Advertisement
Guest User

Untitled

a guest
Sep 7th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Marten;
  5. using Xunit;
  6.  
  7. namespace marten_test
  8. {
  9. public class CreateAndQuery
  10. {
  11. [Fact]
  12. public void QueryLinq()
  13. {
  14. using (var store = DocumentStore.For("host=localhost;database=sk1;password=sean;username=postgres"))
  15. using (var session = store.LightweightSession())
  16. {
  17. var u = session.Query<User>().First(x => x.Address.Line1.Contains("77"));
  18. Assert.Equal("Darth", u.FirstName);
  19. }
  20. }
  21.  
  22. [Fact]
  23. public void QueryText()
  24. {
  25. using (var store = DocumentStore.For("host=localhost;database=sk1;password=sean;username=postgres"))
  26. using (var session = store.LightweightSession())
  27. {
  28. var value = session.Query<User>("select data from mt_doc_user where data->'Address'->>'Line1' = '7 High Street'").First();
  29. Assert.Equal("Han", value.FirstName);
  30. }
  31. }
  32.  
  33. [Fact]
  34. public void Create()
  35. {
  36. using (var store = DocumentStore.For("host=localhost;database=sk1;password=sean;username=postgres"))
  37. using (var session = store.LightweightSession())
  38. {
  39. var user1 = new User
  40. {
  41. FirstName = "Han", LastName = "Solo",
  42. Address = new Address {Line1 = "7 High Street", Town = "London"},
  43. PreviousAddresses =
  44. {
  45. new Address {Line1 = "6 High Street", Town = "Liverpool"},
  46. new Address {Line1 = "67 Fore Street", Town = "Troon"}
  47. }
  48. };
  49. var user2 = new User
  50. {
  51. FirstName = "Darth", LastName = "Vader",
  52. Address = new Address {Line1 = "77 High Street", Town = "Londinium"},
  53. PreviousAddresses =
  54. {
  55. new Address {Line1 = "66 High Street", Town = "Liverpoole"},
  56. new Address {Line1 = "77 Fore Street", Town = "Troonton"}
  57. }
  58. };
  59. session.Store(user1, user2);
  60. session.SaveChanges();
  61. }
  62. }
  63. }
  64.  
  65. public class User
  66. {
  67. public Guid Id { get; set; }
  68. public string FirstName { get; set; }
  69. public string LastName { get; set; }
  70. public bool Internal { get; set; }
  71. public string UserName { get; set; }
  72.  
  73. public Address Address { get; set; }
  74. public List<Address> PreviousAddresses { get; set; }
  75.  
  76. public User()
  77. {
  78. PreviousAddresses = new List<Address>();
  79. }
  80. }
  81.  
  82. public class Address
  83. {
  84. public string Line1 { get; set; }
  85. public string Town { get; set; }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement