|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- """Test consolidators
-
- Consolidators work on a List of Faetures, and Features a constructed from Interval objects.
- As such, we need to mock an Interval object and a Feature object to test the consolidators.
- """
- import unittest
- from pipeline.consolidators import OverlapConsolidator
-
-
- class TestOverlapConsolidation(unittest.TestCase):
- """overlap consolidator only uses Interval .start and .end attributes, so we can mock them here"""
- class MockInterval():
- """Mock interval object for overlap testing -- only has start and end attributes"""
- def __init__(self, start, end):
- self.start = start
- self.end = end
- class MockFeature():
- """Mock feature object for overlap testing -- only has interval attribute"""
- def __init__(self, interval):
- self.interval = interval
-
- def setUp(self):
- """Set up test data"""
- self.empty_features = []
- self.fakepath = "/tmp/fakepath"
- self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
- self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
- self.populated_features = [self.featureA, self.featureB]
-
- def test_init(self):
- # test empty features
- with self.assertRaises(ValueError):
- OverlapConsolidator(self.empty_features)
- # test populated features
- consolidator = OverlapConsolidator(features=self.populated_features)
- self.assertEqual(consolidator.features, self.populated_features)
-
- def test_consolidate(self):
- # 4 features for overlap test: ABCD, overlap A-B, C-D
- self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
- self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
- self.featureC = self.MockFeature(interval=self.MockInterval(start=20, end=30))
- self.featureD = self.MockFeature(interval=self.MockInterval(start=25, end=35))
- self.overlapping_features = [self.featureA, self.featureB, self.featureC, self.featureD]
- consolidator = OverlapConsolidator(features=self.overlapping_features)
- consolidator.consolidate()
- # check that A-B and C-D have been merged
- self.assertEqual(len(consolidator.features), 2)
-
- # 4 features for no overlap test
- self.feature1 = self.MockFeature(interval=self.MockInterval(start=0, end=10))
- self.feature2 = self.MockFeature(interval=self.MockInterval(start=15, end=25))
- self.feature3 = self.MockFeature(interval=self.MockInterval(start=30, end=40))
- self.feature4 = self.MockFeature(interval=self.MockInterval(start=45, end=55))
- self.non_overlapping_features = [self.feature1, self.feature2, self.feature3, self.feature4]
- consolidator = OverlapConsolidator(features=self.non_overlapping_features)
- consolidator.consolidate()
- # check that no features have been merged
- self.assertEqual(len(consolidator.features), 4)
-
- # 3 features for overlap test: ABC, overlap A-B, B-C
- self.featureA = self.MockFeature(interval=self.MockInterval(start=0, end=10))
- self.featureB = self.MockFeature(interval=self.MockInterval(start=5, end=15))
- self.featureC = self.MockFeature(interval=self.MockInterval(start=12, end=20))
- self.overlapping_features = [self.featureA, self.featureB, self.featureC]
- consolidator = OverlapConsolidator(features=self.overlapping_features)
- consolidator.consolidate()
- # check that A-B and B-C have been merged
- self.assertEqual(len(consolidator.features), 1)
-
- if __name__ == '__main__':
- unittest.main()
|