This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nodes-vapor/patch-model
Patch model
- Loading branch information
Showing
5 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,55 @@ class SanitizedTests: XCTestCase { | |
XCTAssertNil(result["name"]) | ||
XCTAssertNil(result["email"]) | ||
} | ||
|
||
func testPatchBasic() { | ||
let model = try! TestModel(node: [ | ||
"id": 15, | ||
"name": "Rylo Ken", | ||
"email": "[email protected]" | ||
]) | ||
|
||
let request = buildRequest(body: [ | ||
"id": 11, // this should be sanitized | ||
"email": "[email protected]" | ||
]) | ||
|
||
expectNoThrow() { | ||
let model = try request.patchModel(model) | ||
XCTAssertEqual(model.id?.int, 15) | ||
XCTAssertEqual(model.name, "Rylo Ken") | ||
XCTAssertEqual(model.email, "[email protected]") | ||
} | ||
} | ||
|
||
func testPatchFailed() { | ||
let model = try! TestModel(node: [ | ||
"id": 15, | ||
"name": "Rylo Ken", | ||
"email": "[email protected]" | ||
]) | ||
|
||
let request = buildInvalidRequest() | ||
|
||
expect(toThrow: Abort.badRequest) { | ||
let _: TestModel = try request.patchModel(model) | ||
} | ||
} | ||
|
||
func testPatchById() { | ||
Database.default = Database(TestDriver()) | ||
let request = buildRequest(body: [ | ||
"id": 11, // this should be sanitized | ||
"email": "[email protected]" | ||
]) | ||
|
||
expectNoThrow() { | ||
let model: TestModel = try request.patchModel(id: 1) | ||
XCTAssertEqual(model.id?.int, 1, "Id shouldn't have changed") | ||
XCTAssertEqual(model.name, "Jimmy", "Name shouldn't have changed") | ||
XCTAssertEqual(model.email, "[email protected]", "email should've changed") | ||
} | ||
} | ||
} | ||
|
||
extension SanitizedTests { | ||
|
@@ -127,7 +176,11 @@ struct TestModel: Model, Sanitizable { | |
} | ||
|
||
func makeNode(context: Context) throws -> Node { | ||
return .null | ||
return try Node(node: [ | ||
"id": id, | ||
"name": name, | ||
"email": "email" | ||
]) | ||
} | ||
|
||
static func prepare(_ database: Database) throws {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Vapor | ||
import Fluent | ||
|
||
class TestDriver: Driver { | ||
var idKey: String = "id" | ||
|
||
func query<T : Entity>(_ query: Query<T>) throws -> Node { | ||
switch query.action { | ||
case .fetch: | ||
return Node.array([Node.object([ | ||
"id": 1, | ||
"name": "Jimmy", | ||
"email": "[email protected]" | ||
])]) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func schema(_ schema: Schema) throws {} | ||
|
||
@discardableResult | ||
public func raw(_ query: String, _ values: [Node] = []) throws -> Node { | ||
return .null | ||
} | ||
} |