Changeset - a9320f18b399
[Not reviewed]
default
0 2 0
Branko Majic (branko) - 10 years ago 2013-11-09 13:59:22
branko@majic.rs
Noticket: Add more logic to the distinct colour generation. Add some extra tests for this.
2 files changed with 23 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conntrackt/tests/test_utils.py
Show inline comments
 
@@ -130,24 +130,41 @@ class GetDistinctColorsTest(TestCase):
 

	
 
        # Create list that contains True/False for diffs depending on whether
 
        # they're in delta-surrounding of reference point.
 
        equal = [(abs(diff - reference) < delta) for diff in diffs]
 

	
 
        # There should be 12 True values.
 
        self.assertEqual(equal.count(True), 12)
 

	
 
        # Check the difference between first and last colour.
 
        equal = abs(colors[0].hsl["h"] + 1 - colors[12].hsl["h"] - reference) < delta
 
        self.assertEqual(True, equal)
 

	
 
    def test_zero_count(self):
 
        """
 
        Tests if an empty list is returned in case the requested count is 0.
 
        """
 

	
 
        colors = utils.get_distinct_colors(0)
 

	
 
        self.assertIsInstance(colors, list)
 
        self.assertEqual(len(colors), 0)
 

	
 
    def test_negative_count(self):
 
        """
 
        Tests if proper exception is raised in case a negative count is passed.
 
        """
 

	
 
        self.assertRaisesRegexp(ValueError, "^Count must be a non-negative integer value\.$", utils.get_distinct_colors, -10)
 

	
 

	
 
class GenerateProjectDiagramTest(TestCase):
 
    """
 
    Tests the generate_project_diagram function.
 
    """
 

	
 
    def setUp(self):
 
        """
 
        Set-up some test data.
 
        """
 

	
 
        setup_test_data()
conntrackt/utils.py
Show inline comments
 
@@ -95,24 +95,30 @@ def get_distinct_colors(count, start=pal
 

	
 
        count - Total number of colours that should be generated.
 

	
 
        start - First colour that should be taken as a start point. All colours
 
        are generated relative to this colour by increasing the hue. Should be
 
        an instance of palette.Color class. Defaults to RGB colour "#AE1111".
 

	
 
    Return:
 

	
 
        List of distinct palette.Color instances.
 
    """
 

	
 
    # If zero is passed as count, return empty list.
 
    if count == 0:
 
        return []
 
    elif count < 0:
 
        raise ValueError("Count must be a non-negative integer value.")
 

	
 
    # Read the HSL from provided Color.
 
    hue, sat, lum = start.hsl["h"], start.hsl["s"], start.hsl["l"]
 

	
 
    # Calculate the step increase.
 
    step = 1 / float(count)
 

	
 
    # Initiate an empty list that will store the generated colours.
 
    colors = []
 

	
 
    # Generate new colour by increasing the hue as long as we haven't generated
 
    # the requested number of colours.
 
    while len(colors) < count:
0 comments (0 inline, 0 general)