def test_main_if_check_args(self): """Does it exit with 1 if check_args fails?""" sys.argv[1:] = [1, 2, 3, 4] with self.assertRaises(SystemExit) as ctx: main() self.assertEqual(1, ctx.exception.code)
def test_main_credential_file_missing(self, mock_check_args, mock_get_file_contents): """Does it exit with 2 if get_file_contents raises error?""" sys.argv[1:] = ["", "a_file", "another_file"] with self.assertRaises(SystemExit) as ctx: main() self.assertEqual(2, ctx.exception.code) mock_check_args.assert_called_once() mock_get_file_contents.assert_called_once()
def test_main_extract_call(self, mock_json_loads, mock_extract, mock_check_args, mock_get_file_contents): """Is extract called with expected arguments?""" sys.argv[1:] = ["arg_1_val", "", "", "arg_4_val", "arg_5_val"] main() mock_extract.assert_called_once_with("arg_1_val", "first_json_loads_string", "second_json_loads_string", "arg_4_val", "arg_5_val")
def test_main_get_file_contents_calls(self, mock_get_file_contents, mock_extract, mock_check_args, mock_json_loads): """Is get_file_contents called twice with different arg each time?""" credentials_file = "a_file" source_cfg_file = "another_file" sys.argv[1:] = ["", credentials_file, source_cfg_file, "", ""] main() expected_calls = [call(credentials_file), call(source_cfg_file)] mock_get_file_contents.assert_has_calls(expected_calls)
def test_main_json_loads_calls(self, mock_json_loads, mock_extract, mock_check_args, mock_get_file_contents): """Is json_loads called twice with different arg each time?""" sys.argv[1:] = ["", "", "", "", ""] main() expected_calls = [ call("first_call_return_string"), call("second_call_return_string") ] mock_json_loads.assert_has_calls(expected_calls)
def test_postgres_extract(self): """Does extract from postgres create expected output files?""" source_type = "postgres" credentials = "pg_credentials.json" config_file = "job_1001.json" extract_folder = "~/misc/extract_out" extract_filename = "job_1001_out" sys.argv[1:] = [ source_type, credentials, config_file, extract_folder, extract_filename ] main() expected_str_customers = '"John","Smith",30301\n"Kim","Hunter",30302\n' test_file_1 = "~/misc/extract_out/job_1001_out.csv" self.assertEqual(expected_str_customers, get_file_contents(os.path.expanduser(test_file_1))) os.remove(os.path.expanduser(test_file_1))
def test_sendgrid_extract(self): """Does extract from sendgrid create expected output files?""" source_type = "sendgrid" credentials = "sendgrid_credentials.json" config_file = "sendgrid_config.json" extract_folder = "~/misc/extract_out" extract_filename = "sendgrid_out" sys.argv[1:] = [ source_type, credentials, config_file, extract_folder, extract_filename ] main() expected_str = ("\"2018-07-01\",0,0,0\n" "\"2018-07-02\",0,0,0\n" "\"2018-07-03\",0,0,0\n" "\"2018-07-04\",0,0,0\n" "\"2018-07-05\",0,0,0\n" "\"2018-07-06\",0,0,0\n" "\"2018-07-07\",0,0,0\n" "\"2018-07-08\",0,0,0\n" "\"2018-07-09\",0,0,0\n" "\"2018-07-10\",0,0,0\n" "\"2018-07-11\",0,0,0\n" "\"2018-07-12\",0,0,0\n" "\"2018-07-13\",0,0,0\n" "\"2018-07-14\",0,0,0\n" "\"2018-07-15\",0,0,0\n" "\"2018-07-16\",0,0,0\n" "\"2018-07-17\",0,0,0\n" "\"2018-07-18\",0,0,0\n" "\"2018-07-19\",0,0,0\n" "\"2018-07-20\",0,0,0\n" "\"2018-07-21\",0,0,0\n" "\"2018-07-22\",0,0,0\n" "\"2018-07-23\",0,0,0\n" "\"2018-07-24\",0,0,0\n" "\"2018-07-25\",0,0,0\n" "\"2018-07-26\",0,0,0\n" "\"2018-07-27\",0,0,0\n" "\"2018-07-28\",0,0,0\n" "\"2018-07-29\",0,0,0\n" "\"2018-07-30\",3,1,1\n" "\"2018-07-31\",0,0,0\n" "\"2018-08-01\",0,0,0\n" "\"2018-08-02\",0,0,0\n" "\"2018-08-03\",0,0,0\n" "\"2018-08-04\",0,0,0\n" "\"2018-08-05\",0,0,0\n" "\"2018-08-06\",0,0,0\n" "\"2018-08-07\",0,0,0\n" "\"2018-08-08\",0,0,0\n" "\"2018-08-09\",0,0,0\n" "\"2018-08-10\",0,0,0\n" "\"2018-08-11\",0,0,0\n" "\"2018-08-12\",0,0,0\n" "\"2018-08-13\",0,0,0\n" "\"2018-08-14\",0,0,0\n" "\"2018-08-15\",0,0,0\n" "\"2018-08-16\",0,0,0\n" "\"2018-08-17\",15,15,11\n" "\"2018-08-18\",0,0,0\n" "\"2018-08-19\",0,0,4\n" "\"2018-08-20\",0,0,0\n" "\"2018-08-21\",0,0,0\n") test_file_1 = "~/misc/extract_out/sendgrid_out.csv" self.assertEqual(expected_str, get_file_contents(os.path.expanduser(test_file_1))) os.remove(os.path.expanduser(test_file_1))