def client_tst(self, test_csv, save=True, primary_attr='hostname'): resp = self.client.post('/en-US/csv/ajax_csv_importer/', { 'csv-data': test_csv, 'save': save, 'primary-attr': primary_attr }) if resp.status_code != 200: # The exception thrown by csv_import is more useful than a status # code so we are running the function knowing it will fail. TODO, # figure out a better way for tests to know what went wrong. csv_import(test_csv, save=save)
def test_get_related_spaces(self): test_csv = """ hostname, operating_system %name,serial,system_type%type_name,warranty_start,warranty_end,allocation%name baz.mozilla.com,foo,asdf,foobar,2012-01-01,2013-01-01,something """ self.assertRaises(Exception, self.client_tst, test_csv) test_csv = """ hostname, operating_system % name,serial,system_type%type_name,warranty_start,warranty_end,allocation%name baz.mozilla.com,foo%foo,asdf,foobar,2012-01-01,2013-01-01,something """ self.assertRaises(Exception, self.client_tst, test_csv) test_csv = """ hostname,operating_system,serial,system_type%type_name,warranty_start,warranty_end baz.mozilla.com, foo % foo,asdf,foobar,2012-01-01,2013-01-01 """ self.assertRaises(Exception, self.client_tst, test_csv) test_csv = """ hostname,operating_system%version,serial,system_type%type_name,warranty_start,warranty_end,allocation%name baz.mozilla.com, foo %foo,asdf,foobar,2012-01-01,2013-01-01,something """ self.assertRaises(Exception, self.client_tst, test_csv) test_csv = """ hostname,operating_system%name%version,serial,system_type%type_name,warranty_start,warranty_end,allocation%name foobob.mozilla.com,foo% 1.1,asdf,foobar,2012-01-01,2013-01-01,something """ ret = csv_import(test_csv) self.assertEqual(1, len(ret)) self.assertTrue(ret[0]['system'])
def test_keyvalue(self): test_csv = """ hostname,nic.0.mac_address.0,serial,warranty_start,warranty_end,system_type%type_name,allocation%name foobob.mozilla.com,keyvalue,asdf,2012-01-01,2013-01-01,foobar,something """ ret = csv_import(test_csv, save=False) self.assertTrue(ret[0]['kvs'])
def test_multiple_no_save(self): test_csv = """ hostname,operating_system%name%version,serial,system_type%type_name,warranty_start,warranty_end,allocation%name foobob.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 1fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 2fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 3fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 4fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 5fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 6fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 7fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something 8fooboz.mozilla.com,foo%1.1,asdf,foobar,2012-01-01,2013-01-01,something """ before = System.objects.all().count() ret = csv_import(test_csv, save=False) after = System.objects.all().count() self.assertEqual(9, len(ret)) self.assertEqual(before, after)
def do_csv_import(data): try: return csv_import(data, primary_attr=primary_attr, save=save) except ValidationError, e: transaction.rollback() return {'error': e.messages}