<commit_before>//
//  ResourceRepositoryReaderTests.swift
//  WaniKaniKitTests
//
//  Copyright © 2017 <NAME>. All rights reserved.
//

import FMDB
import XCTest
@testable import WaniKaniKit

class ResourceRepositoryReaderTests: XCTestCase {
    
    private var databaseManager: DatabaseManager!
    private var resourceRepository: ResourceRepositoryReader!
    
    private var nextAssignmentID = 100000
    private var nextSubjectID = 1
    private let testUserLevel = 10
    
    private var nextHourReviewTime: Date!
    private var nextDayReviewTime: Date!
    
    override func setUp() {
        super.setUp()
        
        let factory = EphemeralDatabaseConnectionFactory(databaseStorageType: .file)
        databaseManager = DatabaseManager(factory: factory)
        resourceRepository = ResourceRepositoryReader(databaseManager: databaseManager)
        
        nextAssignmentID = 100000
        
        if !databaseManager.open() {
            self.continueAfterFailure = false
            XCTFail("Failed to open database queue")
        }
        
        createTestUser()
        
        let calendar = Calendar.current
        var components = calendar.dateComponents(in: utcTimeZone, from: calendar.date(byAdding: .minute, value: 30, to: Date())!)
        components.second = 0
        components.nanosecond = 0
        
        nextHourReviewTime = components.date!
        
        components = calendar.dateComponents(in: utcTimeZone, from: calendar.date(byAdding: .hour, value: 12, to: calendar.startOfHour(for: Date()))!)
        
        nextDayReviewTime = components.date!
    }
    
    override func tearDown() {
        databaseManager.close()
        databaseManager = nil
        
        super.tearDown()
    }
    
    func testHasStudyQueue() {
        XCTAssertFalse(try resourceRepository.hasStudyQueue())
        
        populateDatabaseForStudyQueue(lessonCount: 10, pendingReviewCount: 23, futureReviewCount: 0)
        
        XCTAssertTrue(try resourceRepository.hasStudyQueue())
    }
    
    func testStudyQueue_Empty() {
        let expected = StudyQueue(lessonsAvailable: 0, reviewsAvailable: 0, nextReviewDate: nil, reviewsAvailableNextHour: 0, reviewsAvailableNextDay: 0)
        
        XCTAssertEqual(try resourceRepository.studyQueue(), expected)
    }
    
    func testStudyQueue_PendingLessonsReviews_NoFutureReviews() {
        populateDatabaseForStudyQueue(lessonCount: 10, pendingReviewCount: 23, futureReviewCount: 0)
        
        let expected = StudyQueue(lessonsAvailable: 10, reviewsAvailable: 23, nextReviewDate: Date(), reviewsAvailableNextHour: 0, reviewsAvailableNextDay: 0)
        
        XCTAssertEqual(try resourceRepository.studyQueue(), expected)
    }
    
    func testStudyQueue_FutureReviewsOnly() {
        populateDatabaseForStudyQueue(lessonCount: 0, pendingReviewCount: 0, futureReviewCount: 6, futureReviewTime: nextHourReviewTime)
        populateDatabaseForStudyQueue(lessonCount: 0, pendingReviewCount: 0, futureReviewCount: 4, futureReviewTime: nextDayReviewTime)
        <commit_msg>Fix test<commit_after>//
//  ResourceRepositoryReaderTests.swift
//  WaniKaniKitTests
//
//  Copyright © 2017 <NAME>. All rights reserved.
//

import FMDB
import XCTest
@testable import WaniKaniKit

class ResourceRepositoryReaderTests: XCTestCase {
    
    private var databaseManager: DatabaseManager!
    private var resourceRepository: ResourceRepositoryReader!
    
    private var nextAssignmentID = 100000
    private var nextSubjectID = 1
    private let testUserLevel = 10
    
    private var nextHourReviewTime: Date!
    private var nextDayReviewTime: Date!
    
    override func setUp() {
        super.setUp()
        
        let factory = EphemeralDatabaseConnectionFactory(databaseStorageType: .file)
        databaseManager = DatabaseManager(factory: factory)
        resourceRepository = ResourceRepositoryReader(databaseManager: databaseManager)
        
        nextAssignmentID = 100000
        
        if !databaseManager.open() {
            self.continueAfterFailure = false
            XCTFail("Failed to open database queue")
        }
        
        createTestUser()
        
        let calendar = Calendar.current
        var components = calendar.dateComponents(in: utcTimeZone, from: calendar.date(byAdding: .minute, value: 30, to: Date())!)
        components.second = 0
        components.nanosecond = 0
        
        nextHourReviewTime = components.date!
        
        components = calendar.dateComponents(in: utcTimeZone, from: calendar.date(byAdding: .hour, value: 12, to: calendar.startOfHour(for: Date()))!)
        
        nextDayReviewTime = components.date!
    }
    
    override func tearDown() {
        databaseManager.close()
        databaseManager = nil
        
        super.tearDown()
    }
    
    func testHasStudyQueue() {
        XCTAssertFalse(try resourceRepository.hasStudyQueue())
        
        populateDatabaseForStudyQueue(lessonCount: 10, pendingReviewCount: 23, futureReviewCount: 0)
        
        XCTAssertTrue(try resourceRepository.hasStudyQueue())
    }
    
    func testStudyQueue_Empty() {
        let expected = StudyQueue(lessonsAvailable: 0, reviewsAvailable: 0, nextReviewDate: nil, reviewsAvailableNextHour: 0, reviewsAvailableNextDay: 0)
        
        XCTAssertEqual(try resourceRepository.studyQueue(), expected)
    }
    
    func testStudyQueue_PendingLessonsReviews_NoFutureReviews() {
        populateDatabaseForStudyQueue(lessonCount: 10, pendingReviewCount: 23, futureReviewCount: 0)
        
        let expected = StudyQueue(lessonsAvailable: 10, reviewsAvailable: 23, nextReviewDate: nil, reviewsAvailableNextHour: 0, reviewsAvailableNextDay: 0)
        
        XCTAssertEqual(try resourceRepository.studyQueue(), expected)
    }
    
    func testStudyQueue_FutureReviewsOnly() {
        populateDatabaseForStudyQueue(lessonCount: 0, pendingReviewCount: 0, futureReviewCount: 6, futureReviewTime: nextHourReviewTime)
        populateDatabaseForStudyQueue(lessonCount: 0, pendingReviewCount: 0, futureReviewCount: 4, futureReviewTime: nextDayReviewTime)
        
{'fraction_non_alphanumeric': 0.07442313942151446, 'fraction_numerical': 0.016574585635359115, 'mean_word_length': 3.0334207077326343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7057926', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1605, 'n_words': 369}