def test_pattern_fail_load(self): """ Invalid patterns should fail """ settings.put('output.file_name_pattern', '[type]-[id]-[title]-[author-[subreddit]-[source_alias]') tp = self.sess.query(sql.Post).filter(sql.Post.title == 'test').first() with self.assertRaises(Exception, msg='Failed to catch broken pattern!'): ng.choose_file_name(tp.urls[0], tp, sql.session(), album_size=1)
def test_existing_file_name(self): """ Generating an incremented file name should work """ tp = self.sess.query( sql.Post).filter(sql.Post.reddit_id == 't3_ahal9v').first() file = ng.choose_file_name(tp.urls[0], tp, sql.session(), album_size=1) self.assertTrue(file.endswith(' - 2'), msg='Failed to increment duplicate post!')
def test_choose_file_name(self): """ Generating a new file name should work """ tp = self.sess.query(sql.Post).filter(sql.Post.title == 'test').first() file = ng.choose_file_name(tp.urls[0], tp, sql.session(), album_size=1) self.assertEqual('aww/test - (testuser)', file, msg='Failed to convert basic Test post!')
def test_album_filename(self): """ Generating new & incremented album names should work """ tp = self.sess.query(sql.Post).join( sql.URL).filter(sql.Post.reddit_id == 't3_98crc8').first() file = ng.choose_file_name(tp.urls[0], tp, sql.session(), album_size=1000) self.assertEqual('aww/album - (testuser2)/0001', file, msg='Failed to generate new Album foldername!') np = self.sess.query(sql.Post).join( sql.URL).filter(sql.Post.reddit_id == 't3_awyf90').first() file = ng.choose_file_name(np.urls[0], np, sql.session(), album_size=1) self.assertEqual('aww/album - (testuser2) - 2/1', file, msg='Failed to create separate album folder!')
def _create_url_file(self, url, post, album_size=1): """ Builds the desired sql.File object for the given sql.URL Object. Automatically adds the File object to the URL. """ filename = name_generator.choose_file_name(url=url, post=post, session=self._session, album_size=album_size) file = sql.File(path=filename) self._session.add(file) url.file = file
def create_url_file(self, sql_url, sql_post, album_size, downloaded=True): """ Creates a SQL File object for the given SQL URL & Post. """ if sql_url.file: return sql_url.file filename = name_generator.choose_file_name(url=sql_url, post=sql_post, session=self.session, album_size=album_size) file = sql.File( path=filename ) self.session.add(file) sql_url.file = file file.downloaded = downloaded return file
def test_pattern_loader(self): """ All pattern tags should work """ settings.put( 'output.file_name_pattern', '[type]-[reddit_id]-[title]-[author]-[subreddit]-[source_alias]-[created_utc]-[created_date]-[created_time]' ) tp = self.sess.query(sql.Post).filter(sql.Post.title == 'test').first() file = ng.choose_file_name(tp.urls[0], tp, sql.session(), album_size=1) self.assertRegex( file, r'Submission-t3_b1rycu-test-testuser-aww-newsource-1552739416-2019-..-..-..\...\...', msg='Failed to convert basic Test post!')