def test_enumerated_difference(self): """ Test set difference of two enumerated domains. """ d1 = Domain.EnumeratedDomain() d2 = Domain.EnumeratedDomain() d1.add([1, 2, 3, 4]) d2.add([3, 4, 5, 6]) self.assertItemsEqual(d1.difference(d2)._domain, [1, 2]) self.assertItemsEqual((d2 - d1)._domain, [5, 6])
def test_enumerated_intersect(self): """ Test intersection of enumerated domains. """ d1 = Domain.EnumeratedDomain() d2 = Domain.EnumeratedDomain() d1.add([1, 2, 3, 4]) d2.add([3, 4, 5, 6]) self.assertItemsEqual(d1.intersect(d2)._domain, [3, 4]) self.assertItemsEqual((d1 & d2)._domain, [3, 4])
def test_enumerated_concat(self): """ Test concatenating two enumerated domains. """ d1 = Domain.EnumeratedDomain() d2 = Domain.EnumeratedDomain() d1.add([1, 2, 3, 4]) d2.add([3, 4, 5, 6]) # Use assertEquals here because I want to test the order self.assertEquals((d1 + d2)._domain, [1, 2, 3, 4, 5, 6]) self.assertEquals((d2 + d1)._domain, [3, 4, 5, 6, 1, 2])
def test_time_domain_default(self): """ Test a TimeDomain with default start time. """ domain = Domain.TimeDomain() actual = domain.draw(3) expected = [time(0, 0), time(0, 1), time(0, 2)] self.assertItemsEqual(actual, expected) self.assertEquals(domain.max_size, 60 * 24)
def test_date_overflow(self): """ Confirm date domain does not overflow. """ domain = Domain.DateDomain(start=date(9999, 12, 30)) actual = domain.draw(100) expected = [date(9999, 12, 30), date(9999, 12, 31)] self.assertItemsEqual(actual, expected) self.assertEquals(domain.max_size, 2)
def test_commit_and_rollback_value(self): """ Test commit and rollback of value constraints. """ d0 = Domain.IntegerDomain() role = Role(name="R1") obj = ObjectType(name="O1", data_type=d0) d1 = Constraint.ValueDomain() d1.add_range("Dog") rvc = Constraint.ValueConstraint(name="RVC", covers=[role], domain=d1) self.assertEquals(role.covered_by, []) rvc.commit() self.assertEquals(role.covered_by, [rvc]) rvc.rollback() self.assertEquals(role.covered_by, []) vc = Constraint.ValueConstraint(name="VTVC", covers=[obj], domain=d1) self.assertEquals(obj.covered_by, []) self.assertEquals(obj.domain, obj.data_type) self.assertEquals(obj.domain, d0) vc.commit() self.assertEquals(obj.covered_by, [vc]) self.assertEquals(obj.domain, d1) self.assertEquals(obj.data_type, d0) vc.rollback() self.assertEquals(obj.covered_by, []) self.assertEquals(obj.domain, obj.data_type) self.assertEquals(obj.domain, d0)
def test_enumerated(self): """ Test EnumeratedDomain. """ domain = Domain.EnumeratedDomain() self.assertItemsEqual(domain.draw(10), []) self.assertEquals(domain.max_size, 0) domain.add(5) domain.add(['a', 'b', 'c']) self.assertItemsEqual(domain.draw(0), []) self.assertEquals(domain.draw(4), [5, 'a', 'b', 'c']) self.assertEquals(domain.max_size, 4) # Add element already in the domain domain.add('a') self.assertEquals(domain.draw(5), [5, 'a', 'b', 'c']) # Add list with partial overlap domain.add([7, 6, 5]) self.assertEquals(domain.draw(10), [5, 'a', 'b', 'c', 6, 7]) # Add list with full overlap domain.add(['a', 'b']) self.assertEquals(domain.draw(10), [5, 'a', 'b', 'c', 6, 7])
def test_int_domain(self): """ Test an IntegerDomain. """ domain = Domain.IntegerDomain() actual = domain.draw(8) expected = [0, 1, 2, 3, 4, 5, 6, 7] self.assertItemsEqual(actual, expected) self.assertEquals(domain.max_size, sys.maxsize)
def test_float_domain(self): """ Test a FloatDomain. """ domain = Domain.FloatDomain() actual = domain.draw(12) expected = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1] self.assertItemsEqual(actual, expected) self.assertEquals(domain.max_size, sys.maxsize)
def test_bool_domain_full(self): """ Test a complete BoolDomain. """ domain = Domain.BoolDomain() actual = domain.draw(2) expected = [False, True] self.assertItemsEqual(actual, expected) self.assertEquals(domain.max_size, 2)
def test_string_domain_no_prefix(self): """ Test a StringDomain with no prefix. """ domain = Domain.StringDomain() actual = domain.draw(5) expected = ['0', '1', '2', '3', '4'] self.assertItemsEqual(actual, expected) self.assertEquals(domain.max_size, sys.maxsize)
def test_time_overflow(self): """ Confirm that TimeDomain does not overflow. """ domain = Domain.TimeDomain(start=time(23, 58)) actual = domain.draw(400) expected = [time(23, 58), time(23, 59)] self.assertItemsEqual(actual, expected) # Minutes from 11:58pm to 11:59pm self.assertEquals(domain.max_size, 2)
def test_time_domain(self): """ Test a TimeDomain with provided start time. """ domain = Domain.TimeDomain(start=time(11, 58)) actual = domain.draw(4) expected = [time(11, 58), time(11, 59), time(12, 0), time(12, 1)] self.assertItemsEqual(actual, expected) # Minutes from 11:58am to 11:59pm self.assertEquals(domain.max_size, 60 * 24 / 2 + 2)
def test_datetime_domain_default(self): """ Test a DateTimeDomain with default start time. """ actual = Domain.DateTimeDomain().draw(3) expected = [ datetime(2000, 1, 1, 0, 0), datetime(2000, 1, 1, 0, 1), datetime(2000, 1, 1, 0, 2) ] self.assertItemsEqual(actual, expected)
def test_datetime_domain(self): """ Test a DateTimeDomain with provided start time. """ actual = Domain.DateTimeDomain( start=datetime(1999, 12, 31, 23, 58)).draw(3) expected = [ datetime(1999, 12, 31, 23, 58), datetime(1999, 12, 31, 23, 59), datetime(2000, 1, 1, 0, 0) ] self.assertItemsEqual(actual, expected)
def test_date_domain(self): """ Test a DateDomain with a provided start date.""" domain = Domain.DateDomain(start=date(1961, 2, 28)) actual = domain.draw(3) expected = [date(1961, 2, 28), date(1961, 3, 1), date(1961, 3, 2)] self.assertItemsEqual(actual, expected) diff = (date(9999, 12, 31) - date(1961, 2, 28)).total_seconds() diff = int(diff) / (60 * 60 * 24) self.assertEqual(domain.max_size, diff + 1)
def test_datetime_domain_overflow(self): """ Confirm a DateTimeDomain does not overflow. """ domain = Domain.DateTimeDomain(start=datetime(9999, 12, 31, 23, 58)) actual = domain.draw(300) expected = [ datetime(9999, 12, 31, 23, 58), datetime(9999, 12, 31, 23, 59) ] self.assertItemsEqual(actual, expected) self.assertEquals(domain.max_size, 2)
def test_forced_stop_iteration(self): """ Just for 100% coverage, force a StopIteration exception within Domain.draw(). """ domain = Domain.Domain(max_size=100) # Create custom _generate() function that can only produce 5 items # despite max_size domain._generate = lambda n: (i for i in xrange(5)) actual = domain.draw(7) # Should raise StopIteration exception within # the for loop expected = [0, 1, 2, 3, 4] self.assertItemsEqual(actual, expected)
import sys import cld3 import time from packages import Item from lib import Domain from pubsublogger import publisher from Helper import Process if __name__ == '__main__': publisher.port = 6380 publisher.channel = 'Script' # Section name in bin/packages/modules.cfg config_section = 'Languages' # Setup the I/O queues p = Process(config_section) while True: message = p.get_from_set() if message is None: publisher.debug( "{} queue is empty, waiting".format(config_section)) time.sleep(1) continue item_id = Item.get_item_id(message) if Item.is_crawled(item_id): domain = Item.get_item_domain(item_id) Domain.add_domain_languages_by_item_id(domain, item_id)
def test_string_domain_prefix(self): """ Test a StringDomain with a prefix. """ actual = Domain.StringDomain(prefix='Test').draw(4) expected = ['Test0', 'Test1', 'Test2', 'Test3'] self.assertItemsEqual(actual, expected)
def test_bool_domain_partial(self): """ Test a partial BoolDomain. """ actual = Domain.BoolDomain().draw(1) expected = [False] self.assertItemsEqual(actual, expected)
def test_not_implemented_exception(self): """ Just for 100% coverage, confirm not implemented exception raised on Domain class's _generate function. """ domain = Domain.Domain() with self.assertRaises(NotImplementedError) as ex: g = domain._generate(10)
def test_empty_domain(self): """ Test an empty domain. """ actual = Domain.IntegerDomain().draw(0) expected = [] self.assertItemsEqual(actual, expected)
def test_full_domain(self): """ Test an exceeded domain. """ actual = Domain.BoolDomain().draw(20) expected = [False, True] self.assertItemsEqual(actual, expected)