Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Momo Ozawa committed May 8, 2024
1 parent 3fa19e6 commit c45251e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
3 changes: 3 additions & 0 deletions WordPress/Classes/RemotePost+Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Foundation
@objc
extension RemotePost {
var foreignID: String? {
guard let metadata else {
return nil
}
let metadataItems = metadata.compactMap { value -> RemotePostMetadataItem? in
guard let dictionary = value as? [String: Any] else {
assertionFailure("Unexpected value: \(value)")
Expand Down
7 changes: 5 additions & 2 deletions WordPress/Classes/Services/PostRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ final class PostRepository {
private let coreDataStack: CoreDataStackSwift
private let remoteFactory: PostServiceRemoteFactory
private let isSyncPublishingEnabled: Bool
private let foreignIdGenerator: (() -> String)

init(coreDataStack: CoreDataStackSwift = ContextManager.shared,
remoteFactory: PostServiceRemoteFactory = PostServiceRemoteFactory(),
isSyncPublishingEnabled: Bool = FeatureFlag.syncPublishing.enabled) {
isSyncPublishingEnabled: Bool = FeatureFlag.syncPublishing.enabled,
foreignIdGenerator: (() -> String)? = nil) {
self.coreDataStack = coreDataStack
self.remoteFactory = remoteFactory
self.isSyncPublishingEnabled = isSyncPublishingEnabled
self.foreignIdGenerator = foreignIdGenerator ?? { UUID().uuidString }
}

/// Sync a specific post from the API
Expand Down Expand Up @@ -225,7 +228,7 @@ final class PostRepository {
@MainActor
private func _create(_ post: AbstractPost, changes: RemotePostUpdateParameters?) async throws -> RemotePost {
let service = try getRemoteService(for: post.blog)
post.foreignID = UUID().uuidString
post.foreignID = foreignIdGenerator()
var parameters = RemotePostCreateParameters(post: post)
if let changes {
parameters.apply(changes)
Expand Down
55 changes: 52 additions & 3 deletions WordPress/WordPressTest/PostRepositorySaveTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PostRepositorySaveTests: CoreDataTestCase {
.build()
try mainContext.save()

repository = PostRepository(coreDataStack: contextManager)
repository = PostRepository(coreDataStack: contextManager, foreignIdGenerator: { "foreign-id" })
}

override func tearDown() {
Expand Down Expand Up @@ -47,6 +47,13 @@ class PostRepositorySaveTests: CoreDataTestCase {
"author" : 29043,
"content" : "content-1",
"date" : "2024-03-07T23:00:40+0000",
"metadata" : [
{
"key" : "wp_jp_foreign_id",
"operation" : "update",
"value" : "foreign-id"
}
],
"status" : "draft",
"title" : "Hello",
"type" : "post"
Expand Down Expand Up @@ -108,6 +115,13 @@ class PostRepositorySaveTests: CoreDataTestCase {
"excerpt" : "excerpt-a",
"featured_image" : 92,
"format" : "format-a",
"metadata" : [
{
"key" : "wp_jp_foreign_id",
"operation" : "update",
"value" : "foreign-id"
}
],
"password" : "1234",
"slug" : "slug-a",
"status" : "draft",
Expand Down Expand Up @@ -153,6 +167,13 @@ class PostRepositorySaveTests: CoreDataTestCase {
"author" : 29043,
"content" : "content-1",
"date" : "2024-03-07T23:00:40+0000",
"metadata" : [
{
"key" : "wp_jp_foreign_id",
"operation" : "update",
"value" : "foreign-id"
}
],
"status" : "publish",
"title" : "Hello",
"type" : "post"
Expand Down Expand Up @@ -193,6 +214,13 @@ class PostRepositorySaveTests: CoreDataTestCase {
"author" : 29043,
"content" : "content-1",
"date" : "2024-03-07T23:00:40+0000",
"metadata" : [
{
"key" : "wp_jp_foreign_id",
"operation" : "update",
"value" : "foreign-id"
}
],
"status" : "future",
"title" : "Hello",
"type" : "post"
Expand Down Expand Up @@ -235,6 +263,13 @@ class PostRepositorySaveTests: CoreDataTestCase {
"author" : 29043,
"content" : "content-1",
"date" : "2024-03-07T23:00:40+0000",
"metadata" : [
{
"key" : "wp_jp_foreign_id",
"operation" : "update",
"value" : "foreign-id"
}
],
"status" : "publish",
"title" : "Hello",
"type" : "post"
Expand Down Expand Up @@ -1203,6 +1238,13 @@ class PostRepositorySaveTests: CoreDataTestCase {
{
"author" : 29043,
"content" : "content-a",
"metadata" : [
{
"key" : "wp_jp_foreign_id",
"operation" : "update",
"value" : "foreign-id"
}
],
"status" : "draft",
"title" : "title-a",
"type" : "post"
Expand Down Expand Up @@ -1285,6 +1327,13 @@ class PostRepositorySaveTests: CoreDataTestCase {
{
"author" : 29043,
"content" : "content-a",
"metadata" : [
{
"key" : "wp_jp_foreign_id",
"operation" : "update",
"value" : "foreign-id"
}
],
"status" : "draft",
"title" : "title-a",
"type" : "post"
Expand Down Expand Up @@ -1453,8 +1502,8 @@ private func stub(condition: @escaping (URLRequest) -> Bool, _ response: @escapi
}
}

private func assertRequestBody(_ request: URLRequest, expected: String, file: StaticString = #file, line: UInt = #line) throws {
let parameters = try request.getBodyParameters()
private func assertRequestBody(_ request: URLRequest, expected: String, file: StaticString = #file, line: UInt = #line, filterMetadata: Bool = true) throws {
var parameters = try request.getBodyParameters()
let data = try JSONSerialization.data(withJSONObject: parameters, options: [.sortedKeys, .prettyPrinted])
let string = try XCTUnwrap(String(data: data, encoding: .utf8))
XCTAssertEqual(string, expected, "Unexpected request parameters", file: file, line: line)
Expand Down

0 comments on commit c45251e

Please sign in to comment.