ConfigProductTests.swift (3303B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2026 Bohdan, Volodymyr Potuzhnyi 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 import XCTest 18 @testable import TalerPOS 19 20 final class ConfigProductTests: XCTestCase { 21 22 func testDisplayDescriptionIsHiddenWhenSameAsProductName() { 23 let product = ConfigProduct( 24 productName: "Coffee", 25 description: "Coffee", 26 price: Amount(currency: "KUDOS", value: 2, fraction: 0), 27 categories: [1] 28 ) 29 30 XCTAssertEqual("Coffee", product.displayName) 31 XCTAssertNil(product.displayDescription) 32 } 33 34 func testDisplayPriceFormatsAmount() { 35 let product = ConfigProduct( 36 description: "Coffee", 37 price: Amount(currency: "KUDOS", value: 2, fraction: 50000000), 38 categories: [1] 39 ) 40 41 XCTAssertEqual("2.50 KUDOS", product.price.readableAmount) 42 } 43 44 func testStockLimitSubtractsSoldAndLostFromTotalStock() { 45 let product = ConfigProduct( 46 description: "finite_quantity", 47 price: Amount(currency: "KUDOS", value: 10, fraction: 0), 48 categories: [], 49 totalStock: 10, 50 unitTotalStock: "10", 51 totalSold: 0, 52 totalLost: 2 53 ) 54 55 XCTAssertEqual(8, product.stockLimit) 56 } 57 58 func testStockLimitSubtractsUnitSoldAndLost() { 59 let product = ConfigProduct( 60 description: "test", 61 price: Amount(currency: "KUDOS", value: 5, fraction: 0), 62 categories: [], 63 unitTotalStock: "10", 64 unitTotalSold: "2", 65 unitTotalLost: "3" 66 ) 67 68 XCTAssertEqual(5, product.stockLimit) 69 } 70 71 func testStockLimitWithNoSoldOrLostReturnsTotalStock() { 72 let product = ConfigProduct( 73 description: "test", 74 price: Amount(currency: "KUDOS", value: 5, fraction: 0), 75 categories: [], 76 totalStock: 10 77 ) 78 79 XCTAssertEqual(10, product.stockLimit) 80 } 81 82 func testStockLimitReturnsNilForUnlimitedStock() { 83 let product = ConfigProduct( 84 description: "test", 85 price: Amount(currency: "KUDOS", value: 5, fraction: 0), 86 categories: [], 87 totalStock: -1 88 ) 89 90 XCTAssertNil(product.stockLimit) 91 } 92 93 func testStockLimitClampsToZeroWhenSoldAndLostExceedStock() { 94 let product = ConfigProduct( 95 description: "test", 96 price: Amount(currency: "KUDOS", value: 5, fraction: 0), 97 categories: [], 98 totalStock: 5, 99 totalSold: 3, 100 totalLost: 5 101 ) 102 103 XCTAssertEqual(0, product.stockLimit) 104 } 105 }