def testGenerateNetFixtures(self): """ This isn't actually a test. It just takes advantage of the test harness to spam a bunch of messages to the nigeria app and spit out the data in a format that can be sucked into a fixture. It should be moved to some data generator at some point, but is being left here for laziness sake """ # this is the number of net reports that will be generated count = 0 # the sender will always be the same, for now phone = "55555" expected_actual_match_percent = .8 # allow specifying the minimum and maximum dates for message generation min_date = datetime(2009,4,1) max_date = datetime(2009,4,30) min_time = time.mktime(min_date.timetuple()) max_time = time.mktime(max_date.timetuple()) # these are the locations that will be chosen. The actual # location will be a distribution point under one of these # wards wards = [200101, 200102, 200103, 200104, 200105, 200106, 200107, 200108, 200109, 200110, 200201] all_net_strings = [] for i in range(count): # this first part generates a net form at a random DP date = datetime.fromtimestamp(random.randint(min_time, max_time)) ward = Location.objects.get(code=random.choice(wards)) dp = random.choice(ward.children.all()) distributed = random.randint(50,500) expected = random.randint(0,2000) # create an actual amount based on the likelihood of match if random.random() < expected_actual_match_percent: actual = expected else: actual = random.randint(0,2000) discrepancy = random.randint(0,distributed/5) net_string = "%s@%s > llin nets %s %s %s %s %s" % (phone, date.strftime("%Y%m%d%H%M"), dp.code, distributed, expected, actual, discrepancy) all_net_strings.append(net_string) # the second part generates a net card form at a random MT date = datetime.fromtimestamp(random.randint(min_time, max_time)) ward = Location.objects.get(code=random.choice(wards)) dp = random.choice(ward.children.all()) mt = random.choice(dp.children.all()) settlements = random.randint(3, 50) people = random.randint(50, 600) coupons = random.randint(50, 600) net_card_string = "%s@%s > llin net cards %s %s %s %s" % (phone, date.strftime("%Y%m%d%H%M"), mt.code, settlements, people, coupons ) all_net_strings.append(net_card_string) script = "\n".join(all_net_strings) self.runScript(script) dumpdata = Command() filename = os.path.abspath(os.path.join(os.path.dirname(__file__),"fixtures/test_net_data.json")) options = { "indent" : 2 } datadump = dumpdata.handle("bednets", **options)
def dumpFixture(res): import os.path from django.core.management.commands.dumpdata import Command from django.core.management.base import CommandError try: (open("allruns.json","w") .write(Command().handle(use_natural_keys=True, indent=2))) except CommandError, e: log.msg("You might run in to https://code.djangoproject.com/ticket/16317") log.err(e)
def _dumpdata(self): dumpdata = Command() filename = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_schools.json")) options = {"indent": 2} datadump = dumpdata.handle("locations", "reporters", "schools", "blaster", **options) file = open(filename, "w") file.write(datadump) file.close() print "=== Successfully wrote fixtures to %s ===" % filename
def testPropertyDataDump(self): for i, value in enumerate(self.testing_data): prop = Property.objects.create(name='name %s' % i, source='foo', value=value) dumpdata = Command() jsondata = dumpdata.handle('mbdb') data = json.loads(jsondata) value_data = data[0]['fields']['value'] # dump data will always dump the pickled data stringified self.assertEqual(unicode(value), value_data) prop.delete()
def testPropertyDataDump(self): for i, value in enumerate(self.testing_data): prop = Property.objects.create(name='name %s' % i, source='foo', value=value) dumpdata = Command() # when calling handle() directly it's unable to pick up defaults # in Command.option_list so we have to pick that up manually defaults = dict((x.dest, x.default) for x in Command.option_list) jsondata = dumpdata.handle('mbdb', **defaults) data = json.loads(jsondata) value_data = data[0]['fields']['value'] # dump data will always dump the pickled data stringified self.assertEqual(unicode(value), value_data) prop.delete()
def testFixture(self): """"This isn't actually a test. It just takes advantage of the test harness to spam a bunch of messages to the supply app and spit out the data in a format that can be sucked into a fixture""" # this is the number of transactions that will be generated transaction_count = 0 # these are the locations that will be the origins, chosen randomly # from this list # the destinations will be chosen randomly from the origins' children originating_locations = [20, 2001, 2002, 2003,2004] stock_levels = dict([[loc, random.randint(1, 10000) * 10 + 50000] for loc in originating_locations]) # the sender will always be the same, for now phone = "55555" all_txns = [] # these are the percentages these items will match waybill_match_percent = .9 amount_match_percent = .9 loc_match_percent = .95 num_locs = len(Location.objects.all()) # allow specifying the minimum and maximum dates for message generation min_date = datetime(2009,4,1) max_date = datetime(2009,4,30) min_time = time.mktime(min_date.timetuple()) max_time = time.mktime(max_date.timetuple()) # generate the array of dates we're going to use at the start. This is so we can order # our transactions iss_dates = [] for i in range(transaction_count): iss_dates.append(datetime.fromtimestamp(random.randint(min_time, max_time))) iss_dates.sort() rec_dates = [] for i in range(transaction_count): # make the date from a min and max timestamp rec_dates.append(datetime.fromtimestamp( random.randint( # the min is the shipment date time.mktime(iss_dates[i].timetuple()), #the max is the shipment date + 0 to 4 days time.mktime((iss_dates[i] + timedelta(random.randint(0,4))).timetuple())))) for i in range(transaction_count): # get some random data based on the parameters we've set above origin = Location.objects.get(code=random.choice(originating_locations )) destination = random.choice(origin.children.all()) waybill = random.randint(10000,99999) amount = random.randint(1, 500) * 10 diff = stock_levels[int(origin.code)] - amount if diff > 0: stock = diff else: stock = random.randint(1, 10000) * 10 stock_levels[int(origin.code)] = stock issue_string = "%s@%s > llin issue from %s to %s %s %s %s" % (phone, iss_dates[i].strftime("%Y%m%d%H%M"), origin.code, destination.code, waybill, amount, stock) all_txns.append(issue_string) # create a waybill number based on the likelihood of match if random.random() < waybill_match_percent: ret_waybill = waybill else: ret_waybill = random.randint(10000,99999) # create an amount based on the likelihood of match if random.random() < amount_match_percent: ret_amount = amount else: ret_amount = random.randint(1, 500) * 10 # create an origin and destination based on the likelihood of match if random.random() < loc_match_percent: ret_orig = origin else: ret_orig = Location.objects.get(pk=random.randint(1,num_locs)) if random.random() < loc_match_percent: ret_dest = destination else: ret_dest = Location.objects.get(pk=random.randint(1, num_locs)) if stock_levels.has_key(int(ret_dest.code)): ret_stock = stock_levels[int(ret_dest.code)] + amount else: # make sure the stock at the receiver is higher than the amount of the bill ret_stock = random.randint(1, 2000) * 10 + ret_amount stock_levels[int(ret_dest.code)] = ret_stock # make sure the date received is after the date sent receive_string = "%s@%s > llin receive from %s to %s %s %s %s" % (phone, rec_dates[i].strftime("%Y%m%d%H%M"), ret_orig.code, ret_dest.code, ret_waybill, ret_amount, ret_stock) all_txns.append(receive_string) script = "\n".join(all_txns) self.runScript(script) dumpdata = Command() filename = os.path.abspath(os.path.join(os.path.dirname(__file__),"fixtures/test_transactions_stock.json")) options = { "indent" : 2 } datadump = dumpdata.handle("supply", **options)