Skip to content

Commit

Permalink
Merge pull request #106 from launchableinc/go-test-test
Browse files Browse the repository at this point in the history
Replace mock with responses
  • Loading branch information
ninjinkun authored Jan 25, 2021
2 parents f61698c + 8ad4f6e commit 2848c44
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
10 changes: 10 additions & 0 deletions tests/cli_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import os
import unittest
import types
import responses

import click.testing
from click.testing import CliRunner

from launchable.__main__ import main
from launchable.utils.session import clean_session_files
from launchable.utils.http_client import get_base_url


class CliTestCase(unittest.TestCase):
Expand All @@ -28,6 +30,14 @@ def setUp(self):
self.maxDiff = None
os.environ['LAUNCHABLE_TOKEN'] = self.launchable_token

responses.add(responses.POST, "{}/intake/organizations/{}/workspaces/{}/builds/{}/test_sessions".format(get_base_url(), self.organization, self.workspace, self.build_name),
json={'id': self.session_id}, status=200)
responses.add(responses.POST, "{}/intake/organizations/{}/workspaces/{}/subset".format(get_base_url(), self.organization, self.workspace),
json={'testPaths': []}, status=200)

responses.add(responses.POST, "{}/intake/organizations/{}/workspaces/{}/subset".format(get_base_url(), self.organization, self.workspace,),
json={'testPaths': []}, status=200)

def tearDown(self):
clean_session_files()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from pathlib import Path
from unittest import mock
import responses
import json
import gzip
from launchable.utils.http_client import get_base_url
from launchable.utils.session import read_session
from tests.cli_test_case import CliTestCase

Expand All @@ -12,25 +10,21 @@ class GoTestTest(CliTestCase):
test_files_dir = Path(__file__).parent.joinpath(
'../data/go_test/').resolve()

@mock.patch('requests.request')
def test_subset_with_session(self, mock_post):
@responses.activate
def test_subset_with_session(self):
pipe = "TestExample1\nTestExample2\nTestExample3\nTestExample4\nok github.com/launchableinc/rocket-car-gotest 0.268s"
result = self.cli('subset', '--target', '10%',
'--session', self.session, 'go-test', input=pipe)
self.assertEqual(result.exit_code, 0)

payload = self.gzipped_json_payload(mock_post)
payload = json.loads(gzip.decompress(
responses.calls[0].request.body).decode())
expected = self.load_json_from_file(
self.test_files_dir.joinpath('subset_result.json'))
self.assert_json_orderless_equal(expected, payload)

@responses.activate
def test_subset_without_session(self):
responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/builds/{}/test_sessions".format(get_base_url(), self.build_name),
json={'id': self.session_id}, status=200)
responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/subset".format(get_base_url()),
json={'testPaths': []}, status=200)

pipe = "TestExample1\nTestExample2\nTestExample3\nTestExample4\nok github.com/launchableinc/rocket-car-gotest 0.268s"
result = self.cli('subset', '--target', '10%', '--build',
self.build_name, 'go-test', input=pipe)
Expand All @@ -44,13 +38,14 @@ def test_subset_without_session(self):
self.test_files_dir.joinpath('subset_result.json'))
self.assert_json_orderless_equal(expected, payload)

@mock.patch('requests.request')
def test_record_tests_with_session(self, mock_post):
@responses.activate
def test_record_tests_with_session(self):
result = self.cli('record', 'tests', '--session',
self.session, 'go-test', str(self.test_files_dir) + "/")
self.assertEqual(result.exit_code, 0)

payload = self.gzipped_json_payload(mock_post)
payload = json.loads(gzip.decompress(
b''.join(responses.calls[0].request.body)).decode())
# Remove timestamp because it depends on the machine clock
for c in payload['events']:
del c['created_at']
Expand All @@ -61,11 +56,6 @@ def test_record_tests_with_session(self, mock_post):

@responses.activate
def test_record_tests_without_session(self):
responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/builds/{}/test_sessions".format(get_base_url(), self.build_name),
json={'id': self.session_id}, status=200)
responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/builds/{}/test_sessions/{}/events".format(get_base_url(), self.build_name, self.session_id),
json={}, status=200)

result = self.cli('record', 'tests', '--build',
self.build_name, 'go-test', str(self.test_files_dir) + "/")
self.assertEqual(result.exit_code, 0)
Expand Down
19 changes: 11 additions & 8 deletions tests/test_runners/test_googletest.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
from pathlib import Path
from unittest import mock

import responses
import json
import gzip
from tests.cli_test_case import CliTestCase


class GoogleTestTest(CliTestCase):
test_files_dir = Path(__file__).parent.joinpath(
'../data/googletest/').resolve()

@mock.patch('requests.request')
def test_subset(self, mock_post):
@responses.activate
def test_subset(self):
# I use "ctest -N" to get this list.
pipe = "Test project github.com/launchableinc/rocket-car-googletest\n Test #1: FooTest.Bar\n Test #2: FooTest.Baz\n Test #3: FooTest.Foo\n Test #4: */ParameterizedTest.Bar/*\n\nTotal Tests: 4"
result = self.cli('subset', '--target', '10%',
'--session', self.session, 'googletest', input=pipe)
self.assertEqual(result.exit_code, 0)

payload = self.gzipped_json_payload(mock_post)
payload = json.loads(gzip.decompress(
responses.calls[0].request.body).decode())
expected = self.load_json_from_file(
self.test_files_dir.joinpath('subset_result.json'))
self.assert_json_orderless_equal(expected, payload)

@mock.patch('requests.request')
def test_record_test_googletest(self, mock_post):
@responses.activate
def test_record_test_googletest(self):
result = self.cli('record', 'tests', '--session', self.session,
'googletest', str(self.test_files_dir) + "/")
self.assertEqual(result.exit_code, 0)

payload = self.gzipped_json_payload(mock_post)
payload = json.loads(gzip.decompress(
b''.join(responses.calls[0].request.body)).decode())
expected = self.load_json_from_file(
self.test_files_dir.joinpath('record_test_result.json'))

Expand Down
13 changes: 8 additions & 5 deletions tests/test_runners/test_gradle.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from pathlib import Path
from unittest import mock

import responses
import json
import gzip
from tests.cli_test_case import CliTestCase


class GradleTest(CliTestCase):
test_files_dir = Path(__file__).parent.joinpath('../data/gradle/recursion').resolve()
result_file_path = test_files_dir.joinpath('expected.json')

@mock.patch('requests.request')
def test_record_test_gradle(self, mock_post):
@responses.activate
def test_record_test_gradle(self):
result = self.cli('record', 'tests', '--session', self.session, 'gradle', str(self.test_files_dir) + "/**/reports")
self.assertEqual(result.exit_code, 0)

payload = self.gzipped_json_payload(mock_post)
expected = self.load_json_from_file(self.result_file_path)
payload = json.loads(gzip.decompress(
b''.join(responses.calls[0].request.body)).decode())

expected = self.load_json_from_file(self.result_file_path)
self.assert_json_orderless_equal(expected, payload)
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from pathlib import Path
from unittest import mock

import responses
import json
import gzip
from tests.cli_test_case import CliTestCase


class MinitestTest(CliTestCase):
test_files_dir = Path(__file__).parent.joinpath('../data/minitest/').resolve()
result_file_path = test_files_dir.joinpath('record_test_result.json')

@mock.patch('requests.request')
def test_record_test_minitest(self, mock_post):
@responses.activate
def test_record_test_minitest(self):
result = self.cli('record', 'tests', '--session', self.session, 'minitest', str(self.test_files_dir) + "/")
self.assertEqual(result.exit_code, 0)

payload = self.gzipped_json_payload(mock_post)
payload = json.loads(gzip.decompress(
b''.join(responses.calls[0].request.body)).decode())

expected = self.load_json_from_file(self.result_file_path)
self.assert_json_orderless_equal(expected, payload)

0 comments on commit 2848c44

Please sign in to comment.