|
| 1 | +// |
| 2 | +/**************************************************************************** |
| 3 | +* Copyright 2019, Optimizely, Inc. and contributors * |
| 4 | +* * |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); * |
| 6 | +* you may not use this file except in compliance with the License. * |
| 7 | +* You may obtain a copy of the License at * |
| 8 | +* * |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 * |
| 10 | +* * |
| 11 | +* Unless required by applicable law or agreed to in writing, software * |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, * |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 14 | +* See the License for the specific language governing permissions and * |
| 15 | +* limitations under the License. * |
| 16 | +***************************************************************************/ |
| 17 | + |
| 18 | + |
| 19 | +import XCTest |
| 20 | + |
| 21 | +class DataStoreTests: XCTestCase { |
| 22 | + |
| 23 | + override func setUp() { |
| 24 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 25 | + if let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { |
| 26 | + if (!FileManager.default.fileExists(atPath: url.path)) { |
| 27 | + do { |
| 28 | + try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil) |
| 29 | + } |
| 30 | + catch { |
| 31 | + print(error) |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + override func tearDown() { |
| 40 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 41 | + } |
| 42 | + |
| 43 | + func testMemoryStore() { |
| 44 | + // This is an example of a functional test case. |
| 45 | + // Use XCTAssert and related functions to verify your tests produce the correct results. |
| 46 | + |
| 47 | + let datastore = DataStoreMemory<String>(storeName: "testingDataStoreMemory") |
| 48 | + |
| 49 | + datastore.saveItem(forKey: "testString", value: "value") |
| 50 | + |
| 51 | + let value = datastore.getItem(forKey: "testString") as! String |
| 52 | + |
| 53 | + XCTAssert(value == "value") |
| 54 | + |
| 55 | + datastore.unsubscribe() |
| 56 | + |
| 57 | + datastore.subscribe() |
| 58 | + |
| 59 | + datastore.load(forKey: "testingDataStoreMemory") |
| 60 | + |
| 61 | + let v2 = datastore.getItem(forKey: "testString") as! String |
| 62 | + |
| 63 | + XCTAssert(v2 == value) |
| 64 | + } |
| 65 | + |
| 66 | + func testFileStore() { |
| 67 | + // This is an example of a functional test case. |
| 68 | + // Use XCTAssert and related functions to verify your tests produce the correct results. |
| 69 | + |
| 70 | + let datastore = DataStoreFile<[String]>(storeName: "testingDataStoreFile") |
| 71 | + |
| 72 | + datastore.saveItem(forKey: "testString", value: ["value"]) |
| 73 | + |
| 74 | + let vj = datastore.getItem(forKey: "testString") as! [String] |
| 75 | + |
| 76 | + XCTAssert(vj.first == "value") |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + func testUserDefaults() { |
| 81 | + // This is an example of a functional test case. |
| 82 | + // Use XCTAssert and related functions to verify your tests produce the correct results. |
| 83 | + |
| 84 | + let datastore = DataStoreMemory<String>(storeName: "testingDataStoreUserDefaults") |
| 85 | + |
| 86 | + datastore.saveItem(forKey: "testString", value: "value") |
| 87 | + |
| 88 | + let value = datastore.getItem(forKey: "testString") as! String |
| 89 | + |
| 90 | + XCTAssert(value == "value") |
| 91 | + |
| 92 | + datastore.unsubscribe() |
| 93 | + |
| 94 | + datastore.subscribe() |
| 95 | + |
| 96 | + datastore.load(forKey: "testingDataStoreUserDefaults") |
| 97 | + |
| 98 | + let v2 = datastore.getItem(forKey: "testString") as! String |
| 99 | + |
| 100 | + XCTAssert(v2 == value) |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + func testPerformanceExample() { |
| 105 | + // This is an example of a performance test case. |
| 106 | + self.measure { |
| 107 | + // Put the code you want to measure the time of here. |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments