<commit_before>import XCTest
import MongoKitten

class ConnectionSettingsTests : XCTestCase {
    struct ConnectionStringTest {
        enum Result {
            case throwsError
            case connectionSettings(ConnectionSettings)
        }
        
        var string: String
        var expectedResult: Result
    }
    
    let connectionStringTests: [ConnectionStringTest] = [
        // TODO: add more connection strings
        .init(
            string: "mongodb://henk:bar@<EMAIL>:1234/kaas?ssl&sslVerify=false",
            expectedResult: .connectionSettings(
                .init(authentication: .auto(username: "henk", password: "<PASSWORD>"), hosts: [.init(hostname: "foo.be", port: 1234)], targetDatabase: "kaas", useSSL: true, verifySSLCertificates: false)
            ))
    ]
    
    func testConnectionStrings() {
        for test in connectionStringTests {
            switch test.expectedResult {
            case .throwsError:
                XCTAssertThrowsError(try ConnectionSettings(test.string))
            case .connectionSettings(let expectedSettings):
                do {
                    let generatedSettings = try ConnectionSettings(test.string)
                    XCTAssertEqual(generatedSettings, expectedSettings)
                } catch {
                    XCTFail("\(error) – String: \(test.string)")
                }
            }
        }
    }
}<commit_msg>Added fam test.<commit_after>import XCTest
import MongoKitten

class ConnectionSettingsTests : XCTestCase {
    struct ConnectionStringTest {
        enum Result {
            case throwsError
            case connectionSettings(ConnectionSettings)
        }
        
        var string: String
        var expectedResult: Result
    }
    
    let connectionStringTests: [ConnectionStringTest] = [
        // TODO: add more connection strings
        .init(
            string: "mongodb://henk:bar@foo.be:1234/kaas?ssl&sslVerify=false",
            expectedResult: .connectionSettings(
                .init(authentication: .auto(username: "henk", password: "<PASSWORD>"), hosts: [.init(hostname: "foo.be", port: 1234)], targetDatabase: "kaas", useSSL: true, verifySSLCertificates: false)
            ))
    ]
    
    func testConnectionStrings() {
        for test in connectionStringTests {
            switch test.expectedResult {
            case .throwsError:
                XCTAssertThrowsError(try ConnectionSettings(test.string))
            case .connectionSettings(let expectedSettings):
                do {
                    let generatedSettings = try ConnectionSettings(test.string)
                    XCTAssertEqual(generatedSettings, expectedSettings)
                } catch {
                    XCTFail("\(error) – String: \(test.string)")
                }
            }
        }
    }
    
    func test_findAndModify() throws {
        
        do {
            let mongoSettings = try ConnectionSettings("mongodb://localhost:27017/helixtest")
            let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
            let mongodb = try MongoDatabase.lazyConnect(settings: mongoSettings, on: elg)

            let db = mongodb

            // insert dummy data otherwise mongo won't know that transactions aren't supported because it hasn't found a host just yet since no commands were executed.
            let r = try db["queue_collection"].insert(["email": "<EMAIL>","phone": "12345678900"]).wait()

            // Start transaction
            let transactionDatabase = try db.startTransaction(autoCommitChanges: false)
            // Acquire read lock on dummy data for duration of transaction
            let readLockedDocument = try transactionDatabase["queue_collection"].findOneAndUpdate(where: ["email": "<EMAIL>"],
                                                                                                  to: ["$set": ["readLock": ["id": ObjectId()] as Document]],
                                                                                                  returnValue: .modified).execute().wait()

            // Attempt to modify the document (should fail because I have a read lock on the document)
            let nonTransactionQuery = try db["queue_collection"].findOneAndUpdate(where: ["email": "<EMAIL>"],
                                                        to: ["$set": ["phone": "1111111111"]],
                                                        returnValue: .modified)
                                            .execute()
                                            .wait()
            
            print(nonTransactionQuery)
            
            // Update the document that we have a read lock on.
            let original = try transactionDatabase["queue_collection"].findOneAndUpdate(where: ["email": "<EMAIL>"],
                                                                                        to: ["$set": ["phone": "0000000000"]],
                                                                                        returnValue: .original).execute().wait()
            // verify that the document was not mutated outside the transaction
            print("Original document after transaction update.")
            print(original)
            // Commit the transaction
            try transactionDatabase.commit().wait()

            // Fetch the newly modified document to verify it was mutated after the transaction was commited
            let res = try mongodb["queue_collection"].findOne().wait()
            print("Modified document after transaction update.")
            print(res)
        } catch {
            print(error)
        }
    }
    
}
{'fraction_non_alphanumeric': 0.08892025405786874, 'fraction_numerical': 0.00935074100211715, 'mean_word_length': 1.6257526632700323, 'pattern_counts': {'":': 12, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7654664', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1228, 'n_words': 341}