Skip to content

Commit

Permalink
Better test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Burak Yigit Kaya committed Dec 29, 2015
1 parent 1cbfaa1 commit 4356dc3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ python:
- "3.4"
- "3.5"
install: "pip install ."
script: "python phabricator/tests.py"
script: "python -m phabricator.tests.test_phabricator"
Empty file added phabricator/tests/__init__.py
Empty file.
Empty file.
1 change: 1 addition & 0 deletions phabricator/tests/resources/certificate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fdhcq3zsyijnm4h6gmh43zue5umsmng5t4dlwodvmiz4cnc6fl6fzrvjbfg2ftktrcddan7b3xtgmfge2afbrh4uwam6pfxpq5dbkhbl6mgaijdzpq5efw2ynlnjhoeqyh6dakl4yg346gbhabzkcxreu7hcjhw6vo6wwa7ky2sjdk742khlgsakwtme6sr2dfkhlxxkcqw3jngyrq5zj7m6m7hnscuzlzsviawnvg47pe7l4hxiexpbb5k456r
5 changes: 5 additions & 0 deletions phabricator/tests/resources/responses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"conduit.connect": "{\"result\":{\"connectionID\":1759,\"sessionKey\":\"lwvyv7f6hlzb2vawac6reix7ejvjty72svnir6zy\",\"userPHID\":\"PHID-USER-6ij4rnamb2gsfpdkgmny\"},\"error_code\":null,\"error_info\":null}",
"user.whoami": "{\"result\":{\"phid\":\"PHID-USER-6ij4rnamz2gxfpbkamny\",\"userName\":\"testaccount\",\"realName\":\"Test Account\"},\"error_code\":null,\"error_info\":null}",
"maniphest.find": "{\"result\":{\"PHID-TASK-4cgpskv6zzys6rp5rvrc\":{\"id\":\"722\",\"phid\":\"PHID-TASK-4cgpskv6zzys6rp5rvrc\",\"authorPHID\":\"PHID-USER-5022a9389121884ab9db\",\"ownerPHID\":\"PHID-USER-5022a9389121884ab9db\",\"ccPHIDs\":[\"PHID-USER-5022a9389121884ab9db\",\"PHID-USER-ba8aeea1b3fe2853d6bb\"],\"status\":\"3\",\"priority\":\"Needs Triage\",\"title\":\"Relations should be two-way\",\"description\":\"When adding a differential revision you can specify Maniphest Tickets to add the relation. However, this doesnt add the relation from the ticket -> the differently.(This was added via the commit message)\",\"projectPHIDs\":[\"PHID-PROJ-358dbc2e601f7e619232\",\"PHID-PROJ-f58a9ac58c333f106a69\"],\"uri\":\"https://secure.phabricator.com/T722\",\"auxiliary\":[],\"objectName\":\"T722\",\"dateCreated\":\"1325553508\",\"dateModified\":\"1325618490\"}},\"error_code\":null,\"error_info\":null}"
}
48 changes: 30 additions & 18 deletions phabricator/tests.py → phabricator/tests/test_phabricator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
except ImportError:
import mock

from pkg_resources import resource_string
import json

import phabricator


RESPONSES = {
'conduit.connect': '{"result":{"connectionID":1759,"sessionKey":"lwvyv7f6hlzb2vawac6reix7ejvjty72svnir6zy","userPHID":"PHID-USER-6ij4rnamb2gsfpdkgmny"},"error_code":null,"error_info":null}',
'user.whoami': '{"result":{"phid":"PHID-USER-6ij4rnamz2gxfpbkamny","userName":"testaccount","realName":"Test Account"},"error_code":null,"error_info":null}',
'maniphest.find': '{"result":{"PHID-TASK-4cgpskv6zzys6rp5rvrc":{"id":"722","phid":"PHID-TASK-4cgpskv6zzys6rp5rvrc","authorPHID":"PHID-USER-5022a9389121884ab9db","ownerPHID":"PHID-USER-5022a9389121884ab9db","ccPHIDs":["PHID-USER-5022a9389121884ab9db","PHID-USER-ba8aeea1b3fe2853d6bb"],"status":"3","priority":"Needs Triage","title":"Relations should be two-way","description":"When adding a differential revision you can specify Maniphest Tickets to add the relation. However, this doesnt add the relation from the ticket -> the differently.(This was added via the commit message)","projectPHIDs":["PHID-PROJ-358dbc2e601f7e619232","PHID-PROJ-f58a9ac58c333f106a69"],"uri":"https:\/\/secure.phabricator.com\/T722","auxiliary":[],"objectName":"T722","dateCreated":"1325553508","dateModified":"1325618490"}},"error_code":null,"error_info":null}'
}

CERTIFICATE = (
'fdhcq3zsyijnm4h6gmh43zue5umsmng5t4dlwodvmiz4cnc6fl6f'
'zrvjbfg2ftktrcddan7b3xtgmfge2afbrh4uwam6pfxpq5dbkhbl'
'6mgaijdzpq5efw2ynlnjhoeqyh6dakl4yg346gbhabzkcxreu7hc'
'jhw6vo6wwa7ky2sjdk742khlgsakwtme6sr2dfkhlxxkcqw3jngy'
'rq5zj7m6m7hnscuzlzsviawnvg47pe7l4hxiexpbb5k456r'
RESPONSES = json.loads(
resource_string(
'phabricator.tests.resources',
'responses.json'
).decode('utf8')
)
CERTIFICATE = resource_string(
'phabricator.tests.resources',
'certificate.txt'
).decode('utf8').strip()


# Protect against local user's .arcrc interference.
phabricator.ARCRC = {}
Expand All @@ -52,7 +52,9 @@ def test_generate_hash(self):
@mock.patch('phabricator.httplib.HTTPConnection')
def test_connect(self, mock_connection):
mock_obj = mock_connection.return_value = mock.Mock()
mock_obj.getresponse.return_value = StringIO(RESPONSES['conduit.connect'])
mock_obj.getresponse.return_value = StringIO(
RESPONSES['conduit.connect']
)
mock_obj.getresponse.return_value.status = 200

api = phabricator.Phabricator(
Expand Down Expand Up @@ -97,24 +99,34 @@ def test_bad_status(self, mock_connection):
with self.assertRaises(phabricator.httplib.HTTPException):
api.user.whoami()


@mock.patch('phabricator.httplib.HTTPConnection')
def test_maniphest_find(self, mock_connection):
mock_obj = mock_connection.return_value = mock.Mock()
mock_obj.getresponse.return_value = StringIO(RESPONSES['maniphest.find'])
mock_obj.getresponse.return_value = StringIO(
RESPONSES['maniphest.find']
)
mock_obj.getresponse.return_value.status = 200

api = phabricator.Phabricator(username='test', certificate='test', host='http://localhost')
api = phabricator.Phabricator(
username='test',
certificate='test',
host='http://localhost'
)
api._conduit = True

result = api.maniphest.find(ownerphids=['PHID-USER-5022a9389121884ab9db'])
result = api.maniphest.find(
ownerphids=['PHID-USER-5022a9389121884ab9db']
)
self.assertEqual(len(result), 1)

# Test iteration
self.assertIsInstance([x for x in result], list)

# Test getattr
self.assertEqual(result['PHID-TASK-4cgpskv6zzys6rp5rvrc']['status'], '3')
self.assertEqual(
result['PHID-TASK-4cgpskv6zzys6rp5rvrc']['status'],
'3'
)

def test_validation(self):
self.api._conduit = True
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
description='Phabricator API Bindings',
packages=find_packages(),
zip_safe=False,
test_suite='nose.collector',
test_suite='phabricator.tests.test_phabricator',
tests_require=tests_requires,
include_package_data=True,
classifiers=[
Expand Down

0 comments on commit 4356dc3

Please sign in to comment.