Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test unittst coredata #55

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Calq.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
F1B292DF2A3BC1DF00AF2ACD /* MockDataProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B292DE2A3BC1DF00AF2ACD /* MockDataProvider.swift */; };
F1B386472A1CFE6800F0C5AC /* CalqTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B386462A1CFE6800F0C5AC /* CalqTests.swift */; };
F1B52D172A323BCB0082DEBE /* MockCoreDataStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B52D162A323BCB0082DEBE /* MockCoreDataStack.swift */; };
F1B8018B2AADD9EA003DBE47 /* exampleData_v2.json in Resources */ = {isa = PBXBuildFile; fileRef = F1B8018A2AADD9EA003DBE47 /* exampleData_v2.json */; };
F1BC83E92AA78F17000D7CD2 /* JSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1BC83E82AA78F17000D7CD2 /* JSONTests.swift */; };
F1BE91C22A41DF2A00E85494 /* SubjectListVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1BE91C12A41DF2A00E85494 /* SubjectListVM.swift */; };
F1C04B5229BA376800DAC128 /* MediumBarChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1C04B5129BA376800DAC128 /* MediumBarChart.swift */; };
Expand Down Expand Up @@ -214,6 +215,7 @@
F1B386442A1CFE6800F0C5AC /* CalqTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CalqTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
F1B386462A1CFE6800F0C5AC /* CalqTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalqTests.swift; sourceTree = "<group>"; };
F1B52D162A323BCB0082DEBE /* MockCoreDataStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCoreDataStack.swift; sourceTree = "<group>"; };
F1B8018A2AADD9EA003DBE47 /* exampleData_v2.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = exampleData_v2.json; sourceTree = "<group>"; };
F1BC83E82AA78F17000D7CD2 /* JSONTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONTests.swift; sourceTree = "<group>"; };
F1BE91C12A41DF2A00E85494 /* SubjectListVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubjectListVM.swift; sourceTree = "<group>"; };
F1C04B5129BA376800DAC128 /* MediumBarChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediumBarChart.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -446,6 +448,7 @@
F1BC83E82AA78F17000D7CD2 /* JSONTests.swift */,
F1DD87ED2AA795910095C057 /* LineChartTests.swift */,
F1DD87EF2AA799700095C057 /* BarChartTests.swift */,
F1B8018A2AADD9EA003DBE47 /* exampleData_v2.json */,
);
path = CalqTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -614,6 +617,7 @@
buildActionMask = 2147483647;
files = (
F1A710352A4384D900255F10 /* exampleData_v0.json in Resources */,
F1B8018B2AADD9EA003DBE47 /* exampleData_v2.json in Resources */,
F1A710332A437F8300255F10 /* exampleData_v1.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
3 changes: 2 additions & 1 deletion Calq/lib/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ struct Util {
/// Returns the apps settings
static func getSettings() -> AppSettings {
do {
let requestResult: [NSManagedObject] = try Util.getContext().fetch(AppSettings.fetchRequest())
let fetchRequest = NSFetchRequest<AppSettings>(entityName: "AppSettings")
let requestResult = try context.fetch(fetchRequest)

if requestResult.isEmpty {
let item = AppSettings(context: Util.getContext())
Expand Down
13 changes: 11 additions & 2 deletions CalqTests/BarChartTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ import XCTest
@testable import Calq

final class BarChartTests: XCTestCase {

override class func setUp() {
Util.setContext(TestCoreDataStack.sharedContext)
}

func testCreateSubjectBarData() {
// TODO:
MockDataProvider.getSubjectWithTests()
let data = createSubjectBarData().first!
XCTAssertEqual(data.value, 11)
}

func testCreateHalfYearBarChartData() {
// TODO:
MockDataProvider.getSubjectWithTests()
let data = createHalfYearBarChartData()
let expected: [Double] = [0, 11, 0, 0]
XCTAssertEqual(data.map { $0.value}, expected)
}
}
15 changes: 11 additions & 4 deletions CalqTests/CalqTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ final class CalqTests: XCTestCase {
}

func testAverageString_WithoutTests() {
let sub = MockDataProvider.getSubjectWithoutTests()
let average = Util.averageString(MockDataProvider.getSubjectWithoutTests())
XCTAssertEqual(average, "-- -- -- -- ")
}
Expand Down Expand Up @@ -170,16 +169,24 @@ final class CalqTests: XCTestCase {
XCTAssertGreaterThan(Util.getTypes().count, 1)
}

func testGetTypes_WhenOnly1Exists() {
/* func testGetTypes_WhenOnly1Exists() {
// TODO:
}
/*
Util.deleteSettings()
JSON.loadDemoData()
*/
let type = Util.getTypes().first!
Util.deleteType(type: type)
XCTAssertEqual(Util.getTypes().count, 2)
}*/

func testgetTypeGrades() {
XCTAssertNotNil(Util.getTypeGrades(0))
}

func testIsPrimaryType() {
// TODO:
let type = Util.getTypes().filter { $0.name == "Klausur"}.first!
XCTAssertTrue(Util.isPrimaryType(type))
}

func testIsPrimaryType_GradeType() {
Expand Down
29 changes: 16 additions & 13 deletions CalqTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,33 @@ final class JSONTests: XCTestCase {
}

func testImportV0() {
Util.deleteSettings()

let testBundle = Bundle(for: type(of: self))
guard let ressourceURL = testBundle.url(forResource: "exampleData_v0", withExtension: "json") else {
return assertionFailure("ExampleFile does not exist")
}
loadJSON(ressource: "exampleData_v0")

do { try JSON.importJSONfromDevice(ressourceURL) } catch { return assertionFailure("Failed to load resource") }
XCTAssertEqual(Util.getAllSubjects().count, 1)
}

func testImportV1() {
loadJSON(ressource: "exampleData_v1")

XCTAssertEqual(Util.getAllSubjects().count, 1)
}

func testImportV2() {
loadJSON(ressource: "exampleData_v2")

let settings = Util.getSettings()
XCTAssertEqual(settings.hasFiveExams, false)
}

// MARK: load Data
func loadJSON(ressource: String) {
Util.deleteSettings()

let testBundle = Bundle(for: type(of: self))
guard let ressourceURL = testBundle.url(forResource: "exampleData_v1", withExtension: "json") else {
guard let ressourceURL = testBundle.url(forResource: ressource, withExtension: "json") else {
return assertionFailure("ExampleFile does not exist")
}

do { try JSON.importJSONfromDevice(ressourceURL) } catch { return assertionFailure("Failed to load resource") }
XCTAssertEqual(Util.getAllSubjects().count, 1)
}

func testImportV2() {
// TODO:
}
}
8 changes: 7 additions & 1 deletion CalqTests/LineChartTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import XCTest

final class LineChartTests: XCTestCase {

override class func setUp() {
Util.setContext(TestCoreDataStack.sharedContext)
}

func testLineChartData() {
// TODO:
MockDataProvider.getSubjectWithTests()
let data = lineChartData().first!.first!
XCTAssertEqual(data.value, 11/15)
}
}
2 changes: 2 additions & 0 deletions CalqTests/MockDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CoreData
@testable import Calq

class MockDataProvider {
@discardableResult
static func getSubjectWithTests() -> UserSubject {
let settings: AppSettings = Util.deleteSettings()
let sub = UserSubject(context: Util.getContext())
Expand All @@ -32,6 +33,7 @@ class MockDataProvider {
return sub
}

@discardableResult
static func getSubjectWithoutTests() -> UserSubject {
let settings: AppSettings = Util.deleteSettings()
let sub = UserSubject(context: Util.getContext())
Expand Down
25 changes: 25 additions & 0 deletions CalqTests/exampleData_v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

{"formatVersion": 2, "colorfulCharts": true, "highlightedType": 1, "hasFiveExams": false, "gradeTypes": [], "exam3Spanisch": 11,
"usersubjects": [
{"name": "Spanisch", "lk": true, "color": "#79c685", "inactiveYears": "",
"subjecttests": [
{"name": "Bioolympiadee", "year": 3, "grade":14, "date": "1632820440.0", "type": 0} ,
{"name": "Klausur Genetik #2", "year": 3, "grade":13, "date": "1638978893.0", "type": 1} ,
{"name": "Klausur Ökologie", "year": 2, "grade":8, "date": "1625562840.0", "type": 1} ,
{"name": "Protokoll Experiment", "year": 1, "grade":11, "date": "1602062040.0", "type": 0} ,
{"name": "Exkursionsbericht", "year": 2, "grade":14, "date": "1626192991.0", "type": 0} ,
{"name": "Test Stoffwechselprozesse", "year": 2, "grade":14, "date": "1616494440.0", "type": 0} ,
{"name": "Test Zellbestandteile", "year": 1, "grade":14, "date": "1604398440.0", "type": 0} ,
{"name": "Klausur Enzyme", "year": 1, "grade":11, "date": "1606385640.0", "type": 1} ,
{"name": "Test Genetik", "year": 3, "grade":14, "date": "1639414125.0", "type": 0} ,
{"name": "Experiment Blattfarbstoffe", "year": 2, "grade":14, "date": "1615284840.0", "type": 0} ,
{"name": "2. Klausur", "year": 1, "grade":10, "date": "1613470440.0", "type": 1} ,
{"name": "Test Neurologie", "year": 4, "grade":14, "date": "1644418358.0", "type": 0} ,
{"name": "Wertediskussion PID", "year": 3, "grade":14, "date": "1641830542.0", "type": 0} ,
{"name": "Klausur Genetik", "year": 3, "grade":12, "date": "1635790651.0", "type": 1} ,
{"name": "Test Stammbaumanalyse", "year": 3, "grade":14, "date": "1637602982.0", "type": 0} ,
{"name": "Vorabitur", "year": 4, "grade":11, "date": "1646661978.0", "type": 1}
]
}
]
}
Loading