Skip to content

Commit

Permalink
Fix tests (#920)
Browse files Browse the repository at this point in the history
* fix: fix tests for mod_sample

* fix: fix regression tests tests

* fix: fix test tests tests
  • Loading branch information
canihavesomecoffee authored Nov 3, 2024
1 parent 1e3a9ac commit 4ced84e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
17 changes: 13 additions & 4 deletions tests/test_regression/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from mod_sample.models import Sample
from mod_test.models import Test, TestResultFile
from tests.base import BaseTestCase
from tests.test_auth.test_controllers import MockUser


class TestControllers(BaseTestCase):
Expand Down Expand Up @@ -48,21 +49,25 @@ def test_regression_test_status_toggle(self):
self.assertEqual('True', response.json['active'])

@mock.patch('mod_regression.controllers.RegressionTestOutput')
def test_download_result_file_not_found(self, mock_regression_output):
@mock.patch('mod_auth.controllers.g')
def test_download_result_file_not_found(self, mock_g, mock_regression_output):
"""Test that non-existent result file gives 404."""
from mod_regression.controllers import test_result_file
mock_regression_output.query.filter.return_value.first.return_value = None
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(NotFound):
test_result_file(1)

mock_regression_output.query.filter.assert_called_once_with(mock_regression_output.id == 1)

@mock.patch('mod_regression.controllers.RegressionTestOutputFiles')
def test_download_result_file_not_found_variant(self, mock_regression_output_file):
@mock.patch('mod_auth.controllers.g')
def test_download_result_file_not_found_variant(self, mock_g, mock_regression_output_file):
"""Test that non-existent result file gives 404."""
from mod_regression.controllers import multiple_test_result_file
mock_regression_output_file.query.filter.return_value.first.return_value = None
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(NotFound):
multiple_test_result_file(1)
Expand All @@ -71,21 +76,25 @@ def test_download_result_file_not_found_variant(self, mock_regression_output_fil

@mock.patch('mod_regression.controllers.serve_file_download')
@mock.patch('mod_regression.controllers.RegressionTestOutput')
def test_download_result_file(self, mock_regression_output, mock_serve):
@mock.patch('mod_auth.controllers.g')
def test_download_result_file(self, mock_g, mock_regression_output, mock_serve):
"""Test that correct result file triggers serve download."""
from mod_regression.controllers import test_result_file

mock_g.user = MockUser(id=1, role="None")
response = test_result_file(1)

mock_regression_output.query.filter.assert_called_once_with(mock_regression_output.id == 1)
mock_serve.assert_called_once()

@mock.patch('mod_regression.controllers.serve_file_download')
@mock.patch('mod_regression.controllers.RegressionTestOutputFiles')
def test_download_result_file_variant(self, mock_regression_output_file, mock_serve):
@mock.patch('mod_auth.controllers.g')
def test_download_result_file_variant(self, mock_g, mock_regression_output_file, mock_serve):
"""Test that correct result file triggers serve download for variants."""
from mod_regression.controllers import multiple_test_result_file

mock_g.user = MockUser(id=1, role="None")
response = multiple_test_result_file(1)

mock_regression_output_file.query.filter.assert_called_once_with(mock_regression_output_file.id == 1)
Expand Down
34 changes: 26 additions & 8 deletions tests/test_sample/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from mod_sample.media_info_parser import InvalidMediaInfoError
from mod_sample.models import Sample, Tag
from tests.base import BaseTestCase
from tests.test_auth.test_controllers import MockUser


def raise_media_exception():
Expand Down Expand Up @@ -75,23 +76,27 @@ def test_sample_id(self):

@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
def test_download_sample(self, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample(self, mock_g, mock_sample, mock_serve_download):
"""Test function download_sample."""
from mod_sample.controllers import download_sample

mock_g.user = MockUser(id=1, role="None")
response = download_sample(1)

self.assertEqual(response, mock_serve_download())
mock_sample.query.filter.assert_called_once_with(mock_sample.id == 1)

@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
def test_download_sample_raise_exception(self, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample_raise_exception(self, mock_g, mock_sample, mock_serve_download):
"""Test function download_sample to raise SampleNotFoundException."""
from mod_sample.controllers import (SampleNotFoundException,
download_sample)

mock_sample.query.filter.return_value.first.return_value = None
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(SampleNotFoundException):
download_sample(1)
Expand All @@ -102,10 +107,12 @@ def test_download_sample_raise_exception(self, mock_sample, mock_serve_download)
@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
@mock.patch('mod_sample.controllers.os')
def test_download_sample_media_info(self, mock_os, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample_media_info(self, mock_g, mock_os, mock_sample, mock_serve_download):
"""Test function download_sample_media_info."""
from mod_sample.controllers import download_sample_media_info

mock_g.user = MockUser(id=1, role="None")
response = download_sample_media_info(1)

self.assertEqual(response, mock_serve_download())
Expand All @@ -115,12 +122,14 @@ def test_download_sample_media_info(self, mock_os, mock_sample, mock_serve_downl
@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
@mock.patch('mod_sample.controllers.os')
def test_download_sample_media_info_path_wrong(self, mock_os, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample_media_info_path_wrong(self, mock_g, mock_os, mock_sample, mock_serve_download):
"""Test function download_sample_media_info with wrong path for media info."""
from mod_sample.controllers import (SampleNotFoundException,
download_sample_media_info)

mock_os.path.isfile.return_value = False
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(SampleNotFoundException):
download_sample_media_info(1)
Expand All @@ -131,12 +140,14 @@ def test_download_sample_media_info_path_wrong(self, mock_os, mock_sample, mock_
@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
@mock.patch('mod_sample.controllers.os')
def test_download_sample_media_info_sample_not_found(self, mock_os, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample_media_info_sample_not_found(self, mock_g, mock_os, mock_sample, mock_serve_download):
"""Test function download_sample_media_info to raise SampleNotFoundException."""
from mod_sample.controllers import (SampleNotFoundException,
download_sample_media_info)

mock_sample.query.filter.return_value.first.return_value = None
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(SampleNotFoundException):
download_sample_media_info(1)
Expand All @@ -147,10 +158,13 @@ def test_download_sample_media_info_sample_not_found(self, mock_os, mock_sample,
@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
@mock.patch('mod_sample.controllers.ExtraFile')
def test_download_sample_additional(self, mock_extra, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample_additional(self, mock_g, mock_extra, mock_sample, mock_serve_download):
"""Test function download_sample_additional."""
from mod_sample.controllers import download_sample_additional

mock_g.user = MockUser(id=1, role="None")

response = download_sample_additional(1, 1)

self.assertEqual(response, mock_serve_download())
Expand All @@ -160,12 +174,14 @@ def test_download_sample_additional(self, mock_extra, mock_sample, mock_serve_do
@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
@mock.patch('mod_sample.controllers.ExtraFile')
def test_download_sample_additional_sample_not_found(self, mock_extra, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample_additional_sample_not_found(self, mock_g, mock_extra, mock_sample, mock_serve_download):
"""Test function download_sample_additional to raise SampleNotFoundException."""
from mod_sample.controllers import (SampleNotFoundException,
download_sample_additional)

mock_sample.query.filter.return_value.first.return_value = None
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(SampleNotFoundException):
download_sample_additional(1, 1)
Expand All @@ -176,12 +192,14 @@ def test_download_sample_additional_sample_not_found(self, mock_extra, mock_samp
@mock.patch('mod_sample.controllers.serve_file_download')
@mock.patch('mod_sample.controllers.Sample')
@mock.patch('mod_sample.controllers.ExtraFile')
def test_download_sample_additional_extrafile_not_found(self, mock_extra, mock_sample, mock_serve_download):
@mock.patch('mod_auth.controllers.g')
def test_download_sample_additional_extrafile_not_found(self, mock_g, mock_extra, mock_sample, mock_serve_download):
"""Test function download_sample_additional to raise SampleNotFoundException when extra file not found."""
from mod_sample.controllers import (SampleNotFoundException,
download_sample_additional)

mock_extra.query.filter.return_value.first.return_value = None
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(SampleNotFoundException):
download_sample_additional(1, 1)
Expand Down
14 changes: 11 additions & 3 deletions tests/test_test/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mod_test.models import (Test, TestPlatform, TestProgress, TestResult,
TestResultFile, TestStatus)
from tests.base import BaseTestCase
from tests.test_auth.test_controllers import MockUser


class TestControllers(BaseTestCase):
Expand Down Expand Up @@ -209,12 +210,14 @@ def test_generate_diff_download(self, mock_response, mock_request, mock_test_res
self.assertTrue(response, mock_response())

@mock.patch('mod_test.controllers.Test')
def test_download_build_log_file_test_not_found(self, mock_test):
@mock.patch('mod_auth.controllers.g')
def test_download_build_log_file_test_not_found(self, mock_g, mock_test):
"""Try to download build log for invalid test."""
from mod_test.controllers import (TestNotFoundException,
download_build_log_file)

mock_test.query.filter.return_value.first.return_value = None
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(TestNotFoundException):
download_build_log_file(1)
Expand All @@ -223,12 +226,14 @@ def test_download_build_log_file_test_not_found(self, mock_test):

@mock.patch('mod_test.controllers.os')
@mock.patch('mod_test.controllers.Test')
def test_download_build_log_file_log_not_file(self, mock_test, mock_os):
@mock.patch('mod_auth.controllers.g')
def test_download_build_log_file_log_not_file(self, mock_g, mock_test, mock_os):
"""Try to download build log for invalid file path."""
from mod_test.controllers import (TestNotFoundException,
download_build_log_file)

mock_os.path.isfile.side_effect = TestNotFoundException('msg')
mock_g.user = MockUser(id=1, role="None")

with self.assertRaises(TestNotFoundException):
download_build_log_file('1')
Expand All @@ -239,11 +244,14 @@ def test_download_build_log_file_log_not_file(self, mock_test, mock_os):
@mock.patch('mod_test.controllers.os')
@mock.patch('mod_test.controllers.Test')
@mock.patch('mod_test.controllers.serve_file_download')
def test_download_build_log_file(self, mock_serve, mock_test, mock_os):
@mock.patch('mod_auth.controllers.g')
def test_download_build_log_file(self, mock_g, mock_serve, mock_test, mock_os):
"""Try to download build log."""
from mod_test.controllers import (TestNotFoundException,
download_build_log_file)

mock_g.user = MockUser(id=1, role="None")

response = download_build_log_file('1')

self.assertEqual(response, mock_serve())
Expand Down

0 comments on commit 4ced84e

Please sign in to comment.