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

Proposed by Nicola Larosa
Status: Merged
Approved by: dobey
Approved revision: 46
Merged at revision: not available
Proposed branch: lp:~teknico/desktopcouch/more-varied-random-contacts
Merge into: lp:desktopcouch
Diff against target: None lines
To merge this branch: bzr merge lp:~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.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
1=== modified file 'desktopcouch/contacts/testing/create.py'
2--- desktopcouch/contacts/testing/create.py 2009-08-19 10:07:54 +0000
3+++ desktopcouch/contacts/testing/create.py 2009-09-04 08:59:18 +0000
4@@ -30,25 +30,55 @@
5 'Daniel', 'William', 'James', 'Alfie', 'Grace', 'Ruby', 'Olivia',
6 'Emily', 'Jessica', 'Sophie', 'Chloe', 'Lily', 'Ella', 'Amelia')
7 LAST_NAMES = ('Dalglish', 'Grobbelaar', 'Lawrenson', 'Beglin', 'Nicol',
8- 'Whelan', 'Hansen', 'Johnston', 'Rush', 'Molby', 'MacDonald', 'McMahon')
9+ 'Whelan', 'Hansen', 'Johnston', 'Rush', 'Molby', 'MacDonald', 'McMahon',
10+ 'Penny', 'Leicester', 'Langley', 'Commodore', 'Touchstone', 'Fielding')
11+COMPANIES = ('Gostram', 'Grulthing', 'Nasform', 'Smarfish', 'Builsank')
12+STREET_TYPES = ('Street', 'Road', 'Lane', 'Avenue', 'Square', 'Park', 'Mall')
13 CITIES = ('Scunthorpe', 'Birmingham', 'Cambridge', 'Durham', 'Bedford')
14 COUNTRIES = ('England', 'Ireland', 'Scotland', 'Wales')
15
16-def random_string(length=10):
17- """Return a string of random lowercase letters and of specified length"""
18- return ''.join(random.sample(string.lowercase, length))
19+def head_or_tails():
20+ """Randomly return True or False"""
21+ return random.choice((False, True))
22+
23+def random_bools(num_bools, at_least_one_true=True):
24+ """
25+ Return a sequence of random booleans. If at_least_one_true,
26+ guarantee at list one True value.
27+ """
28+ if num_bools < 2:
29+ raise RuntimeError('Cannot build a sequence smaller than two elements')
30+ bools = [head_or_tails() for __ in range(num_bools)]
31+ if at_least_one_true and not any(bools):
32+ bools[random.randrange(num_bools)] = True
33+ return bools
34+
35+def random_string(length=10, upper=False):
36+ """
37+ Return a string, of specified length, of random lower or uppercase letters.
38+ """
39+ charset = string.uppercase if upper else string.lowercase
40+ return ''.join(random.sample(charset, length))
41
42 def random_postal_address():
43 """Return something that looks like a postal address"""
44- return '%s Street' % random.choice(LAST_NAMES)
45+ return '%d %s %s' % (random.randint(1, 100), random.choice(LAST_NAMES),
46+ random.choice(STREET_TYPES))
47
48-def random_email_address(first_name, last_name):
49- """Return something that looks like an email address"""
50- return '%s.%s@%s.com' % (first_name, last_name, random_string(3))
51+def random_email_address(
52+ first_name='', last_name='', company='', address_type='other'):
53+ """
54+ Return something that looks like an email address.
55+ address_type values: 'personal', 'work', 'other'.
56+ """
57+ # avoid including the dot if one or both names are missing
58+ pers_name = '.'.join([name for name in (first_name, last_name) if name])
59+ return {'personal': pers_name, 'work': company,
60+ }.get(address_type, random_string(5)) + '@example.com'
61
62 def random_postal_code():
63 """Return something that looks like a postal code"""
64- return '%s12 3%s' % (random_string(2), random_string(2))
65+ return '%s12 3%s' % (random_string(2, True), random_string(2, True))
66
67 def random_phone_number():
68 """Return something that looks like a phone number"""
69@@ -65,11 +95,39 @@
70 # Record schema
71 fielddict = {'record_type': doctype, 'record_type_version': '1.0'}
72 # Names
73- first_name = random.choice(FIRST_NAMES) + str(maincount)
74- last_name = random.choice(LAST_NAMES)
75+ # at least one of the three will be present
76+ has_first_name, has_last_name, has_company_name = random_bools(3)
77+ first_name = (random.choice(FIRST_NAMES) + str(maincount)
78+ ) if has_first_name else ''
79+ last_name = random.choice(LAST_NAMES) if has_last_name else ''
80+ company = (random.choice(COMPANIES) + str(maincount)
81+ ) if has_company_name else ''
82+ # Address places and types
83+ address_places, email_types, phone_places = [], [], []
84+ if has_first_name or has_last_name:
85+ for (has_it, seq, val) in zip(
86+ # at least one of the three will be present
87+ random_bools(3),
88+ (address_places, email_types, phone_places),
89+ ('home', 'personal', 'home')):
90+ if has_it:
91+ seq.append(val)
92+ if has_company_name:
93+ for (has_it, seq) in zip(
94+ # at least one of the three will be present
95+ random_bools(3),
96+ (address_places, email_types, phone_places)):
97+ if has_it:
98+ seq.append('work')
99+ for (has_it, seq) in zip(
100+ # none of the three may be present
101+ random_bools(3, at_least_one_true=False),
102+ (address_places, email_types, phone_places)):
103+ if has_it:
104+ seq.append('other')
105 # Addresses
106 addresses = {}
107- for address_type in ('home', 'work', 'other'):
108+ for address_type in address_places:
109 addresses[str(uuid.uuid4())] = {
110 'address1': random_postal_address(), 'address2': '',
111 'pobox': '', 'city': random.choice(CITIES),
112@@ -77,20 +135,23 @@
113 'country': random.choice(COUNTRIES), 'description': address_type}
114 # Email addresses
115 email_addresses = {}
116- for email_address_type in ('home', 'work', 'other'):
117+ for email_address_type in email_types:
118 email_addresses[str(uuid.uuid4())] = {
119- 'address': random_email_address(first_name, last_name),
120+ 'address': random_email_address(
121+ first_name, last_name, company, email_address_type),
122 'description': email_address_type}
123 # Phone numbers
124 phone_numbers = {}
125- for phone_number_type in ('home', 'work', 'other'):
126+ for phone_number_type in phone_places:
127 phone_numbers[str(uuid.uuid4())] = {
128 'priority': 0, 'number': random_phone_number(),
129 'description': phone_number_type}
130 # Store data in fielddict
131+ if (has_first_name or has_last_name) and head_or_tails():
132+ fielddict['birth_date'] = random_birth_date()
133 fielddict.update({'first_name': first_name, 'last_name': last_name,
134 'addresses': addresses, 'email_addresses': email_addresses,
135- 'phone_numbers': phone_numbers, 'birth_date': random_birth_date()})
136+ 'phone_numbers': phone_numbers, 'company': company})
137 # Possibly add example application annotations
138 if app_annots:
139 fielddict['application_annotations'] = app_annots

Subscribers

People subscribed via source and target branches