def test_duplicate_entries_not_made(self): """ If there is no change in an entry (besides changed_by and change_date), a new entry is not made. """ with io.open(self.fixture_path, "rb") as data: entries_created = deserialize_json(data, self.test_username) self.assertEqual(2, entries_created) with io.open(self.fixture_path, "rb") as data: entries_created = deserialize_json(data, self.test_username) self.assertEqual(0, entries_created) # Importing twice will still only result in 2 records (second import a no-op). self.assertEqual(2, ExampleDeserializeConfig.objects.count()) # Change Betty. betty = ExampleDeserializeConfig.current('betty') betty.int_field = -8 betty.save() self.assertEqual(3, ExampleDeserializeConfig.objects.count()) self.assertEqual(-8, ExampleDeserializeConfig.current('betty').int_field) # Now importing will add a new entry for Betty. with io.open(self.fixture_path, "rb") as data: entries_created = deserialize_json(data, self.test_username) self.assertEqual(1, entries_created) self.assertEqual(4, ExampleDeserializeConfig.objects.count()) self.assertEqual(5, ExampleDeserializeConfig.current('betty').int_field)
def test_duplicate_entries_not_made(self): """ If there is no change in an entry (besides changed_by and change_date), a new entry is not made. """ with open(self.fixture_path) as data: entries_created = deserialize_json(data, self.test_username) self.assertEquals(2, entries_created) with open(self.fixture_path) as data: entries_created = deserialize_json(data, self.test_username) self.assertEquals(0, entries_created) # Importing twice will still only result in 2 records (second import a no-op). self.assertEquals(2, ExampleDeserializeConfig.objects.count()) # Change Betty. betty = ExampleDeserializeConfig.current('betty') betty.int_field = -8 betty.save() self.assertEquals(3, ExampleDeserializeConfig.objects.count()) self.assertEquals(-8, ExampleDeserializeConfig.current('betty').int_field) # Now importing will add a new entry for Betty. with open(self.fixture_path) as data: entries_created = deserialize_json(data, self.test_username) self.assertEquals(1, entries_created) self.assertEquals(4, ExampleDeserializeConfig.objects.count()) self.assertEquals(5, ExampleDeserializeConfig.current('betty').int_field)
def test_invalid_json(self): """ Tests the error handling when there is invalid JSON. """ test_json = textwrap.dedent(""" { "model": "config_models.ExampleDeserializeConfig", "data": [{"name": "dino" """) with self.assertRaisesRegexp(Exception, "JSON parse error"): deserialize_json(BytesIO(test_json), self.test_username)
def test_invalid_model(self): """ Tests the error handling when the configuration model specified does not exist. """ test_json = textwrap.dedent(""" { "model": "xxx.yyy", "data":[{"name": "dino"}] } """) with self.assertRaisesRegexp(Exception, "No installed app"): deserialize_json(BytesIO(test_json), self.test_username)
def test_bad_username(self): """ Tests the error handling when the specified user does not exist. """ test_json = textwrap.dedent(""" { "model": "config_models.ExampleDeserializeConfig", "data": [{"name": "dino"}] } """) with self.assertRaisesRegexp(Exception, "User matching query does not exist"): deserialize_json(BytesIO(test_json), "unknown_username")
def test_bad_username(self): """ Tests the error handling when the specified user does not exist. """ test_json = textwrap.dedent(""" { "model": "example.ExampleDeserializeConfig", "data": [{"name": "dino"}] } """) if six.PY3: test_json = test_json.encode('utf-8') with six.assertRaisesRegex(self, Exception, "User matching query does not exist"): deserialize_json(six.BytesIO(test_json), "unknown_username")
def handle(self, *args, **options): if 'file' not in options or not options['file']: raise CommandError(_("A file containing JSON must be specified.")) if 'username' not in options or not options['username']: raise CommandError(_("A valid username must be specified.")) json_file = options['file'] if not os.path.exists(json_file): raise CommandError(_("File {0} does not exist").format(json_file)) self.stdout.write(_("Importing JSON data from file {0}").format(json_file)) with open(json_file) as data: created_entries = deserialize_json(data, options['username']) self.stdout.write(_("Import complete, {0} new entries created").format(created_entries))
def handle(self, *args, **options): if 'file' not in options or not options['file']: raise CommandError(_("A file containing JSON must be specified.")) if 'username' not in options or not options['username']: raise CommandError(_("A valid username must be specified.")) json_file = options['file'] if not os.path.exists(json_file): raise CommandError(_("File {0} does not exist").format(json_file)) self.stdout.write(_("Importing JSON data from file {0}").format(json_file)) with io.open(json_file, "rb") as data: created_entries = deserialize_json(data, options['username']) self.stdout.write(_("Import complete, {0} new entries created").format(created_entries))
def test_existing_entries_not_removed(self): """ Any existing configuration model entries are retained (though they may be come history)-- deserialize_json is purely additive. """ ExampleDeserializeConfig(name="fred", enabled=True).save() ExampleDeserializeConfig(name="barney", int_field=200).save() with open(self.fixture_path) as data: entries_created = deserialize_json(data, self.test_username) self.assertEquals(2, entries_created) self.assertEquals(4, ExampleDeserializeConfig.objects.count()) self.assertEquals(3, len(ExampleDeserializeConfig.objects.current_set())) self.assertEquals(5, ExampleDeserializeConfig.current('betty').int_field) self.assertEquals(200, ExampleDeserializeConfig.current('barney').int_field) # The JSON file changes "enabled" to False for Fred. fred = ExampleDeserializeConfig.current('fred') self.assertFalse(fred.enabled)
def test_existing_entries_not_removed(self): """ Any existing configuration model entries are retained (though they may be come history)-- deserialize_json is purely additive. """ ExampleDeserializeConfig(name="fred", enabled=True).save() ExampleDeserializeConfig(name="barney", int_field=200).save() with open(self.fixture_path) as data: # pylint: disable=open-builtin entries_created = deserialize_json(data, self.test_username) self.assertEquals(2, entries_created) self.assertEquals(4, ExampleDeserializeConfig.objects.count()) self.assertEquals(3, len(ExampleDeserializeConfig.objects.current_set())) self.assertEquals(5, ExampleDeserializeConfig.current('betty').int_field) self.assertEquals(200, ExampleDeserializeConfig.current('barney').int_field) # The JSON file changes "enabled" to False for Fred. fred = ExampleDeserializeConfig.current('fred') self.assertFalse(fred.enabled)
def test_deserialize_models(self): """ Tests the "happy path", where 2 instances of the test model should be created. A valid username is supplied for the operation. """ start_date = timezone.now() with open(self.fixture_path) as data: entries_created = deserialize_json(data, self.test_username) self.assertEquals(2, entries_created) self.assertEquals(2, ExampleDeserializeConfig.objects.count()) betty = ExampleDeserializeConfig.current('betty') self.assertTrue(betty.enabled) self.assertEquals(5, betty.int_field) self.assertGreater(betty.change_date, start_date) self.assertEquals(self.test_username, betty.changed_by.username) fred = ExampleDeserializeConfig.current('fred') self.assertFalse(fred.enabled) self.assertEquals(10, fred.int_field) self.assertGreater(fred.change_date, start_date) self.assertEquals(self.test_username, fred.changed_by.username)
def test_deserialize_models(self): """ Tests the "happy path", where 2 instances of the test model should be created. A valid username is supplied for the operation. """ start_date = timezone.now() with io.open(self.fixture_path, "rb") as data: entries_created = deserialize_json(data, self.test_username) self.assertEqual(2, entries_created) self.assertEqual(2, ExampleDeserializeConfig.objects.count()) betty = ExampleDeserializeConfig.current('betty') self.assertTrue(betty.enabled) self.assertEqual(5, betty.int_field) self.assertGreater(betty.change_date, start_date) self.assertEqual(self.test_username, betty.changed_by.username) fred = ExampleDeserializeConfig.current('fred') self.assertFalse(fred.enabled) self.assertEqual(10, fred.int_field) self.assertGreater(fred.change_date, start_date) self.assertEqual(self.test_username, fred.changed_by.username)