Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chrome/test/functional/autofill_dataset_generator.py

Issue 6685077: Two sets of Autofill tests.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Generates profile dictionaries for Autofill.
7
8 Used to test autofill.AutofillTest.FormFillLatencyAfterSubmit.
9 Can be used as a stand alone script with -h to print out help text by running:
10 python autofill_dataset_generator.py -h
11 """
12
13 import codecs
14 import logging
15 from optparse import OptionParser
16 import os
17 import random
18 import re
19 import sys
20
21
22 class NullHandler(logging.Handler):
23 def emit(self, record):
24 pass
25
26
27 class DatasetGenerator(object):
28 """Generates a dataset of dictionaries.
29
30 The lists (such as address_construct, city_construct) define the way the
31 corresponding field is generated. They accomplish this by specifying a
32 list of function-args lists.
33 """
34 address_construct = [
35 [ random.randint, 1, 10000],
36 [ None, u'foobar'],
37 [ random.choice, [ u'St', u'Ave', u'Ln', u'Ct', ]],
38 [ random.choice, [ u'#1', u'#2', u'#3', ]],
39 ]
40
41 city_construct = [
42 [ random.choice, [ u'San Jose', u'San Francisco', u'Sacramento',
43 u'Los Angeles', ]],
44 ]
45
46 state_construct = [
47 [ None, u'CA']
48 ]
49
50 zip_construct = [
51 [ None, u'95110']
52 ]
53
54 logger = logging.getLogger(__name__)
55 logger.addHandler(NullHandler())
56 log_handlers = {'StreamHandler': None}
57
58 def __init__(self, output_filename=None, logging_level=None):
59 """Constructs dataset generator object.
60
61 Creates 'fields' data member which is a list of pair (two values) lists.
62 These pairs are comprised of a field key e.g. u'NAME_FIRST' and a
63 generator method e.g. self.GenerateNameFirst which will generate the value.
64 If we want the value to always be the same e.g. u'John' we can use this
65 instead of a method. We can even use None keyword which will give
66 a value of u''.
67
68 'output_pattern' for one field would have been: "{u'NAME_FIRST': u'%s',}"
69 which is ready to accept a value for the 'NAME_FIRST' field key once
70 this value is generated.
71 'output_pattern' is used in 'GenerateNextDict()' to generate the next
72 dict line.
73
74 Args:
75 output_filename: specified filename of generated dataset to be saved.
76 Default value is None and no saving takes place.
77 logging_level: set verbosity levels, default is None.
78 """
79 if logging_level:
80 if not self.log_handlers['StreamHandler']:
81 console = logging.StreamHandler()
82 console.setLevel(logging.INFO)
83 self.log_handlers['StreamHandler'] = console
84 self.logger.addHandler(console)
85 self.logger.setLevel(logging_level)
86 else:
87 if self.log_handlers['StreamHandler']:
88 self.logger.removeHandler(self._log_handlers['StreamHandler'])
89 self.log_handlers['StreamHandler'] = None
90
91 self.output_filename = output_filename
92
93 self.dict_no = 0
94 self.fields = [
95 [u'NAME_FIRST', self.GenerateNameFirst],
96 [u'NAME_MIDDLE', None],
97 [u'NAME_LAST', None],
98 [u'EMAIL_ADDRESS', self.GenerateEmail],
99 [u'COMPANY_NAME', None],
100 [u'ADDRESS_HOME_LINE1', self.GenerateAddress],
101 [u'ADDRESS_HOME_LINE2', None],
102 [u'ADDRESS_HOME_CITY', self.GenerateCity],
103 [u'ADDRESS_HOME_STATE', self.GenerateState],
104 [u'ADDRESS_HOME_ZIP', self.GenerateZip],
105 [u'ADDRESS_HOME_COUNTRY', u'United States'],
106 [u'PHONE_HOME_WHOLE_NUMBER', None],
107 [u'PHONE_FAX_WHOLE_NUMBER', u'6501234555'],
108 ]
109
110 self.next_dict = {}
111 self.output_pattern = u"{'" +\
112 u', '.join([u'u"%s" : u"%%s"' % key for key, method in self.fields]) +\
113 u',}'
dennis_jeffrey 2011/03/25 16:56:14 It seems more conventional to use implicit string
dyu1 2011/03/25 19:10:57 Done.
114
115 def _GenerateField(self, field_construct):
116 """Generates each field in each dictionary.
117
118 Args:
119 field_construct: it is a list of lists.
120 The first value (index 0) of each containing list is a function or None.
121 The remaining values are the args. If function is None then arg is just
122 returned.
123
124 Example 1: zip_construct = [[ None, u'95110']]. There is one
125 containing list only and function here is None and arg is u'95110'.
126 This just returns u'95110'.
127
128 Example 2: address_construct = [ [ random.randint, 1, 10000],
129 [ None, u'foobar'] ] This has two containing lists and it will return
130 the result of:
131 random.randint(1, 10000) + ' ' + u'foobar'
132 which could be u'7832 foobar'
133 """
134 parts = []
135 for function_and_args in field_construct:
136 function = function_and_args[0]
137 args = function_and_args[1:]
138 if not function:
139 function = lambda x: x
140 parts.append(u'%s' % function(*args))
141 return (' ').join(parts)
142
143 def GenerateAddress(self):
144 """Uses _GenerateField() and address_construct to gen a random address.
145
146 Returns:
147 A random address.
148 """
149 return self._GenerateField(self.address_construct)
150
151 def GenerateCity(self):
152 """Uses _GenerateField() and city_construct to gen a random city.
153
154 Returns:
155 A random city.
156 """
157 return self._GenerateField(self.city_construct)
158
159 def GenerateState(self):
160 """Uses _GenerateField() and state_construct to generate a state.
161
162 Returns:
163 A state.
164 """
165 return self._GenerateField(self.state_construct)
166
167 def GenerateZip(self):
168 """Uses _GenerateField() and zip_construct to generate a zip code.
169
170 Returns:
171 A zip code.
172 """
173 return self._GenerateField(self.zip_construct)
174
175 def GenerateCountry(self):
176 """Uses _GenerateField() and country_construct to generate a country.
177
178 Returns:
179 A country.
180 """
181 return self._GenerateField(self.country_construct)
182
183 def GenerateNameFirst(self):
184 """Generates a numerical first name.
185
186 The name is the number of the current dict.
187 i.e. u'1', u'2', u'3'
188
189 Returns:
190 A numerical first name.
191 """
192 return u'%s' % self.dict_no
193
194 def GenerateEmail(self):
195 """Generates an email that corresponds to the first name.
196
197 i.e. u'1@example.com', u'2@example.com', u'3@example.com'
198
199 Returns:
200 An email address that corresponds to the first name.
201 """
202 return u'%s@example.com' % self.dict_no
203
204
205 def GenerateNextDict(self):
206 """Generates next dictionary of the dataset.
207
208 Returns:
209 The output dictionary.
210 """
211 self.dict_no += 1
212 output_dict = {}
213 for key, method_or_value in self.fields:
214 if not method_or_value:
215 self.next_dict[key] = ''
216 elif type(method_or_value) in [str, unicode]:
217 self.next_dict[key] = '%s' % method_or_value
218 else:
219 self.next_dict[key] = method_or_value()
220 return self.next_dict
221
222 def GenerateDataset(self, num_of_dict_to_generate=10):
223 """Generates a list of dictionaries.
224
225 Args:
226 num_of_dict_to_generate: The number of dictionaries to be generated.
227 Default value is 10.
228
229 Returns:
230 The dictionary list.
231 """
232 random.seed(0)
233 if self.output_filename:
234 output_file = codecs.open(
235 self.output_filename, mode='wb', encoding='utf-8-sig')
236 else:
237 output_file = None
238 try:
239 list_of_dict = []
240 if output_file:
241 output_file.write('[')
242 output_file.write(os.linesep)
243
244 while self.dict_no < num_of_dict_to_generate:
245 output_dict = self.GenerateNextDict()
246 list_of_dict.append(output_dict)
247 output_line = self.output_pattern % tuple(
248 [output_dict[key] for [key, method] in self.fields])
249 if output_file:
250 output_file.write(output_line)
251 output_file.write(os.linesep)
252 self.logger.info(
253 '%d: %s' % (self.dict_no, output_line.encode(sys.stdout.encoding,
254 'ignore')))
255
256 if output_file:
257 output_file.write(']')
258 output_file.write(os.linesep)
259 self.logger.info('%d dictionaries generated SUCCESSFULLY!', self.dict_no)
260 self.logger.info('--- FINISHED ---')
261 return list_of_dict
262 finally:
263 if output_file:
264 output_file.close()
265
266
267 def main():
268 # Command line options.
269 parser = OptionParser()
270 parser.add_option(
271 '-o', '--output', dest='output_filename', default='',
272 help='write output to FILE [optional]', metavar='FILE')
273 parser.add_option(
274 '-d', '--dict', type='int', dest='dict_no', metavar='DICT_NO', default=10,
275 help='DICT_NO: number of dictionaries to be generated [default: %default]')
276 parser.add_option(
277 '-l', '--log_level', dest='log_level', default='debug',
278 metavar='LOG_LEVEL',
279 help='LOG_LEVEL: "debug", "info", "warning" or "error" [default: %default]')
280
281 (options, args) = parser.parse_args()
282 if args:
283 parser.print_help()
284 sys.exit(1)
285 options.log_level = options.log_level.lower()
286 if options.log_level not in ['debug', 'info', 'warning', 'error']:
287 parser.error('Wrong log_level argument.')
288 parser.print_help()
289 else:
290 if options.log_level == 'debug':
291 options.log_level = logging.DEBUG
292 elif options.log_level == 'info':
293 options.log_level = logging.INFO
294 elif options.log_level == 'warning':
295 options.log_level = logging.WARNING
296 elif options.log_level == 'error':
297 options.log_level = logging.ERROR
298
299 gen = DatasetGenerator(options.output_filename, options.log_level)
300 gen.GenerateDataset(options.dict_no)
301
302
303 if __name__ == '__main__':
304 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698