Merge lp://qastaging/~teknico/desktopcouch/more-varied-random-contacts into lp://qastaging/desktopcouch

Proposed by Nicola Larosa
Status: Merged
Approved by: dobey
Approved revision: 46
Merged at revision: not available
Proposed branch: lp://qastaging/~teknico/desktopcouch/more-varied-random-contacts
Merge into: lp://qastaging/desktopcouch
Diff against target: None lines
To merge this branch: bzr merge lp://qastaging/~teknico/desktopcouch/more-varied-random-contacts
Reviewer Review Type Date Requested Status
dobey (community) Approve
Eric Casteleijn (community) Approve
Review via email: mp+11192@code.qastaging.launchpad.net

Commit message

Improve the randomization of automatically generated contacts, creating a greater variety of combinations of absent and present data, useful for testing.

To post a comment you must log in.
Revision history for this message
Nicola Larosa (teknico) wrote :

This branch improves the randomization of automatically generated contacts, creating a greater variety of combinations of absent and present data, useful for testing.

Revision history for this message
Eric Casteleijn (thisfred) wrote :

Looks good!

review: Approve
Revision history for this message
dobey (dobey) :
review: Approve
47. By Nicola Larosa

Merge from trunk

48. By Nicola Larosa

Added some tests.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'desktopcouch/contacts/testing/create.py'
--- desktopcouch/contacts/testing/create.py 2009-08-19 10:07:54 +0000
+++ desktopcouch/contacts/testing/create.py 2009-09-04 08:59:18 +0000
@@ -30,25 +30,55 @@
30 'Daniel', 'William', 'James', 'Alfie', 'Grace', 'Ruby', 'Olivia',30 'Daniel', 'William', 'James', 'Alfie', 'Grace', 'Ruby', 'Olivia',
31 'Emily', 'Jessica', 'Sophie', 'Chloe', 'Lily', 'Ella', 'Amelia')31 'Emily', 'Jessica', 'Sophie', 'Chloe', 'Lily', 'Ella', 'Amelia')
32LAST_NAMES = ('Dalglish', 'Grobbelaar', 'Lawrenson', 'Beglin', 'Nicol',32LAST_NAMES = ('Dalglish', 'Grobbelaar', 'Lawrenson', 'Beglin', 'Nicol',
33 'Whelan', 'Hansen', 'Johnston', 'Rush', 'Molby', 'MacDonald', 'McMahon')33 'Whelan', 'Hansen', 'Johnston', 'Rush', 'Molby', 'MacDonald', 'McMahon',
34 'Penny', 'Leicester', 'Langley', 'Commodore', 'Touchstone', 'Fielding')
35COMPANIES = ('Gostram', 'Grulthing', 'Nasform', 'Smarfish', 'Builsank')
36STREET_TYPES = ('Street', 'Road', 'Lane', 'Avenue', 'Square', 'Park', 'Mall')
34CITIES = ('Scunthorpe', 'Birmingham', 'Cambridge', 'Durham', 'Bedford')37CITIES = ('Scunthorpe', 'Birmingham', 'Cambridge', 'Durham', 'Bedford')
35COUNTRIES = ('England', 'Ireland', 'Scotland', 'Wales')38COUNTRIES = ('England', 'Ireland', 'Scotland', 'Wales')
3639
37def random_string(length=10):40def head_or_tails():
38 """Return a string of random lowercase letters and of specified length"""41 """Randomly return True or False"""
39 return ''.join(random.sample(string.lowercase, length))42 return random.choice((False, True))
43
44def random_bools(num_bools, at_least_one_true=True):
45 """
46 Return a sequence of random booleans. If at_least_one_true,
47 guarantee at list one True value.
48 """
49 if num_bools < 2:
50 raise RuntimeError('Cannot build a sequence smaller than two elements')
51 bools = [head_or_tails() for __ in range(num_bools)]
52 if at_least_one_true and not any(bools):
53 bools[random.randrange(num_bools)] = True
54 return bools
55
56def random_string(length=10, upper=False):
57 """
58 Return a string, of specified length, of random lower or uppercase letters.
59 """
60 charset = string.uppercase if upper else string.lowercase
61 return ''.join(random.sample(charset, length))
4062
41def random_postal_address():63def random_postal_address():
42 """Return something that looks like a postal address"""64 """Return something that looks like a postal address"""
43 return '%s Street' % random.choice(LAST_NAMES)65 return '%d %s %s' % (random.randint(1, 100), random.choice(LAST_NAMES),
66 random.choice(STREET_TYPES))
4467
45def random_email_address(first_name, last_name):68def random_email_address(
46 """Return something that looks like an email address"""69 first_name='', last_name='', company='', address_type='other'):
47 return '%s.%s@%s.com' % (first_name, last_name, random_string(3))70 """
71 Return something that looks like an email address.
72 address_type values: 'personal', 'work', 'other'.
73 """
74 # avoid including the dot if one or both names are missing
75 pers_name = '.'.join([name for name in (first_name, last_name) if name])
76 return {'personal': pers_name, 'work': company,
77 }.get(address_type, random_string(5)) + '@example.com'
4878
49def random_postal_code():79def random_postal_code():
50 """Return something that looks like a postal code"""80 """Return something that looks like a postal code"""
51 return '%s12 3%s' % (random_string(2), random_string(2))81 return '%s12 3%s' % (random_string(2, True), random_string(2, True))
5282
53def random_phone_number():83def random_phone_number():
54 """Return something that looks like a phone number"""84 """Return something that looks like a phone number"""
@@ -65,11 +95,39 @@
65 # Record schema95 # Record schema
66 fielddict = {'record_type': doctype, 'record_type_version': '1.0'}96 fielddict = {'record_type': doctype, 'record_type_version': '1.0'}
67 # Names97 # Names
68 first_name = random.choice(FIRST_NAMES) + str(maincount)98 # at least one of the three will be present
69 last_name = random.choice(LAST_NAMES)99 has_first_name, has_last_name, has_company_name = random_bools(3)
100 first_name = (random.choice(FIRST_NAMES) + str(maincount)
101 ) if has_first_name else ''
102 last_name = random.choice(LAST_NAMES) if has_last_name else ''
103 company = (random.choice(COMPANIES) + str(maincount)
104 ) if has_company_name else ''
105 # Address places and types
106 address_places, email_types, phone_places = [], [], []
107 if has_first_name or has_last_name:
108 for (has_it, seq, val) in zip(
109 # at least one of the three will be present
110 random_bools(3),
111 (address_places, email_types, phone_places),
112 ('home', 'personal', 'home')):
113 if has_it:
114 seq.append(val)
115 if has_company_name:
116 for (has_it, seq) in zip(
117 # at least one of the three will be present
118 random_bools(3),
119 (address_places, email_types, phone_places)):
120 if has_it:
121 seq.append('work')
122 for (has_it, seq) in zip(
123 # none of the three may be present
124 random_bools(3, at_least_one_true=False),
125 (address_places, email_types, phone_places)):
126 if has_it:
127 seq.append('other')
70 # Addresses128 # Addresses
71 addresses = {}129 addresses = {}
72 for address_type in ('home', 'work', 'other'):130 for address_type in address_places:
73 addresses[str(uuid.uuid4())] = {131 addresses[str(uuid.uuid4())] = {
74 'address1': random_postal_address(), 'address2': '',132 'address1': random_postal_address(), 'address2': '',
75 'pobox': '', 'city': random.choice(CITIES),133 'pobox': '', 'city': random.choice(CITIES),
@@ -77,20 +135,23 @@
77 'country': random.choice(COUNTRIES), 'description': address_type}135 'country': random.choice(COUNTRIES), 'description': address_type}
78 # Email addresses136 # Email addresses
79 email_addresses = {}137 email_addresses = {}
80 for email_address_type in ('home', 'work', 'other'):138 for email_address_type in email_types:
81 email_addresses[str(uuid.uuid4())] = {139 email_addresses[str(uuid.uuid4())] = {
82 'address': random_email_address(first_name, last_name),140 'address': random_email_address(
141 first_name, last_name, company, email_address_type),
83 'description': email_address_type}142 'description': email_address_type}
84 # Phone numbers143 # Phone numbers
85 phone_numbers = {}144 phone_numbers = {}
86 for phone_number_type in ('home', 'work', 'other'):145 for phone_number_type in phone_places:
87 phone_numbers[str(uuid.uuid4())] = {146 phone_numbers[str(uuid.uuid4())] = {
88 'priority': 0, 'number': random_phone_number(),147 'priority': 0, 'number': random_phone_number(),
89 'description': phone_number_type}148 'description': phone_number_type}
90 # Store data in fielddict149 # Store data in fielddict
150 if (has_first_name or has_last_name) and head_or_tails():
151 fielddict['birth_date'] = random_birth_date()
91 fielddict.update({'first_name': first_name, 'last_name': last_name,152 fielddict.update({'first_name': first_name, 'last_name': last_name,
92 'addresses': addresses, 'email_addresses': email_addresses,153 'addresses': addresses, 'email_addresses': email_addresses,
93 'phone_numbers': phone_numbers, 'birth_date': random_birth_date()})154 'phone_numbers': phone_numbers, 'company': company})
94 # Possibly add example application annotations155 # Possibly add example application annotations
95 if app_annots:156 if app_annots:
96 fielddict['application_annotations'] = app_annots157 fielddict['application_annotations'] = app_annots

Subscribers

People subscribed via source and target branches