You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.7 KiB

  1. using System;
  2. using System.Linq;
  3. using Xunit;
  4. using Microsoft.eShopOnContainers.Services.Ordering.SqlData.UnitOfWork;
  5. using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel;
  6. using Microsoft.eShopOnContainers.Services.Ordering.Domain.Contracts;
  7. using Microsoft.eShopOnContainers.Services.Ordering.SqlData.Repositories;
  8. using Microsoft.EntityFrameworkCore;
  9. namespace DataIntegrationTests
  10. {
  11. //Basic documentation for Testing EF Core classes
  12. // http://ef.readthedocs.io/en/latest/miscellaneous/testing.html
  13. public class Tests
  14. {
  15. [Fact]
  16. public async void Add_order_to_data_model()
  17. {
  18. // All contexts that share the same service provider will share the same database
  19. //Using InMemory DB
  20. //var options = DbContextUtil.CreateNewContextOptionsForInMemoryDB();
  21. //Using Sql Server
  22. var options = DbContextUtil.CreateNewContextOptionsForSqlDb();
  23. // Run the test against one instance of the context
  24. using (var context = new OrderingDbContext(options))
  25. {
  26. IOrderRepository orderRepository = new OrderRepository(context);
  27. //Create generic Address ValueObject
  28. Address sampleAddress = new Address("15703 NE 61st Ct.",
  29. "Redmond",
  30. "Washington",
  31. "WA",
  32. "United States",
  33. "US",
  34. "98052",
  35. 47.661492,
  36. -122.131309
  37. );
  38. //Create sample Orders
  39. Order order1 = new Order(Guid.NewGuid(), sampleAddress, sampleAddress);
  40. //Add a few OrderItems
  41. order1.AddNewOrderItem(Guid.NewGuid(), 2, 25, 30);
  42. order1.AddNewOrderItem(Guid.NewGuid(), 1, 58, 0);
  43. order1.AddNewOrderItem(Guid.NewGuid(), 1, 60, 0);
  44. order1.AddNewOrderItem(Guid.NewGuid(), 3, 12, 0);
  45. order1.AddNewOrderItem(Guid.NewGuid(), 5, 3, 0);
  46. orderRepository.Add(order1);
  47. int numChanges = await orderRepository.UnitOfWork.CommitAsync();
  48. //With no Async Repository
  49. //context.Orders.Add(order1);
  50. //context.SaveChanges();
  51. }
  52. //// Use a separate instance of the context to verify correct data was saved to database
  53. using (var context = new OrderingDbContext(options))
  54. {
  55. var orders = context.Orders
  56. .Include(o => o.ShippingAddress)
  57. .Include(o => o.BillingAddress)
  58. .ToList();
  59. //Could be using .Load() if you don't want to create a List
  60. //OTHER SAMPLE
  61. //var company = context.Companies
  62. // .Include(co => co.Employees).ThenInclude(emp => emp.Employee_Car)
  63. // .Include(co => co.Employees).ThenInclude(emp => emp.Employee_Country)
  64. // .FirstOrDefault(co => co.companyID == companyID);
  65. //Assert when running test with a clean In-Memory DB
  66. //Assert.Equal(1, context.Orders.Count());
  67. string cityName = orders.First<Order>().ShippingAddress.City;
  68. Assert.Equal("Redmond", cityName);
  69. }
  70. }
  71. }
  72. }