def test_code(): """`Code` makers should return a random code""" # Configured with the default character set maker = text_makers.Code(quotas.Quota(4)) # Check the assembled result assembled = maker._assemble() assert len(assembled) == 4 assert set(assembled).issubset(set(maker.default_charset)) # Check the finished result finished = maker._finish(assembled) assert len(finished) == 4 assert set(assembled).issubset(set(maker.default_charset)) # Configured with a custom charset custom_charset = 'ABCDEF1234567890' maker = text_makers.Code(quotas.Quota(6), custom_charset) # Check the assembled result assembled = maker._assemble() assert len(assembled) == 6 assert set(assembled).issubset(set(custom_charset)) # Check the finished result finished = maker._finish(assembled) assert len(finished) == 6 assert set(assembled).issubset(set(custom_charset))
def test_list_of(): """ `ListOf` makers should return a list of values generated by calling a maker multiple times. """ # Configured to not reset sub-maker maker = makers.ListOf(selection_makers.Cycle(list('abcde')), quotas.Quota(6)) # Check the assembled result assembled = maker._assemble() assert assembled == [[i, None] for i in [0, 1, 2, 3, 4, 0]] # Check the finished result finished = maker._finish(assembled) assert finished == list('abcdea') # Check that calling the maker again continues from where we left off assembled = maker._assemble() assert assembled == [[i, None] for i in [1, 2, 3, 4, 0, 1]] # Configured to reset sub-maker maker = makers.ListOf(selection_makers.Cycle(list('abcde')), quotas.Quota(6), reset_maker=True) # Call the maker twice assembled = maker._assemble() assembled = maker._assemble() # Check the result was reset after the first call assert assembled == [[i, None] for i in [0, 1, 2, 3, 4, 0]]
def test_factory_assemble(): """ The `Factory.assemble` method should return a quota of assembled documents/dictionaries for the given blueprint. """ # Configure the blueprint class DragonBlueprint(blueprints.Blueprint): _frame_cls = Dragon _meta_fields = {'dummy_prop'} name = makers.Static('Burt') breed = makers.Static('Fire-drake') dummy_prop = makers.Static('foo') # Configure the factory factory = Factory() # Assemble a list of documents using the factory documents = factory.assemble(DragonBlueprint, quotas.Quota(10)) # Check the assembled output of the factory is as expected for document in documents: assert document == { 'breed': 'Fire-drake', 'dummy_prop': 'foo', 'name': 'Burt' }
def test_factory_finish(): """ The `Factory.assemble` method should return a list of finished documents/dictionaries and meta documents/dictionaries in the form `[(document, meta_document), ...]` from a blueprint and list of preassembled documents/dictionaries. """ # Configure the blueprint class DragonBlueprint(blueprints.Blueprint): _frame_cls = Dragon _meta_fields = {'dummy_prop'} name = makers.Static('Burt') breed = makers.Static('Fire-drake') dummy_prop = makers.Static('foo') # Configure the factory factory = Factory() # Assemble a list of documents using the factory documents = factory.assemble(DragonBlueprint, quotas.Quota(10)) # Finish the assembled documents documents = factory.finish(DragonBlueprint, documents) # Check the assembled output of the factory is as expected for document in documents: assert document == { 'breed': 'Fire-drake', 'name': 'Burt', 'dummy_prop': 'foo' }
def test_markov(): """`Markov` makers should return random text built from a text body""" # Set up markov word database with open('tests/factory/data/markov.txt') as f: text_makers.Markov.init_word_db('test', f.read()) # Configured to return a body of text maker = text_makers.Markov('test', 'body', quotas.Quota(5)) # Check the assembled result assembled = maker._assemble() assert len(assembled.split('\n')) == 5 # Check the finished result finished = maker._finish(assembled) assert len(finished.split('\n')) == 5 # Configured to return a paragraph maker = text_makers.Markov('test', 'paragraph', quotas.Quota(5)) # Check the assembled result assembled = maker._assemble() paragraphs = [p for p in assembled.split('.') if p.strip()] assert len(paragraphs) == 5 # Check the finished result finished = maker._finish(assembled) paragraphs = [p for p in finished.split('.') if p.strip()] assert len(paragraphs) == 5 # Configured to return a sentence maker = text_makers.Markov('test', 'sentence', quotas.Quota(5)) # Check the assembled result assembled = maker._assemble() words = [w for w in assembled.split(' ') if w.strip()] assert len(words) == 5 # Check the finished result finished = maker._finish(assembled) words = [w for w in finished.split(' ') if w.strip()] assert len(words) == 5
def test_quota(): """ The base `Quota` should convert to a quantity identical to the one it is initialized with. """ quota = quotas.Quota(5) # Check quota converted to an integer assert int(quota) == 5 # Check quota converted to a float assert float(quota) == 5.0
def test_lorem(): """`Lorem` makers should return lorem ipsum""" # Configured to return a body of text maker = text_makers.Lorem('body', quotas.Quota(5)) # Check the assembled result assembled = maker._assemble() assert len(assembled.split('\n')) == 5 # Check the finished result finished = maker._finish(assembled) assert len(finished.split('\n')) == 5 # Configured to return a paragraph maker = text_makers.Lorem('paragraph', quotas.Quota(5)) # Check the assembled result assembled = maker._assemble() paragraphs = [p for p in assembled.split('.') if p.strip()] assert len(paragraphs) == 5 # Check the finished result finished = maker._finish(assembled) paragraphs = [p for p in finished.split('.') if p.strip()] assert len(paragraphs) == 5 # Configured to return a sentence maker = text_makers.Lorem('sentence', quotas.Quota(5)) # Check the assembled result assembled = maker._assemble() words = [w for w in assembled.split(' ') if w.strip()] assert len(words) == 5 # Check the finished result finished = maker._finish(assembled) words = [w for w in finished.split(' ') if w.strip()] assert len(words) == 5
def test_counter(): """`Counter` makers should return a number sequence""" # Configured with defaults maker = number_makers.Counter() for i in range(1, 100): # Check the assembled result assembled = maker._assemble() assert assembled == i # Check the finished result finished = maker._finish(assembled) assert finished == i # Configuered with custom start from and step maker = number_makers.Counter(quotas.Quota(10), quotas.Quota(5)) for i in range(10, 100, 5): # Check the assembled result assembled = maker._assemble() assert assembled == i # Check the finished result finished = maker._finish(assembled) assert finished == i # Reset should reset the counter to the initial start from value maker.reset() # Check the assembled result assembled = maker._assemble() assert assembled == 10 # Check the finished result finished = maker._finish(assembled) assert finished == 10
def test_factory_populate(mongo_client, mocker): """ The `Factory.populate` method should return populate a database collection using using a blueprint and list of preassembled documents/dictionaries. """ # Configure the blueprint class DragonBlueprint(blueprints.Blueprint): _frame_cls = Dragon _meta_fields = {'dummy_prop'} name = makers.Static('Burt') breed = makers.Static('Fire-drake') dummy_prop = makers.Static('foo') # Configure the factory factory = Factory() # Assemble a list of documents using the factory documents = factory.assemble(DragonBlueprint, quotas.Quota(10)) # Add listeners for fake and faked Dragon._on_fake = lambda sender, frames: print('qwe') Dragon._on_faked = lambda sender, frames: None mocker.spy(Dragon, '_on_fake') mocker.spy(Dragon, '_on_faked') Dragon.listen('fake', Dragon._on_fake) Dragon.listen('faked', Dragon._on_faked) # Populate the database with our fake documents frames = factory.populate(DragonBlueprint, documents) # Check each maker reset method was called assert Dragon._on_fake.call_count == 1 assert Dragon._on_faked.call_count == 1 # Check the frames created for frame in frames: assert frame._id is not None assert frame.name == 'Burt' assert frame.breed == 'Fire-drake' assert frame.dummy_prop
def test_factory_reassemble(): """ The `Blueprint.reassemble` method should reassemble the given fields in a list of preassembled documents/dictionaries. """ # Configure the blueprint class DragonBlueprint(blueprints.Blueprint): _frame_cls = Dragon _meta_fields = {'dummy_prop'} name = makers.Static('Burt') breed = makers.Static('Fire-drake') dummy_prop = makers.Static('foo') # Configure the factory factory = Factory() # Assemble a list of documents using the factory documents = factory.assemble(DragonBlueprint, quotas.Quota(10)) # Re-configure the blueprint and factory class DragonBlueprint(blueprints.Blueprint): _frame_cls = Dragon _meta_fields = {'dummy_prop'} name = makers.Static('Fred') breed = makers.Static('Cold-drake') dummy_prop = makers.Static('bar') factory = Factory() # Reassemble the documents factory.reassemble(DragonBlueprint, {'breed', 'name'}, documents) # Check the reassembled output of the factory is as expected for document in documents: assert document == { 'breed': 'Cold-drake', 'dummy_prop': 'foo', 'name': 'Fred' }
def test_one_of(): """ `OneOf` makers should return one value at random (optionally weighted) from a list of items (python types of or makers). """ # Seed the random generator to ensure test results are consistent random.seed(110679) # Configured to return an item from a list of python types maker = selection_makers.OneOf(['foo', 'bar', 'zee']) counts = {'foo': 0, 'bar': 0, 'zee': 0} for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert assembled[0] in [0, 1, 2] and assembled[1] == None # Check the finished result finished = maker._finish(assembled) assert finished in ['foo', 'bar', 'zee'] # Count the occurances counts[finished] += 1 # Confirm the counts are approx. evenly distributed for value in ['foo', 'bar', 'zee']: assert int(round(counts[value] / 100)) == 3 # Configured to return an item from a list of makers maker = selection_makers.OneOf( [makers.Static('foo'), makers.Static('bar'), makers.Static('zee')]) counts = {'foo': 0, 'bar': 0, 'zee': 0} for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert assembled[0] in [0, 1, 2] assert assembled[1] in ['foo', 'bar', 'zee'] # Check the finished result finished = maker._finish(assembled) assert finished in ['foo', 'bar', 'zee'] # Count the occurances counts[finished] += 1 # Confirm the counts are approx. evenly distributed for value in ['foo', 'bar', 'zee']: assert int(round(counts[value] / 100)) == 3 # Configured to return using a weighted bias maker = selection_makers.OneOf( ['foo', 'bar', 'zee'], [quotas.Quota(10), quotas.Quota(30), quotas.Quota(60)]) counts = {'foo': 0, 'bar': 0, 'zee': 0} for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert assembled[0] in [0, 1, 2] and assembled[1] == None # Check the finished result finished = maker._finish(assembled) assert finished in ['foo', 'bar', 'zee'] # Count the occurances counts[finished] += 1 # Confirm the counts are approx. distributed based on the weights assert int(round(counts['foo'] / 100)) == 1 assert int(round(counts['bar'] / 100)) == 3 assert int(round(counts['zee'] / 100)) == 6
def test_some_of(): """ `SomeOf` makers should return a list of values at random (optionally weighted) from a list of items (python types of or makers). """ # Seed the random generator to ensure test results are consistent random.seed(110679) # Define the choices we'll be sampling from choices = ['foo', 'bar', 'zee', 'oof', 'rab', 'eez'] choices_range = range(0, len(choices)) choices_set = set(choices) # Configured to return a sample from a list of python types maker = selection_makers.SomeOf(list(choices), quotas.Quota(3)) counts = {c: 0 for c in choices} for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert len(assembled) == 3 for item in assembled: assert item[0] in choices_range and item[1] == None # Check the finished result finished = maker._finish(assembled) assert len(set(finished)) == 3 assert set(finished).issubset(choices_set) # Count occurances for value in finished: counts[value] += 1 # Confirm the counts are approx. evenly distributed for value in choices: assert int(round(counts[value] / 100)) == 5 # Configured to return a sample from a list of makers maker = selection_makers.SomeOf([makers.Static(c) for c in choices], quotas.Quota(3)) counts = {c: 0 for c in choices} for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert len(assembled) == 3 for item in assembled: assert item[0] in choices_range and item[1] in choices # Check the finished result finished = maker._finish(assembled) assert len(set(finished)) == 3 assert set(finished).issubset(choices_set) # Count occurances for value in finished: counts[value] += 1 # Confirm the counts are approx. evenly distributed for value in choices: assert int(round(counts[value] / 100)) == 5 # Configured to return a sample from a list of python types weighted maker = selection_makers.SomeOf(list(choices), quotas.Quota(3), weights=[1, 2, 4, 8, 16, 32]) counts = {c: 0 for c in choices} for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert len(assembled) == 3 for item in assembled: assert item[0] in choices_range and item[1] == None # Check the finished result finished = maker._finish(assembled) assert len(finished) == 3 assert set(finished).issubset(choices_set) # Count occurances for value in finished: counts[value] += 1 # Confirm the counts are approx. based on the weights for i, value in enumerate(choices): count = counts[value] / 1000 prob = maker.p(i, 3, [1, 2, 4, 8, 16, 32]) tol = prob * 0.15 assert count > (prob - tol) and count < (prob + tol) # Configured to return a sample from a list of python types with replacement maker = selection_makers.SomeOf([makers.Static(c) for c in choices], quotas.Quota(3), with_replacement=False) not_uniques = 0 for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert len(assembled) == 3 for item in assembled: assert item[0] in choices_range and item[1] in choices # Check the finished result finished = maker._finish(assembled) assert len(finished) == 3 assert set(finished).issubset(choices_set) # Count occurances of values with non-unique values if len(set(value)) < 3: not_uniques += 1 # Check that some values where generated with non-unique items assert not_uniques > 0 # Configured to return a sample from a list of python types weighted with # replacement. maker = selection_makers.SomeOf(list(choices), quotas.Quota(3), weights=[1, 2, 4, 8, 16, 32], with_replacement=True) counts = {c: 0 for c in choices} for i in range(0, 1000): # Check the assembled result assembled = maker._assemble() assert len(assembled) == 3 for item in assembled: assert item[0] in choices_range and item[1] == None # Check the finished result finished = maker._finish(assembled) assert len(finished) == 3 assert set(finished).issubset(choices_set) # Count occurances for value in finished: counts[value] += 1 # Confirm the counts are approx. (+/- 15% tolerance) based on the weights weight = 1 for value in choices: count = counts[value] prob = (weight / 63.0) * 3000.0 tol = prob * 0.15 assert count > (prob - tol) and count < (prob + tol) weight *= 2
def test_image_url(): """ `ImageURL` makers should return the URL for a placeholder image service such as `http://fakeimg.pl`. """ # Generate an image URL for the default service provider maker = image_makers.ImageURL( quotas.Quota(100), quotas.Quota(200), background='DDDDDD', foreground='888888', options={'text': 'foo'} ) # Check the assembled result image_url = '//fakeimg.pl/100x200/DDDDDD/888888/?text=foo' assembled = maker._assemble() assert assembled == image_url # Check the finished result finished = maker._finish(assembled) assert finished == image_url # Configured for a custom service provider def placehold_it_formatter( service_url, width, height, background, foreground, options ): """Generage an image URL for the placehold.it service""" # Build the base URL image_tmp = '{service_url}/{width}x{height}/{background}/{foreground}' image_url = image_tmp.format( service_url=service_url, width=width, height=height, background=background, foreground=foreground ) # Check for a format option fmt = '' if 'format' in options: fmt = options.pop('format') image_url += '.' + fmt # Add any other options if options: image_url += '?' + urlencode(options) return image_url maker = image_makers.ImageURL( quotas.Quota(100), quotas.Quota(200), background='DDDDDD', foreground='888888', options={'text': 'foo', 'format': 'png'}, service_url='//placehold.it', service_formatter=placehold_it_formatter ) # Check the assembled result image_url = '//placehold.it/100x200/DDDDDD/888888.png?text=foo' assembled = maker._assemble() assert assembled == image_url # Check the finished result finished = maker._finish(assembled) assert finished == image_url