Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 4dd5a77

Browse files
committed
Add unit tests for decoding Stats Insight entities
1 parent 1ecf67f commit 4dd5a77

13 files changed

+1152
-4
lines changed

WordPressKit.xcodeproj/project.pbxproj

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
01438D392B6A361B0097D60A /* stats-summary.json in Resources */ = {isa = PBXBuildFile; fileRef = 01438D372B6A35FB0097D60A /* stats-summary.json */; };
1414
01438D3B2B6A36BF0097D60A /* StatsTotalsSummaryData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01438D3A2B6A36BF0097D60A /* StatsTotalsSummaryData.swift */; };
1515
0152100C28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0152100B28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift */; };
16+
01D251942BB1834A006349C0 /* StatsInsightDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01D251932BB1834A006349C0 /* StatsInsightDecodingTests.swift */; };
17+
01D251972BB18F2A006349C0 /* stats-insight-last-post.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D251962BB18F2A006349C0 /* stats-insight-last-post.json */; };
18+
01D251992BB1961D006349C0 /* stats-insight-followers.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D251982BB1961D006349C0 /* stats-insight-followers.json */; };
19+
01D2519B2BB19723006349C0 /* stats-insight.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D2519A2BB19723006349C0 /* stats-insight.json */; };
20+
01D2519D2BB19845006349C0 /* stats.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D2519C2BB19845006349C0 /* stats.json */; };
21+
01D2519F2BB19957006349C0 /* stats-insight-publicize.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D2519E2BB19957006349C0 /* stats-insight-publicize.json */; };
22+
01D251A12BB19997006349C0 /* stats-insight-summary.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D251A02BB19997006349C0 /* stats-insight-summary.json */; };
23+
01D251A32BB19A22006349C0 /* stats-insight-comments.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D251A22BB19A22006349C0 /* stats-insight-comments.json */; };
24+
01D251A52BB19ABE006349C0 /* stats-insight-tag-and-category.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D251A42BB19ABE006349C0 /* stats-insight-tag-and-category.json */; };
25+
01D251A72BB19AE1006349C0 /* stats-insight-streak.json in Resources */ = {isa = PBXBuildFile; fileRef = 01D251A62BB19AE1006349C0 /* stats-insight-streak.json */; };
1626
0847B92C2A4442730044D32F /* IPLocationRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0847B92B2A4442730044D32F /* IPLocationRemote.swift */; };
1727
08C7493E2A45EA11000DA0E2 /* IPLocationRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08C7493D2A45EA11000DA0E2 /* IPLocationRemoteTests.swift */; };
1828
0C1C08412B9CD79900E52F8C /* PostServiceRemoteExtended.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1C08402B9CD79900E52F8C /* PostServiceRemoteExtended.swift */; };
@@ -739,6 +749,16 @@
739749
01438D372B6A35FB0097D60A /* stats-summary.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-summary.json"; sourceTree = "<group>"; };
740750
01438D3A2B6A36BF0097D60A /* StatsTotalsSummaryData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatsTotalsSummaryData.swift; sourceTree = "<group>"; };
741751
0152100B28EDA9E400DD6783 /* StatsAnnualAndMostPopularTimeInsightDecodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsAnnualAndMostPopularTimeInsightDecodingTests.swift; sourceTree = "<group>"; };
752+
01D251932BB1834A006349C0 /* StatsInsightDecodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsInsightDecodingTests.swift; sourceTree = "<group>"; };
753+
01D251962BB18F2A006349C0 /* stats-insight-last-post.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight-last-post.json"; sourceTree = "<group>"; };
754+
01D251982BB1961D006349C0 /* stats-insight-followers.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight-followers.json"; sourceTree = "<group>"; };
755+
01D2519A2BB19723006349C0 /* stats-insight.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight.json"; sourceTree = "<group>"; };
756+
01D2519C2BB19845006349C0 /* stats.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = stats.json; sourceTree = "<group>"; };
757+
01D2519E2BB19957006349C0 /* stats-insight-publicize.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight-publicize.json"; sourceTree = "<group>"; };
758+
01D251A02BB19997006349C0 /* stats-insight-summary.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight-summary.json"; sourceTree = "<group>"; };
759+
01D251A22BB19A22006349C0 /* stats-insight-comments.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight-comments.json"; sourceTree = "<group>"; };
760+
01D251A42BB19ABE006349C0 /* stats-insight-tag-and-category.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight-tag-and-category.json"; sourceTree = "<group>"; };
761+
01D251A62BB19AE1006349C0 /* stats-insight-streak.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "stats-insight-streak.json"; sourceTree = "<group>"; };
742762
0847B92B2A4442730044D32F /* IPLocationRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPLocationRemote.swift; sourceTree = "<group>"; };
743763
08C7493D2A45EA11000DA0E2 /* IPLocationRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPLocationRemoteTests.swift; sourceTree = "<group>"; };
744764
0C1C08402B9CD79900E52F8C /* PostServiceRemoteExtended.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostServiceRemoteExtended.swift; sourceTree = "<group>"; };
@@ -1485,6 +1505,22 @@
14851505
/* End PBXFrameworksBuildPhase section */
14861506

14871507
/* Begin PBXGroup section */
1508+
01D251952BB18351006349C0 /* MockData */ = {
1509+
isa = PBXGroup;
1510+
children = (
1511+
01D251962BB18F2A006349C0 /* stats-insight-last-post.json */,
1512+
01D251982BB1961D006349C0 /* stats-insight-followers.json */,
1513+
01D2519A2BB19723006349C0 /* stats-insight.json */,
1514+
01D2519C2BB19845006349C0 /* stats.json */,
1515+
01D2519E2BB19957006349C0 /* stats-insight-publicize.json */,
1516+
01D251A02BB19997006349C0 /* stats-insight-summary.json */,
1517+
01D251A22BB19A22006349C0 /* stats-insight-comments.json */,
1518+
01D251A42BB19ABE006349C0 /* stats-insight-tag-and-category.json */,
1519+
01D251A62BB19AE1006349C0 /* stats-insight-streak.json */,
1520+
);
1521+
path = MockData;
1522+
sourceTree = "<group>";
1523+
};
14881524
08C7493C2A45E9E3000DA0E2 /* Privacy */ = {
14891525
isa = PBXGroup;
14901526
children = (
@@ -2709,7 +2745,9 @@
27092745
F3FF8A1D279C86FE00E5C90F /* Insights */ = {
27102746
isa = PBXGroup;
27112747
children = (
2748+
01D251952BB18351006349C0 /* MockData */,
27122749
F3FF8A1E279C871A00E5C90F /* StatsDotComFollowersInsightTests.swift */,
2750+
01D251932BB1834A006349C0 /* StatsInsightDecodingTests.swift */,
27132751
);
27142752
path = Insights;
27152753
sourceTree = "<group>";
@@ -2957,6 +2995,7 @@
29572995
3297E28D25647E0300287D21 /* jetpack-scan-enqueue-success.json in Resources */,
29582996
FE50966A2A30A4F900DDD071 /* jetpack-social-403.json in Resources */,
29592997
7403A2F71EF06FEB00DED7DC /* me-settings-change-display-name-bad-json-failure.json in Resources */,
2998+
01D251992BB1961D006349C0 /* stats-insight-followers.json in Resources */,
29602999
74B335E41F06F6B30053A184 /* WordPressComRestApiMultipleErrors.json in Resources */,
29613000
740B23E61F17FB4200067A2A /* xmlrpc-metaweblog-newpost-invalid-posttype-failure.xml in Resources */,
29623001
73D592FC21E550D300E4CF84 /* site-verticals-single.json in Resources */,
@@ -3011,6 +3050,7 @@
30113050
9AB6D64E218731AB0008F274 /* post-revisions-mapping-success.json in Resources */,
30123051
404057C7221B36070060250C /* stats-search-term-result.json in Resources */,
30133052
7403A2F41EF06FEB00DED7DC /* me-settings-auth-failure.json in Resources */,
3053+
01D251972BB18F2A006349C0 /* stats-insight-last-post.json in Resources */,
30143054
FA87FE0B24EB4419003FBEE3 /* reader-post-comments-subscribe-success.json in Resources */,
30153055
930999581F16598A00F006A1 /* get-single-theme-v1.1.json in Resources */,
30163056
74B335E61F06F6E90053A184 /* WordPressComRestApiMedia.json in Resources */,
@@ -3023,6 +3063,7 @@
30233063
FFE247B420C891E6002DF3A2 /* WordPressComOAuthSuccess.json in Resources */,
30243064
404057D4221C5FC40060250C /* stats-countries-data.json in Resources */,
30253065
74D67F1F1F15C3240010C5ED /* people-send-invitation-success.json in Resources */,
3066+
01D251A52BB19ABE006349C0 /* stats-insight-tag-and-category.json in Resources */,
30263067
FFE247B020C891E6002DF3A2 /* WordPressComAuthenticateWithIDToken2FANeededSuccess.json in Resources */,
30273068
8B2F4BEB24ABCA700056C08A /* reader-cards-success.json in Resources */,
30283069
436D563E2118E34D00CEAA33 /* supported-states-success.json in Resources */,
@@ -3071,6 +3112,7 @@
30713112
E1E89C681FD6B2E9006E7A33 /* plugin-directory-jetpack.json in Resources */,
30723113
74D67F201F15C3240010C5ED /* people-validate-invitation-failure.json in Resources */,
30733114
FEEFD8B4280DD60200A3E261 /* blogging-prompts-success.json in Resources */,
3115+
01D2519F2BB19957006349C0 /* stats-insight-publicize.json in Resources */,
30743116
74D67F161F15C2D70010C5ED /* site-users-update-role-bad-json-failure.json in Resources */,
30753117
93BD27661EE73442002BB00B /* me-success.json in Resources */,
30763118
404057DC221C9FD80060250C /* stats-referrer-data.json in Resources */,
@@ -3096,9 +3138,11 @@
30963138
984E34F422EF9465005C3F92 /* stats-file-downloads.json in Resources */,
30973139
93F50A3C1F226C0100B5BEBA /* WordPressComRestApiFailThrottled.json in Resources */,
30983140
740B23ED1F17FB7E00067A2A /* xmlrpc-bad-username-password-error.xml in Resources */,
3141+
01D2519B2BB19723006349C0 /* stats-insight.json in Resources */,
30993142
FA4261722570CC91003A01E2 /* activity-groups-bad-json-failure.json in Resources */,
31003143
3297E28C25647E0300287D21 /* jetpack-scan-enqueue-failure.json in Resources */,
31013144
9A881754223C01E400A3AB20 /* jetpack-service-error-activation-install.json in Resources */,
3145+
01D251A12BB19997006349C0 /* stats-insight-summary.json in Resources */,
31023146
93BD275B1EE73442002BB00B /* is-available-username-failure.json in Resources */,
31033147
FFE247B320C891E6002DF3A2 /* WordPressComOAuthNeeds2FAFail.json in Resources */,
31043148
E13EE1491F332B8500C15787 /* site-plugins-success.json in Resources */,
@@ -3122,6 +3166,7 @@
31223166
9A2D0B2F225E1245009E585F /* jetpack-service-check-site-success.json in Resources */,
31233167
829BA4301FACF187003ADEEA /* activity-rewind-status-restore-failure.json in Resources */,
31243168
8B16CE962525045F007BE5A9 /* reader-posts-success.json in Resources */,
3169+
01D251A32BB19A22006349C0 /* stats-insight-comments.json in Resources */,
31253170
C92EFF7325E7444400E0308D /* common-starter-site-designs-empty-designs.json in Resources */,
31263171
74D67F391F15C3740010C5ED /* site-viewers-delete-bad-json.json in Resources */,
31273172
BA8EA71524A0610200D5CC9F /* plugin-service-remote-featured-plugins-invalid.json in Resources */,
@@ -3213,6 +3258,7 @@
32133258
FFE247B220C891E6002DF3A2 /* WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json in Resources */,
32143259
74D67F371F15C3740010C5ED /* site-users-delete-success.json in Resources */,
32153260
74D67F341F15C3740010C5ED /* site-users-delete-bad-json-failure.json in Resources */,
3261+
01D251A72BB19AE1006349C0 /* stats-insight-streak.json in Resources */,
32163262
7403A2FF1EF06FEB00DED7DC /* me-settings-revert-email-success.json in Resources */,
32173263
FEB7A88F271873BD00A8CF85 /* reader-post-comments-update-notification-success.json in Resources */,
32183264
74B335E21F06F6730053A184 /* WordPressComRestApiFailRequestInvalidToken.json in Resources */,
@@ -3246,6 +3292,7 @@
32463292
FA79F1872591730D00D235A9 /* backup-get-backup-status-complete-success.json in Resources */,
32473293
74D67F381F15C3740010C5ED /* site-viewers-delete-auth-failure.json in Resources */,
32483294
826016FA1F9FAF6300533B6C /* activity-log-success-1.json in Resources */,
3295+
01D2519D2BB19845006349C0 /* stats.json in Resources */,
32493296
9A881752223C01E400A3AB20 /* jetpack-service-error-login-failure.json in Resources */,
32503297
FEE4EF5F2730334D003CDA3C /* comments-v2-view-context-success.json in Resources */,
32513298
740B23E41F17FB4200067A2A /* xmlrpc-metaweblog-editpost-success.xml in Resources */,
@@ -3615,6 +3662,7 @@
36153662
9AB6D64A218727D60008F274 /* PostServiceRemoteRESTRevisionsTest.swift in Sources */,
36163663
01438D382B6A35FB0097D60A /* stats-summary.json in Sources */,
36173664
7430C9BD1F192C0F0051B8E6 /* ReaderPostServiceRemoteTests.m in Sources */,
3665+
01D251942BB1834A006349C0 /* StatsInsightDecodingTests.swift in Sources */,
36183666
1DC837C229B9F04F009DCD4B /* RemoteVideoPressVideoTests.swift in Sources */,
36193667
FAD1345125909DEA00A8FEB1 /* JetpackBackupServiceRemoteTests.swift in Sources */,
36203668
8B2F4BE924ABC9DC0056C08A /* ReaderPostServiceRemote+CardsTests.swift in Sources */,

WordPressKit/Insights/StatsAnnualAndMostPopularTimeInsight.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public struct StatsAnnualAndMostPopularTimeInsight: Codable {
4343
}
4444
}
4545

46-
4746
extension StatsAnnualAndMostPopularTimeInsight: StatsInsightData {
4847
public static var pathComponent: String {
4948
return "stats/insights"

WordPressKit/Insights/StatsLastPostInsight.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension StatsLastPostInsight: StatsInsightData {
4242
}
4343

4444
public init?(jsonDictionary: [String: AnyObject]) {
45-
fatalError("This shouldn't be ever called, instead init?(jsonDictionary:_ views:_) be called instead.")
45+
self.init(jsonDictionary: jsonDictionary, views: 0)
4646
}
4747

4848
// MARK: -
@@ -86,11 +86,11 @@ extension StatsLastPostInsight {
8686
throw DecodingError.dataCorruptedError(forKey: .publishedDate, in: container, debugDescription: "Date string does not match format expected by formatter.")
8787
}
8888
publishedDate = date
89-
likesCount = try container.decodeIfPresent(Int.self, forKey: .likesCount) ?? 0
89+
likesCount = (try? container.decodeIfPresent(Int.self, forKey: .likesCount)) ?? 0
9090
postID = try container.decode(Int.self, forKey: .postID)
9191
featuredImageURL = try? container.decodeIfPresent(URL.self, forKey: .featuredImageURL)
9292

9393
let discussionContainer = try container.nestedContainer(keyedBy: DiscussionKeys.self, forKey: .discussion)
94-
commentsCount = try discussionContainer.decode(Int.self, forKey: .commentsCount)
94+
commentsCount = (try? discussionContainer.decodeIfPresent(Int.self, forKey: .commentsCount)) ?? 0
9595
}
9696
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"date": "2024-03-26",
3+
"authors": [
4+
{
5+
"name": "novasupernova",
6+
"link": "?user_id=3217654",
7+
"gravatar": "https://1.gravatar.com/avatar/12345abcdef12345abcdef12345abcdef?s=64&d=https%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D64",
8+
"comments": "400",
9+
"follow_data": {
10+
"params": {
11+
"stat-source": "stats_comments",
12+
"follow-text": "Follow",
13+
"following-text": "Following",
14+
"following-hover-text": "Unfollow",
15+
"blog_domain": "supernovanova.wordpress.com",
16+
"blog_url": "http://supernovanova.wordpress.com",
17+
"blog_id": 101010101,
18+
"site_id": 101010101,
19+
"blog_title": "Supernova's Space",
20+
"is_following": true
21+
},
22+
"type": "follow"
23+
}
24+
},
25+
{
26+
"name": "Bob the Builder",
27+
"link": "?user_id=98765432",
28+
"gravatar": "https://2.gravatar.com/avatar/abcdef123456abcdef123456abcdef1234?s=64&d=https%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D64",
29+
"comments": "150",
30+
"follow_data": {
31+
"params": {
32+
"stat-source": "stats_comments",
33+
"follow-text": "Follow",
34+
"following-text": "Following",
35+
"following-hover-text": "Unfollow",
36+
"blog_domain": "bobthebuilder.wordpress.com",
37+
"blog_url": "http://bobthebuilder.wordpress.com",
38+
"blog_id": 202020202,
39+
"site_id": 202020202,
40+
"blog_title": "Building Blocks",
41+
"is_following": false
42+
},
43+
"type": "follow"
44+
}
45+
}
46+
],
47+
"posts": [
48+
{
49+
"name": "Exploring the Depths of Space",
50+
"link": "http://novasupernova.wordpress.com/2024/03/26/exploring-the-depths-of-space/",
51+
"id": "98765",
52+
"comments": "200"
53+
},
54+
{
55+
"name": "Building the Future",
56+
"link": "http://bobthebuilder.wordpress.com/2024/03/26/building-the-future/",
57+
"id": "54321",
58+
"comments": "120"
59+
}
60+
],
61+
"monthly_comments": 320,
62+
"total_comments": 150000,
63+
"most_active_day": "2024-03-25 18:00:00",
64+
"most_active_time": "17:00",
65+
"most_commented_post": {
66+
"name": "Exploring the Depths of Space",
67+
"link": "http://novasupernova.wordpress.com/2024/03/26/exploring-the-depths-of-space/",
68+
"id": "98765",
69+
"comments": "200"
70+
}
71+
}

0 commit comments

Comments
 (0)