basic unit testing

This commit is contained in:
richard 2013-05-16 00:44:32 +01:00
parent 574169c8b6
commit 50ebd4f1c1
4 changed files with 127 additions and 4 deletions

View file

@ -35,10 +35,10 @@ def test():
print(files[file]) print(files[file])
# upload file # upload file
print(m.upload('tests.py')) print(m.upload('examples.py'))
# search for a file in account # search for a file in account
file = m.find('tests.py') file = m.find('examples.py')
if file: if file:
# get public link # get public link

View file

@ -84,7 +84,7 @@ class Mega(object):
data = [data] data = [data]
req = requests.post( req = requests.post(
'{0}://g.api.{1}/cs'.format(self.schema, self.domain), '{0}://eu.api.{1}/cs'.format(self.schema, self.domain),
params=params, params=params,
data=json.dumps(data), data=json.dumps(data),
timeout=self.timeout) timeout=self.timeout)

View file

@ -30,7 +30,7 @@ def get_package_data(package):
setup( setup(
name='mega.py', name='mega.py',
version='0.9.14', version='0.9.15',
packages=get_packages('mega'), packages=get_packages('mega'),
package_data=get_package_data('mega'), package_data=get_package_data('mega'),
description='Python lib for the Mega.co.nz API', description='Python lib for the Mega.co.nz API',

123
tests/unit-tests.py Normal file
View file

@ -0,0 +1,123 @@
"""
These unit tests will upload a test file,a test folder and a test contact,
Perform api operations on them,
And them remove them from your account.
"""
from mega import Mega
import unittest
import random
import os
email = 'your@email.com'
password = 'password'
mega = Mega()
m = mega.login(email, password)
FIND_RESP = None
TEST_CONTACT = 'test@mega.co.nz'
TEST_PUBLIC_URL = 'https://mega.co.nz/#!EYI2VagT!Ic1yblki8oM4v6XHquCe4gu84kxc4glFchj8OvcT5lw'
TEST_FILE = os.path.basename(__file__)
TEST_FOLDER = 'mega.py_testfolder_{0}'.format(random.random())
def pre_test():
m.upload(TEST_FILE)
# cached response to lower API requests for testing
FIND_RESP = m.find(TEST_FILE)
if FIND_RESP:
return True
else:
raise ValueError('Pre-test functions failed!')
class TestMega(unittest.TestCase):
def test_mega(self):
self.assertTrue(isinstance(mega, Mega))
def test_login(self):
self.assertTrue(isinstance(mega, Mega))
def test_get_user(self):
resp = m.get_user()
self.assertTrue(isinstance(resp, dict))
def test_get_quota(self):
resp = m.get_quota()
self.assertTrue(isinstance(resp, long))
def test_get_storage_space(self):
resp = m.get_storage_space(mega=True)
self.assertTrue(isinstance(resp, dict))
def test_get_files(self):
files = m.get_files()
self.assertTrue(isinstance(files, dict))
def test_find(self):
file = FIND_RESP
if file:
self.assertTrue(isinstance(file, tuple), msg='this is {0} end'.format(file))
def test_get_link(self):
file = FIND_RESP
if file:
link = m.get_link(file)
self.assertTrue(isinstance(link, str))
def test_import_public_url(self):
resp = m.import_public_url(TEST_PUBLIC_URL)
file_handle = m.get_id_from_obj(resp)
resp = m.destroy(file_handle)
self.assertTrue(isinstance(resp, int))
def test_create_folder(self):
resp = m.create_folder(TEST_FOLDER)
self.assertTrue(isinstance(resp, dict))
def test_rename(self):
file = m.find(TEST_FOLDER)
if file:
resp = m.rename(file, TEST_FOLDER)
self.assertTrue(isinstance(resp, int))
def test_delete_folder(self):
folder_node = m.find(TEST_FOLDER)[0]
resp = m.delete(folder_node)
self.assertTrue(isinstance(resp, int))
def test_delete(self):
file = m.find(TEST_FILE)
if file:
resp = m.delete(file[0])
self.assertTrue(isinstance(resp, int))
else:
raise ValueError('file not found')
def test_destroy(self):
file = m.find(TEST_FILE)
if file:
resp = m.destroy(file[0])
self.assertTrue(isinstance(resp, int))
else:
raise ValueError('file not found')
def test_empty_trash(self):
#resp None if already empty, else int
resp = m.empty_trash()
if resp is not None:
self.assertTrue(isinstance(resp, int))
def test_add_contact(self):
resp = m.add_contact(TEST_CONTACT)
self.assertTrue(isinstance(resp, int))
def test_remove_contact(self):
resp = m.remove_contact(TEST_CONTACT)
self.assertTrue(isinstance(resp, int))
if __name__ == '__main__':
if pre_test():
unittest.main()