Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
[tests] Some unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Xiangyu Bu <[email protected]>
  • Loading branch information
xybu committed Jan 16, 2017
1 parent 94ee573 commit 869fe7b
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 8 deletions.
2 changes: 1 addition & 1 deletion onedrived/models/pretty_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def pretty_print_bytes(size, precision=2):
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
index = 0
while size > 1024:
while size >= 1024:
index += 1 # increment the index of the suffix
size /= 1024.0 # apply the division
return "%.*f %s" % (precision, size, suffixes[index])
Expand Down
3 changes: 2 additions & 1 deletion onedrived/od_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def has_pending_task(self, local_path):
return local_path in self.tasks_by_path

def remove_children_tasks(self, local_parent_path):
p = local_parent_path + '/'
with self._lock:
for t in self.queued_tasks[:]:
if t.local_abspath.startswith(local_parent_path):
if t.local_abspath.startswith(p) or t.local_abspath == local_parent_path:
self.queued_tasks.remove(t)
del self.tasks_by_path[t.local_abspath]
6 changes: 6 additions & 0 deletions tests/data/drive_config_item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"account_id": "acct_id",
"drive_id": "drive_id",
"ignorefile_path": "/home/xb/.config/onedrived/ignore_v2.txt",
"localroot_path": "/home/xb/OneDrive"
}
7 changes: 7 additions & 0 deletions tests/data/quota_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"total": 1000,
"used": 100,
"remaining": 900,
"deleted": 500,
"state": "normal"
}
16 changes: 16 additions & 0 deletions tests/test_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest

from onedrived import od_context


class TestUserContext(unittest.TestCase):

def setUp(self):
self.ctx = od_context.UserContext(loop=None)

def test_get_login_username(self):
self.assertIsInstance(od_context.get_login_username(), str)


if __name__ == '__main__':
unittest.main()
50 changes: 44 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import json
import unittest

from tests import get_resource
import onedrivesdk

from onedrived import models
from tests import get_resource


class TestAccountProfile(unittest.TestCase):

def setUp(self):
self.data = json.loads(get_resource('data/me_profile_response.json', pkg_name='tests'))
self.account = models.account_profile.OneDriveAccountProfile(self.data)

def test_properties(self):
data = json.loads(get_resource('data/me_profile_response.json', pkg_name='tests'))
account = models.account_profile.OneDriveAccountProfile(data)
self.assertEqual(data['id'], account.account_id)
self.assertEqual(data['name'], account.account_name)
self.assertEqual(data['emails']['account'], account.account_email)
self.assertEqual(self.data['id'], self.account.account_id)
self.assertEqual(self.data['name'], self.account.account_name)
self.assertEqual(self.data['emails']['account'], self.account.account_email)
self.assertEqual(self.data['first_name'], self.account.account_firstname)
self.assertEqual(self.data['last_name'], self.account.account_lastname)

def test_tostring(self):
self.assertIsInstance(str(self.account), str)


class TestPathFilter(unittest.TestCase):
Expand Down Expand Up @@ -95,5 +104,34 @@ def test_auto_correction(self):
self.assert_cases(cases)


class TestPrettyApi(unittest.TestCase):

def test_pretty_print_bytes(self):
self.assertEqual('0.000 B', models.pretty_api.pretty_print_bytes(size=0, precision=3))
self.assertEqual('1.00 KB', models.pretty_api.pretty_print_bytes(size=1025, precision=2))
self.assertEqual('1.0 MB', models.pretty_api.pretty_print_bytes(size=1048576, precision=1))
self.assertEqual('1.50 GB', models.pretty_api.pretty_print_bytes(size=1610612736, precision=2))

def test_pretty_quota(self):
quota = onedrivesdk.Quota(json.loads(get_resource('data/quota_response.json', 'tests')))
self.assertIsInstance(models.pretty_api.pretty_quota(quota), str)


class TestDriveConfig(unittest.TestCase):

def setUp(self):
self.drive_dict = json.loads(get_resource('data/drive_config_item.json', 'tests'))
self.drive_config = models.drive_config.LocalDriveConfig(**self.drive_dict)

def test_properties(self):
self.assertEqual(self.drive_dict['account_id'], self.drive_config.account_id)
self.assertEqual(self.drive_dict['drive_id'], self.drive_config.drive_id)
self.assertEqual(self.drive_dict['ignorefile_path'], self.drive_config.ignorefile_path)
self.assertEqual(self.drive_dict['localroot_path'], self.drive_config.localroot_path)

def test_to_dict(self):
self.assertDictEqual(self.drive_dict, self.drive_config.data)


if __name__ == '__main__':
unittest.main()
44 changes: 44 additions & 0 deletions tests/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest

from onedrived import od_task
from onedrived import tasks


class TestTaskPool(unittest.TestCase):

def _get_dummy_task(self, local_abspath=None):
t = tasks.base.TaskBase(repo=None, task_pool=self.task_pool)
t.local_abspath = local_abspath
return t

def setUp(self):
self.task_pool = od_task.TaskPool()

def test_add_and_pop(self):
ts = [self._get_dummy_task(local_abspath='/1'), self._get_dummy_task(local_abspath='/2')]
for i, t in enumerate(ts):
self.assertEqual(i, self.task_pool.outstanding_task_count)
self.assertTrue(self.task_pool.add_task(t))
self.assertEqual(2, self.task_pool.outstanding_task_count)
self.assertFalse(self.task_pool.add_task(self._get_dummy_task(local_abspath='/1')))
for i, t in enumerate(ts):
self.assertIs(t, self.task_pool.pop_task())
self.assertEqual(len(ts) - i - 1, self.task_pool.outstanding_task_count)
self.assertEqual(0, self.task_pool.outstanding_task_count)

def test_has_pending_task(self):
self.task_pool.add_task(self._get_dummy_task(local_abspath='/foo/bar'))
self.assertTrue(self.task_pool.has_pending_task('/foo/bar'))
for s in ('/foo/ba', '/foo/barz', '/foo/bar/baz', '/foo'):
self.assertFalse(self.task_pool.has_pending_task(s))

def test_remove_children_tasks(self):
for s in ('/foo', '/foo2', '/foo/bar', '/foo2/bar', '/foo/bar/baz'):
self.task_pool.add_task(self._get_dummy_task(local_abspath=s))
self.task_pool.remove_children_tasks(local_parent_path='/foo')
self.assertEqual(2, self.task_pool.outstanding_task_count)
self.assertEqual('/foo2', self.task_pool.pop_task().local_abspath)
self.assertEqual('/foo2/bar', self.task_pool.pop_task().local_abspath)

if __name__ == '__main__':
unittest.main()
17 changes: 17 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest

from onedrived import od_task
from onedrived import tasks


class TestTaskBase(unittest.TestCase):

def test_task_base(self):
p = '/home/xb/123'
base = tasks.base.TaskBase(repo=None, task_pool=od_task.TaskPool())
base.local_abspath = p
self.assertEqual(p, base.local_abspath)


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

0 comments on commit 869fe7b

Please sign in to comment.