Skip to content

Commit 50ebd4f

Browse files
author
richard
committed
basic unit testing
1 parent 574169c commit 50ebd4f

File tree

4 files changed

+127
-4
lines changed

4 files changed

+127
-4
lines changed

tests.py renamed to examples.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def test():
3535
print(files[file])
3636

3737
# upload file
38-
print(m.upload('tests.py'))
38+
print(m.upload('examples.py'))
3939

4040
# search for a file in account
41-
file = m.find('tests.py')
41+
file = m.find('examples.py')
4242

4343
if file:
4444
# get public link

mega/mega.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def api_request(self, data):
8484
data = [data]
8585

8686
req = requests.post(
87-
'{0}://g.api.{1}/cs'.format(self.schema, self.domain),
87+
'{0}://eu.api.{1}/cs'.format(self.schema, self.domain),
8888
params=params,
8989
data=json.dumps(data),
9090
timeout=self.timeout)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_package_data(package):
3030

3131
setup(
3232
name='mega.py',
33-
version='0.9.14',
33+
version='0.9.15',
3434
packages=get_packages('mega'),
3535
package_data=get_package_data('mega'),
3636
description='Python lib for the Mega.co.nz API',

tests/unit-tests.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""
2+
These unit tests will upload a test file,a test folder and a test contact,
3+
Perform api operations on them,
4+
And them remove them from your account.
5+
"""
6+
from mega import Mega
7+
import unittest
8+
import random
9+
import os
10+
11+
12+
password = 'password'
13+
14+
mega = Mega()
15+
m = mega.login(email, password)
16+
17+
FIND_RESP = None
18+
TEST_CONTACT = '[email protected]'
19+
TEST_PUBLIC_URL = 'https://mega.co.nz/#!EYI2VagT!Ic1yblki8oM4v6XHquCe4gu84kxc4glFchj8OvcT5lw'
20+
TEST_FILE = os.path.basename(__file__)
21+
TEST_FOLDER = 'mega.py_testfolder_{0}'.format(random.random())
22+
23+
24+
def pre_test():
25+
m.upload(TEST_FILE)
26+
# cached response to lower API requests for testing
27+
FIND_RESP = m.find(TEST_FILE)
28+
if FIND_RESP:
29+
return True
30+
else:
31+
raise ValueError('Pre-test functions failed!')
32+
33+
34+
class TestMega(unittest.TestCase):
35+
36+
def test_mega(self):
37+
self.assertTrue(isinstance(mega, Mega))
38+
39+
def test_login(self):
40+
self.assertTrue(isinstance(mega, Mega))
41+
42+
def test_get_user(self):
43+
resp = m.get_user()
44+
self.assertTrue(isinstance(resp, dict))
45+
46+
def test_get_quota(self):
47+
resp = m.get_quota()
48+
self.assertTrue(isinstance(resp, long))
49+
50+
def test_get_storage_space(self):
51+
resp = m.get_storage_space(mega=True)
52+
self.assertTrue(isinstance(resp, dict))
53+
54+
def test_get_files(self):
55+
files = m.get_files()
56+
self.assertTrue(isinstance(files, dict))
57+
58+
def test_find(self):
59+
file = FIND_RESP
60+
if file:
61+
self.assertTrue(isinstance(file, tuple), msg='this is {0} end'.format(file))
62+
63+
def test_get_link(self):
64+
file = FIND_RESP
65+
if file:
66+
link = m.get_link(file)
67+
self.assertTrue(isinstance(link, str))
68+
69+
def test_import_public_url(self):
70+
resp = m.import_public_url(TEST_PUBLIC_URL)
71+
file_handle = m.get_id_from_obj(resp)
72+
resp = m.destroy(file_handle)
73+
self.assertTrue(isinstance(resp, int))
74+
75+
def test_create_folder(self):
76+
resp = m.create_folder(TEST_FOLDER)
77+
self.assertTrue(isinstance(resp, dict))
78+
79+
def test_rename(self):
80+
file = m.find(TEST_FOLDER)
81+
if file:
82+
resp = m.rename(file, TEST_FOLDER)
83+
self.assertTrue(isinstance(resp, int))
84+
85+
def test_delete_folder(self):
86+
folder_node = m.find(TEST_FOLDER)[0]
87+
resp = m.delete(folder_node)
88+
self.assertTrue(isinstance(resp, int))
89+
90+
def test_delete(self):
91+
file = m.find(TEST_FILE)
92+
if file:
93+
resp = m.delete(file[0])
94+
self.assertTrue(isinstance(resp, int))
95+
else:
96+
raise ValueError('file not found')
97+
98+
def test_destroy(self):
99+
file = m.find(TEST_FILE)
100+
if file:
101+
resp = m.destroy(file[0])
102+
self.assertTrue(isinstance(resp, int))
103+
else:
104+
raise ValueError('file not found')
105+
106+
def test_empty_trash(self):
107+
#resp None if already empty, else int
108+
resp = m.empty_trash()
109+
if resp is not None:
110+
self.assertTrue(isinstance(resp, int))
111+
112+
def test_add_contact(self):
113+
resp = m.add_contact(TEST_CONTACT)
114+
self.assertTrue(isinstance(resp, int))
115+
116+
def test_remove_contact(self):
117+
resp = m.remove_contact(TEST_CONTACT)
118+
self.assertTrue(isinstance(resp, int))
119+
120+
121+
if __name__ == '__main__':
122+
if pre_test():
123+
unittest.main()

0 commit comments

Comments
 (0)