Skip to content

Commit

Permalink
Merge pull request #122 from JVT038/add-tests
Browse files Browse the repository at this point in the history
Add tests for database related ops
  • Loading branch information
JVT038 authored Jan 29, 2024
2 parents fa56340 + 3de5151 commit 14c9b11
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 13 deletions.
3 changes: 2 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ class Config(object):
META_EXTENSIONS = ['MP3', 'OPUS', 'FLAC', 'OGG', 'MP4', 'M4A', 'WAV']
VIDEO_EXTENSIONS = ['MP4', 'M4A', 'FLV', 'WEBM', 'OGG', 'MKV', 'AVI']
AUDIO_EXTENSIONS = ['AAC', 'FLAC', 'MP3', 'M4A', 'OPUS', 'VORBIS', 'WAV']
INIT_DB = os.environ.get('INIT_DB', True)
INIT_DB = os.environ.get('INIT_DB', True)
TESTING = False
18 changes: 10 additions & 8 deletions metatube/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ def ffmpeg(self, ffmpeg_path):

@staticmethod
def get_ffmpeg():
return Config.query.get(1).ffmpeg_directory
# return db.session.get(Config, 1).ffmpeg_directory
return db.session.get(Config, 1).ffmpeg_directory# type: ignore

@staticmethod
def get_hwt():
return Config.query.get(1).hardware_transcoding
return db.session.get(Config, 1).hardware_transcoding # type: ignore

def set_amount(self, amount):
self.amount = int(amount)
Expand Down Expand Up @@ -54,19 +55,19 @@ def set_hwtranscoding(self, hw_transcoding):

@staticmethod
def get_metadata_sources():
return Config.query.get(1).metadata_sources
return db.session.get(Config, 1).metadata_sources # type: ignore

@staticmethod
def get_spotify():
return Config.query.get(1).spotify_api
return db.session.get(Config, 1).spotify_api # type: ignore

@staticmethod
def get_genius():
return Config.query.get(1).genius_api
return db.session.get(Config, 1).genius_api # type: ignore

@staticmethod
def get_max():
return Config.query.get(1).amount
return db.session.get(Config, 1).amount # type: ignore

class Templates(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=True)
Expand Down Expand Up @@ -132,7 +133,7 @@ def delete(self):
def searchdefault():
return Templates.query.filter_by(default = True).first()

def setdefault(self, defaulttemplate):
def setdefault(self, defaulttemplate = None):
self.default = True
if defaulttemplate is not None:
defaulttemplate.default = False
Expand Down Expand Up @@ -241,4 +242,5 @@ def updatefilepath(self, filepath):

def delete(self):
db.session.delete(self)
db.session.commit()
db.session.commit()
logger.info('Deleted item %s', self.name)
2 changes: 1 addition & 1 deletion metatube/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ffmpeg():
def __init__(self):
ffmpeg_path = Config.query.get(1).ffmpeg_directory
ffmpeg_path = Config.get_ffmpeg()
if len(ffmpeg_path) > 0:
if (ffmpeg_path.startswith('/') or (ffmpeg_path[0].isalpha() and ffmpeg_path[1].startswith(':\\'))) and len(ffmpeg_path) > 0:
self.ffmpeg_path = ffmpeg_path
Expand Down
9 changes: 6 additions & 3 deletions metatube/init/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
class Default():
def __init__(self, app, url):
self._app = app
app.app_context().push()
if app.config['TESTING'] is False:
app.app_context().push()
self._url = url
self._methods = ['config', 'templates']
self._ffmpeg = app.config["FFMPEG"]
Expand All @@ -25,10 +26,12 @@ def config(self):
amount = 5,
hardware_transcoding = "None",
metadata_sources='deezer',
spotify_api = 'None',
genius_api = 'None',
auth = False,
auth_username = "",
auth_password = ""
)
) # type: ignore
db.session.add(config)
db.session.commit()
logger.info('Created default rows for the configuration table')
Expand All @@ -54,7 +57,7 @@ def templates(self):
proxy_password = "",
proxy_address = "",
proxy_port = ""
)
) # type: ignore
db.session.add(template)
db.session.commit()
logger.info('Created default rows for the Templates table')
Expand Down
237 changes: 237 additions & 0 deletions tests/testDatabase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
from email.policy import default
from config import Config
from metatube.init import Default
from metatube import create_app, db
from metatube.database import Config as env
from metatube.database import Templates, Database
from datetime import datetime
import os
import unittest

basedir = os.path.abspath(os.path.dirname(__file__))

class TestConfig(Config):
TESTING = True
FFMPEG = 'bin'
META_EXTENSIONS = ['MP3', 'OPUS', 'FLAC', 'OGG', 'MP4', 'M4A', 'WAV']
VIDEO_EXTENSIONS = ['MP4', 'M4A', 'FLV', 'WEBM', 'OGG', 'MKV', 'AVI']
AUDIO_EXTENSIONS = ['AAC', 'FLAC', 'MP3', 'M4A', 'OPUS', 'VORBIS', 'WAV']
DOWNLOADS = '/path/to/downloads'
LOGGER = False
LOG_LEVEL = 40
INIT_DB = False
# SQLALCHEMY_DATABASE_URI = os.path.join('sqlite:///', basedir, 'metatube/test.db')
# SQLALCHEMY_DATABASE_URI = 'sqlite://'

class TestAutoMigration(unittest.TestCase):
def setUp(self):
self.app = create_app(TestConfig)
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()

def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()

def testAutoCreation(self):
db.drop_all()
relative_url = 'sqlite:///' + os.path.join('metatube', Config.SQLALCHEMY_DATABASE_URI.replace('sqlite:///', ''))
default = Default(self.app, relative_url)
self.assertTrue(default.init_db())

def testConfigTable(self):
config = env(
ffmpeg_directory = self.app.config['FFMPEG'],
amount = 5,
hardware_transcoding = "None",
metadata_sources='deezer',
spotify_api = 'None',
genius_api = 'None',
auth = False,
auth_username = "",
auth_password = ""
) # type: ignore
db.session.add(config)
db.session.commit()

self.assertEqual(config.get_ffmpeg(), self.app.config['FFMPEG'])
self.assertEqual(config.get_hwt(), 'None')
self.assertEqual(config.get_metadata_sources(), 'deezer')
self.assertEqual(config.get_spotify(), 'None')
self.assertEqual(config.get_genius(), 'None')
self.assertEqual(config.get_max(), 5)
self.assertEqual(config.query.count(), 1)

config.set_amount(10)
config.set_spotify('client; secret')
config.set_genius('api_key')
config.set_metadata('source')
config.set_hwtranscoding('VAAPI')

self.assertEqual(config.get_metadata_sources(), 'source')
self.assertEqual(config.get_spotify(), 'client; secret')
self.assertEqual(config.get_genius(), 'api_key')
self.assertEqual(config.get_max(), 10)
self.assertEqual(config.get_hwt(), 'VAAPI')

def testTemplatesTable(self):
defaultTemplate = Templates( # type: ignore
id = 0,
name = 'Default',
type = 'Audio',
extension = 'mp3',
output_folder = self.app.config['DOWNLOADS'],
output_name = f"%(title)s.%(ext)s",
bitrate = 'best',
resolution = 'best;best',
default = True,
proxy_status = False
)
db.session.add(defaultTemplate)
db.session.commit()

self.assertEqual(Templates.counttemplates(), 1)

self.assertTrue(Templates.check_existing('Default'))
self.assertFalse(Templates.check_existing('asdf'))

self.assertIsInstance(defaultTemplate, Templates)
self.assertEqual(defaultTemplate.id, 0) # type: ignore
self.assertEqual(defaultTemplate.name, 'Default') # type: ignore
self.assertEqual(defaultTemplate.type, 'Audio') # type: ignore
self.assertEqual(defaultTemplate.extension, 'mp3') # type: ignore
self.assertEqual(defaultTemplate.output_folder, self.app.config['DOWNLOADS']) # type: ignore
self.assertEqual(defaultTemplate.output_name, f"%(title)s.%(ext)s") # type: ignore
self.assertEqual(defaultTemplate.bitrate, 'best') # type: ignore
self.assertEqual(defaultTemplate.resolution, 'best;best') # type: ignore
self.assertEqual(defaultTemplate.default, True) # type: ignore
self.assertEqual(defaultTemplate.proxy_status, False) # type: ignore
self.assertIsNone(defaultTemplate.proxy_type) # type: ignore
self.assertIsNone(defaultTemplate.proxy_username) # type: ignore
self.assertIsNone(defaultTemplate.proxy_password) # type: ignore
self.assertIsNone(defaultTemplate.proxy_address) # type: ignore
self.assertIsNone(defaultTemplate.proxy_port) # type: ignore

newTemplateId = Templates.add({
"name": "test",
"type": "test",
"ext": "mp4",
"output_folder": self.app.config["DOWNLOADS"],
"output_name": "%(title)s - %(artist)s.%(ext)s",
"bitrate": "best",
"resolution": "best;best",
"proxy": {
"status": True,
"type": "socks5",
"username": "proxy_username",
"password": "proxy_password",
"address": "http://localhost",
"port": "1000"
}
})
newTemplate = Templates.fetchtemplate(newTemplateId)
newTemplate.setdefault(defaultTemplate) # type: ignore
self.assertEqual(newTemplateId, 1)
self.assertEqual(Templates.fetchalltemplates(), [defaultTemplate, newTemplate])
self.assertEqual(Templates.counttemplates(), 2)

newTemplate.delete() # type: ignore
self.assertEqual(Templates.counttemplates(), 1)

defaultTemplate.edit({ # type: ignore
"name": "test",
"type": "test",
"ext": "mp4",
"output_folder": "random/download/path",
"output_name": "%(title)s - %(artist)s.%(ext)s",
"bitrate": "worst",
"resolution": "worst;worst",
"proxy": {
"status": True,
"type": "socks5",
"username": "proxy_username",
"password": "proxy_password",
"address": "http://localhost",
"port": "1000"
}
})

self.assertEqual(defaultTemplate.id, 0) # type: ignore
self.assertEqual(defaultTemplate.name, 'test') # type: ignore
self.assertEqual(defaultTemplate.type, 'test') # type: ignore
self.assertEqual(defaultTemplate.extension, 'mp4') # type: ignore
self.assertEqual(defaultTemplate.output_folder, 'random/download/path') # type: ignore
self.assertEqual(defaultTemplate.output_name, "%(title)s - %(artist)s.%(ext)s") # type: ignore
self.assertEqual(defaultTemplate.bitrate, 'worst') # type: ignore
self.assertEqual(defaultTemplate.resolution, 'worst;worst') # type: ignore
self.assertEqual(defaultTemplate.default, False) # type: ignore
self.assertEqual(defaultTemplate.proxy_status, True) # type: ignore
self.assertEqual(defaultTemplate.proxy_type, 'socks5') # type: ignore
self.assertEqual(defaultTemplate.proxy_username, 'proxy_username') # type: ignore
self.assertEqual(defaultTemplate.proxy_password, 'proxy_password') # type: ignore
self.assertEqual(defaultTemplate.proxy_address, 'http://localhost') # type: ignore
self.assertEqual(defaultTemplate.proxy_port, 1000) # type: ignore

def testDatabase(self):
self.assertEqual(Database.getrecords(), [])

itemId = Database.insert({
'filepath': os.path.join(self.app.config['DOWNLOADS'], '/file.mp3'),
'name': 'Never Gonna Give You Up',
'artist': ['Rick Astley'],
'album': 'Whenever You Need Somebody',
'date': '12-11-1987',
'image': 'https://i.scdn.co/image/ab67616d0000b273baf89eb11ec7c657805d2da0',
'track_id': '4cOdK2wGLETKBW3PvgPWqT',
'ytid': 'dQw4w9WgXcQ'
})
item = Database.fetchitem(itemId)

self.assertIsInstance(item, Database)
self.assertEqual(Database.getrecords(), [item])
self.assertEqual(Database.searchrecords('never'), [item])
self.assertEqual(Database.itemtodict(item), {
'id': '1',
'filepath': os.path.join(self.app.config['DOWNLOADS'], '/file.mp3'),
'name': 'Never Gonna Give You Up',
'artist': 'Rick Astley',
'album': 'Whenever You Need Somebody',
'date': '1987-12-11 00:00:00',
'length': 'None',
'cover': 'https://i.scdn.co/image/ab67616d0000b273baf89eb11ec7c657805d2da0',
'audio_id': '4cOdK2wGLETKBW3PvgPWqT',
'youtube_id': 'dQw4w9WgXcQ'
})
self.assertIs(Database.checkfile(item.filepath), item) # type: ignore
self.assertIs(Database.checkyt(item.youtube_id), item) # type: ignore
self.assertIs(Database.checktrackid(item.audio_id), item) # type: ignore

item.update({ # type: ignore
'filepath': os.path.join(self.app.config['DOWNLOADS'], '/test.mp3'),
'name': 'Cool song name',
'artist': 'Famous artist',
'album': 'Some album',
'date': datetime.now().date(),
'image': '/path/to/cover.png',
'track_id': 'someid',
'youtube_id': 'y6120QOlsfU',
'length': None
})

self.assertEqual(Database.itemtodict(item), {
'id': '1',
'filepath': os.path.join(self.app.config['DOWNLOADS'], '/test.mp3'),
'name': 'Cool song name',
'artist': 'Famous artist',
'album': 'Some album',
'date': datetime.now().strftime("%Y-%m-%d 00:00:00"),
'length': 'None',
'cover': '/path/to/cover.png',
'audio_id': 'someid',
'youtube_id': 'y6120QOlsfU'
})

if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 14c9b11

Please sign in to comment.