Skip to content

Commit 89ce35a

Browse files
committed
Added test-cases for JSON resources marshaling:
1. repo_hooks.go
1 parent 5dfec5a commit 89ce35a

1 file changed

Lines changed: 258 additions & 0 deletions

File tree

‎github/repos_hooks_test.go‎

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,261 @@ func TestRepositoriesService_TestHook_invalidOwner(t *testing.T) {
296296
_, err := client.Repositories.TestHook(ctx, "%", "%", 1)
297297
testURLParseError(t, err)
298298
}
299+
func TestBranchWebHookPayload_Marshal(t *testing.T) {
300+
testJSONMarshal(t, &WebHookPayload{}, "{}")
301+
302+
v := &WebHookPayload{
303+
Action: String("action"),
304+
After: String("after"),
305+
Before: String("before"),
306+
Commits: []*WebHookCommit{
307+
{
308+
Added: []string{"1", "2", "3"},
309+
Author: &WebHookAuthor{
310+
Email: String("[email protected]"),
311+
Name: String("abc"),
312+
Username: String("abc_12"),
313+
},
314+
Committer: &WebHookAuthor{
315+
Email: String("[email protected]"),
316+
Name: String("abc"),
317+
Username: String("abc_12"),
318+
},
319+
ID: String("1"),
320+
Message: String("WebHookCommit"),
321+
Modified: []string{"abc", "efg", "erd"},
322+
Removed: []string{"cmd", "rti", "duv"},
323+
},
324+
},
325+
Compare: String("compare"),
326+
Created: Bool(true),
327+
Forced: Bool(false),
328+
HeadCommit: &WebHookCommit{
329+
Added: []string{"1", "2", "3"},
330+
Author: &WebHookAuthor{
331+
Email: String("[email protected]"),
332+
Name: String("abc"),
333+
Username: String("abc_12"),
334+
},
335+
Committer: &WebHookAuthor{
336+
Email: String("[email protected]"),
337+
Name: String("abc"),
338+
Username: String("abc_12"),
339+
},
340+
ID: String("1"),
341+
Message: String("WebHookCommit"),
342+
Modified: []string{"abc", "efg", "erd"},
343+
Removed: []string{"cmd", "rti", "duv"},
344+
},
345+
Installation: &Installation{
346+
ID: Int64(12),
347+
},
348+
Organization: &Organization{
349+
ID: Int64(22),
350+
},
351+
Pusher: &User{
352+
Login: String("[email protected]"),
353+
ID: Int64(112),
354+
},
355+
Repo: &Repository{
356+
ID: Int64(321),
357+
NodeID: String("node_321"),
358+
},
359+
Sender: &User{
360+
Login: String("[email protected]"),
361+
ID: Int64(202),
362+
},
363+
}
364+
365+
want := `{
366+
"action": "action",
367+
"after": "after",
368+
"before": "before",
369+
"commits": [
370+
{
371+
"added": ["1", "2", "3"],
372+
"author":{
373+
"email": "[email protected]",
374+
"name": "abc",
375+
"username": "abc_12"
376+
},
377+
"committer": {
378+
"email": "[email protected]",
379+
"name": "abc",
380+
"username": "abc_12"
381+
},
382+
"id": "1",
383+
"message": "WebHookCommit",
384+
"modified": ["abc", "efg", "erd"],
385+
"removed": ["cmd", "rti", "duv"]
386+
}
387+
],
388+
"compare": "compare",
389+
"created": true,
390+
"forced": false,
391+
"head_commit": {
392+
"added": ["1", "2", "3"],
393+
"author":{
394+
"email": "[email protected]",
395+
"name": "abc",
396+
"username": "abc_12"
397+
},
398+
"committer": {
399+
"email": "[email protected]",
400+
"name": "abc",
401+
"username": "abc_12"
402+
},
403+
"id": "1",
404+
"message": "WebHookCommit",
405+
"modified": ["abc", "efg", "erd"],
406+
"removed": ["cmd", "rti", "duv"]
407+
},
408+
"installation": {
409+
"id": 12
410+
},
411+
"organization": {
412+
"id" : 22
413+
},
414+
"pusher":{
415+
"login": "[email protected]",
416+
"id": 112
417+
},
418+
"repository":{
419+
"id": 321,
420+
"node_id": "node_321"
421+
},
422+
"sender":{
423+
"login": "[email protected]",
424+
"id": 202
425+
}
426+
}`
427+
428+
testJSONMarshal(t, v, want)
429+
}
430+
431+
func TestBranchWebHookAuthor_Marshal(t *testing.T) {
432+
testJSONMarshal(t, &WebHookAuthor{}, "{}")
433+
434+
v := &WebHookAuthor{
435+
Email: String("[email protected]"),
436+
Name: String("abc"),
437+
Username: String("abc_12"),
438+
}
439+
440+
want := `{
441+
"email": "[email protected]",
442+
"name": "abc",
443+
"username": "abc_12"
444+
}`
445+
446+
testJSONMarshal(t, v, want)
447+
}
448+
449+
func TestBranchWebHookCommit_Marshal(t *testing.T) {
450+
testJSONMarshal(t, &WebHookPayload{}, "{}")
451+
452+
v := &WebHookCommit{
453+
Added: []string{"1", "2", "3"},
454+
Author: &WebHookAuthor{
455+
Email: String("[email protected]"),
456+
Name: String("abc"),
457+
Username: String("abc_12"),
458+
},
459+
Committer: &WebHookAuthor{
460+
Email: String("[email protected]"),
461+
Name: String("abc"),
462+
Username: String("abc_12"),
463+
},
464+
ID: String("1"),
465+
Message: String("WebHookCommit"),
466+
Modified: []string{"abc", "efg", "erd"},
467+
Removed: []string{"cmd", "rti", "duv"},
468+
}
469+
470+
want := `{
471+
"added": ["1", "2", "3"],
472+
"author":{
473+
"email": "[email protected]",
474+
"name": "abc",
475+
"username": "abc_12"
476+
},
477+
"committer": {
478+
"email": "[email protected]",
479+
"name": "abc",
480+
"username": "abc_12"
481+
},
482+
"id": "1",
483+
"message": "WebHookCommit",
484+
"modified": ["abc", "efg", "erd"],
485+
"removed": ["cmd", "rti", "duv"]
486+
}`
487+
488+
testJSONMarshal(t, v, want)
489+
}
490+
491+
func TestBranchcreateHookRequest_Marshal(t *testing.T) {
492+
testJSONMarshal(t, &WebHookAuthor{}, "{}")
493+
494+
v := &createHookRequest{
495+
Name: "abc",
496+
Events: []string{"1", "2", "3"},
497+
Active: Bool(true),
498+
Config: map[string]interface{}{
499+
"thing": "@123",
500+
},
501+
}
502+
503+
want := `{
504+
"name": "abc",
505+
"active": true,
506+
"events": ["1","2","3"],
507+
"config":{
508+
"thing": "@123"
509+
}
510+
}`
511+
512+
testJSONMarshal(t, v, want)
513+
}
514+
func TestBranchHook_Marshal(t *testing.T) {
515+
testJSONMarshal(t, &Hook{}, "{}")
516+
517+
v := &Hook{
518+
CreatedAt: &referenceTime,
519+
UpdatedAt: &referenceTime,
520+
URL: String("url"),
521+
ID: Int64(1),
522+
Type: String("type"),
523+
Name: String("name"),
524+
TestURL: String("testurl"),
525+
PingURL: String("pingurl"),
526+
LastResponse: map[string]interface{}{
527+
"item": "item",
528+
},
529+
Config: map[string]interface{}{
530+
"thing": "@123",
531+
},
532+
Events: []string{"1", "2", "3"},
533+
Active: Bool(true),
534+
}
535+
536+
want := `{
537+
"created_at": ` + referenceTimeStr + `,
538+
"updated_at": ` + referenceTimeStr + `,
539+
"url": "url",
540+
"id": 1,
541+
"type": "type",
542+
"name": "name",
543+
"test_url": "testurl",
544+
"ping_url": "pingurl",
545+
"last_response":{
546+
"item": "item"
547+
},
548+
"config":{
549+
"thing": "@123"
550+
},
551+
"events": ["1","2","3"],
552+
"active": true
553+
}`
554+
555+
testJSONMarshal(t, v, want)
556+
}

0 commit comments

Comments
 (0)