<commit_before>//
//  SampleCodeTest.swift
//  CBL Swift
//
//  Created by <NAME> on 1/25/18.
//  Copyright © 2018 Couchbase. All rights reserved.
//

import XCTest
import CouchbaseLiteSwift


class SampleCodeTest: CBLTestCase {
    
    var database: Database!
    
    var replicator: Replicator!
    
    // MARK: Database
    
    func dontTestNewDatabase() throws {
        // <doc>
        do {
            self.database = try Database(name: "my-database")
        } catch {
            print(error)
        }
        // </doc>
    }
    
    func dontTestEncryption() throws {
        // <doc>
        let config = DatabaseConfiguration()
        config.encryptionKey = EncryptionKey.password("<PASSWORD>")
        self.database = try Database(name: "my-database", config: config)
        // </doc>
    }
    
    func dontTestLogging() throws {
        // <doc>
        Database.setLogLevel(.verbose, domain: .replicator)
        Database.setLogLevel(.verbose, domain: .query)
        // </doc>
    }
    
    func dontTestLoadingPrebuilt() throws {
        // <doc>
        let path = Bundle.main.path(forResource: "travel-sample", ofType: "cblite2")!
        if !Database.exists(withName: "travel-sample") {
            do {
                try Database.copy(fromPath: path, toDatabase: "travel-sample", withConfig: nil)
            } catch {
                fatalError("Could not load pre-built database")
            }
        }
        // </doc>
    }
    
    // MARK: Document
    
    func dontTestInitializer() throws {
        database = self.db
        
        // <doc>
        let dict: [String: Any] = ["type": "task",
                                   "owner": "todo",
                                   "createdAt": Date()]
        let newTask = MutableDocument(data: dict)
        try database.saveDocument(newTask)
        // </doc>
    }
    
    func dontTestMutability() throws {
        database = self.db
        let newTask = MutableDocument()
        
        // <doc>
        // newTask is a MutableDocument
        newTask.setString("apples", forKey: "name")
        try database.saveDocument(newTask)
        // </doc>
    }
    
    func dontTestTypedAcessors() throws {
        let newTask = MutableDocument()
        
        // <doc>
        newTask.setValue(Date(), forKey: "createdAt")
        let date = newTask.date(forKey: "createdAt")
        // </doc>
        
        print("\(date!)")
    }
    
    func dontTestBatchOperations() throws {
        // <doc>
        do {
            try database.inBatch {
                for i in 0...10 {
                    let doc = MutableDocument()
                    doc.setValue("user", forKey: "type")
                    doc.setValue("user \(i)", forKey: "name")
                    doc.setBoolean(false, forKey: "admin")
                    try database.saveDocument(doc)
                    print("saved user document \(doc.string(forKey: "name")!)")
                }
            }
        } catch let error {
            print(error.localizedDescription)
        }
        // </doc>
    }
    
    func dontTestBlob() throws {
        database = self.db
        let newTask = MutableDocument()
        var image: UIImage!
        
        // <doc>
        let appleImage = UIImage(named: "avatar.jpg")!
        let imageData = UIImageJPEGRepresentation(appleImage, 1)!
        
        let blob = Blob(contentType: "image/jpg", data: imageData)
        newTask.setBlob(blob, forKey: "avatar")
        try database.saveDocument(newTask)
        
        if let taskBlob = newTask.blob(forKey: "image") {
            image = UIImage(data: taskBlob.content!)
        }
        // </doc>
        
        print("\(image)")
    }
    
    // MARK: Query
    
    func dontTestIndexing() throws {
        database = self.db
        
        // <doc>
        let index = IndexBuilder.valueIndex(items:<commit_msg>Fix Swift SampleCodeTest build failure on Mac target<commit_after>//
//  SampleCodeTest.swift
//  CBL Swift
//
//  Created by <NAME> on 1/25/18.
//  Copyright © 2018 Couchbase. All rights reserved.
//

import XCTest
import CouchbaseLiteSwift


class SampleCodeTest: CBLTestCase {
    
    var database: Database!
    
    var replicator: Replicator!
    
    // MARK: Database
    
    func dontTestNewDatabase() throws {
        // <doc>
        do {
            self.database = try Database(name: "my-database")
        } catch {
            print(error)
        }
        // </doc>
    }
    
    func dontTestEncryption() throws {
        // <doc>
        let config = DatabaseConfiguration()
        config.encryptionKey = EncryptionKey.password("<PASSWORD>")
        self.database = try Database(name: "my-database", config: config)
        // </doc>
    }
    
    func dontTestLogging() throws {
        // <doc>
        Database.setLogLevel(.verbose, domain: .replicator)
        Database.setLogLevel(.verbose, domain: .query)
        // </doc>
    }
    
    func dontTestLoadingPrebuilt() throws {
        // <doc>
        let path = Bundle.main.path(forResource: "travel-sample", ofType: "cblite2")!
        if !Database.exists(withName: "travel-sample") {
            do {
                try Database.copy(fromPath: path, toDatabase: "travel-sample", withConfig: nil)
            } catch {
                fatalError("Could not load pre-built database")
            }
        }
        // </doc>
    }
    
    // MARK: Document
    
    func dontTestInitializer() throws {
        database = self.db
        
        // <doc>
        let dict: [String: Any] = ["type": "task",
                                   "owner": "todo",
                                   "createdAt": Date()]
        let newTask = MutableDocument(data: dict)
        try database.saveDocument(newTask)
        // </doc>
    }
    
    func dontTestMutability() throws {
        database = self.db
        let newTask = MutableDocument()
        
        // <doc>
        // newTask is a MutableDocument
        newTask.setString("apples", forKey: "name")
        try database.saveDocument(newTask)
        // </doc>
    }
    
    func dontTestTypedAcessors() throws {
        let newTask = MutableDocument()
        
        // <doc>
        newTask.setValue(Date(), forKey: "createdAt")
        let date = newTask.date(forKey: "createdAt")
        // </doc>
        
        print("\(date!)")
    }
    
    func dontTestBatchOperations() throws {
        // <doc>
        do {
            try database.inBatch {
                for i in 0...10 {
                    let doc = MutableDocument()
                    doc.setValue("user", forKey: "type")
                    doc.setValue("user \(i)", forKey: "name")
                    doc.setBoolean(false, forKey: "admin")
                    try database.saveDocument(doc)
                    print("saved user document \(doc.string(forKey: "name")!)")
                }
            }
        } catch let error {
            print(error.localizedDescription)
        }
        // </doc>
    }
    
    func dontTestBlob() throws {
    #if TARGET_OS_IPHONE
        database = self.db
        let newTask = MutableDocument()
        var image: UIImage!
        
        // <doc>
        let appleImage = UIImage(named: "avatar.jpg")!
        let imageData = UIImageJPEGRepresentation(appleImage, 1)!
        
        let blob = Blob(contentType: "image/jpg", data: imageData)
        newTask.setBlob(blob, forKey: "avatar")
        try database.saveDocument(newTask)
        
        if let taskBlob = newTask.blob(forKey: "image") {
            image = UIImage(data: taskBlob.content!)
        }
        // </doc>
        
        print("\(image)")
    #endif
    }
    
    // MARK: Query
    
    func dontTestIndexing() throws {
        database = self.db
        
        // <doc>
        let index = IndexBuilder.valueIndex(items:
{'fraction_non_alphanumeric': 0.1190204288795838, 'fraction_numerical': 0.0035528486232711586, 'mean_word_length': 1.5206267988487367, 'pattern_counts': {'":': 6, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1762045', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2235, 'n_words': 501}