Skip to content

Commit 94537e6

Browse files
committed
implemented validating test tag after updating the tags
1 parent b4f0791 commit 94537e6

File tree

1 file changed

+110
-71
lines changed

1 file changed

+110
-71
lines changed

GoInfoGame/GoInfoGameTests/UserFlowTests.swift

Lines changed: 110 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,61 @@ import CoreLocation
1515

1616
/**
1717
Used to test the flow of information
18-
This fetches the information and sends things down
18+
This fetches the information and sends things down
1919
*/
2020
final class UserFlowTests: XCTestCase {
21-
22-
// let opManager = OverpassRequestManager()
21+
22+
// let opManager = OverpassRequestManager()
2323

2424
let dbInstance = DatabaseConnector.shared
2525

2626
override func setUpWithError() throws {
2727
// Put setup code here. This method is called before the invocation of each test method in the class.
28-
// seedData()
28+
// seedData()
2929
}
3030

3131
func seedData() {
32-
// let expec = expectation(description: "Fetches the elements from Overpass Manager and stores in Database")
33-
// let kirklandBBox = BBox(minLat: 47.70312160869372, maxLat: 47.718964653825054, minLon: -122.20866792353317, maxLon: -122.18570621653987)
34-
// opManager.fetchElements(fromBBox: kirklandBBox) { fetchedElements in
35-
// // Get the count of nodes and ways
36-
// let allValues = fetchedElements.values
37-
//
38-
// let nodes = allValues.filter({$0 is OPNode}).filter({!$0.tags.isEmpty})
39-
// let ways = allValues.filter({$0 is OPWay}).filter({!$0.tags.isEmpty})
40-
// let allElements = allValues.filter({!$0.tags.isEmpty})
41-
// self.dbInstance.saveElements(allElements) // Save all where there are tags
42-
// expec.fulfill()
43-
// }
44-
//
45-
// waitForExpectations(timeout: 10)
32+
// let expec = expectation(description: "Fetches the elements from Overpass Manager and stores in Database")
33+
// let kirklandBBox = BBox(minLat: 47.70312160869372, maxLat: 47.718964653825054, minLon: -122.20866792353317, maxLon: -122.18570621653987)
34+
// opManager.fetchElements(fromBBox: kirklandBBox) { fetchedElements in
35+
// // Get the count of nodes and ways
36+
// let allValues = fetchedElements.values
37+
//
38+
// let nodes = allValues.filter({$0 is OPNode}).filter({!$0.tags.isEmpty})
39+
// let ways = allValues.filter({$0 is OPWay}).filter({!$0.tags.isEmpty})
40+
// let allElements = allValues.filter({!$0.tags.isEmpty})
41+
// self.dbInstance.saveElements(allElements) // Save all where there are tags
42+
// expec.fulfill()
43+
// }
44+
//
45+
// waitForExpectations(timeout: 10)
4646
}
4747

4848
func testDataInserts() throws {
49-
// let nodesFromStorage = dbInstance.getNodes()
50-
// let waysFromStorage = dbInstance.getWays()
51-
// XCTAssert(nodesFromStorage.count > 0)
52-
// // Get the Nodes from the above
53-
// let nodeElements = nodesFromStorage.map({$0.asNode()})
54-
// let wayElements = waysFromStorage.map({$0.asWay()})
55-
// let testQuest = TestQuest()
56-
// var applicableElements: [Element] = []
57-
// for singleNode in nodeElements {
58-
//// testQuest.isApplicable(element: singleNode)
59-
// let isApplicable = testQuest.isApplicable(element: singleNode)
60-
// if (isApplicable){
61-
// applicableElements.append(singleNode)
62-
// print(singleNode.tags)
63-
// }
64-
// }
65-
// for singleWay in wayElements {
66-
// let isApplicable = testQuest.isApplicable(element: singleWay)
67-
// if (isApplicable){
68-
// applicableElements.append(singleWay)
69-
// print(singleWay.tags)
70-
// }
71-
// }
72-
// print(applicableElements.count)
49+
// let nodesFromStorage = dbInstance.getNodes()
50+
// let waysFromStorage = dbInstance.getWays()
51+
// XCTAssert(nodesFromStorage.count > 0)
52+
// // Get the Nodes from the above
53+
// let nodeElements = nodesFromStorage.map({$0.asNode()})
54+
// let wayElements = waysFromStorage.map({$0.asWay()})
55+
// let testQuest = TestQuest()
56+
// var applicableElements: [Element] = []
57+
// for singleNode in nodeElements {
58+
//// testQuest.isApplicable(element: singleNode)
59+
// let isApplicable = testQuest.isApplicable(element: singleNode)
60+
// if (isApplicable){
61+
// applicableElements.append(singleNode)
62+
// print(singleNode.tags)
63+
// }
64+
// }
65+
// for singleWay in wayElements {
66+
// let isApplicable = testQuest.isApplicable(element: singleWay)
67+
// if (isApplicable){
68+
// applicableElements.append(singleWay)
69+
// print(singleWay.tags)
70+
// }
71+
// }
72+
// print(applicableElements.count)
7373
}
7474

7575
func testPerformanceDBFetch() throws {
@@ -99,14 +99,14 @@ final class UserFlowTests: XCTestCase {
9999
for node in nodeElements {
100100
// Get the quests and try to iterate
101101
for quest in allQuests {
102-
// if quest.filter.isEmpty {continue} // Ignore quest
102+
// if quest.filter.isEmpty {continue} // Ignore quest
103103
// isApplicable API is not being used and is not available now.
104-
// if quest.isApplicable(element: node){
105-
// // Create a duplicate of the quest
106-
// nodeQuests.append(quest)
107-
// print(quest)
108-
// break
109-
// }
104+
// if quest.isApplicable(element: node){
105+
// // Create a duplicate of the quest
106+
// nodeQuests.append(quest)
107+
// print(quest)
108+
// break
109+
// }
110110
}
111111
}
112112
print(nodeQuests.count)
@@ -116,20 +116,20 @@ final class UserFlowTests: XCTestCase {
116116
// print(nodeQuests.count)
117117
for way in wayElements{
118118
for quest in allQuests {
119-
// if quest.filter.isEmpty {continue} // Ignore quest
120-
// isApplicable API is not being used and is not available now.
121-
// if quest.isApplicable(element: way){
122-
// // Create a duplicate of the quest
123-
// wayQuests.append(quest)
124-
// print(quest)
125-
// break
126-
// }
119+
// if quest.filter.isEmpty {continue} // Ignore quest
120+
// isApplicable API is not being used and is not available now.
121+
// if quest.isApplicable(element: way){
122+
// // Create a duplicate of the quest
123+
// wayQuests.append(quest)
124+
// print(quest)
125+
// break
126+
// }
127127
}
128128
}
129129
print(wayQuests.count)
130130
}
131131
}
132-
132+
133133
override func tearDownWithError() throws {
134134
// Put teardown code here. This method is called after the invocation of each test method in the class.
135135
}
@@ -146,7 +146,7 @@ final class UserFlowTests: XCTestCase {
146146
let addedTags = ["lit":"yes"]
147147
let changedNode = dbInstance.addNodeTags(id: String(nodeId), tags: addedTags)
148148
// Create a changeset
149-
let newChangeset = dbInstance.createChangeset(id: String(nodeId), type: .node, tags: addedTags, isUndo: false )
149+
let newChangeset = dbInstance.createChangeset(id: String(nodeId), type: .node, tags: addedTags)
150150
// Need to figure out the id of the changeset
151151
XCTAssertEqual(newChangeset?.elementType, .node)
152152
XCTAssertEqual(newChangeset?.elementId, String(nodeId))
@@ -180,7 +180,7 @@ final class UserFlowTests: XCTestCase {
180180
switch result {
181181
case .success(let changesetId):
182182
DispatchQueue.main.async {
183-
// your code here
183+
// your code here
184184
self.dbInstance.assignChangesetId(obj: changeset.id, changesetId: changesetId)
185185
}
186186

@@ -198,22 +198,22 @@ final class UserFlowTests: XCTestCase {
198198
}
199199
waitForExpectations(timeout: 10)
200200
}
201-
201+
202202
func testExample() throws {
203203
// This is an example of a functional test case.
204204
// Use XCTAssert and related functions to verify your tests produce the correct results.
205205
// Any test you write for XCTest can be annotated as throws and async.
206206
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
207207
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
208208
}
209-
209+
210210
func testPerformanceExample() throws {
211211
// This is an example of a performance test case.
212212
self.measure {
213213
// Put the code you want to measure the time of here.
214214
}
215215
}
216-
216+
217217

218218
@MainActor
219219
func testQuestUndoFlow() {
@@ -240,11 +240,12 @@ final class UserFlowTests: XCTestCase {
240240
let workspaceID: Int = 380 // 380 workspace id is for Medina City Test under Test Project Group 1
241241
let nodeID: Int = 301834
242242

243+
DatabaseConnector.shared.clearDB()
244+
243245
// 1. login
244246
let loginViewModel = PosmLoginViewModel()
245247
loginViewModel.username = "[email protected]"
246248
loginViewModel.password = "Test@1234"
247-
DatabaseConnector.shared.clearDB()
248249
loginViewModel.$isLoginSuccess
249250
.dropFirst()
250251
.receive(on: DispatchQueue.main)
@@ -290,11 +291,17 @@ final class UserFlowTests: XCTestCase {
290291
let newTestingTags = [testingTagKey: testingTagValue]
291292
if let lognFormQuest = node.displayUnit.parent as? LongElementQuest {
292293
lognFormQuest.updateTags(id: node.id, tags: newTestingTags, type: .node)
293-
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
294-
if let element = DatabaseConnector.shared.getNode(id: nodeID, version: .original) {
295-
XCTAssert(element.tags[testingTagKey] == testingTagValue, "Testing tag is not updated")
296-
}
297-
expectation.fulfill()
294+
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { [weak self] in
295+
self?.getNodeTags(id: nodeID, workspaceId: workspaceID, completion: { result in
296+
switch result {
297+
case .failure(let error):
298+
XCTFail("\(error)")
299+
case .success(let tags):
300+
XCTAssert(tags[testingTagKey] == testingTagValue, "Testing tag is not present")
301+
XCTAssertEqual(tags["ext:gig_complete"], "yes","Gig complete tag not preset")
302+
}
303+
expectation.fulfill()
304+
})
298305
}
299306
} else {
300307
XCTAssert(false, "long quest not found")
@@ -316,9 +323,41 @@ final class UserFlowTests: XCTestCase {
316323
}
317324
.store(in: &cancellables)
318325
loginViewModel.performLogin()
319-
320326

321-
wait(for: [expectation], timeout: 25.0)
327+
328+
wait(for: [expectation], timeout: 50.0)
329+
330+
}
331+
332+
func getNodeTags(id: Int, workspaceId: Int, completion: @escaping (Result<[String: String], Error>) -> Void) {
333+
var request = URLRequest(url: URL(string: "https://osm.workspaces-stage.sidewalks.washington.edu/api/0.6/node/\(id).json")!,timeoutInterval: Double.infinity)
334+
request.addValue("\(workspaceId)", forHTTPHeaderField: "X-Workspace")
335+
336+
request.httpMethod = "GET"
337+
338+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
339+
guard let data = data else {
340+
print(String(describing: error))
341+
completion(.failure(error ?? NSError(domain: "failed", code: 101, userInfo: nil)))
342+
return
343+
}
344+
do {
345+
if let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
346+
print(jsonObject)
347+
if let element = (jsonObject["elements"] as? [[String: Any]])?.first,
348+
let tags = element["tags"] as? [String : String] {
349+
completion(.success(tags))
350+
} else {
351+
completion(.failure(NSError(domain: "tags not found", code: 101, userInfo: nil)))
352+
}
353+
}
354+
} catch {
355+
print("JSON parsing error: \(error)")
356+
completion(.failure(error))
357+
}
358+
}
359+
360+
task.resume()
322361

323362
}
324363
}

0 commit comments

Comments
 (0)