您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

71 行
2.1KB

  1. using CartWise.Domain.Entities;
  2. using CartWise.Domain.Enums;
  3. namespace CartWise.Domain.Tests;
  4. public class PurchaseTests
  5. {
  6. private static readonly DateTime Now = new(2026, 8, 10, 12, 0, 0, DateTimeKind.Utc);
  7. [Fact]
  8. public void Constructor_DefaultsToManualSourceAndNoStore()
  9. {
  10. var purchase = new Purchase(Guid.NewGuid(), "user-1", Now, Now);
  11. Assert.Equal(PurchaseSourceType.Manual, purchase.SourceType);
  12. Assert.Null(purchase.StoreLocationId);
  13. Assert.Empty(purchase.Items);
  14. }
  15. [Fact]
  16. public void Constructor_ThrowsWhenHouseholdIdIsEmpty()
  17. {
  18. Assert.Throws<ArgumentException>(() => new Purchase(Guid.Empty, "user-1", Now, Now));
  19. }
  20. [Theory]
  21. [InlineData("")]
  22. [InlineData(" ")]
  23. [InlineData(null)]
  24. public void Constructor_ThrowsWhenPurchasedByUserIdIsMissing(string? userId)
  25. {
  26. Assert.Throws<ArgumentException>(() => new Purchase(Guid.NewGuid(), userId!, Now, Now));
  27. }
  28. }
  29. public class PurchaseItemTests
  30. {
  31. private static readonly DateTime Now = new(2026, 8, 10, 12, 0, 0, DateTimeKind.Utc);
  32. [Fact]
  33. public void Constructor_DefaultsQuantityToOne()
  34. {
  35. var item = new PurchaseItem(Guid.NewGuid(), "Milk", 3.99m, Now);
  36. Assert.Equal(1m, item.Quantity);
  37. Assert.Equal(3.99m, item.LinePrice);
  38. }
  39. [Theory]
  40. [InlineData("")]
  41. [InlineData(" ")]
  42. [InlineData(null)]
  43. public void Constructor_ThrowsWhenDescriptionIsMissing(string? description)
  44. {
  45. Assert.Throws<ArgumentException>(() => new PurchaseItem(Guid.NewGuid(), description!, 3.99m, Now));
  46. }
  47. [Fact]
  48. public void Constructor_ThrowsWhenQuantityIsZeroOrNegative()
  49. {
  50. Assert.Throws<ArgumentOutOfRangeException>(() => new PurchaseItem(Guid.NewGuid(), "Milk", 3.99m, Now, quantity: 0));
  51. Assert.Throws<ArgumentOutOfRangeException>(() => new PurchaseItem(Guid.NewGuid(), "Milk", 3.99m, Now, quantity: -1));
  52. }
  53. [Fact]
  54. public void Constructor_ThrowsWhenLinePriceIsNegative()
  55. {
  56. Assert.Throws<ArgumentOutOfRangeException>(() => new PurchaseItem(Guid.NewGuid(), "Milk", -1m, Now));
  57. }
  58. }

Powered by TurnKey Linux.