def create_basic_sampledata(): print 'Generating 100 happy and 100 sad responses...' happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) # Note: We're abusing sentence_generator to just return random # choice from a tuple of things. products = sentence_generator(PRODUCT_TUPLES) locales = locale_generator() urls = sentence_generator(URLS) # Create 100 happy responses. now = time.time() objs = [] for i in range(100): product = products.next() now = now - random.randint(500, 2000) if product[0] in ALWAYS_API: api = '1' elif product[0] in NEVER_API: api = None else: api = random.choice(('1', None)) objs.append( ResponseFactory.build(happy=True, description=happy_feedback.next(), product=product[0], version=product[1], platform=product[2], user_agent=product[3], locale=locales.next(), created=datetime.datetime.fromtimestamp(now), api=api)) # Create 100 sad responses. now = time.time() for i in range(100): product = products.next() now = now - random.randint(500, 2000) if product[0] in ALWAYS_API: api = '1' elif product[0] in NEVER_API: api = None else: api = random.choice(('1', None)) objs.append( ResponseFactory.build(happy=False, description=sad_feedback.next(), product=product[0], version=product[1], platform=product[2], locale=locales.next(), user_agent=product[3], url=urls.next(), created=datetime.datetime.fromtimestamp(now), api=api)) Response.objects.bulk_create(objs)
def test_happy_to_rating(self): """Test we don't populate rating from happy""" resp = ResponseFactory.build(happy=True, rating=None) resp.save() eq_(resp.rating, None) resp = ResponseFactory.build(happy=False, rating=None) resp.save() eq_(resp.rating, None)
def test_rating_to_happy(self): """Test that we do populate happy from rating""" data = {1: False, 2: False, 3: False, 4: True, 5: True} for rat, expected in data.items(): # Create the response, but DON'T save it to the db. resp = ResponseFactory.build(happy=None, rating=rat) resp.save() eq_(resp.happy, expected)
def create_basic_sampledata(): happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) products = sentence_generator(PRODUCTS) platforms = sentence_generator(PLATFORMS) locales = sentence_generator(settings.DEV_LANGUAGES) urls = sentence_generator(URLS) # Create 100 happy responses. now = time.time() objs = [] for i in range(100): product = products.next() now = now - random.randint(500, 2000) objs.append( ResponseFactory.build( happy=True, description=happy_feedback.next(), product=product[0], version=product[1], platform=platforms.next(), locale=locales.next(), created=datetime.datetime.fromtimestamp(now) ) ) # Create 100 sad responses. now = time.time() for i in range(100): product = products.next() now = now - random.randint(500, 2000) objs.append( ResponseFactory.build( happy=False, description=sad_feedback.next(), product=product[0], version=product[1], platform=platforms.next(), locale=locales.next(), url=urls.next(), created=datetime.datetime.fromtimestamp(now) ) ) Response.objects.bulk_create(objs)
def create_additional_sampledata(samplesize="1000"): samplesize = int(samplesize) print "Generating {0} feedback responses...".format(samplesize) happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) products = sentence_generator(PRODUCT_TUPLES) urls = sentence_generator(URLS) locales = locale_generator() objs = [] now = time.time() for i in range(samplesize): now = now - random.randint(500, 2000) happy = random.choice([True, False]) if happy: description = happy_feedback.next() url = u"" else: description = sad_feedback.next() url = urls.next() product = products.next() if product[0] in ALWAYS_API: api = "1" elif product[0] in NEVER_API: api = None else: api = random.choice(("1", None)) objs.append( ResponseFactory.build( happy=happy, description=description, product=product[0], version=product[1], platform=product[2], url=url, user_agent=product[3], locale=locales.next(), created=datetime.datetime.fromtimestamp(now), api=api, ) ) # Bulk-save the objects to the db 500 at a time and # print something to stdout about it. if i % 500 == 0: Response.objects.bulk_create(objs) objs = [] print " {0}...".format(i) if objs: print " {0}...".format(samplesize) Response.objects.bulk_create(objs) objs = []
def create_additional_sampledata(samplesize='1000'): samplesize = int(samplesize) print 'Generating {0} feedback responses...'.format(samplesize) happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) products = sentence_generator(PRODUCT_TUPLES) urls = sentence_generator(URLS) locales = locale_generator() objs = [] now = time.time() for i in range(samplesize): now = now - random.randint(500, 2000) happy = random.choice([True, False]) if happy: description = happy_feedback.next() url = u'' else: description = sad_feedback.next() url = urls.next() product = products.next() if product[0] in ALWAYS_API: api = '1' elif product[0] in NEVER_API: api = None else: api = random.choice(('1', None)) objs.append( ResponseFactory.build(happy=happy, description=description, product=product[0], version=product[1], platform=product[2], url=url, user_agent=product[3], locale=locales.next(), created=datetime.datetime.fromtimestamp(now), api=api)) # Bulk-save the objects to the db 500 at a time and # print something to stdout about it. if i % 500 == 0: Response.objects.bulk_create(objs) objs = [] print ' {0}...'.format(i) if objs: print ' {0}...'.format(samplesize) Response.objects.bulk_create(objs) objs = []
def create_basic_sampledata(): happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) products = sentence_generator(PRODUCTS) platforms = sentence_generator(PLATFORMS) locales = sentence_generator(settings.DEV_LANGUAGES) urls = sentence_generator(URLS) # Create 100 happy responses. now = time.time() objs = [] for i in range(100): product = products.next() now = now - random.randint(500, 2000) objs.append( ResponseFactory.build( happy=True, description=happy_feedback.next(), product=product[0], version=product[1], platform=platforms.next(), locale=locales.next(), created=datetime.datetime.fromtimestamp(now))) # Create 100 sad responses. now = time.time() for i in range(100): product = products.next() now = now - random.randint(500, 2000) objs.append( ResponseFactory.build( happy=False, description=sad_feedback.next(), product=product[0], version=product[1], platform=platforms.next(), locale=locales.next(), url=urls.next(), created=datetime.datetime.fromtimestamp(now))) Response.objects.bulk_create(objs)
def create_additional_sampledata(samplesize): samplesize = int(samplesize) print 'Working on generating {0} feedback responses....'.format( samplesize) happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) products = sentence_generator(PRODUCTS) urls = sentence_generator(URLS) user_agents = sentence_generator(USER_AGENTS) locales = sentence_generator(settings.DEV_LANGUAGES) objs = [] now = time.time() for i in range(samplesize): now = now - random.randint(500, 2000) happy = random.choice([True, False]) if happy: description = happy_feedback.next() url = u'' else: description = sad_feedback.next() url = urls.next() product = products.next() objs.append( ResponseFactory.build( happy=happy, description=description, product=product[0], version=product[1], url=url, user_agent=user_agents.next(), locale=locales.next(), created=datetime.datetime.fromtimestamp(now) ) ) # Bulk-save the objects to the db 500 at a time and # print something to stdout about it. if i % 500 == 0: Response.objects.bulk_create(objs) objs = [] print ' {0}...'.format(i) if objs: print ' {0}...'.format(samplesize) Response.objects.bulk_create(objs) objs = []
def test_rating_to_happy(self): """Test that we do populate happy from rating""" data = { 1: False, 2: False, 3: False, 4: True, 5: True } for rat, expected in data.items(): # Create the response, but DON'T save it to the db. resp = ResponseFactory.build(happy=None, rating=rat) resp.save() eq_(resp.happy, expected)
def create_additional_sampledata(samplesize): samplesize = int(samplesize) print 'Working on generating {0} feedback responses....'.format(samplesize) happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) products = sentence_generator(PRODUCTS) urls = sentence_generator(URLS) user_agents = sentence_generator(USER_AGENTS) locales = sentence_generator(settings.DEV_LANGUAGES) objs = [] now = time.time() for i in range(samplesize): now = now - random.randint(500, 2000) happy = random.choice([True, False]) if happy: description = happy_feedback.next() url = u'' else: description = sad_feedback.next() url = urls.next() product = products.next() objs.append( ResponseFactory.build( happy=happy, description=description, product=product[0], version=product[1], url=url, user_agent=user_agents.next(), locale=locales.next(), created=datetime.datetime.fromtimestamp(now))) # Bulk-save the objects to the db 500 at a time and # print something to stdout about it. if i % 500 == 0: Response.objects.bulk_create(objs) objs = [] print ' {0}...'.format(i) if objs: print ' {0}...'.format(samplesize) Response.objects.bulk_create(objs) objs = []
def create_basic_sampledata(): print "Generating 100 happy and 100 sad responses..." happy_feedback = sentence_generator(HAPPY_FEEDBACK) sad_feedback = sentence_generator(SAD_FEEDBACK) # Note: We're abusing sentence_generator to just return random # choice from a tuple of things. products = sentence_generator(PRODUCT_TUPLES) locales = locale_generator() urls = sentence_generator(URLS) # Create 100 happy responses. now = time.time() objs = [] for i in range(100): product = products.next() now = now - random.randint(500, 2000) if product[0] in ALWAYS_API: api = "1" elif product[0] in NEVER_API: api = None else: api = random.choice(("1", None)) objs.append( ResponseFactory.build( happy=True, description=happy_feedback.next(), product=product[0], version=product[1], platform=product[2], user_agent=product[3], locale=locales.next(), created=datetime.datetime.fromtimestamp(now), api=api, ) ) # Create 100 sad responses. now = time.time() for i in range(100): product = products.next() now = now - random.randint(500, 2000) if product[0] in ALWAYS_API: api = "1" elif product[0] in NEVER_API: api = None else: api = random.choice(("1", None)) objs.append( ResponseFactory.build( happy=False, description=sad_feedback.next(), product=product[0], version=product[1], platform=product[2], locale=locales.next(), user_agent=product[3], url=urls.next(), created=datetime.datetime.fromtimestamp(now), api=api, ) ) Response.objects.bulk_create(objs)