import pytest from clusterview.algorithms import Algorithms, CentroidGrouping from clusterview.colors import Color from clusterview.points import Point, PointSet def test_empty_centroids(): with pytest.raises(ValueError): Algorithms.euclidean_grouping([], None) def test_wrong_point_set(): centroid_g1 = Point(101, 81, Color.ORANGE, 8, 800, 600) centroid_g2 = Point(357, 222, Color.RED, 8, 800, 600) centroid_g3 = Point(728, 47, Color.PURPLE, 8, 800, 600) centroids = [centroid_g1, centroid_g2, centroid_g3] with pytest.raises(ValueError): Algorithms.euclidean_grouping(centroids, None) def test_empty_point_set(): centroid_g1 = Point(101, 81, Color.ORANGE, 8, 800, 600) centroid_g2 = Point(357, 222, Color.RED, 8, 800, 600) centroid_g3 = Point(728, 47, Color.PURPLE, 8, 800, 600) centroids = [centroid_g1, centroid_g2, centroid_g3] with pytest.raises(ValueError): Algorithms.euclidean_grouping(centroids, []) def test_euclidean_grouping(): centroid_g1 = Point(101, 81, Color.ORANGE, 8, 800, 600) centroid_g2 = Point(357, 222, Color.RED, 8, 800, 600) centroid_g3 = Point(728, 47, Color.PURPLE, 8, 800, 600) centroids = [centroid_g1, centroid_g2, centroid_g3] point1_g1 = Point(67, 59, Color.GREY, 8, 800, 600) point2_g1 = Point(116, 53, Color.GREY, 8, 800, 600) point3_g1 = Point(144, 105, Color.GREY, 8, 800, 600) point1_g2 = Point(388, 243, Color.GREY, 8, 800, 600) point2_g2 = Point(358, 248, Color.GREY, 8, 800, 600) point3_g2 = Point(426, 202, Color.GREY, 8, 800, 600) point1_g3 = Point(750, 47, Color.GREY, 8, 800, 600) point2_g3 = Point(741, 85, Color.GREY, 8, 800, 600) point3_g3 = Point(700, 72, Color.GREY, 8, 800, 600) # This PointSet is the PointSet that excludes the centroids. point_set = PointSet(8, 800, 600) point_set.add_point(67, 59, Color.GREY) point_set.add_point(116, 53, Color.GREY) point_set.add_point(144, 105, Color.GREY) point_set.add_point(388, 243, Color.GREY) point_set.add_point(358, 248, Color.GREY) point_set.add_point(426, 202, Color.GREY) point_set.add_point(750, 47, Color.GREY) point_set.add_point(741, 85, Color.GREY) point_set.add_point(700, 72, Color.GREY) centroid_grouping_1 = CentroidGrouping(centroid_g1, [point1_g1, point2_g1, point3_g1]) centroid_grouping_2 = CentroidGrouping(centroid_g2, [point1_g2, point2_g2, point3_g2]) centroid_grouping_3 = CentroidGrouping(centroid_g3, [point1_g3, point2_g3, point3_g3]) expected = [centroid_grouping_1, centroid_grouping_2, centroid_grouping_3] actual = Algorithms.euclidean_grouping(centroids, point_set) assert len(actual) == len(expected) # Since I don't want to figure out what grouping is where I'll accept # the linearity of `in`. assert centroid_grouping_1 in actual assert centroid_grouping_2 in actual assert centroid_grouping_3 in actual