73 while value in _GENERATED_VALUES.get((self.e_schema.type, attrname), ()): |
73 while value in _GENERATED_VALUES.get((self.e_schema.type, attrname), ()): |
74 index += 1 |
74 index += 1 |
75 value = self.__generate_value(attrname, index, **kwargs) |
75 value = self.__generate_value(attrname, index, **kwargs) |
76 _GENERATED_VALUES.setdefault((self.e_schema.type, attrname), set()).add(value) |
76 _GENERATED_VALUES.setdefault((self.e_schema.type, attrname), set()).add(value) |
77 return value |
77 return value |
78 |
78 |
79 def __generate_value(self, attrname, index, **kwargs): |
79 def __generate_value(self, attrname, index, **kwargs): |
80 """generates a consistent value for 'attrname'""" |
80 """generates a consistent value for 'attrname'""" |
81 attrtype = str(self.e_schema.destination(attrname)).lower() |
81 attrtype = str(self.e_schema.destination(attrname)).lower() |
82 # Before calling generate_%s functions, try to find values domain |
82 # Before calling generate_%s functions, try to find values domain |
83 etype = self.e_schema.type |
83 etype = self.e_schema.type |
143 if maxvalue is not None and maxvalue <= 0 and minvalue is None: |
143 if maxvalue is not None and maxvalue <= 0 and minvalue is None: |
144 minvalue = maxvalue - index # i.e. randint(-index, 0) |
144 minvalue = maxvalue - index # i.e. randint(-index, 0) |
145 else: |
145 else: |
146 maxvalue = maxvalue or index |
146 maxvalue = maxvalue or index |
147 return randint(minvalue or 0, maxvalue) |
147 return randint(minvalue or 0, maxvalue) |
148 |
148 |
149 generate_int = generate_integer |
149 generate_int = generate_integer |
150 |
150 |
151 def generate_float(self, attrname, index): |
151 def generate_float(self, attrname, index): |
152 """generates a consistent value for 'attrname' if it's a float""" |
152 """generates a consistent value for 'attrname' if it's a float""" |
153 return float(randint(-index, index)) |
153 return float(randint(-index, index)) |
154 |
154 |
155 def generate_decimal(self, attrname, index): |
155 def generate_decimal(self, attrname, index): |
156 """generates a consistent value for 'attrname' if it's a float""" |
156 """generates a consistent value for 'attrname' if it's a float""" |
157 return Decimal(str(self.generate_float(attrname, index))) |
157 return Decimal(str(self.generate_float(attrname, index))) |
158 |
158 |
159 def generate_date(self, attrname, index): |
159 def generate_date(self, attrname, index): |
160 """generates a random date (format is 'yyyy-mm-dd')""" |
160 """generates a random date (format is 'yyyy-mm-dd')""" |
161 return date(randint(2000, 2004), randint(1, 12), randint(1, 28)) |
161 return date(randint(2000, 2004), randint(1, 12), randint(1, 28)) |
162 |
162 |
163 def generate_time(self, attrname, index): |
163 def generate_time(self, attrname, index): |
164 """generates a random time (format is ' HH:MM')""" |
164 """generates a random time (format is ' HH:MM')""" |
165 return timedelta(0, 11, index%60) #'11:%02d' % (index % 60) |
165 return timedelta(0, 11, index%60) #'11:%02d' % (index % 60) |
166 |
166 |
167 def generate_datetime(self, attrname, index): |
167 def generate_datetime(self, attrname, index): |
168 """generates a random date (format is 'yyyy-mm-dd HH:MM')""" |
168 """generates a random date (format is 'yyyy-mm-dd HH:MM')""" |
169 return datetime(randint(2000, 2004), randint(1, 12), randint(1, 28), 11, index%60) |
169 return datetime(randint(2000, 2004), randint(1, 12), randint(1, 28), 11, index%60) |
170 |
170 |
171 |
171 |
172 def generate_bytes(self, attrname, index, format=None): |
172 def generate_bytes(self, attrname, index, format=None): |
173 # modpython way |
173 # modpython way |
174 fakefile = Binary("%s%s" % (attrname, index)) |
174 fakefile = Binary("%s%s" % (attrname, index)) |
175 fakefile.filename = "file_%s" % attrname |
175 fakefile.filename = "file_%s" % attrname |
176 fakefile.value = fakefile.getvalue() |
176 fakefile.value = fakefile.getvalue() |
177 return fakefile |
177 return fakefile |
178 |
178 |
179 def generate_boolean(self, attrname, index): |
179 def generate_boolean(self, attrname, index): |
180 """generates a consistent value for 'attrname' if it's a boolean""" |
180 """generates a consistent value for 'attrname' if it's a boolean""" |
181 return index % 2 == 0 |
181 return index % 2 == 0 |
182 |
182 |
183 def generate_Any_data_format(self, index, **kwargs): |
183 def generate_Any_data_format(self, index, **kwargs): |
184 # data_format attribute of Image/File has no vocabulary constraint, we |
184 # data_format attribute of Image/File has no vocabulary constraint, we |
185 # need this method else stupid values will be set which make mtconverter |
185 # need this method else stupid values will be set which make mtconverter |
186 # raise exception |
186 # raise exception |
187 return u'application/octet-stream' |
187 return u'application/octet-stream' |
188 |
188 |
189 def generate_Any_content_format(self, index, **kwargs): |
189 def generate_Any_content_format(self, index, **kwargs): |
190 # content_format attribute of EmailPart has no vocabulary constraint, we |
190 # content_format attribute of EmailPart has no vocabulary constraint, we |
191 # need this method else stupid values will be set which make mtconverter |
191 # need this method else stupid values will be set which make mtconverter |
192 # raise exception |
192 # raise exception |
193 return u'text/plain' |
193 return u'text/plain' |
363 sym.add( (subj, obj) ) |
363 sym.add( (subj, obj) ) |
364 if rschema.symetric and (obj, subj) in sym: |
364 if rschema.symetric and (obj, subj) in sym: |
365 continue |
365 continue |
366 subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality') |
366 subjcard, objcard = rschema.rproperty(subj, obj, 'cardinality') |
367 # process mandatory relations first |
367 # process mandatory relations first |
368 if subjcard in '1+' or objcard in '1+': |
368 if subjcard in '1+' or objcard in '1+': |
369 queries += self.make_relation_queries(sedict, oedict, |
369 queries += self.make_relation_queries(sedict, oedict, |
370 rschema, subj, obj) |
370 rschema, subj, obj) |
371 else: |
371 else: |
372 delayed.append( (subj, obj) ) |
372 delayed.append( (subj, obj) ) |
373 for subj, obj in delayed: |
373 for subj, obj in delayed: |
374 queries += self.make_relation_queries(sedict, oedict, rschema, |
374 queries += self.make_relation_queries(sedict, oedict, rschema, |
375 subj, obj) |
375 subj, obj) |
376 return queries |
376 return queries |
377 |
377 |
378 def qargs(self, subjeids, objeids, subjcard, objcard, subjeid, objeid): |
378 def qargs(self, subjeids, objeids, subjcard, objcard, subjeid, objeid): |
379 if subjcard in '?1': |
379 if subjcard in '?1': |
380 subjeids.remove(subjeid) |
380 subjeids.remove(subjeid) |
381 if objcard in '?1': |
381 if objcard in '?1': |
382 objeids.remove(objeid) |
382 objeids.remove(objeid) |
450 for subjeid, objeid in zip(subjeidsiter, objeidsiter): |
450 for subjeid, objeid in zip(subjeidsiter, objeidsiter): |
451 if subjeid != objeid and not (subjeid, objeid) in used: |
451 if subjeid != objeid and not (subjeid, objeid) in used: |
452 used.add( (subjeid, objeid) ) |
452 used.add( (subjeid, objeid) ) |
453 yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
453 yield q, self.qargs(subjeids, objeids, subjcard, objcard, |
454 subjeid, objeid) |
454 subjeid, objeid) |
455 |
455 |
456 def check_card_satisfied(card, remaining, subj, rschema, obj): |
456 def check_card_satisfied(card, remaining, subj, rschema, obj): |
457 if card in '1+' and remaining: |
457 if card in '1+' and remaining: |
458 raise Exception("can't satisfy cardinality %s for relation %s %s %s" |
458 raise Exception("can't satisfy cardinality %s for relation %s %s %s" |
459 % (card, subj, rschema, obj)) |
459 % (card, subj, rschema, obj)) |
460 |
460 |