# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import unittest

from beam.resource import Resource


class TestResource(unittest.TestCase):

    def test_used_bytes(self):
        self.assertEqual(Resource(100, 50).used_bytes, 100)

    def test_free_bytes(self):
        self.assertEqual(Resource(100, 50).free_bytes, 50)

    def test_total_bytes(self):
        self.assertEqual(Resource(100, 50).total_bytes, 150)

    def test_used_percentage(self):
        self.assertAlmostEqual(Resource(100, 50).used_percentage, 0.666, 2)

    def test_free_percentage(self):
        self.assertAlmostEqual(Resource(100, 50).free_percentage, 0.333, 2)

    def test_percentages_sum_to_one(self):
        resource = Resource(14682693209, 522188218791)
        self.assertEqual(resource.used_percentage + resource.free_percentage,
                         1)

    def test_zero_total_bytes(self):
        self.assertEqual(Resource(0, 0).used_percentage, 1)

    def test_from_response_none(self):
        with self.assertRaises(ValueError):
            Resource.from_response(None)

    def test_from_response_too_many_fragments(self):
        with self.assertRaises(ValueError):
            Resource.from_response('12884901888,6155997184,6728904704,48,22')

    def test_from_response_too_few_fragments(self):
        with self.assertRaises(ValueError):
            Resource.from_response('12884901888,6155997184,6728904704')

    def test_from_response_float(self):
        with self.assertRaises(ValueError):
            Resource.from_response('12884901.888,6155997184,6728904704,48')

    def test_from_response_string(self):
        with self.assertRaises(ValueError):
            Resource.from_response('12884901888,surprise,6728904704,48')

    def test_from_response(self):
        self.assertEqual(Resource(6155997184, 6728904704),
                         Resource.from_response('12884901888,6155997184,'
                                                '6728904704,48'))

    def test_eq_false_class(self):
        self.assertNotEqual(22, Resource(100, 50))

    def test_eq_false_used(self):
        self.assertNotEqual(Resource(100, 50), Resource(50, 50))

    def test_eq_false_free(self):
        self.assertNotEqual(Resource(100, 50), Resource(100, 100))

    def test_eq_true(self):
        self.assertEqual(Resource(100, 50), Resource(100, 50))

    def test_str(self):
        resource = Resource(6155997184, 6728904704)
        self.assertEqual(str(resource),
                         'Resource(6155997184 bytes used (47.78%), 6728904704 '
                         'bytes free (52.22%))')

@@ -56,16 +56,26 @@
 literals
+, division
 %0Aimport 
@@ -161,16 +161,169 @@
 Case):%0A%0A
+    _USED_BYTES = 100%0A    _FREE_BYTES = 50%0A%0A    @classmethod%0A    def setUpClass(cls):%0A        cls.resource = Resource(cls._USED_BYTES, cls._FREE_BYTES)%0A%0A
     def 
@@ -370,33 +370,29 @@
 rtEqual(
-R
+self.r
 esource
-(100, 50)
 .used_by
@@ -396,19 +396,32 @@
 _bytes, 
-100
+self._USED_BYTES
 )%0A%0A    d
@@ -471,33 +471,29 @@
 rtEqual(
-R
+self.r
 esource
-(100, 50)
 .free_by
@@ -497,18 +497,32 @@
 _bytes, 
-50
+self._FREE_BYTES
 )%0A%0A    d
@@ -573,33 +573,29 @@
 rtEqual(
-R
+self.r
 esource
-(100, 50)
 .total_b
@@ -603,12 +603,69 @@
 tes,
- 150
+%0A                         self._USED_BYTES + self._FREE_BYTES
 )%0A%0A 
@@ -734,162 +734,334 @@
 ual(
-Resource(100, 50).used_percentage, 0.666, 2)%0A%0A    def test_free_percentage(self):%0A        self.assertAlmostEqual(Resource(100, 50).free_percentage, 0.333,
+%0A            self.resource.used_percentage,%0A            self._USED_BYTES / (self._USED_BYTES + self._FREE_BYTES),%0A            2)%0A%0A    def test_free_percentage(self):%0A        self.assertAlmostEqual(%0A            self.resource.free_percentage,%0A            1 - self._USED_BYTES / (self._USED_BYTES + self._FREE_BYTES),%0A           
  2)%0A
@@ -2455,33 +2455,29 @@
 ual(22, 
-R
+self.r
 esource
-(100, 50)
 )%0A%0A    d
@@ -2533,33 +2533,29 @@
 otEqual(
-R
+self.r
 esource
-(100, 50)
 , Resour
@@ -2629,33 +2629,29 @@
 otEqual(
-R
+self.r
 esource
-(100, 50)
 , Resour
@@ -2722,44 +2722,36 @@
 ual(
-R
+self.r
 esource
-(100, 50), Resource(100, 50)
+, self.resource
 )%0A%0A 

Make Resource tests more maintainable