1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
# Copyright (c) The Spyder Development Team |
4
|
|
|
# |
5
|
|
|
# Licensed under the terms of the MIT License |
6
|
|
|
# (See LICENSE.txt for details) |
7
|
|
|
# ----------------------------------------------------------------------------- |
8
|
|
|
"""Tests github repo.""" |
9
|
|
|
|
10
|
|
|
# Standard library imports |
11
|
|
|
import os |
12
|
|
|
|
13
|
|
|
# Third party imports |
14
|
|
|
import pytest |
15
|
|
|
|
16
|
|
|
# Local imports |
17
|
|
|
from loghub.external.github import ApiError |
18
|
|
|
from loghub.main import GitHubRepo |
19
|
|
|
|
20
|
|
|
REPO = 'spyder-ide/loghub' |
21
|
|
|
TEST_TOKEN = os.environ.get('TEST_TOKEN', '').replace('x', '') |
22
|
|
|
TEST_USER = os.environ.get('TEST_USER', '').replace('x', '') |
23
|
|
|
TEST_PASS = os.environ.get('TEST_CODE', '').replace('x', '') |
24
|
|
|
TEST_MILESTONE = 'test-milestone' |
25
|
|
|
TEST_TAG = 'v0.1.2' |
26
|
|
|
NOT_ON_CI = os.environ.get('CIRCLECI') != 'true' |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
# --- Fixtures |
30
|
|
|
# ----------------------------------------------------------------------------- |
31
|
|
|
@pytest.fixture |
32
|
|
|
def gh_repo(): |
33
|
|
|
return GitHubRepo(token=TEST_TOKEN, repo=REPO) |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
# --- Tests |
37
|
|
|
# ----------------------------------------------------------------------------- |
38
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
39
|
|
|
def test_valid_user_password(): |
40
|
|
|
gh = GitHubRepo(username=TEST_USER, password=TEST_PASS, repo=REPO) |
41
|
|
|
assert bool(gh.milestone(TEST_MILESTONE)) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
45
|
|
|
def test_invalid_user_password(): |
46
|
|
|
gh = GitHubRepo( |
47
|
|
|
username='invalid-user', password='invalid-password', repo=REPO) |
48
|
|
|
with pytest.raises(ApiError): |
49
|
|
|
gh.milestone(TEST_MILESTONE) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
53
|
|
|
def test_valid_token(): |
54
|
|
|
gh = GitHubRepo(token=TEST_TOKEN, repo=REPO) |
55
|
|
|
gh.milestone(TEST_MILESTONE) |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def test_invalid_token(): |
59
|
|
|
gh = GitHubRepo(token='this-is-an-invalid-token', repo=REPO) |
60
|
|
|
with pytest.raises(ApiError): |
61
|
|
|
gh.milestone(TEST_MILESTONE) |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
65
|
|
|
def test_tags(gh_repo): |
66
|
|
|
tags = gh_repo.tags() |
67
|
|
|
titles = [tag['ref'].replace('refs/tags/', '') for tag in tags] |
68
|
|
|
assert TEST_TAG in titles |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
72
|
|
|
def test_valid_tag(gh_repo): |
73
|
|
|
tag = gh_repo.tag(TEST_TAG) |
74
|
|
|
assert bool(tag) |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
78
|
|
|
def test_invalid_tag(gh_repo): |
79
|
|
|
with pytest.raises(SystemExit): |
80
|
|
|
gh_repo.tag('invalid-tag') |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
84
|
|
|
def test_milestones(gh_repo): |
85
|
|
|
milestones = gh_repo.milestones() |
86
|
|
|
titles = [milestone['title'] for milestone in milestones] |
87
|
|
|
assert TEST_MILESTONE in titles |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
91
|
|
|
def test_valid_milestone(gh_repo): |
92
|
|
|
assert bool(gh_repo.milestone(TEST_MILESTONE)) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
96
|
|
|
def test_invalid_milestone(gh_repo): |
97
|
|
|
with pytest.raises(SystemExit): |
98
|
|
|
gh_repo.milestone('invalid-milestone') |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
102
|
|
|
def test_issues(gh_repo): |
103
|
|
|
milestone_data = gh_repo.milestone(TEST_MILESTONE) |
104
|
|
|
print(milestone_data) |
105
|
|
|
milestone_number = milestone_data['number'] |
106
|
|
|
issues = gh_repo.issues(milestone=milestone_number, state='closed') |
107
|
|
|
assert len(issues) == 3 |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
111
|
|
|
def test_pr_merged(gh_repo): |
112
|
|
|
assert gh_repo.is_merged(25) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only') |
116
|
|
|
def test_pr_closed(gh_repo): |
117
|
|
|
assert not gh_repo.is_merged(22) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def test_dates(): |
121
|
|
|
date = GitHubRepo.str_to_date('2016-10-10T08:08:08Z') |
122
|
|
|
assert date.year == 2016 |
123
|
|
|
assert date.month == 10 |
124
|
|
|
assert date.day == 10 |
125
|
|
|
assert date.hour == 8 |
126
|
|
|
assert date.minute == 8 |
127
|
|
|
assert date.second == 8 |
128
|
|
|
|