-
Notifications
You must be signed in to change notification settings - Fork 395
/
test_repos_repo.py
1968 lines (1570 loc) · 65.3 KB
/
test_repos_repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Unit tests for Repositories."""
import datetime
import unittest.mock
from base64 import b64encode
import pytest
from . import helper
from github3 import GitHubError
from github3.exceptions import GitHubException
from github3.models import GitHubCore
from github3.projects import Project
from github3.repos.comment import RepoComment
from github3.repos.commit import RepoCommit
from github3.repos.comparison import Comparison
from github3.repos.contents import Contents
from github3.repos.hook import Hook
from github3.repos.repo import Repository
from github3.repos.repo import ShortRepository
comment_url_for = helper.create_url_helper(
"https://api.github.com/repos/octocat/Hello-World/comments/1"
)
commit_url_for = helper.create_url_helper(
"https://api.github.com/repos/octocat/Hello-World/"
"commits/6dcb09b5b57875f334f61aebed695e2e4193db5e"
)
compare_url_for = helper.create_url_helper(
"https://api.github.com/repos/octocat/Hello-World/compare/master...topic"
)
contents_url_for = helper.create_url_helper(
"https://api.github.com/repos/github3py/github3.py/contents/README.rst"
"?ref=master"
)
hook_url_for = helper.create_url_helper(
"https://api.github.com/repos/octocat/Hello-World/hooks/1"
)
url_for = helper.create_url_helper(
"https://api.github.com/repos/octocat/Hello-World"
)
get_repo_example_data = helper.create_example_data_helper("repo_example")
get_repo_2_12_example_data = helper.create_example_data_helper(
"repo_2_12_example"
)
get_comment_example_data = helper.create_example_data_helper(
"comment_example"
)
get_commit_example_data = helper.create_example_data_helper("commit_example")
get_compare_example_data = helper.create_example_data_helper(
"compare_example"
)
get_content_example_data = helper.create_example_data_helper(
"content_example"
)
get_hook_example_data = helper.create_example_data_helper("hook_example")
create_file_contents_example_data = helper.create_example_data_helper(
"create_file_contents_example"
)
comment_example_data = get_comment_example_data()
commit_example_data = get_commit_example_data()
compare_example_data = get_compare_example_data()
content_example_data = get_content_example_data()
create_file_contents_example_data = create_file_contents_example_data()
hook_example_data = get_hook_example_data()
repo_example_data = get_repo_example_data()
repo_2_12_example_data = get_repo_2_12_example_data()
class TestRepository(helper.UnitHelper):
"""Unit test for regular Repository methods."""
described_class = Repository
example_data = repo_example_data
def test_add_collaborator(self):
"""Verify the request to add a collaborator to a repository."""
self.instance.add_collaborator("sigmavirus24")
self.session.put.assert_called_once_with(
url_for("collaborators/sigmavirus24")
)
def test_add_null_collaborator(self):
"""Verify no request is made when adding `None` as a collaborator."""
self.instance.add_collaborator(None)
assert self.session.put.called is False
def test_asset(self):
"""Test retrieving an asset uses the right headers.
The Releases section of the API is still in Beta and uses custom
headers
"""
self.instance.asset(1)
self.session.get.assert_called_once_with(url_for("releases/assets/1"))
def test_asset_requires_a_positive_id(self):
"""Test that a positive asset id is required."""
self.instance.asset(0)
assert self.session.get.called is False
def test_create_file(self):
"""Verify the request for creating a file on a repository."""
data = {
"path": "hello.txt",
"message": "my commit message",
"content": b"bXkgbmV3IGZpbGUgY29udGVudHM=",
"committer": {
"name": "Scott Chacon",
"email": "schacon@gmail.com",
},
}
self.instance.create_file(**data)
b64_encoded_content = b64encode(data["content"]).decode("utf-8")
data.update({"content": b64_encoded_content})
del data["path"]
self.put_called_with(url_for("contents/hello.txt"), data=data)
def test_create_file_required_content(self):
"""Verify the request for creating a file on a repository."""
data = {
"path": "hello.txt",
"message": "my commit message",
"content": 123,
"committer": {
"name": "Scott Chacon",
"email": "schacon@gmail.com",
},
}
with pytest.raises(ValueError):
self.instance.create_file(**data)
def test_create_fork(self):
"""Verify the request to fork a repository."""
self.instance.create_fork()
self.post_called_with(url_for("forks"))
def test_create_fork_to_organization(self):
"""Verify the request to fork a repository to an organization."""
self.instance.create_fork("mattchung")
self.post_called_with(
url_for("forks"), data={"organization": "mattchung"}
)
def test_create_hook(self):
"""Verify the request to create a hook."""
data = {
"name": "web",
"config": {
"url": "http://example.com/webhook",
"content_type": "json",
},
}
self.instance.create_hook(**data)
self.post_called_with(
url_for("hooks"),
data={
"name": "web",
"config": {
"url": "http://example.com/webhook",
"content_type": "json",
},
"events": ["push"],
"active": True,
},
)
def test_create_hook_requires_valid_name(self):
"""Test that we check the validity of a hook."""
self.instance.create_hook(name="", config="config")
assert self.session.post.called is False
def test_create_hook_requires_valid_config(self):
"""Test that we check the validity of a hook."""
self.instance.create_hook(name="name", config={})
assert self.session.post.called is False
def test_create_hook_requires_valid_name_and_config(self):
"""Test that we check the validity of a hook."""
self.instance.create_hook(name="name", config="")
assert self.session.post.called is False
def test_create_issue(self):
"""Verify the request to create an issue."""
data = {
"title": "Unit Issue",
"body": "Fake body",
"assignee": "sigmavirus24",
"milestone": 1,
"labels": ["bug", "enhancement"],
}
self.instance.create_issue(**data)
self.post_called_with(url_for("issues"), data=data)
def test_create_issue_multiple_assignees(self):
"""Verify the request to create an issue with multiple assignees."""
data = {
"title": "Unit Issue",
"body": "Fake body",
"assignees": ["itsmemattchung", "sigmavirus24"],
"milestone": 1,
"labels": ["bug", "enhancement"],
}
self.instance.create_issue(**data)
self.post_called_with(url_for("issues"), data=data)
def test_create_issue_require_valid_issue(self):
"""Test that we check the validity of an issue."""
self.instance.create_issue(title=None)
assert self.session.post.called is False
def test_create_key(self):
"""Verify the request to create a key."""
data = {
"title": "octocat@octomac",
"key": "ssh-rsa AAA",
"read_only": False,
}
self.instance.create_key(**data)
self.post_called_with(url_for("keys"), data=data)
def test_create_key_readonly(self):
"""Verify the request to create a key with readonly true."""
data = {
"title": "octocat@octomac",
"key": "ssh-rsa AAA",
"read_only": True,
}
self.instance.create_key(**data)
self.post_called_with(url_for("keys"), data=data)
def test_create_key_requires_a_valid_title(self):
"""Test that we check the validity of a key."""
self.instance.create_key(title=None, key="ssh-rsa ...")
assert self.session.post.called is False
def test_create_key_requires_a_valid_key(self):
"""Test that we check the validity of a key."""
self.instance.create_key(title="foo", key="")
assert self.session.post.called is False
def test_create_key_requires_a_valid_title_and_key(self):
"""Test that we check the validity of a key."""
self.instance.create_key(title="foo", key="")
assert self.session.post.called is False
def test_create_label(self):
"""Verify the request for creating a label."""
data = {"name": "foo", "color": "fafafa"}
self.instance.create_label(**data)
self.post_called_with(
url_for("labels"),
data=data,
headers={
"Accept": "application/vnd.github.symmetra-preview+json"
},
)
def test_create_label_required_name(self):
"""Verify the request for creating a label."""
data = {"name": "", "color": "fafafa"}
self.instance.create_label(**data)
assert self.session.post.called is False
def test_create_label_required_color(self):
"""Verify the request for creating a label."""
data = {"name": "foo", "color": ""}
self.instance.create_label(**data)
assert self.session.post.called is False
def test_create_label_required_name_and_color(self):
"""Verify the request for creating a label."""
data = {"name": "", "color": ""}
self.instance.create_label(**data)
assert self.session.post.called is False
def test_create_milestone(self):
"""Verify the request for creating a milestone."""
data = {"title": "foo"}
self.instance.create_milestone(**data)
self.post_called_with(url_for("milestones"), data=data)
def test_create_milestone_accepted_state(self):
"""Verify the request for creating a milestone."""
data = {"title": "foo", "state": "in_progress"}
self.instance.create_milestone(**data)
self.post_called_with(url_for("milestones"), data={"title": "foo"})
def test_create_project(self):
"""Verify the request for creating a project."""
data = {"name": "test-project", "body": "project body"}
self.instance.create_project(**data)
self.post_called_with(
url_for("projects"), data=data, headers=Project.CUSTOM_HEADERS
)
def test_create_pull_private_required_data(self):
"""Verify the request for creating a pull request."""
with unittest.mock.patch.object(
GitHubCore, "_remove_none"
) as rm_none:
data = {}
self.instance._create_pull(data)
rm_none.assert_called_once_with({})
assert self.session.post.called is False
def test_create_pull_private(self):
"""Verify the request for creating a pull request."""
data = {"title": "foo", "base": "master", "head": "feature_branch"}
self.instance._create_pull(data)
self.post_called_with(url_for("pulls"), data=data)
def test_create_pull(self):
"""Verify the request for creating a pull request."""
data = {
"title": "foo",
"base": "master",
"head": "feature_branch",
"body": "body",
}
with unittest.mock.patch.object(Repository, "_create_pull") as pull:
self.instance.create_pull(**data)
pull.assert_called_once_with(data)
def test_create_pull_maintainer_can_modify(self):
"""Verify maintainer_can_modify option for creating a pull request."""
data = {
"title": "foo",
"base": "master",
"head": "feature_branch",
"body": "body",
"maintainer_can_modify": False,
}
with unittest.mock.patch.object(Repository, "_create_pull") as pull:
self.instance.create_pull(**data)
pull.assert_called_once_with(data)
def test_create_pull_from_issue(self):
"""Verify the request for creating a pull request from an issue."""
with unittest.mock.patch.object(Repository, "_create_pull") as pull:
data = {"issue": 1, "base": "master", "head": "feature_branch"}
self.instance.create_pull_from_issue(**data)
pull.assert_called_once_with(data)
def test_create_pull_from_issue_required_issue_number(self):
"""Verify the request for creating a pull request from an issue."""
with unittest.mock.patch.object(Repository, "_create_pull") as pull:
pull_request = self.instance.create_pull_from_issue(
issue=-1, base="master", head="feature_branch"
)
assert pull.called is False
assert pull_request is None
def test_create_ref(self):
"""Verify the request to create a reference."""
self.instance.create_ref("refs/heads/foo", "my-fake-sha")
self.post_called_with(
url_for("git/refs"),
data={"ref": "refs/heads/foo", "sha": "my-fake-sha"},
)
def test_create_branch_ref(self):
"""Verify the request to create a branch."""
self.instance.create_branch_ref("branch-name", "my-fake-sha")
self.post_called_with(
url_for("git/refs"),
data={"ref": "refs/heads/branch-name", "sha": "my-fake-sha"},
)
def test_create_ref_requires_a_reference_with_two_slashes(self):
"""Test that we check the validity of a reference."""
self.instance.create_ref("refs/heads", "my-fake-sha")
assert self.session.post.called is False
def test_create_ref_requires_a_reference_start_with_refs(self):
"""Test that we check the validity of a reference."""
self.instance.create_ref("my-silly-ref/foo/bar", "my-fake-sha")
assert self.session.post.called is False
def test_create_ref_requires_a_non_None_sha(self):
"""Test that we don't send an empty SHA."""
self.instance.create_ref("refs/heads/valid", None)
assert self.session.post.called is False
def test_create_ref_requires_a_truthy_sha(self):
"""Test that we don't send an empty SHA."""
self.instance.create_ref("refs/heads/valid", "")
assert self.session.post.called is False
def test_create_status(self):
"""Verify the request for creating a status on a commit."""
data = {
"state": "success",
"target_url": "foo",
"description": "bar",
"context": "default",
}
with unittest.mock.patch.object(
GitHubCore, "_remove_none"
) as rm_none:
self.instance.create_status(sha="fake-sha", **data)
rm_none.assert_called_once_with(data)
self.post_called_with(url_for("statuses/fake-sha"), data=data)
def test_create_status_required_sha(self):
"""Verify the request for creating a status on a commit."""
self.instance.create_status(sha="", state="success")
assert self.session.post.called is False
def test_create_status_required_state(self):
"""Verify the request for creating a status on a commit."""
self.instance.create_status(sha="fake-sha", state="")
assert self.session.post.called is False
def test_create_status_required_sha_and_state(self):
"""Verify the request for creating a status on a commit."""
self.instance.create_status(sha="", state="")
assert self.session.post.called is False
def test_create_tag_that_is_not_lightweight(self):
"""Verify we can create an annotated tag."""
self.instance.create_tag(
tag="tag-name",
message="message",
sha="my-sha",
obj_type="commit",
tagger={
"name": "Ian Cordasco",
"email": "example@example.com",
"date": "2015-11-01T12:16:00Z",
},
)
self.post_called_with(
url_for("git/tags"),
data={
"tag": "tag-name",
"message": "message",
"object": "my-sha",
"type": "commit",
"tagger": {
"name": "Ian Cordasco",
"email": "example@example.com",
"date": "2015-11-01T12:16:00Z",
},
},
)
def test_create_tree(self):
"""Verify the request to create a tree."""
self.instance.create_tree([{"foo": "bar"}])
self.post_called_with(
url_for("git/trees"), data={"tree": [{"foo": "bar"}]}
)
def test_create_tree_with_base_tree(self):
"""Verify the request to create a tree with a base tree."""
self.instance.create_tree([{"foo": "bar"}], base_tree="sha")
self.post_called_with(
url_for("git/trees"),
data={"tree": [{"foo": "bar"}], "base_tree": "sha"},
)
def test_create_tree_rejects_invalid_trees(self):
"""Verify no request is made if tree is not a list or is None."""
self.instance.create_tree({"foo": "bar"})
self.instance.create_tree(None)
assert self.session.post.called is False
def test_delete(self):
"""Verify the request for deleting a repository."""
self.instance.delete()
assert self.session.delete.called is True
def test_delete_key(self):
"""Verify the request for deleting a key on the repository."""
self.instance.delete_key(1)
self.session.delete.assert_called_once_with(url_for("keys/1"))
def test_delete_key_required_id(self):
"""Verify the request for deleting a key on the repository."""
assert self.instance.delete_key(-1) is False
self.session.delete.called is False
def test_delete_subscription(self):
"""Verify the request for deleting a subscription."""
self.instance.delete_subscription()
self.session.delete.assert_called_once_with(url_for("subscription"))
def test_directory_contents(self):
"""Verify the request made to retrieve a directory's contents."""
self.instance.directory_contents("path/to/directory")
self.session.get.assert_called_once_with(
url_for("contents/path/to/directory"), params={"ref": None}
)
def test_directory_contents_with_ref(self):
"""Verify the request made to retrieve a directory's contents."""
self.instance.directory_contents("path/to/directory", ref="some-sha")
self.session.get.assert_called_once_with(
url_for("contents/path/to/directory"), params={"ref": "some-sha"}
)
def test_deployment(self):
"""Verify the request made to retrieve a deployment."""
self.instance.deployment(10)
self.session.get.assert_called_once_with(url_for("deployments/10"))
def test_deployment_requires_positive_int(self):
"""Verify that a positive deployment id is required."""
self.instance.deployment(-10)
assert self.session.get.called is False
def test_edit(self):
"""Verify the request for editing a repository."""
data = {
"name": "hello-world",
"description": "repo description",
"homepage": "homepage_url",
"private": True,
"has_issues": True,
"has_wiki": True,
"has_downloads": True,
"default_branch": "develop",
"allow_rebase_merge": True,
"allow_squash_merge": True,
"allow_merge_commit": False,
"has_projects": False,
}
with unittest.mock.patch.object(
Repository, "_update_attributes"
) as up_attr:
assert self.instance.edit(**data) is True
assert up_attr.called is True
self.patch_called_with(url_for(), data=data)
def test_edit_required_name(self):
"""Verify the request for editing a repository."""
assert self.instance.edit(None) is False
assert self.session.patch.called is False
def test_file_contents(self):
"""Verify the request made to retrieve a dictionary's contents."""
self.instance.file_contents("path/to/file.txt", ref="some-sha")
self.session.get.assert_called_once_with(
url_for("contents/path/to/file.txt"), params={"ref": "some-sha"}
)
def test_git_commit_required_sha(self):
"""Verify the request for retrieving a git commit from a repository."""
self.instance.git_commit("")
assert self.session.get.called is False
def test_git_commit(self):
"""Verify the request for retrieving a git commit from a repository."""
self.instance.git_commit("fake-sha")
self.session.get.assert_called_once_with(
url_for("git/commits/fake-sha")
)
def test_hook(self):
"""Verify the request for retrieving a hook on a repository."""
self.instance.hook(1)
self.session.get.assert_called_once_with(url_for("hooks/1"))
def test_hook_required_hook(self):
"""Verify the request for retrieving a hook on a repository."""
self.instance.hook(-1)
assert self.session.get.called is False
def test_is_assignee(self):
"""
Verify the request for checking if a user can be assigned issues
on a repository.
"""
self.instance.is_assignee("octocat")
self.session.get.assert_called_once_with(url_for("assignees/octocat"))
def test_is_assignee_required_username(self):
"""
Verify the request for checking if a user can be assigned issues
on a repository.
"""
assert self.instance.is_assignee("") is False
assert self.session.get.called is False
def test_import_issue(self):
"""Verify the request for importing an issue into a repository."""
data = {
"title": "Foo",
"body": "Foobar body",
"created_at": "2014-03-16T17:15:42Z",
"assignee": "octocat",
"milestone": 1,
"closed": True,
"labels": ["easy", "bug"],
"comments": [
{"created_at": "2014-03-18T17:15:42Z", "body": "comment body"}
],
}
issue = {
"issue": {
"title": "Foo",
"body": "Foobar body",
"created_at": "2014-03-16T17:15:42Z",
"assignee": "octocat",
"milestone": 1,
"closed": True,
"labels": ["easy", "bug"],
},
"comments": [
{"created_at": "2014-03-18T17:15:42Z", "body": "comment body"}
],
}
with unittest.mock.patch.object(
GitHubCore, "_remove_none"
) as rm_none:
self.instance.import_issue(**data)
rm_none.assert_any_call(issue["issue"])
rm_none.assert_any_call(issue)
self.post_called_with(
url_for("import/issues"),
data=issue,
headers={
"Accept": "application/vnd.github.golden-comet-preview+json"
},
)
def test_imported_issue(self):
"""Verify the request for retrieving an imported issue."""
self.instance.imported_issue(1)
self.session.get.assert_called_once_with(
url_for("import/issues/1"),
headers={
"Accept": "application/vnd.github.golden-comet-preview+json"
},
)
def test_is_collaborator_required_username(self):
"""
Verify the request for checking if a user is a collaborator on a
repository.
"""
assert self.instance.is_collaborator("") is False
assert self.session.get.called is False
def test_is_collaborator(self):
"""
Verify the request for checking if a user is a collaborator on a
repository.
"""
self.instance.is_collaborator("octocat")
self.session.get.assert_called_once_with(
url_for("collaborators/octocat")
)
def test_issue(self):
"""Verify the request for retrieving an issue on a repository."""
self.instance.issue(1)
self.session.get.assert_called_once_with(url_for("issues/1"))
def test_issue_required_number(self):
"""Verify the request for retrieving an issue on a repository."""
self.instance.issue(-1)
assert self.session.get.called is False
def test_key(self):
"""Test the ability to fetch a deploy key."""
self.instance.key(10)
self.session.get.assert_called_once_with(url_for("keys/10"))
def test_key_requires_positive_id(self):
"""Test that a positive key id is required."""
self.instance.key(-10)
assert self.session.get.called is False
def test_label(self):
"""Verify the request for retrieving a label on a repository."""
self.instance.label("bug")
self.session.get.assert_called_once_with(
url_for("labels/bug"),
headers={
"Accept": "application/vnd.github.symmetra-preview+json"
},
)
def test_label_required_name(self):
"""Verify the request for retrieving a label on a repository."""
self.instance.label("")
assert self.session.get.called is False
def test_latest_pages_build(self):
"""Test retrieving the most recent pages build."""
self.instance.latest_pages_build()
self.session.get.assert_called_once_with(
url_for("pages/builds/latest")
)
def test_latest_release(self):
"""Test the request for retrieving the latest release"""
self.instance.latest_release()
self.session.get.assert_called_once_with(url_for("releases/latest"))
def test_milestone(self):
"""Test retrieving a specific milestone."""
self.instance.milestone(20)
self.session.get.assert_called_once_with(url_for("milestones/20"))
def test_mark_notifications(self):
"""
Verify the request for marking all notifications on a repository
as read.
"""
self.instance.mark_notifications("2012-10-09T23:39:01Z")
self.put_called_with(
url_for("notifications"),
data={"read": True, "last_read_at": "2012-10-09T23:39:01Z"},
)
def test_mark_notifications_required_last_read(self):
"""
Verify the request for marking all notifications on a repository
as read.
"""
self.instance.mark_notifications("")
self.put_called_with(url_for("notifications"), data={"read": True})
def test_merge(self):
"""Verify the request for performing a merge on a repository."""
self.instance.merge(
base="develop", head="feature", message="merging now"
)
self.post_called_with(
url_for("merges"),
data={
"base": "develop",
"head": "feature",
"commit_message": "merging now",
},
)
def test_merge_no_message(self):
"""Verify the request for performing a merge on a repository."""
data = {"base": "develop", "head": "feature"}
self.instance.merge(**data)
self.post_called_with(url_for("merges"), data=data)
def test_milestone_requires_positive_id(self):
"""Test that a positive milestone id is required."""
self.instance.milestone(-1)
assert self.session.get.called is False
def test_pages(self):
"""Test retrieving information about a repository's page."""
self.instance.pages()
self.session.get.assert_called_once_with(url_for("pages"))
def test_parent(self):
"""Verify that parent of repository can be retrieved."""
parent = self.instance.parent
assert isinstance(parent, ShortRepository)
def test_permission(self):
"""Verify permissions of a repository can be retrieved."""
permissions = {"admin": False, "push": False, "pull": True}
assert self.instance.permissions == permissions
def test_project(self):
"""Show that a user can access a single repository project."""
self.instance.project(400435)
self.session.get.assert_called_once_with(
"https://api.github.com/projects/400435",
headers=Project.CUSTOM_HEADERS,
)
def test_pull_request(self):
"""Verify the request for retrieving a pull request."""
self.instance.pull_request(1)
self.session.get.assert_called_once_with(url_for("pulls/1"))
def test_pull_request_required_number(self):
"""Verify the request for retrieving a pull request."""
self.instance.pull_request(-1)
assert self.session.get.called is False
def test_readme(self):
"""Verify the request for retrieving the README."""
self.instance.readme()
self.session.get.assert_called_once_with(url_for("readme"))
def test_ref(self):
"""Verify the request for retrieving a reference."""
self.instance.ref("heads/develop")
self.session.get.assert_called_once_with(
url_for("git/refs/heads/develop")
)
def test_ref_required_ref(self):
"""Verify the request for retrieving a reference."""
self.instance.ref("")
assert self.session.get.called is False
def test_release_from_tag(self):
"""Test the request for retrieving release by tag name"""
self.instance.release_from_tag("v1.0.0")
self.session.get.assert_called_once_with(
url_for("releases/tags/v1.0.0")
)
def test_remove_collaborator(self):
"""Verify the request for removing a collaborator."""
self.instance.remove_collaborator("octocat")
self.session.delete.assert_called_once_with(
url_for("collaborators/octocat")
)
def test_remove_collaborator_required_username(self):
"""Verify the request for removing a collaborator."""
assert self.instance.remove_collaborator("") is False
assert self.session.delete.called is False
def test_replace_topics(self):
"""Verify the request for replacing the topics."""
self.instance.replace_topics(["flask", "bpython", "python"])
self.session.put.assert_called_once_with(
url_for("topics"),
data='{"names": ["flask", "bpython", "python"]}',
headers=self.instance.PREVIEW_HEADERS,
)
def test_source(self):
"""Verify that the source of the repository can be retrieved."""
source = self.instance.source
assert isinstance(source, ShortRepository)
def test_subscription(self):
"""Verify the request for retrieving the subscription on a repo."""
self.instance.subscription()
self.session.get.assert_called_once_with(url_for("subscription"))
def test_tag(self):
"""Verify the request for retrieving an annotated tag."""
self.instance.tag("fake-sha")
self.session.get.assert_called_once_with(url_for("git/tags/fake-sha"))
def test_tag_required_sha(self):
"""Verify the request for retrieving an annotated tag."""
self.instance.tag("")
assert self.session.get.called is False
def test_topics(self):
"""Verify the request for retrieving the topics."""
self.instance.topics()
self.session.get.assert_called_once_with(
url_for("topics"), headers=self.instance.PREVIEW_HEADERS
)
def test_tree(self):
"""Verify the request for retrieving a tree."""
self.instance.tree("fake-sha")
self.session.get.assert_called_once_with(
url_for("git/trees/fake-sha"), params=None
)
def test_tree_required_sha(self):
"""Verify the request for retrieving a tree."""
self.instance.tree("")
assert self.session.get.called is False
def test_tree_optional_recursive(self):
"""Verify the request for recursively retrieving a tree."""
self.instance.tree("fake-sha", recursive=True)
self.session.get.assert_called_once_with(
url_for("git/trees/fake-sha"), params={"recursive": 1}
)
def test_str(self):
"""Verify instance string is formatted correctly."""
owner = self.instance.owner
repository = self.instance.name
assert str(self.instance) == f"{owner}/{repository}"
def test_weekly_commit_count(self):
"""Verify the request for retrieving total commit counts."""
self.instance.weekly_commit_count()
self.session.get.assert_called_once_with(
url_for("stats/participation")
)
def test_views_default(self):
"""Verify the request for retrieving repository daily views."""
self.instance.views()
self.session.get.assert_called_once_with(
url_for("traffic/views"), params={"per": "day"}
)
def test_views_weekly(self):
"""Verify the request for retrieving repository weekly views."""
self.instance.views(per="week")
self.session.get.assert_called_once_with(
url_for("traffic/views"), params={"per": "week"}
)
def test_views_invalid_per(self):
"""Verify invalid views resolution raises ValueError."""
with pytest.raises(ValueError):
self.instance.views(per="invalid")
def test_clones_default(self):
"""Verify the request for retrieving repository daily clones."""
self.instance.clones()
self.session.get.assert_called_once_with(
url_for("traffic/clones"), params={"per": "day"}
)
def test_clones_weekly(self):
"""Verify the request for retrieving repository weekly clones."""
self.instance.clones(per="week")
self.session.get.assert_called_once_with(
url_for("traffic/clones"), params={"per": "week"}
)
def test_clones_invalid_per(self):
"""Verify invalid clones resolution raises ValueError."""
with pytest.raises(ValueError):
self.instance.clones(per="invalid")
class TestRepositoryIterator(helper.UnitIteratorHelper):
"""Unit tests for Repository methods that return iterators."""
described_class = Repository
example_data = repo_example_data