|
| 1 | +import { IDatabase } from './Database'; |
| 2 | +import { IDiscountService, DiscountService } from './DiscountService'; |
| 3 | +import { AccountService } from './AccountService'; |
| 4 | +import { Product, User } from './Entities'; |
| 5 | + |
| 6 | +class MockDatabase implements IDatabase { |
| 7 | + private data: Map<string, unknown> = new Map(); |
| 8 | + |
| 9 | + async save<T>(key: string, value: T): Promise<boolean> { |
| 10 | + this.data.set(key, value); |
| 11 | + return true; |
| 12 | + } |
| 13 | + |
| 14 | + async load<T>(key: string): Promise<T> { |
| 15 | + const value = this.data.get(key); |
| 16 | + if (value === undefined) { |
| 17 | + throw new Error('Table not found'); |
| 18 | + } |
| 19 | + return { ...(value as T) }; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('AccountService', () => { |
| 24 | + let discountService: IDiscountService; |
| 25 | + let accountService: AccountService; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + const db = new MockDatabase(); |
| 29 | + discountService = new DiscountService(db); |
| 30 | + accountService = new AccountService(discountService); |
| 31 | + }); |
| 32 | + |
| 33 | + test('should throw table not found error', async () => { |
| 34 | + try { |
| 35 | + await accountService.userDiscount.loadDiscounts(); |
| 36 | + } catch (e) { |
| 37 | + expect(e.message).toBe('Failed to load user_discount discounts'); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + test('should throw table not found error', async () => { |
| 42 | + try { |
| 43 | + await accountService.userDiscount.loadDiscounts(); |
| 44 | + } catch (e) { |
| 45 | + expect(e.message).toBe('Failed to load user_discount discounts'); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + test('should set and get user discount', async () => { |
| 50 | + await accountService.userDiscount.setUserDiscount(User.Standard, 10); |
| 51 | + await accountService.userDiscount.saveDiscounts(); |
| 52 | + await accountService.userDiscount.loadDiscounts(); |
| 53 | + |
| 54 | + expect(accountService.userDiscount.getUserDiscount(User.Standard)).toBe(10); |
| 55 | + }); |
| 56 | + |
| 57 | + test('should get user discount = 0 for negative value', async () => { |
| 58 | + await accountService.userDiscount.setUserDiscount(User.Standard, -10); |
| 59 | + await accountService.userDiscount.saveDiscounts(); |
| 60 | + await accountService.userDiscount.loadDiscounts(); |
| 61 | + |
| 62 | + expect(accountService.userDiscount.getUserDiscount(User.Standard)).toBe(0); |
| 63 | + }); |
| 64 | + |
| 65 | + test('should calculate user discount = 100 for value > 100', async () => { |
| 66 | + await accountService.userDiscount.setUserDiscount(User.Standard, 200); |
| 67 | + await accountService.userDiscount.saveDiscounts(); |
| 68 | + await accountService.userDiscount.loadDiscounts(); |
| 69 | + |
| 70 | + expect(accountService.userDiscount.getUserDiscount(User.Standard)).toBe(100); |
| 71 | + }); |
| 72 | + |
| 73 | + test('should set and get user product discount', async () => { |
| 74 | + await accountService.userProductDiscount.setUserProductDiscount(User.Standard, Product.Car, 10); |
| 75 | + await accountService.userProductDiscount.saveDiscounts(); |
| 76 | + await accountService.userProductDiscount.loadDiscounts(); |
| 77 | + |
| 78 | + expect(accountService.userProductDiscount.getUserProductDiscount(User.Standard, Product.Car)).toBe(10); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should get user product discount = 0 for negative value', async () => { |
| 82 | + await accountService.userProductDiscount.setUserProductDiscount(User.Standard, Product.Car, -10); |
| 83 | + await accountService.userProductDiscount.saveDiscounts(); |
| 84 | + await accountService.userProductDiscount.loadDiscounts(); |
| 85 | + |
| 86 | + expect(accountService.userProductDiscount.getUserProductDiscount(User.Standard, Product.Car)).toBe(0); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should calculate user product discount = 100 for value > 100', async () => { |
| 90 | + await accountService.userProductDiscount.setUserProductDiscount(User.Standard, Product.Car, 200); |
| 91 | + await accountService.userProductDiscount.saveDiscounts(); |
| 92 | + await accountService.userProductDiscount.loadDiscounts(); |
| 93 | + |
| 94 | + expect(accountService.userProductDiscount.getUserProductDiscount(User.Standard, Product.Car)).toBe(100); |
| 95 | + }); |
| 96 | + |
| 97 | + test('should calculate total discount', async () => { |
| 98 | + await accountService.userDiscount.setUserDiscount(User.Standard, 10); |
| 99 | + await accountService.userProductDiscount.setUserProductDiscount(User.Standard, Product.Car, 20); |
| 100 | + await accountService.saveAllDiscounts(); |
| 101 | + await accountService.loadAllDiscounts(); |
| 102 | + |
| 103 | + const totalDiscount = accountService.calculateTotalDiscount(User.Standard, Product.Car); |
| 104 | + expect(totalDiscount).toBe(30); |
| 105 | + }); |
| 106 | + |
| 107 | + test('should calculate total discount = 0 for negative value', async () => { |
| 108 | + await accountService.userDiscount.setUserDiscount(User.Standard, -10); |
| 109 | + await accountService.userProductDiscount.setUserProductDiscount(User.Standard, Product.Car, -20); |
| 110 | + await accountService.saveAllDiscounts(); |
| 111 | + await accountService.loadAllDiscounts(); |
| 112 | + |
| 113 | + const totalDiscount = accountService.calculateTotalDiscount(User.Standard, Product.Car); |
| 114 | + expect(totalDiscount).toBe(0); |
| 115 | + }); |
| 116 | + |
| 117 | + test('should calculate total discount = 100 for value > 100', async () => { |
| 118 | + await accountService.userDiscount.setUserDiscount(User.Standard, 200); |
| 119 | + await accountService.userProductDiscount.setUserProductDiscount(User.Standard, Product.Car, 10); |
| 120 | + await accountService.saveAllDiscounts(); |
| 121 | + await accountService.loadAllDiscounts(); |
| 122 | + |
| 123 | + const totalDiscount = accountService.calculateTotalDiscount(User.Standard, Product.Car); |
| 124 | + expect(totalDiscount).toBe(100); |
| 125 | + }); |
| 126 | +}); |
0 commit comments