test_check_pr_approval.py 3.7 KB
Newer Older
1 2 3
#! /usr/bin/env python

# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
4
#
5 6 7
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
TestCases for check_pr_approval.py
"""
import unittest
import subprocess
import sys


class Test_check_approval(unittest.TestCase):
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
    def setUp(self):
        self.codeset = 'UTF-8'
        # only key info in it
        self.jsonstr = """
[
  {
    "id": 688077074,
    "node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3Njg4MDc3MDc0",
    "user": {
      "login": "wadefelix",
      "id": 1306724,
      "type": "User",
      "site_admin": false
    },
    "body": "",
    "state": "COMMENTED",
    "author_association": "CONTRIBUTOR"
  },
  {
    "id": 688092580,
    "node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3Njg4MDkyNTgw",
    "user": {
      "login": "MingMingShangTian",
      "id": 13469016,
      "type": "User",
      "site_admin": false
    },
    "body": "LGTM",
    "state": "APPROVED",
    "author_association": "CONTRIBUTOR"
  },
  {
    "id": 689175539,
    "node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3Njg5MTc1NTM5",
    "user": {
      "login": "pangyoki",
      "id": 26408901,
      "type": "User",
      "site_admin": false
    },
    "body": "LGTM",
    "state": "APPROVED",
    "author_association": "CONTRIBUTOR"
  }
]
""".encode(self.codeset)

    def test_ids(self):
        cmd = [sys.executable, 'check_pr_approval.py', '1', '26408901']
75 76 77 78
        subprc = subprocess.Popen(cmd,
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
79 80 81 82 83
        output, error = subprc.communicate(input=self.jsonstr)
        self.assertEqual('TRUE', output.decode(self.codeset).rstrip())

    def test_logins(self):
        cmd = [sys.executable, 'check_pr_approval.py', '1', 'pangyoki']
84 85 86 87
        subprc = subprocess.Popen(cmd,
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
88 89 90 91 92 93 94
        output, error = subprc.communicate(input=self.jsonstr)
        self.assertEqual('TRUE', output.decode(self.codeset).rstrip())

    def test_ids_and_logins(self):
        cmd = [
            sys.executable, 'check_pr_approval.py', '2', 'pangyoki', '13469016'
        ]
95 96 97 98
        subprc = subprocess.Popen(cmd,
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
99 100 101 102 103 104 105 106 107
        output, error = subprc.communicate(input=self.jsonstr)
        #self.assertEqual('', error.rstrip())
        self.assertEqual('TRUE', output.decode(self.codeset).rstrip())

    def test_check_with_required_reviewer_not_approved(self):
        cmd = [
            sys.executable, 'check_pr_approval.py', '2', 'wadefelix',
            ' 13469016'
        ]
108 109 110 111
        subprc = subprocess.Popen(cmd,
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
112 113 114 115 116 117
        output, error = subprc.communicate(input=self.jsonstr)
        self.assertEqual('FALSE', output.decode(self.codeset).rstrip())


if __name__ == '__main__':
    unittest.main()