From 9757661e475ff9ca1afcdcdbffba5910cba7d5df Mon Sep 17 00:00:00 2001 From: Krypto Pank Date: Mon, 26 Dec 2022 16:38:10 +0200 Subject: [PATCH] Fix test suite #6001 --- .../ViewModels/AmountTextFieldViewModel.swift | 1 + .../EtherNumberFormatterTests.swift | 4 ++ .../SendViewControllerTests.swift | 66 +++++++++++-------- .../PaymentCoordinatorTests.swift | 7 +- .../CoinTicker/Types/CoinTicker.swift | 4 +- .../Foundation/Formatter.swift | 18 ++--- .../Types/WalletDependencyContainer.swift | 6 +- 7 files changed, 65 insertions(+), 41 deletions(-) diff --git a/AlphaWallet/Common/Views/ViewModels/AmountTextFieldViewModel.swift b/AlphaWallet/Common/Views/ViewModels/AmountTextFieldViewModel.swift index f4aea05af..d8b0b1ad4 100644 --- a/AlphaWallet/Common/Views/ViewModels/AmountTextFieldViewModel.swift +++ b/AlphaWallet/Common/Views/ViewModels/AmountTextFieldViewModel.swift @@ -139,6 +139,7 @@ final class AmountTextFieldViewModel { }.eraseToAnyPublisher() let text = Publishers.Merge(cryptoAmountToSend, toggleFiatAndCryptoPair(trigger: input.togglePair)) + .map { $0?.droppedTrailingZeros } .eraseToAnyPublisher() return .init( diff --git a/AlphaWalletTests/Foundation/EtherNumberFormatterTests.swift b/AlphaWalletTests/Foundation/EtherNumberFormatterTests.swift index c228eece8..536e84b8b 100644 --- a/AlphaWalletTests/Foundation/EtherNumberFormatterTests.swift +++ b/AlphaWalletTests/Foundation/EtherNumberFormatterTests.swift @@ -139,6 +139,8 @@ class EtherNumberFormatterTests: XCTestCase { } func testMinMaxFormattedNumber() { + Config.setLocale(.english) + let locale: Locale = Config.locale func fiatFormatter(usesGroupingSeparator: Bool = false, currency: Currency) -> NumberFormatter { @@ -158,5 +160,7 @@ class EtherNumberFormatterTests: XCTestCase { XCTAssertEqual(formatter.string(double: 1, minimumFractionDigits: 2, maximumFractionDigits: 6), "1.00") XCTAssertEqual(formatter.string(double: 0.1213123123123, minimumFractionDigits: 2, maximumFractionDigits: 6), "0.12") XCTAssertEqual(formatter.string(double: 1111102222.1213123123123, minimumFractionDigits: 2, maximumFractionDigits: 6), "1,111,102,222.12") + + Config.setLocale(.system) } } diff --git a/AlphaWalletTests/Transfer/ViewControllers/SendViewControllerTests.swift b/AlphaWalletTests/Transfer/ViewControllers/SendViewControllerTests.swift index 28a7e4304..40c23c441 100644 --- a/AlphaWalletTests/Transfer/ViewControllers/SendViewControllerTests.swift +++ b/AlphaWalletTests/Transfer/ViewControllers/SendViewControllerTests.swift @@ -22,13 +22,13 @@ class SendViewControllerTests: XCTestCase { func testNativeCryptocurrencyAllFundsValueSpanish() { let vc = createSendViewControllerAndSetLocale(locale: .spanish, transactionType: nativeCryptocurrencyTransactionType) - XCTAssertEqual(vc.amountTextField.value, "") + XCTAssertEqual(vc.amountTextField.value, "0") let testValue = BigInt("10000000000000000000000") dep.tokensService.setBalanceTestsOnly(balance: .init(value: testValue), for: token) vc.allFundsSelected() - XCTAssertEqual(vc.amountTextField.value, "10000,0000") + XCTAssertEqual(vc.amountTextField.value, "10000") //Reset language to default Config.setLocale(AppLocale.system) @@ -37,14 +37,14 @@ class SendViewControllerTests: XCTestCase { func testNativeCryptocurrencyAllFundsValueEnglish() { let vc = createSendViewControllerAndSetLocale(locale: .japanese, transactionType: nativeCryptocurrencyTransactionType) - XCTAssertEqual(vc.amountTextField.value, "") + XCTAssertEqual(vc.amountTextField.value, "0") let testValue = BigInt("10000000000000000000000") dep.tokensService.setBalanceTestsOnly(balance: .init(value: testValue), for: token) vc.allFundsSelected() - XCTAssertEqual(vc.amountTextField.value, "10000.0000") + XCTAssertEqual(vc.amountTextField.value, "10000") XCTAssertTrue(vc.viewModel.amountToSend == .allFunds) Config.setLocale(AppLocale.system) @@ -53,7 +53,7 @@ class SendViewControllerTests: XCTestCase { func testNativeCryptocurrencyAllFundsValueEnglish2() { let vc = createSendViewControllerAndSetLocale(locale: .english, transactionType: nativeCryptocurrencyTransactionType) - XCTAssertEqual(vc.amountTextField.value, "") + XCTAssertEqual(vc.amountTextField.value, "0") let testValue = BigInt("10000000000000") dep.tokensService.setBalanceTestsOnly(balance: .init(value: testValue), for: token) @@ -70,7 +70,7 @@ class SendViewControllerTests: XCTestCase { dep.tokensService.addOrUpdateTokenTestsOnly(token: token) let vc = createSendViewControllerAndSetLocale(locale: .spanish, transactionType: .erc20Token(token, destination: .none, amount: .amount(0.002))) dep.tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("2000000020224719101120")), for: token) - XCTAssertEqual(vc.amountTextField.value, "0,0020") + XCTAssertEqual(vc.amountTextField.value, "0,002") XCTAssertTrue(vc.viewModel.amountToSend == .amount(0.002)) vc.allFundsSelected() @@ -84,10 +84,10 @@ class SendViewControllerTests: XCTestCase { dep.tokensService.addOrUpdateTokenTestsOnly(token: token) let vc = createSendViewControllerAndSetLocale(locale: .spanish, transactionType: .erc20Token(token, destination: .none, amount: .notSet)) dep.tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("2000000020224719101120")), for: token) - XCTAssertEqual(vc.amountTextField.value, "") + XCTAssertEqual(vc.amountTextField.value, "0") vc.allFundsSelected() - XCTAssertEqual(vc.amountTextField.value, "2000,0000") + XCTAssertEqual(vc.amountTextField.value, "2000") XCTAssertTrue(vc.viewModel.amountToSend == .allFunds) dep.tokensService.setBalanceTestsOnly(balance: .init(value: .zero), for: token) @@ -104,10 +104,10 @@ class SendViewControllerTests: XCTestCase { dep.tokensService.addOrUpdateTokenTestsOnly(token: token) let vc = createSendViewControllerAndSetLocale(locale: .spanish, transactionType: .erc20Token(token, destination: .none, amount: .notSet)) dep.tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("2020224719101120")), for: token) - XCTAssertEqual(vc.amountTextField.value, "") + XCTAssertEqual(vc.amountTextField.value, "0") vc.allFundsSelected() - XCTAssertEqual(vc.amountTextField.value, "0,0020") + XCTAssertEqual(vc.amountTextField.value, "0,002") Config.setLocale(AppLocale.system) } @@ -137,10 +137,10 @@ class SendViewControllerTests: XCTestCase { let vc = createSendViewControllerAndSetLocale(locale: .english, transactionType: .erc20Token(token, destination: .none, amount: .notSet)) dep.tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("2020224719101120")), for: token) - XCTAssertEqual(vc.amountTextField.value, "") + XCTAssertEqual(vc.amountTextField.value, "0") vc.allFundsSelected() - XCTAssertEqual(vc.amountTextField.value, "0.0020") + XCTAssertEqual(vc.amountTextField.value, "0.002") Config.setLocale(AppLocale.system) } @@ -150,7 +150,7 @@ class SendViewControllerTests: XCTestCase { dep.tokensService.addOrUpdateTokenTestsOnly(token: token) let vc = createSendViewControllerAndSetLocale(locale: .english, transactionType: .erc20Token(token, destination: .none, amount: .notSet)) dep.tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("2020224719101120")), for: token) - XCTAssertEqual(vc.amountTextField.value, "") + XCTAssertEqual(vc.amountTextField.value, "0") Config.setLocale(AppLocale.system) @@ -161,13 +161,13 @@ class SendViewControllerTests: XCTestCase { dep.tokensService.addOrUpdateTokenTestsOnly(token: token) let vc = createSendViewControllerAndSetLocale(locale: .english, transactionType: .erc20Token(token, destination: .none, amount: .amount(1.34))) dep.tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("2020224719101120")), for: token) - XCTAssertEqual(vc.amountTextField.value, "1.3400") + XCTAssertEqual(vc.amountTextField.value, "1.34") vc.didScanQRCode("aw.app/ethereum:0xbc8dafeaca658ae0857c80d8aa6de4d487577c63@1?value=1e19") let expectation = self.expectation(description: "did update token balance expectation") DispatchQueue.main.asyncAfter(deadline: .now() + 1) { - XCTAssertEqual(vc.amountTextField.value, "10.0000") + XCTAssertEqual(vc.amountTextField.value, "10") expectation.fulfill() } waitForExpectations(timeout: 10) @@ -180,13 +180,13 @@ class SendViewControllerTests: XCTestCase { dep.tokensService.addOrUpdateTokenTestsOnly(token: token) let vc = createSendViewControllerAndSetLocale(locale: .spanish, transactionType: .erc20Token(token, destination: .none, amount: .amount(1.34))) dep.tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("2020224719101120")), for: token) - XCTAssertEqual(vc.amountTextField.value, "1,3400") + XCTAssertEqual(vc.amountTextField.value, "1,34") vc.didScanQRCode("aw.app/ethereum:0xbc8dafeaca658ae0857c80d8aa6de4d487577c63@1?value=1e17") let expectation = self.expectation(description: "did update token balance expectation") DispatchQueue.main.asyncAfter(deadline: .now() + 1) { - XCTAssertEqual(vc.amountTextField.value, "0,1000") + XCTAssertEqual(vc.amountTextField.value, "0,1") expectation.fulfill() } waitForExpectations(timeout: 10) @@ -208,7 +208,7 @@ class SendViewControllerTests: XCTestCase { let expectation = self.expectation(description: "did update token balance expectation") DispatchQueue.main.asyncAfter(deadline: .now() + 3) { - XCTAssertEqual(vc.amountTextField.value, "1,0040") + XCTAssertEqual(vc.amountTextField.value, "1,004") expectation.fulfill() } waitForExpectations(timeout: 10) @@ -239,7 +239,8 @@ class TokenBalanceTests: XCTestCase { var callbackCount: Int = 0 let callbackCountExpectation: Int = 4 - pipeline.tokenViewModelPublisher(for: token) + pipeline + .tokenViewModelPublisher(for: token) .sink { _ in callbackCount += 1 @@ -251,7 +252,7 @@ class TokenBalanceTests: XCTestCase { tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("3000000020224719101120")!), for: token) let tokenToTicker = TokenMappedToTicker(token: token) - let ticker = CoinTicker.make(for: tokenToTicker) + let ticker = CoinTicker.make(for: tokenToTicker, currency: dep.currencyService.currency) pipeline.addOrUpdateTestsOnly(ticker: ticker, for: tokenToTicker) pipeline.addOrUpdateTestsOnly(ticker: ticker.override(price_usd: 0), for: tokenToTicker) // no changes should be, as value is stay the same @@ -259,14 +260,20 @@ class TokenBalanceTests: XCTestCase { tokensService.setBalanceTestsOnly(balance: .init(value: BigInt("4000000020224719101120")!), for: token) - waitForExpectations(timeout: 10) + waitForExpectations(timeout: 50) } func testBalanceUpdates() { let pipeline = dep.pipeline let tokensService = dep.tokensService - let token = Token(contract: AlphaWallet.Address.make(address: "0x1000000000000000000000000000000000000002"), server: .main, decimals: 18, value: "2000000020224719101120", type: .erc20) + let token = Token( + contract: AlphaWallet.Address.make(address: "0x1000000000000000000000000000000000000002"), + server: .main, + decimals: 18, + value: "2000000020224719101120", + type: .erc20) + var balance = pipeline.tokenViewModel(for: token) XCTAssertNil(balance) @@ -304,7 +311,13 @@ class TokenBalanceTests: XCTestCase { func testBalanceUpdatesPublisherWhenServersChanged() { let pipeline = dep.pipeline let tokensService = dep.tokensService - let token = Token(contract: AlphaWallet.Address.make(address: "0x1000000000000000000000000000000000000003"), server: .main, decimals: 18, value: "2000000020224719101120", type: .erc20) + let token = Token( + contract: AlphaWallet.Address.make(address: "0x1000000000000000000000000000000000000003"), + server: .main, + decimals: 18, + value: "2000000020224719101120", + type: .erc20) + var balance = pipeline.tokenViewModel(for: token) XCTAssertNil(balance) @@ -317,7 +330,8 @@ class TokenBalanceTests: XCTestCase { var callbackCount: Int = 0 let callbackCountExpectation: Int = 13 - pipeline.tokenViewModelPublisher(for: token) + pipeline + .tokenViewModelPublisher(for: token) .sink { _ in callbackCount += 1 if callbackCount == callbackCountExpectation { @@ -326,7 +340,7 @@ class TokenBalanceTests: XCTestCase { }.store(in: &cancelable) let tokenToTicker = TokenMappedToTicker(token: token) - let ticker = CoinTicker.make(for: tokenToTicker) + let ticker = CoinTicker.make(for: tokenToTicker, currency: dep.currencyService.currency) pipeline.addOrUpdateTestsOnly(ticker: ticker, for: tokenToTicker) let group = DispatchGroup() @@ -350,7 +364,7 @@ class TokenBalanceTests: XCTestCase { tokensService.deleteTokenTestsOnly(token: token) } - waitForExpectations(timeout: 30) + waitForExpectations(timeout: 50) } func testTokenDeletion() { diff --git a/AlphaWalletTests/ViewControllers/PaymentCoordinatorTests.swift b/AlphaWalletTests/ViewControllers/PaymentCoordinatorTests.swift index 714528687..3d86ce62f 100644 --- a/AlphaWalletTests/ViewControllers/PaymentCoordinatorTests.swift +++ b/AlphaWalletTests/ViewControllers/PaymentCoordinatorTests.swift @@ -36,6 +36,7 @@ extension WalletDataProcessingPipeline { let transactionsDataStore = FakeTransactionsStorage() let nftProvider = FakeNftProvider() let coinTickersFetcher = CoinTickersFetcherImpl.make() + let currencyService: CurrencyService = .make() let tokensService = AlphaWalletTokensService( sessionsProvider: sessionsProvider, @@ -53,7 +54,7 @@ extension WalletDataProcessingPipeline { coinTickersFetcher: coinTickersFetcher, assetDefinitionStore: .make(), eventsDataStore: eventsDataStore, - currencyService: .make()) + currencyService: currencyService) let fetcher = WalletBalanceFetcher(wallet: wallet, tokensService: pipeline) @@ -76,7 +77,8 @@ extension WalletDataProcessingPipeline { tokensService: tokensService, pipeline: pipeline, fetcher: fetcher, - sessionsProvider: sessionsProvider) + sessionsProvider: sessionsProvider, + currencyService: currencyService) dep.sessionsProvider.start(wallet: wallet) dep.fetcher.start() @@ -94,6 +96,7 @@ extension WalletDataProcessingPipeline { let pipeline: TokensProcessingPipeline let fetcher: WalletBalanceFetcher let sessionsProvider: SessionsProvider + let currencyService: AlphaWalletFoundation.CurrencyService } } diff --git a/modules/AlphaWalletFoundation/AlphaWalletFoundation/CoinTicker/Types/CoinTicker.swift b/modules/AlphaWalletFoundation/AlphaWalletFoundation/CoinTicker/Types/CoinTicker.swift index d79810cd9..8a2d23c3e 100644 --- a/modules/AlphaWalletFoundation/AlphaWalletFoundation/CoinTicker/Types/CoinTicker.swift +++ b/modules/AlphaWalletFoundation/AlphaWalletFoundation/CoinTicker/Types/CoinTicker.swift @@ -25,9 +25,9 @@ public struct CoinTicker: Hashable, Equatable { } extension CoinTicker { - public static func make(for token: TokenMappedToTicker) -> CoinTicker { + public static func make(for token: TokenMappedToTicker, currency: Currency) -> CoinTicker { let id = "for-testing-tickerId-\(token.contractAddress)-\(token.server.chainID)" - return .init(primaryKey: "\(id)-\(Currency.USD.code)", id: id, currency: .USD, lastUpdatedAt: Date(), symbol: "", price_usd: 0.0, percent_change_24h: 0.0, market_cap: 0.0, market_cap_rank: 0.0, total_volume: 0.0, high_24h: 0.0, low_24h: 0.0, market_cap_change_24h: 0.0, market_cap_change_percentage_24h: 0.0, circulating_supply: 0.0, total_supply: 0.0, max_supply: 0.0, ath: 0.0, ath_change_percentage: 0.0) + return .init(primaryKey: "\(id)-\(currency.code)", id: id, currency: currency, lastUpdatedAt: Date(), symbol: "", price_usd: 0.0, percent_change_24h: 0.0, market_cap: 0.0, market_cap_rank: 0.0, total_volume: 0.0, high_24h: 0.0, low_24h: 0.0, market_cap_change_24h: 0.0, market_cap_change_percentage_24h: 0.0, circulating_supply: 0.0, total_supply: 0.0, max_supply: 0.0, ath: 0.0, ath_change_percentage: 0.0) } public func override(currency: Currency) -> CoinTicker { diff --git a/modules/AlphaWalletFoundation/AlphaWalletFoundation/Foundation/Formatter.swift b/modules/AlphaWalletFoundation/AlphaWalletFoundation/Foundation/Formatter.swift index dee81f6b3..1ecd35693 100644 --- a/modules/AlphaWalletFoundation/AlphaWalletFoundation/Foundation/Formatter.swift +++ b/modules/AlphaWalletFoundation/AlphaWalletFoundation/Foundation/Formatter.swift @@ -29,7 +29,7 @@ extension NumberFormatter { return formatter } - public static let percent: NumberFormatter = { + public static var percent: NumberFormatter { let formatter = basicCurrencyFormatter() formatter.positiveFormat = ",###.#" formatter.negativeFormat = "-,###.#" @@ -38,7 +38,7 @@ extension NumberFormatter { formatter.numberStyle = .percent return formatter - }() + } //NOTE: does't work when its stored static let, should be computed var public static var shortCrypto: NumberFormatter { @@ -62,16 +62,16 @@ extension NumberFormatter { return formatter } - public static let scientificAmount: NumberFormatter = { + public static var scientificAmount: NumberFormatter { let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.usesGroupingSeparator = false formatter.locale = Locale(identifier: "en_US") return formatter - }() + } - public static let currencyAccounting: NumberFormatter = { + public static var currencyAccounting: NumberFormatter { let formatter = basicCurrencyFormatter() formatter.currencySymbol = "" formatter.minimumFractionDigits = Constants.formatterFractionDigits @@ -80,16 +80,16 @@ extension NumberFormatter { formatter.isLenient = true return formatter - }() - - public static let alternateAmount: NumberFormatter = { + } + //NOTE: don't use static let, some on formatters has changed in runtime, that brakes logic, use computed var + public static var alternateAmount: NumberFormatter { let formatter = basicCurrencyFormatter() formatter.currencySymbol = "" formatter.minimumFractionDigits = Constants.etherFormatterFractionDigits formatter.maximumFractionDigits = Constants.etherFormatterFractionDigits return formatter - }() + } } fileprivate func basicCurrencyFormatter() -> NumberFormatter { diff --git a/modules/AlphaWalletFoundation/AlphaWalletFoundation/Types/WalletDependencyContainer.swift b/modules/AlphaWalletFoundation/AlphaWalletFoundation/Types/WalletDependencyContainer.swift index 37a043fc7..14c329ca6 100644 --- a/modules/AlphaWalletFoundation/AlphaWalletFoundation/Types/WalletDependencyContainer.swift +++ b/modules/AlphaWalletFoundation/AlphaWalletFoundation/Types/WalletDependencyContainer.swift @@ -20,6 +20,7 @@ public protocol WalletDependency { var pipeline: TokensProcessingPipeline { get } var fetcher: WalletBalanceFetcher { get } var sessionsProvider: SessionsProvider { get } + var currencyService: CurrencyService { get } } public class WalletComponentsFactory: WalletDependencyContainer { @@ -28,7 +29,7 @@ public class WalletComponentsFactory: WalletDependencyContainer { public let assetDefinitionStore: AssetDefinitionStore public let coinTickersFetcher: CoinTickersFetcher public let config: Config - private let currencyService: CurrencyService + public let currencyService: CurrencyService private var walletDependencies: [Wallet: WalletDependency] = [:] private let networkService: NetworkService @@ -41,6 +42,7 @@ public class WalletComponentsFactory: WalletDependencyContainer { public let fetcher: WalletBalanceFetcher public let sessionsProvider: SessionsProvider public let eventsDataStore: NonActivityEventsDataStore + public let currencyService: CurrencyService } public init(analytics: AnalyticsServiceType, nftProvider: NFTProvider, assetDefinitionStore: AssetDefinitionStore, coinTickersFetcher: CoinTickersFetcher, config: Config, currencyService: CurrencyService, networkService: NetworkService) { @@ -76,7 +78,7 @@ public class WalletComponentsFactory: WalletDependencyContainer { let activitiesPipeLine = ActivitiesPipeLine(config: config, wallet: wallet, assetDefinitionStore: assetDefinitionStore, transactionDataStore: transactionsDataStore, tokensService: tokensService, sessionsProvider: sessionsProvider, eventsActivityDataStore: eventsActivityDataStore, eventsDataStore: eventsDataStore, analytics: analytics) - let dependency: WalletDependency = Dependencies(activitiesPipeLine: activitiesPipeLine, transactionsDataStore: transactionsDataStore, importToken: importToken, tokensService: tokensService, pipeline: pipeline, fetcher: fetcher, sessionsProvider: sessionsProvider, eventsDataStore: eventsDataStore) + let dependency: WalletDependency = Dependencies(activitiesPipeLine: activitiesPipeLine, transactionsDataStore: transactionsDataStore, importToken: importToken, tokensService: tokensService, pipeline: pipeline, fetcher: fetcher, sessionsProvider: sessionsProvider, eventsDataStore: eventsDataStore, currencyService: currencyService) walletDependencies[wallet] = dependency