Skip to content

Commit

Permalink
Update datamanager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Jun 18, 2018
1 parent 946862e commit b2b243f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 52 deletions.
3 changes: 2 additions & 1 deletion BookPlayer/Library/DataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class DataManager {
- Returns: `URL` of the file's new location. Returns `nil` if hashing fails.
*/
class func processFile(at origin: URL, destinationFolder: URL, completion:@escaping (URL?) -> Void) {
guard let inputStream = InputStream(url: origin) else {
guard FileManager.default.fileExists(atPath: origin.path),
let inputStream = InputStream(url: origin) else {
completion(nil)
return
}
Expand Down
66 changes: 15 additions & 51 deletions BookPlayerTests/DataManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,80 +61,44 @@ class GetFilesTests: DataManagerTests {

// MARK: - processFiles()
class ProcessFilesTests: DataManagerTests {
func testProcessFilesFromNilFolder() {
let nonExistingFolder = URL(fileURLWithPath: "derp")
DataManager.processFiles(in: nonExistingFolder) { (urls) in
XCTAssert(urls.isEmpty)
}
}
func testProcessNoFiles() {
let documentsFolder = DataManager.getDocumentsFolderURL()
let nonExistingFile = URL(fileURLWithPath: "derp")
let destinationFolder = DataManager.getProcessedFolderURL()

DataManager.processFiles(in: documentsFolder) { (urls) in
XCTAssert(urls.isEmpty)
DataManager.processFile(at: nonExistingFile, destinationFolder: destinationFolder) { (url) in
XCTAssertNil(url)
}
}
func testProcessPendingOneFile() {

func testProcessOneFile() {
let filename = "file.txt"
let bookContents = "bookcontents".data(using: .utf8)!
let documentsFolder = DataManager.getDocumentsFolderURL()

// Add test file to Documents folder
let destination = self.generateTestFile(name: filename, contents: bookContents, destinationFolder: documentsFolder)
let fileUrl = self.generateTestFile(name: filename, contents: bookContents, destinationFolder: documentsFolder)

let expectation = XCTestExpectation(description: "Processing pending files")

DataManager.processFiles(in: documentsFolder) { (urls) in
let destinationFolder = DataManager.getProcessedFolderURL()
DataManager.processFile(at: fileUrl, destinationFolder: destinationFolder) { (url) in
// Test file should no longer be in the Documents folder
XCTAssert(!FileManager.default.fileExists(atPath: destination.path))

let url = urls.first!
XCTAssert(!FileManager.default.fileExists(atPath: fileUrl.path))

XCTAssertNotNil(url)
// Name of processed file shouldn't be the same as the original
XCTAssert(url.lastPathComponent != filename)
XCTAssert(url!.lastPathComponent != filename)
// Test file exists in new location
XCTAssert(FileManager.default.fileExists(atPath: url!.path))

let content = FileManager.default.contents(atPath: url.path)!
let content = FileManager.default.contents(atPath: url!.path)!
XCTAssert(content == bookContents)

expectation.fulfill()
}

wait(for: [expectation], timeout: 15)
}

func testProcessPendingMultipleFiles() {
// Add test file to Documents folder
let filename1 = "file1.txt"
let book1Contents = "book1contents".data(using: .utf8)!
let filename2 = "file2.txt"
let book2Contents = "book2contents".data(using: .utf8)!
let dataArray = [book1Contents, book2Contents]

let documentsFolder = DataManager.getDocumentsFolderURL()

let destination1 = self.generateTestFile(name: filename1, contents: book1Contents, destinationFolder: documentsFolder)
let destination2 = self.generateTestFile(name: filename2, contents: book2Contents, destinationFolder: documentsFolder)

let expectation = XCTestExpectation(description: "Processing pending files")

DataManager.processFiles(in: documentsFolder) { (urls) in
// Test files should no longer be in the Documents folder
XCTAssert(!FileManager.default.fileExists(atPath: destination1.path))
XCTAssert(!FileManager.default.fileExists(atPath: destination2.path))

// Count of returned urls should be the same as the number of test files generated
XCTAssert(urls.count == 2)

for url in urls {
let content = FileManager.default.contents(atPath: url.path)!
XCTAssertNotNil(dataArray.index(of: content))
}

expectation.fulfill()
}

wait(for: [expectation], timeout: 15)
}
}

// MARK: - insertBooks(from:into:or:completion:)
Expand Down

0 comments on commit b2b243f

Please sign in to comment.