2016-06-01 12:03:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
try:
|
|
|
|
import unittest2 as unittest
|
|
|
|
except ImportError:
|
|
|
|
import unittest
|
2016-11-08 13:39:24 +00:00
|
|
|
import mock
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
from pungi import media_split
|
|
|
|
|
|
|
|
|
|
|
|
class ConvertMediaSizeTestCase(unittest.TestCase):
|
|
|
|
def test_size_parser_correct_number_as_int(self):
|
|
|
|
self.assertEqual(media_split.convert_media_size(123), 123)
|
|
|
|
|
|
|
|
def test_size_parser_correct_number_as_str(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(media_split.convert_media_size("123"), 123)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_size_parser_with_unit_b(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(media_split.convert_media_size("123b"), 123)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_size_parser_with_unit_k(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(media_split.convert_media_size("123k"), 123 * 1024)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_size_parser_with_unit_M(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(media_split.convert_media_size("123M"), 123 * 1024 * 1024)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_size_parser_with_unit_G(self):
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(
|
|
|
|
media_split.convert_media_size("123G"), 123 * 1024 * 1024 * 1024
|
|
|
|
)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_size_parser_with_negative_number(self):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
media_split.convert_media_size(-15)
|
|
|
|
|
|
|
|
def test_size_parser_with_unknown_unit(self):
|
|
|
|
with self.assertRaises(ValueError):
|
2020-01-22 10:02:22 +00:00
|
|
|
media_split.convert_media_size("123X")
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConvertFileSizeTestCase(unittest.TestCase):
|
|
|
|
def test_round_up(self):
|
|
|
|
self.assertEqual(media_split.convert_file_size(123, 2048), 2048)
|
|
|
|
|
|
|
|
def test_exactly_block_size(self):
|
|
|
|
self.assertEqual(media_split.convert_file_size(100, 100), 100)
|
|
|
|
|
|
|
|
|
|
|
|
def bl(s):
|
|
|
|
return s * 2048
|
|
|
|
|
|
|
|
|
|
|
|
class MediaSplitterTestCase(unittest.TestCase):
|
2016-11-08 13:39:24 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.compose = mock.Mock()
|
|
|
|
|
2016-06-01 12:03:23 +00:00
|
|
|
def test_sum_size(self):
|
|
|
|
ms = media_split.MediaSplitter(bl(100))
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("first", bl(20))
|
|
|
|
ms.add_file("second", bl(30))
|
|
|
|
ms.add_file("third", 10)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
self.assertEqual(ms.total_size, bl(50) + 10)
|
|
|
|
self.assertEqual(ms.total_size_in_blocks, bl(51))
|
|
|
|
|
|
|
|
def test_add_same_file_twice(self):
|
|
|
|
ms = media_split.MediaSplitter(bl(100))
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("first", bl(20))
|
|
|
|
ms.add_file("first", bl(20))
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
self.assertEqual(ms.total_size, bl(20))
|
|
|
|
|
|
|
|
def test_add_same_file_twice_with_different_size(self):
|
|
|
|
ms = media_split.MediaSplitter(bl(100))
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("first", bl(20))
|
2016-06-01 12:03:23 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("first", bl(30))
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_add_too_big_file(self):
|
|
|
|
ms = media_split.MediaSplitter(bl(100))
|
|
|
|
with self.assertRaises(ValueError):
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("too-big", bl(300))
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_fit_on_one(self):
|
2016-11-08 13:39:24 +00:00
|
|
|
ms = media_split.MediaSplitter(bl(100), compose=self.compose)
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("first", bl(20))
|
|
|
|
ms.add_file("second", bl(30))
|
2016-06-01 12:03:23 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(ms.split(), [{"files": ["first", "second"], "size": bl(50)}])
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_split_on_two_discs(self):
|
2016-11-08 13:39:24 +00:00
|
|
|
ms = media_split.MediaSplitter(bl(100), compose=self.compose)
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("first", bl(25))
|
|
|
|
ms.add_file("second", bl(40))
|
|
|
|
ms.add_file("third", bl(80))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
ms.split(),
|
|
|
|
[
|
|
|
|
{"files": ["first", "second"], "size": bl(65)},
|
|
|
|
{"files": ["third"], "size": bl(80)},
|
|
|
|
],
|
|
|
|
)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
|
|
|
def test_split_with_sticky_file(self):
|
|
|
|
ms = media_split.MediaSplitter(bl(100))
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("sticky", bl(15), sticky=True)
|
|
|
|
ms.add_file("first", bl(25))
|
|
|
|
ms.add_file("second", bl(40))
|
|
|
|
ms.add_file("third", bl(80))
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
ms.split(),
|
|
|
|
[
|
|
|
|
{"files": ["sticky", "first", "second"], "size": bl(80)},
|
|
|
|
{"files": ["sticky", "third"], "size": bl(95)},
|
|
|
|
],
|
|
|
|
)
|
2016-06-01 12:03:23 +00:00
|
|
|
|
2016-11-08 13:39:24 +00:00
|
|
|
def test_split_unlimited_media(self):
|
|
|
|
ms = media_split.MediaSplitter(None, compose=self.compose)
|
2020-01-22 10:02:22 +00:00
|
|
|
ms.add_file("first", bl(25))
|
|
|
|
ms.add_file("second", bl(40))
|
|
|
|
ms.add_file("third", bl(80))
|
2016-11-08 13:39:24 +00:00
|
|
|
|
2020-01-22 10:02:22 +00:00
|
|
|
self.assertEqual(
|
|
|
|
ms.split(), [{"files": ["first", "second", "third"], "size": bl(145)}]
|
|
|
|
)
|