def test_unique_strings(self): s = models.ClientStats(group="", created_by="127.0.0.1", created_on=self.NOW) s.save() s.from_dict(self.DATA) self.assertEqual(models.ClientName.objects.all().count(), 10) self.assertEqual(models.ClientNamesAndValues.objects.all().count(), 10) self.assertEqual(models.ClientStringValue.objects.all().count(), 1) s = models.ClientStats(group="", created_by="127.0.0.2", created_on=self.NOW) s.save() s.from_dict(self.DATA) self.assertEqual(models.ClientName.objects.all().count(), 10) self.assertEqual(models.ClientNamesAndValues.objects.all().count(), 20) self.assertEqual(models.ClientStringValue.objects.all().count(), 1) updated_data = dict(**self.DATA) updated_data['stat10'] = 'Hello!' s = models.ClientStats(group="", created_by="127.0.0.3", created_on=self.NOW) s.save() s.from_dict(updated_data) self.assertEqual(models.ClientName.objects.all().count(), 10) self.assertEqual(models.ClientNamesAndValues.objects.all().count(), 30) self.assertEqual(models.ClientStringValue.objects.all().count(), 2) updated_data = dict(**self.DATA) updated_data['stat12'] = 'Hello!' s = models.ClientStats(group="", created_by="127.0.0.4", created_on=self.NOW) s.save() s.from_dict(updated_data) self.assertEqual(models.ClientName.objects.all().count(), 11) self.assertEqual(models.ClientNamesAndValues.objects.all().count(), 41) self.assertEqual(models.ClientStringValue.objects.all().count(), 2)
def test_from_dict(self): s = models.ClientStats(group="", created_by="127.0.0.1", created_on=self.NOW) s.save() s.from_dict(self.DATA) self.assertListEqual( list( str(x) for x in models.ClientName.objects.all().order_by('name')), [ "stat01.stat02", "stat01.stat03", "stat01.stat04", "stat01.stat05", "stat01.stat06", "stat07", "stat08", "stat09", "stat10", "stat11", ]) self.assertListEqual( list(str(x) for x in s.clientnamesandvalues_set.all()), [ '[email protected][stat01.stat02] = str', '[email protected][stat01.stat03] = str', '[email protected][stat01.stat04] = 1', '[email protected][stat01.stat05] = 1', '[email protected][stat01.stat06] = 1.0', '[email protected][stat07] = str', '[email protected][stat08] = str', '[email protected][stat09] = 1', '[email protected][stat11] = 1.0', '[email protected][stat10] = 1', ])
response = http.HttpResponse(content_type='application/javascript') response.write( simplejson.dumps({ 'code': error.ERROR_JSON, 'error': 'Invalid JSON: %s' % e, 'next': -1, })) return response data['user-agent'] = request.META['HTTP_USER_AGENT'] data['ip'] = request.META[settings.HTTP_REMOTE_ADDR_META] if 'HTTP_REFERER' in request.META: data['referrer'] = request.META['HTTP_REFERER'] # Save the stats s = models.ClientStats(group=group, created_by=user) if _now is not None: s.created_on = _now s.save() s.from_dict(data) # Return success response = http.HttpResponse(content_type='application/javascript') response.write(simplejson.dumps({ 'code': error.SUCCESS, 'next': 5, })) return response ###########################################################################################