@@ -11,6 +11,7 @@ import (
1111 "fmt"
1212 "net/http"
1313 "testing"
14+ "time"
1415)
1516
1617func TestProjectsService_ListOrganizationProjects (t * testing.T ) {
@@ -1076,8 +1077,27 @@ func TestProjectsService_DeleteUserProjectItem_error(t *testing.T) {
10761077func TestProjectV2Item_UnmarshalJSON_Issue (t * testing.T ) {
10771078 t .Parallel ()
10781079
1079- // Test unmarshaling an issue
1080- jsonData := `{
1080+ item := ProjectV2Item {
1081+ ID : Ptr (int64 (123 )),
1082+ NodeID : Ptr ("PVTI_test" ),
1083+ ContentType : Ptr (ProjectV2ItemContentTypeIssue ),
1084+ Content : & ProjectV2ItemContent {
1085+ Issue : & Issue {
1086+ ID : Ptr (int64 (456 )),
1087+ Number : Ptr (10 ),
1088+ Title : Ptr ("Test Issue" ),
1089+ State : Ptr ("open" ),
1090+ Body : Ptr ("Issue body" ),
1091+ Repository : & Repository {
1092+ ID : Ptr (int64 (789 )),
1093+ Name : Ptr ("test-repo" ),
1094+ },
1095+ },
1096+ },
1097+ CreatedAt : & Timestamp {time .Date (2023 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )},
1098+ }
1099+
1100+ want := `{
10811101 "id": 123,
10821102 "node_id": "PVTI_test",
10831103 "content_type": "Issue",
@@ -1095,53 +1115,38 @@ func TestProjectV2Item_UnmarshalJSON_Issue(t *testing.T) {
10951115 "created_at": "2023-01-01T00:00:00Z"
10961116 }`
10971117
1098- var item ProjectV2Item
1099- if err := json .Unmarshal ([]byte (jsonData ), & item ); err != nil {
1100- t .Fatalf ("json.Unmarshal failed: %v" , err )
1101- }
1102-
1103- // Verify basic fields
1104- if item .GetID () != 123 {
1105- t .Errorf ("ID = %v, want 123" , item .GetID ())
1106- }
1107- if item .GetNodeID () != "PVTI_test" {
1108- t .Errorf ("NodeID = %v, want PVTI_test" , item .GetNodeID ())
1109- }
1110- if item .ContentType == nil || * item .ContentType != ProjectV2ItemContentTypeIssue {
1111- t .Errorf ("ContentType = %v, want Issue" , item .ContentType )
1112- }
1113-
1114- // Verify content is unmarshaled as Issue
1115- if item .Content == nil {
1116- t .Fatal ("Content is nil" )
1117- }
1118- if item .GetContent ().GetIssue () == nil {
1119- t .Fatal ("Content.Issue is nil" )
1120- }
1121- if item .GetContent ().GetIssue ().GetNumber () != 10 {
1122- t .Errorf ("Issue.Number = %v, want 10" , item .GetContent ().GetIssue ().GetNumber ())
1123- }
1124- if item .GetContent ().GetIssue ().GetTitle () != "Test Issue" {
1125- t .Errorf ("Issue.Title = %v, want Test Issue" , item .GetContent ().GetIssue ().GetTitle ())
1126- }
1127- if item .GetContent ().GetIssue ().GetState () != "open" {
1128- t .Errorf ("Issue.State = %v, want open" , item .GetContent ().GetIssue ().GetState ())
1129- }
1130-
1131- // Verify other content types are nil
1132- if item .GetContent ().GetPullRequest () != nil {
1133- t .Error ("Content.PullRequest should be nil for Issue content" )
1134- }
1135- if item .GetContent ().GetDraftIssue () != nil {
1136- t .Error ("Content.DraftIssue should be nil for Issue content" )
1137- }
1118+ testJSONUnmarshalOnly (t , item , want )
11381119}
11391120
11401121func TestProjectV2Item_UnmarshalJSON_PullRequest (t * testing.T ) {
11411122 t .Parallel ()
11421123
1143- // Test unmarshaling a pull request
1144- jsonData := `{
1124+ item := ProjectV2Item {
1125+ ID : Ptr (int64 (124 )),
1126+ NodeID : Ptr ("PVTI_pr" ),
1127+ ContentType : Ptr (ProjectV2ItemContentTypePullRequest ),
1128+ Content : & ProjectV2ItemContent {
1129+ PullRequest : & PullRequest {
1130+ ID : Ptr (int64 (457 )),
1131+ Number : Ptr (20 ),
1132+ Title : Ptr ("Test PR" ),
1133+ State : Ptr ("closed" ),
1134+ Merged : Ptr (true ),
1135+ MergeCommitSHA : Ptr ("abc123" ),
1136+ Head : & PullRequestBranch {
1137+ Ref : Ptr ("feature-branch" ),
1138+ SHA : Ptr ("def456" ),
1139+ },
1140+ Base : & PullRequestBranch {
1141+ Ref : Ptr ("main" ),
1142+ SHA : Ptr ("ghi789" ),
1143+ },
1144+ },
1145+ },
1146+ CreatedAt : & Timestamp {time .Date (2023 , 1 , 2 , 0 , 0 , 0 , 0 , time .UTC )},
1147+ }
1148+
1149+ want := `{
11451150 "id": 124,
11461151 "node_id": "PVTI_pr",
11471152 "content_type": "PullRequest",
@@ -1164,53 +1169,27 @@ func TestProjectV2Item_UnmarshalJSON_PullRequest(t *testing.T) {
11641169 "created_at": "2023-01-02T00:00:00Z"
11651170 }`
11661171
1167- var item ProjectV2Item
1168- if err := json .Unmarshal ([]byte (jsonData ), & item ); err != nil {
1169- t .Fatalf ("json.Unmarshal failed: %v" , err )
1170- }
1171-
1172- // Verify basic fields
1173- if item .GetID () != 124 {
1174- t .Errorf ("ID = %v, want 124" , item .GetID ())
1175- }
1176- if item .ContentType == nil || * item .ContentType != ProjectV2ItemContentTypePullRequest {
1177- t .Errorf ("ContentType = %v, want PullRequest" , item .ContentType )
1178- }
1179-
1180- // Verify content is unmarshaled as PullRequest
1181- if item .Content == nil {
1182- t .Fatal ("Content is nil" )
1183- }
1184- if item .GetContent ().GetPullRequest () == nil {
1185- t .Fatal ("Content.PullRequest is nil" )
1186- }
1187- if item .GetContent ().GetPullRequest ().GetNumber () != 20 {
1188- t .Errorf ("PullRequest.Number = %v, want 20" , item .GetContent ().GetPullRequest ().GetNumber ())
1189- }
1190- if item .GetContent ().GetPullRequest ().GetTitle () != "Test PR" {
1191- t .Errorf ("PullRequest.Title = %v, want Test PR" , item .GetContent ().GetPullRequest ().GetTitle ())
1192- }
1193- if ! item .GetContent ().GetPullRequest ().GetMerged () {
1194- t .Errorf ("PullRequest.Merged = %t, want true" , item .GetContent ().GetPullRequest ().GetMerged ())
1195- }
1196- if item .GetContent ().GetPullRequest ().GetMergeCommitSHA () != "abc123" {
1197- t .Errorf ("PullRequest.MergeCommitSHA = %v, want abc123" , item .GetContent ().GetPullRequest ().GetMergeCommitSHA ())
1198- }
1199-
1200- // Verify other content types are nil
1201- if item .GetContent ().GetIssue () != nil {
1202- t .Error ("Content.Issue should be nil for PullRequest content" )
1203- }
1204- if item .GetContent ().GetDraftIssue () != nil {
1205- t .Error ("Content.DraftIssue should be nil for PullRequest content" )
1206- }
1172+ testJSONUnmarshalOnly (t , item , want )
12071173}
12081174
12091175func TestProjectV2Item_UnmarshalJSON_DraftIssue (t * testing.T ) {
12101176 t .Parallel ()
12111177
1212- // Test unmarshaling a draft issue
1213- jsonData := `{
1178+ item := ProjectV2Item {
1179+ ID : Ptr (int64 (125 )),
1180+ NodeID : Ptr ("PVTI_draft" ),
1181+ ContentType : Ptr (ProjectV2ItemContentTypeDraftIssue ),
1182+ Content : & ProjectV2ItemContent {
1183+ DraftIssue : & ProjectV2DraftIssue {
1184+ ID : Ptr (int64 (458 )),
1185+ Title : Ptr ("Draft Issue Title" ),
1186+ Body : Ptr ("Draft issue body content" ),
1187+ },
1188+ },
1189+ CreatedAt : & Timestamp {time .Date (2023 , 1 , 3 , 0 , 0 , 0 , 0 , time .UTC )},
1190+ }
1191+
1192+ want := `{
12141193 "id": 125,
12151194 "node_id": "PVTI_draft",
12161195 "content_type": "DraftIssue",
@@ -1223,72 +1202,33 @@ func TestProjectV2Item_UnmarshalJSON_DraftIssue(t *testing.T) {
12231202 "created_at": "2023-01-03T00:00:00Z"
12241203 }`
12251204
1226- var item ProjectV2Item
1227- if err := json .Unmarshal ([]byte (jsonData ), & item ); err != nil {
1228- t .Fatalf ("json.Unmarshal failed: %v" , err )
1229- }
1230-
1231- // Verify basic fields
1232- if item .GetID () != 125 {
1233- t .Errorf ("ID = %v, want 125" , item .GetID ())
1234- }
1235- if item .ContentType == nil || * item .ContentType != ProjectV2ItemContentTypeDraftIssue {
1236- t .Errorf ("ContentType = %v, want DraftIssue" , item .ContentType )
1237- }
1238-
1239- // Verify content is unmarshaled as DraftIssue
1240- if item .Content == nil {
1241- t .Fatal ("Content is nil" )
1242- }
1243- if item .GetContent ().GetDraftIssue () == nil {
1244- t .Fatal ("Content.DraftIssue is nil" )
1245- }
1246- if item .GetContent ().GetDraftIssue ().GetID () != 458 {
1247- t .Errorf ("DraftIssue.ID = %v, want 458" , item .GetContent ().GetDraftIssue ().GetID ())
1248- }
1249- if item .GetContent ().GetDraftIssue ().GetTitle () != "Draft Issue Title" {
1250- t .Errorf ("DraftIssue.Title = %v, want Draft Issue Title" , item .GetContent ().GetDraftIssue ().GetTitle ())
1251- }
1252- if item .GetContent ().GetDraftIssue ().GetBody () != "Draft issue body content" {
1253- t .Errorf ("DraftIssue.Body = %v, want Draft issue body content" , item .GetContent ().GetDraftIssue ().GetBody ())
1254- }
1255-
1256- // Verify other content types are nil
1257- if item .GetContent ().GetIssue () != nil {
1258- t .Error ("Content.Issue should be nil for DraftIssue content" )
1259- }
1260- if item .GetContent ().GetPullRequest () != nil {
1261- t .Error ("Content.PullRequest should be nil for DraftIssue content" )
1262- }
1205+ testJSONUnmarshalOnly (t , item , want )
12631206}
12641207
12651208func TestProjectV2Item_UnmarshalJSON_NullContent (t * testing.T ) {
12661209 t .Parallel ()
12671210
1268- // Test with null content
1269- jsonData := `{
1211+ item := ProjectV2Item {
1212+ ID : Ptr (int64 (126 )),
1213+ NodeID : Ptr ("PVTI_null" ),
1214+ ContentType : Ptr (ProjectV2ItemContentTypeIssue ),
1215+ Content : nil , // Content is null
1216+ }
1217+
1218+ want := `{
12701219 "id": 126,
12711220 "node_id": "PVTI_null",
12721221 "content_type": "Issue",
12731222 "content": null
12741223 }`
12751224
1276- var item ProjectV2Item
1277- if err := json .Unmarshal ([]byte (jsonData ), & item ); err != nil {
1278- t .Fatalf ("json.Unmarshal failed: %v" , err )
1279- }
1280-
1281- // Content should be nil
1282- if item .Content != nil {
1283- t .Error ("Content should be nil when content is null in JSON" )
1284- }
1225+ testJSONUnmarshalOnly (t , item , want )
12851226}
12861227
12871228func TestProjectV2Item_UnmarshalJSON_MissingContentType (t * testing.T ) {
12881229 t .Parallel ()
12891230
1290- // Test without content_type field
1291- jsonData := `{
1231+ want := `{
12921232 "id": 127,
12931233 "node_id": "PVTI_no_type",
12941234 "content": {
@@ -1297,37 +1237,27 @@ func TestProjectV2Item_UnmarshalJSON_MissingContentType(t *testing.T) {
12971237 }
12981238 }`
12991239
1300- var item ProjectV2Item
1301- if err := json .Unmarshal ([]byte (jsonData ), & item ); err != nil {
1302- t .Fatalf ("json.Unmarshal failed: %v" , err )
1240+ item := ProjectV2Item {
1241+ ID : Ptr (int64 (127 )),
1242+ NodeID : Ptr ("PVTI_no_type" ),
1243+ Content : nil ,
13031244 }
13041245
1305- // Should handle missing ContentType gracefully - content should be nil
1306- // since we can't determine the type
1307- if item .Content != nil {
1308- t .Error ("Content should be nil when ContentType is missing" )
1309- }
1246+ testJSONUnmarshalOnly (t , item , want )
13101247}
13111248
13121249func TestProjectV2Item_UnmarshalJSON_EmptyJSON (t * testing.T ) {
13131250 t .Parallel ()
13141251
1315- // Test with null JSON
1316- var item ProjectV2Item
1317- if err := json .Unmarshal ([]byte ("null" ), & item ); err != nil {
1318- t .Fatalf ("json.Unmarshal failed with null: %v" , err )
1319- }
1252+ item := ProjectV2Item {}
1253+ want := "null"
13201254
1321- // Verify item is in zero state after unmarshaling null
1322- if item .Content != nil {
1323- t .Error ("Content should be nil after unmarshaling null" )
1324- }
1255+ testJSONUnmarshalOnly (t , item , want )
13251256}
13261257
13271258func TestProjectV2Item_UnmarshalJSON_InvalidJSON (t * testing.T ) {
13281259 t .Parallel ()
13291260
1330- // Test with invalid JSON
13311261 var item ProjectV2Item
13321262 if err := json .Unmarshal ([]byte ("~~~" ), & item ); err == nil {
13331263 t .Error ("expected error for invalid JSON, got nil" )
0 commit comments