|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +""" |
| 17 | +TestCases for check_pr_approval.py |
| 18 | +""" |
| 19 | +import unittest |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | + |
| 23 | + |
| 24 | +class Test_check_approval(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + self.codeset = 'UTF-8' |
| 27 | + # only key info in it |
| 28 | + self.jsonstr = """ |
| 29 | +[ |
| 30 | + { |
| 31 | + "id": 688077074, |
| 32 | + "node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3Njg4MDc3MDc0", |
| 33 | + "user": { |
| 34 | + "login": "wadefelix", |
| 35 | + "id": 1306724, |
| 36 | + "type": "User", |
| 37 | + "site_admin": false |
| 38 | + }, |
| 39 | + "body": "", |
| 40 | + "state": "COMMENTED", |
| 41 | + "author_association": "CONTRIBUTOR" |
| 42 | + }, |
| 43 | + { |
| 44 | + "id": 688092580, |
| 45 | + "node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3Njg4MDkyNTgw", |
| 46 | + "user": { |
| 47 | + "login": "MingMingShangTian", |
| 48 | + "id": 13469016, |
| 49 | + "type": "User", |
| 50 | + "site_admin": false |
| 51 | + }, |
| 52 | + "body": "LGTM", |
| 53 | + "state": "APPROVED", |
| 54 | + "author_association": "CONTRIBUTOR" |
| 55 | + }, |
| 56 | + { |
| 57 | + "id": 689175539, |
| 58 | + "node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3Njg5MTc1NTM5", |
| 59 | + "user": { |
| 60 | + "login": "pangyoki", |
| 61 | + "id": 26408901, |
| 62 | + "type": "User", |
| 63 | + "site_admin": false |
| 64 | + }, |
| 65 | + "body": "LGTM", |
| 66 | + "state": "APPROVED", |
| 67 | + "author_association": "CONTRIBUTOR" |
| 68 | + } |
| 69 | +] |
| 70 | +""".encode(self.codeset) |
| 71 | + |
| 72 | + def test_ids(self): |
| 73 | + cmd = [sys.executable, 'check_pr_approval.py', '1', '26408901'] |
| 74 | + subprc = subprocess.Popen( |
| 75 | + cmd, |
| 76 | + stdin=subprocess.PIPE, |
| 77 | + stdout=subprocess.PIPE, |
| 78 | + stderr=subprocess.PIPE) |
| 79 | + output, error = subprc.communicate(input=self.jsonstr) |
| 80 | + self.assertEqual('TRUE', output.decode(self.codeset).rstrip()) |
| 81 | + |
| 82 | + def test_logins(self): |
| 83 | + cmd = [sys.executable, 'check_pr_approval.py', '1', 'pangyoki'] |
| 84 | + subprc = subprocess.Popen( |
| 85 | + cmd, |
| 86 | + stdin=subprocess.PIPE, |
| 87 | + stdout=subprocess.PIPE, |
| 88 | + stderr=subprocess.PIPE) |
| 89 | + output, error = subprc.communicate(input=self.jsonstr) |
| 90 | + self.assertEqual('TRUE', output.decode(self.codeset).rstrip()) |
| 91 | + |
| 92 | + def test_ids_and_logins(self): |
| 93 | + cmd = [ |
| 94 | + sys.executable, 'check_pr_approval.py', '2', 'pangyoki', '13469016' |
| 95 | + ] |
| 96 | + subprc = subprocess.Popen( |
| 97 | + cmd, |
| 98 | + stdin=subprocess.PIPE, |
| 99 | + stdout=subprocess.PIPE, |
| 100 | + stderr=subprocess.PIPE) |
| 101 | + output, error = subprc.communicate(input=self.jsonstr) |
| 102 | + #self.assertEqual('', error.rstrip()) |
| 103 | + self.assertEqual('TRUE', output.decode(self.codeset).rstrip()) |
| 104 | + |
| 105 | + def test_check_with_required_reviewer_not_approved(self): |
| 106 | + cmd = [ |
| 107 | + sys.executable, 'check_pr_approval.py', '2', 'wadefelix', |
| 108 | + ' 13469016' |
| 109 | + ] |
| 110 | + subprc = subprocess.Popen( |
| 111 | + cmd, |
| 112 | + stdin=subprocess.PIPE, |
| 113 | + stdout=subprocess.PIPE, |
| 114 | + stderr=subprocess.PIPE) |
| 115 | + output, error = subprc.communicate(input=self.jsonstr) |
| 116 | + self.assertEqual('FALSE', output.decode(self.codeset).rstrip()) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + unittest.main() |
0 commit comments