From 5352475d0672a7751c734a05ef53b3d9063dece6 Mon Sep 17 00:00:00 2001 From: Jigar-f Date: Wed, 27 Sep 2023 12:31:07 +0530 Subject: [PATCH] Catch error and return. --- .../DatabaseFramework/Db/Database.swift | 14 +++- .../DatabaseFrameworkTests.swift | 83 +++++++++++++++---- 2 files changed, 78 insertions(+), 19 deletions(-) diff --git a/ios/DatabaseFramework/DatabaseFramework/DatabaseFramework/Db/Database.swift b/ios/DatabaseFramework/DatabaseFramework/DatabaseFramework/Db/Database.swift index ce9812558..5b2f9b231 100644 --- a/ios/DatabaseFramework/DatabaseFramework/DatabaseFramework/Db/Database.swift +++ b/ios/DatabaseFramework/DatabaseFramework/DatabaseFramework/Db/Database.swift @@ -116,7 +116,19 @@ class TransactionManager: NSObject, MinisqlTxProtocol { try begin() } - try statement.run(bindings) + do { + try statement.run(bindings) + } catch let error { + // Check if the error is a UNIQUE constraint error + //Show how if we return same error go is not abel to catch it + + let errorMessage = String(describing: error) + if errorMessage.contains("UNIQUE constraint failed") { + throw NSError(domain: "UNIQUE constraint failed", code: 19, userInfo: nil) + } else { + throw error + } + } return QueryResult(changes: database.totalChanges) } diff --git a/ios/DatabaseFramework/DatabaseFramework/DatabaseFrameworkTests/DatabaseFrameworkTests.swift b/ios/DatabaseFramework/DatabaseFramework/DatabaseFrameworkTests/DatabaseFrameworkTests.swift index c29d41b7c..c68a5633b 100644 --- a/ios/DatabaseFramework/DatabaseFramework/DatabaseFrameworkTests/DatabaseFrameworkTests.swift +++ b/ios/DatabaseFramework/DatabaseFramework/DatabaseFrameworkTests/DatabaseFrameworkTests.swift @@ -7,30 +7,77 @@ import XCTest @testable import DatabaseFramework +import Internalsdk -final class DatabaseFrameworkTests: XCTestCase { - - override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. +final class DatabaseFrameworkTests: XCTestCase,TestsupportTestingTProtocol { + + func errorf(_ text: String?) { + XCTFail(text!) } - - override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. + + func failNow() { + XCTFail("failing now!") + } + + override func setUp() { + super.setUp() + continueAfterFailure = false } + + + func testTransactions() throws { + let db = try newDB() + TestsupportTestTransactions(self, db) + } + + + func testSubscriptions() throws { + let db = try newDB() + TestsupportTestSubscriptions(self, db) + } + - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct results. - // Any test you write for XCTest can be annotated as throws and async. - // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error. - // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards. + func testSubscribeToInitialDetails() throws { + let db = try newDB() + TestsupportTestSubscribeToInitialDetails(self, db) + } + + + func testList() throws { + let db = try newDB() + TestsupportTestList(self, db) + } + + + func testSearch() throws { + let db = try newDB() + TestsupportTestSearch(self, db) + } + + func testSearchChinese() throws { + let db = try newDB() + TestsupportTestSearchChinese(self, db) + } + + + // Utils methods + private func newDB() throws -> MinisqlDBProtocol { + return try DatabaseFactory.getDbManager(databasePath: newDBPath()) } - func testPerformanceExample() throws { - // This is an example of a performance test case. - self.measure { - // Put the code you want to measure the time of here. - } + private func newDBPath() -> String { + let fileManager = FileManager.default + let directory = fileManager.temporaryDirectory + let subdirectory = UUID().uuidString + let dbDir = directory.appendingPathComponent(subdirectory) + + do { + try fileManager.createDirectory(at: dbDir, withIntermediateDirectories: true, attributes: nil) + let dbLocation = dbDir.appendingPathComponent("db").path + return dbLocation + } catch { + return "" // Return an empty string or handle the error accordingly. + } } }