def test_with_no_result(self, authenticated, query_api): api = MockAPI() authenticated.return_value = False api = MockAPI(config["file"], 0) query_api.return_value = api.get_json() gist = Gist(config["user"], config["file"]) filtered = [g for g in gist.filter_gists()] self.assertEqual(len(filtered), 0)
def test_select_file_with_no_option(self, authenticated, query_api): api = MockAPI() authenticated.return_value = False api = MockAPI(config["file"], 0) query_api.return_value = api.get_json() gist = Gist(config["user"], config["file"]) selected = gist.select_file([g for g in api.get_results()]) self.assertFalse(selected)
def test_select_file_with_two_options(self, authenticated, query_api, ask): api = MockAPI(config["file"], 2) authenticated.return_value = False query_api.return_value = api.get_json() ask.return_value = "2" gist = Gist(config["user"], config["file"]) selected = gist.select_file([g for g in api.get_results()]) self.assertEqual(selected["id"], "2") self.assertEqual(selected["raw_url"], "URL #2") self.assertEqual(selected["description"], "Gist #2")
def test_with_sample_gists(self, authenticated, query_api): authenticated.return_value = False query_api.return_value = config["gists"] gist = Gist(config["user"], config["file"]) filtered = [g for g in gist.filter_gists()] hashed_url = md5(filtered[0]["raw_url"].encode("utf-8")).hexdigest() self.assertEqual(len(filtered), 1) self.assertEqual(filtered[0]["id"], "409fac6ac23bf515f495") self.assertEqual(filtered[0]["description"], config["file"]) self.assertEqual(hashed_url, "847fe81c7fdc3b6bd7184379fcd42773")
def test_select_file_with_two_options(self, authenticated, filter_g, ask): authenticated.return_value = False two_options = [ {"id": 12345, "description": "Gist #1", "raw_url": "URL #1"}, {"id": 67890, "description": "Gist #2", "raw_url": "URL #2"}, ] filter_g.return_value = two_options ask.return_value = "2" gist = Gist(config["user"], config["file"]) self.assertEqual(gist.load_gist_info(), two_options[1])
def test_select_file_with_one_option(self, authenticated, query_api): api = MockAPI() authenticated.return_value = False api = MockAPI(config["file"]) query_api.return_value = api.get_json() results = [g for g in api.get_results()] gist = Gist(config["user"], config["file"]) selected = gist.select_file(results) self.assertEqual(selected["id"], "1") self.assertEqual(selected["raw_url"], "URL #1") self.assertEqual(selected["description"], "Gist #1")
def test_with_two_results(self, authenticated, query_api, ask): api = MockAPI() authenticated.return_value = False api = MockAPI(config["file"], 2) query_api.return_value = api.get_json() ask.return_value = 1 gist = Gist(config["user"], config["file"]) filtered = [g for g in gist.filter_gists()] self.assertEqual(len(filtered), 2, api.get_json()) self.assertEqual(filtered[0]["id"], "1") self.assertEqual(filtered[0]["description"], "Gist #1") self.assertEqual(filtered[0]["raw_url"], "URL #1") self.assertEqual(filtered[1]["id"], "2") self.assertEqual(filtered[1]["description"], "Gist #2") self.assertEqual(filtered[1]["raw_url"], "URL #2")
def test_select_file_with_no_option(self, authenticated, filter_gists): authenticated.return_value = False filter_gists.return_value = list() gist = Gist(config["user"], config["file"]) self.assertFalse(gist.load_gist_info())
def test_select_file_with_one_option(self, authenticated, filter_gists): authenticated.return_value = False one_option = [{"id": 12345, "description": "Gist #1", "raw_url": "URL #1"}] filter_gists.return_value = one_option gist = Gist(config["user"], config["file"]) self.assertEqual(gist.load_gist_info(), one_option[0])
def setUp(self, authenticated, query_api, curl): authenticated.return_value = False query_api.return_value = config['gists'] curl.return_value = config['content'] self.gist = Gist(config['user'], config['file'])
class TestDownload(TestCase): @patch('getgist.__main__.Gist.curl') @patch('getgist.__main__.Gist.query_api') @patch('getgist.__main__.Gist.authenticated') def setUp(self, authenticated, query_api, curl): authenticated.return_value = False query_api.return_value = config['gists'] curl.return_value = config['content'] self.gist = Gist(config['user'], config['file']) def tearDown(self): for name in os.listdir(self.gist.local_dir): if search(r'^({})(\.bkp(.\d)?)?$'.format(config['file']), name): os.remove(os.path.join(self.gist.local_dir, name)) @patch('getgist.__main__.Gist.ask') def test_local_dir(self, mocked_ask): mocked_ask.return_value = 'y' self.assertTrue(os.path.exists(self.gist.local_dir)) self.assertTrue(os.path.isdir(self.gist.local_dir)) @patch('getgist.__main__.Gist.ask') @patch('getgist.__main__.Gist.curl') def test_download(self, curl, ask): ask.return_value = 'y' curl.return_value = config['content'] self.gist.save() self.assertTrue(os.path.exists(self.gist.local_path)) self.assertEqual(open(self.gist.local_path).read(), config['content']) @patch('getgist.__main__.Gist.ask') @patch('getgist.__main__.Gist.curl') def test_backup(self, curl, ask): # mock input value ask.return_value = 'n' curl.return_value = config['content'] # backup filenames backup1 = '{}.bkp'.format(self.gist.local_path) backup2 = '{}.bkp1'.format(self.gist.local_path) backup3 = '{}.bkp2'.format(self.gist.local_path) # 1st download self.gist.save() self.assertTrue(os.path.exists(self.gist.local_path)) self.assertEqual(config['content'], open(self.gist.local_path).read()) # 2nd download self.gist.save() self.assertTrue(os.path.exists(backup1)) self.assertEqual(config['content'], open(backup1).read()) # 3rd download self.gist.save() self.assertTrue(os.path.exists(backup2)) self.assertEqual(config['content'], open(backup2).read()) # 4th download self.gist.save() self.assertTrue(os.path.exists(backup3)) self.assertEqual(config['content'], open(backup3).read())