def test_cascade(self): # Mentioning cascade adds it to the command mock_cursor = MagicMock() with patch('libya_elections.db_utils.get_cursor') as mock_get_cursor: mock_get_cursor.return_value = mock_cursor delete_all(self.db, [Citizen], cascade=True) expected_cmd = "TRUNCATE civil_registry_citizen CASCADE" mock_cursor.execute.assert_called_with(expected_cmd)
def test_simple_delete(self): # Nothing in the test database? # (Need to use a model that has no foreign keys pointing at it. # Otherwise, doesn't matter which model.) model = Whitelist factory = WhitelistFactory self.assertFalse(model.objects.exists()) # Add something factory() self.assertTrue(model.objects.exists()) delete_all(self.db, [model]) self.assertFalse(model.objects.exists())
def test_postgres_only(self): # If db is not postgres, raises exception # Note that overriding DATABASES in a test triggers # a warning, but I think since we're not actually doing anything # in the database while we have it overridden, it's okay. # (The code under test just inspects the setting and returns.) DATABASES = { self.db: { 'ENGINE': 'elcheaposqldb', } } with override_settings(DATABASES=DATABASES): with self.assertRaises(NotImplementedError): delete_all(self.db, [])
def setUp(self): # We'll use Citizen and TempCitizen. Start with them empty # so each test can set them up as desired. delete_all('default', [Citizen, TempCitizen], cascade=True)
def setUp(self): delete_all('default', [Citizen, TempCitizen, CitizenMetadata], cascade=True)
def import_citizen_dump(input_filename, max_change_percent=DEFAULT_MAX_CHANGE_PERCENT, encoding='UTF-8'): with transaction.atomic(): # Clear out TempCitizen table. (We clear it at the end too, but this makes # extra sure that we start with it empty.) delete_all('default', [TempCitizen]) num_records_at_start = Citizen.objects.count() # # 1. Fill our temp table with the data from the latest dump # logger.info("Loading data from dump") input_file = codecs.open(input_filename, encoding=encoding) logger.info("Reading %s" % input_filename) batch = BatchOperations(TempCitizen) records_read = 0 for record in get_records(input_file): records_read += 1 batch.add(record) batch.flush() # # 2. Sync data from temp table to our real table # logger.info("Updating our own database") stats = mirror_database(from_model=TempCitizen, to_model=Citizen) # See what % of the records we're changing if num_records_at_start > 0: num_changes = (stats.modified_record_count + stats.new_record_count + stats.not_there_anymore_count) percent_changed = 100 * (num_changes / num_records_at_start) if percent_changed > max_change_percent: raise TooManyChanges( "Too many changes, aborting Citizen data import. Max change is %f%% but " "the import would have changed %f%% records (%d/%d). Use " "--max-change-percent=NN to override this limit if necessary." % (max_change_percent, percent_changed, num_changes, num_records_at_start)) # Add our data stats.records_read = records_read # Make a note of when we did it timestamp = now() CitizenMetadata.objects.update_or_create(defaults=dict(dump_time=timestamp)) # Flag any records that turned up missing if stats.missing_pks: Citizen.objects.filter(pk__in=stats.missing_pks, missing=None).update(missing=timestamp) # And we're done! # Clear out our temp table (no point in taking up disk space) delete_all('default', [TempCitizen]) return stats