Last active
March 30, 2016 12:54
-
-
Save MikeLing/1de0c9ac75525764c14ec50048b0ec81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This file contains tests for mozci/sources/tc.py.""" | |
import json | |
from mozci.sources import tc | |
import unittest | |
from mock import patch, Mock | |
from mozhginfo.push import Push | |
MOCK_REPO_URL = "https://hg.mozilla.org/projects/%(repo_name)s" | |
MOCK_REVISION = """ | |
{ | |
"82366": { | |
"changesets": [ | |
"4e030c8cf8c35158c9924f6bb33ffe8af00c162b" | |
], | |
"date": 1438992451, | |
"user": "nobody@mozilla.com" | |
} | |
} | |
""" | |
def mock_get(data): | |
"""Mock of requests.get. The object returned must have headers and iter_content properties.""" | |
response = Mock() | |
def iter_content(chunk_size=4): | |
"""Mocking requests.get().iter_content.""" | |
rest = data | |
while rest: | |
chunk = rest[:chunk_size] | |
rest = rest[chunk_size:] | |
yield chunk | |
response.headers = {'content-length': str(len(data))} | |
response.iter_content = iter_content | |
return response | |
class TestGenerateMetadata(unittest.TestCase): | |
def setUp(self): | |
"""Setting up values that will be used in every test.""" | |
self.repo_name = 'repo1' | |
self.revision = '8a9823b951f6af481fe247fb5a57dac5a2f8bf13' | |
self.name = 'It just a mock description of the mock' | |
self.description = 'Task graph generated via Mozilla CI tools' | |
push_id = json.loads(MOCK_REVISION).keys()[0] | |
self.push = Push(push_id=push_id, push_info=json.loads(MOCK_REVISION)[push_id]) | |
@patch('mozci.repositories.query_repo_url', | |
return_value=mock_get(MOCK_REPO_URL % {'repo_name': 'repo1'})) | |
@patch('mozhginfo.pushlog_client.query_push_by_revision', | |
return_value=Mock(Push(push_id="82366", | |
push_info=json.loads(MOCK_REVISION)["82366"]))) | |
def test_generate_metadata(self, query_repo_url, query_push_by_revision): | |
response = { | |
'name': self.name, | |
'description': self.description, | |
'owner': self.push.user, | |
'source': '%s/rev/%s' % (MOCK_REPO_URL, self.revision) | |
} | |
self.assertEqual(response, | |
tc.generate_metadata(repo_name=self.repo_name, | |
revision=self.revision, | |
name=self.name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment