"""Tests for github_tools.""" from unittest.mock import patch import github_tools from tests import helpers @patch('github_tools.FORKED_CLIENT_REPO') def test_post_file(fixture_repo): """Assert inner method called.""" github_tools.post_file('unit_test', 'unit_test') fixture_repo.create_file.assert_called() @patch('github_tools.FORKED_CLIENT_REPO') def test_update_file_git(fixture_repo): """Assert inner method called.""" github_tools.update_file_git('unit_test', 'unit_test') fixture_repo.get_contents.assert_called() fixture_repo.update_file.assert_called() @patch('github_tools.CLIENT_ORIGIN_REPO') def test_create_pull_request(client_origin_repo): """Assert inner method called.""" github_tools.create_pull_request() client_origin_repo.create_pull.assert_called() def test_get_prefix_for_removing_join_access_filter(): """Return string.""" filter_string = helpers.model_input['remove_access_filter_join'] result = github_tools.get_prefix_for_removing_join_access_filter( filter_string) assert result == r'\s{4}join:\s?dt_access_filter_1091' def test_change_blocks_in_model_code_remove_join(): """Return regexed string. No access filter join.""" remove = helpers.model_input.get('remove_access_filter_join') prefix = github_tools.get_prefix_for_removing_join_access_filter(remove) result = github_tools.change_blocks_in_model_code( prefix, '', helpers.model_lkml_origin) assert result == helpers.model_lkml_no_join def test_test_change_blocks_in_model_code_replace_filter(): """Return regexed string. Replaced filter.""" new_filter = helpers.model_input.get('new_access_filter') result = github_tools.change_blocks_in_model_code( github_tools.PREFIX_FOR_REPLACING_ACCESS_FILTER, new_filter, helpers.model_lkml_origin) assert result == helpers.model_lkml_new_filter def test_change_connection_in_model_file(): """Return string.""" result = github_tools.change_connection_in_model_file( 'connection: "snowflake_via_lookerappadmin"', 'looker_high_performance') assert result == 'connection: "looker_high_performance"' def test_edit_model_file(): """Return fully formatted file contents. Replaced access filter, erased access filter join, edited connection. """ model = helpers.model_input filter = model.get('new_access_filter') remove = model.get('remove_access_filter_join') connect = model.get('connection') result = github_tools.edit_model_file( helpers.model_lkml_origin, filter, remove, connect) assert result == helpers.model_lkml_edited def test_check_file_in_repo_positive(fixture_repo): """Return string.""" result = github_tools.check_file_in_repo( fixture_repo, 'name') assert result == 'decoded' def test_test_check_file_in_repo_negative(failing_repo): """Return False.""" result = github_tools.check_file_in_repo(failing_repo, 'wrong_name') assert result is False @patch('github_tools.check_file_in_repo') def test_check_file_in_client_repo(check_file_in_repo): """Calls check_file_in_repo function.""" check_file_in_repo.return_value = True github_tools.check_file_in_client_repo('file_name') check_file_in_repo.assert_called() @patch('github_tools.check_file_in_repo') def test_check_file_in_main_repo(check_file_in_repo): """Calls check_file_in_repo function.""" check_file_in_repo.return_value = True github_tools.check_file_in_main_repo('file_name') check_file_in_repo.assert_called() @patch('github_tools.check_file_in_client_repo') @patch('github_tools.edit_model_file') @patch('github_tools.check_file_in_main_repo') def test_sync_git_model_equal( check_file_in_main_repo, edit_model_file, check_file_in_client_repo): """Return True when client model equals main model.""" check_file_in_main_repo.return_value = True edit_model_file.return_value = True check_file_in_client_repo.return_value = True model = helpers.model_input filter = model.get('new_access_filter') remove = model.get('remove_access_filter_join') connect = model.get('connection') result = github_tools.sync_git_model('this_name', filter, remove, connect) assert result is True @patch('github_tools.update_file_git') @patch('github_tools.check_file_in_client_repo') @patch('github_tools.edit_model_file') @patch('github_tools.check_file_in_main_repo') def test_sync_git_model_update( check_file_in_main_repo, edit_model_file, check_file_in_client_repo, update_file_git): """Return True and updates exiting client model.""" check_file_in_main_repo.return_value = True edit_model_file.return_value = 'this' check_file_in_client_repo.return_value = 'that' update_file_git.return_value = True model = helpers.model_input filter = model.get('new_access_filter') remove = model.get('remove_access_filter_join') connect = model.get('connection') result = github_tools.sync_git_model('this_name', filter, remove, connect) update_file_git.assert_called() assert result is True @patch('github_tools.post_file') @patch('github_tools.check_file_in_client_repo') @patch('github_tools.edit_model_file') @patch('github_tools.check_file_in_main_repo') def test_sync_git_model_post( check_file_in_main_repo, edit_model_file, check_file_in_client_repo, post_file): """Return True and post a model if such doesn't exist on client instance.""" check_file_in_main_repo.return_value = True edit_model_file.return_value = 'this' check_file_in_client_repo.return_value = False post_file.return_value = True model = helpers.model_input filter = model.get('new_access_filter') remove = model.get('remove_access_filter_join') connect = model.get('connection') result = github_tools.sync_git_model('this_name', filter, remove, connect) post_file.assert_called() assert result is True @patch('github_tools.check_file_in_main_repo') def test_sync_git_model_false(check_file_in_main_repo): """Return False if not found model file in main repo.""" check_file_in_main_repo.return_value = False model = helpers.model_input filter = model.get('new_access_filter') remove = model.get('remove_access_filter_join') connect = model.get('connection') result = github_tools.sync_git_model('this_name', filter, remove, connect) assert result is False @patch('github_tools.check_file_in_main_repo') def test_check_for_absent_view_files_positive(check_file_in_main_repo): """Return an empty set, evaluating to False.""" check_file_in_main_repo.return_value = True result = github_tools.check_for_absent_view_files(helpers.view_names) assert bool(result) is False @patch('github_tools.check_file_in_main_repo') def test_check_for_absent_view_files_negative(check_file_in_main_repo): """Return set of missing files (strings).""" check_file_in_main_repo.return_value = False result = github_tools.check_for_absent_view_files(helpers.view_names) assert result == set(helpers.view_names) @patch('github_tools.update_file_git') @patch('github_tools.check_file_in_client_repo') @patch('github_tools.check_file_in_main_repo') def test_sync_view_files_update( check_file_in_main_repo, check_file_in_client_repo, update_file_git): """Assert update is called.""" check_file_in_main_repo.return_value = 'this' check_file_in_client_repo.return_value = 'that' update_file_git.return_value = True github_tools.sync_view_files({'list', 'of', 'things'}) update_file_git.assert_called() @patch('github_tools.post_file') @patch('github_tools.check_file_in_client_repo') @patch('github_tools.check_file_in_main_repo') def test_sync_view_files_post( check_file_in_main_repo, check_file_in_client_repo, post_file): """Assert post is called.""" check_file_in_main_repo.return_value = 'this' check_file_in_client_repo.return_value = False post_file.return_value = True github_tools.sync_view_files({'list', 'of', 'things'}) post_file.assert_called() @patch('github_tools.sync_view_files') @patch('github_tools.check_for_absent_view_files') def test_complete_views_sync_positive( check_for_absent_view_files, sync_view_files): """Return True.""" check_for_absent_view_files.return_value = False sync_view_files.return_value = True result = github_tools.complete_views_sync(helpers.view_names) assert result is True @patch('github_tools.check_for_absent_view_files') def test_complete_views_sync_negative(check_for_absent_view_files): """Return False.""" check_for_absent_view_files.return_value = { 'absent.view.lkml', 'not_in_here.view.lkml'} result = github_tools.complete_views_sync({'sets', 'files'}) assert result is False