def test_is_sqlite3(self): with tempfile.NamedTemporaryFile(dir=TEST_DB_PATH, suffix='.db') as target: path = target.name self.assertFalse(is_sqlite3(path)) with TempSqliteContext('asdf.db', TEST_DB_PATH) as path: self.assertTrue(is_sqlite3(path))
def test_diff_disk_VariantsDb(self): """Add a new empty sqlite artificially. It should be added to settings and VariantsDb.""" filename = 'tmp' + random_string(10) + '.db' with TempSqliteContext(filename, TEST_DB_PATH): diff_disk_VariantsDb(path=TEST_DB_PATH) dbs = VariantsDb.objects.values_list('filename', flat=True) self.assertIn(filename, dbs)
def test_diff_disk_VariantsDb_update(self): """Add two sqlites with the same filename successively. The second one should be recognized as an update, deactivate the first one an redirect accesses.""" filename = 'tmp' + random_string(10) + '.db' # Create a first new db with TempSqliteContext(filename, TEST_DB_PATH): diff_disk_VariantsDb(path=TEST_DB_PATH) # There exists one and only one old_vdbs = VariantsDb.objects.filter(filename=filename) self.assertEqual(old_vdbs.count(), 1) old_vdb = old_vdbs[0] # Hash is calculated self.assertIsNot(old_vdb.hash, None) old_sha = old_vdb.hash old_pk = old_vdb.id # Set access to test user DbAccess.objects.create(variants_db=old_vdb, user_id=1) with TempSqliteContext(filename, TEST_DB_PATH): # Create a second one with the same name but different content diff_disk_VariantsDb(path=TEST_DB_PATH, check_time=False) vdbs = VariantsDb.objects.filter(filename=filename) self.assertEqual(vdbs.count(), 2) # The older one is still here but inactive vdbs = VariantsDb.objects.filter(filename=filename) self.assertEqual(vdbs.count(), 2) vdbs = VariantsDb.objects.filter(filename=filename, is_active=1) self.assertEqual(vdbs.count(), 1) # Only the new one is active, and it has a new hash vdb = vdbs[0] self.assertNotEqual(vdb.pk, old_pk) self.assertNotEqual(vdb.hash, old_sha) # Accesses have changed hands old_accesses = DbAccess.objects.filter(variants_db=old_vdb) self.assertEqual(old_accesses.count(), 1) old_accesses = DbAccess.objects.filter(variants_db=old_vdb, is_active=1) self.assertEqual(old_accesses.count(), 0) accesses = DbAccess.objects.filter(variants_db=vdb, is_active=1) self.assertEqual(accesses.count(), 1)
def test_activate_if_found_on_disk(self): """Create a new sqlite and a correponding entry in VariantsDb, inactive: it should get activated because it exists on disk.""" with TempSqliteContext('qwer.db', TEST_DB_PATH): vdb = VariantsDb.objects.create(name='qwer', filename='qwer.db', location=TEST_DB_PATH, is_active=0) activated = activate_if_found_on_disk(vdb) self.assertTrue(activated) self.assertEqual(vdb.is_active, 1)
def test_is_hash_changed(self): vdb = VariantsDb.objects.create(name='fff', filename='fff.db', location=TEST_DB_PATH) self.assertFalse(is_hash_changed(vdb)) with TempSqliteContext('fff.db', TEST_DB_PATH) as path: sha1 = sha1sum(path) vdb = VariantsDb.objects.create(hash=sha1, name='fff', filename='fff.db', location=TEST_DB_PATH) self.assertFalse(is_hash_changed(vdb), path) vdb.hash = 'xxxx' vdb.save() self.assertEqual(is_hash_changed(vdb, path), sha1)
def test_activate_deactivate_at_gemini_path__remove(self): """Remove a valid db from GEMINI_DB_PATH. It should get deactivated.""" with TempSqliteContext('qwer.db', TEST_DB_PATH) as path: VariantsDb.objects.create(name='qwer', filename='qwer.db', location=TEST_DB_PATH, is_active=1) activate_deactivate_at_gemini_path() db = VariantsDb.objects.get(filename='qwer.db') self.assertEqual(db.is_active, 1) os.remove(path) activate_deactivate_at_gemini_path() db = VariantsDb.objects.get(filename='qwer.db') self.assertEqual(db.is_active, 0)
def test_get_dbs_info__add_remove(self): # Create a new one response = get_dbs_info(EMPTY_REQUEST) content = json.loads(response.content.decode()) ndbs = len(content) with TempSqliteContext('asdf.db') as path: response = get_dbs_info(EMPTY_REQUEST) content = json.loads(response.content.decode()) self.assertGreaterEqual(len(content), 2) self.assertIn('asdf', [x['name'] for x in content]) os.remove(path) response = get_dbs_info(EMPTY_REQUEST) content = json.loads(response.content.decode()) self.assertEqual(len(content), ndbs)
def test_is_valid_vdb(self): vdb = VariantsDb.objects.create(name='fff', filename='fff.db', location=TEST_DB_PATH) # File does not exist self.assertIs(is_valid_vdb(vdb), False) with TempSqliteContext('fff.db', TEST_DB_PATH): self.assertIs(is_valid_vdb(vdb), True) # Not sqlite with tempfile.NamedTemporaryFile(dir=TEST_DB_PATH, suffix='.db') as target: filename = os.path.basename(target.name) vdb = VariantsDb.objects.create(name='fff', filename=filename, location=TEST_DB_PATH) assert os.path.exists(target.name) self.assertIs(is_valid_vdb(vdb, target.name), False)
def test_activate_deactivate_at_gemini_path__update(self): """One inactive db is an old version of another. It should not get activated.""" with TempSqliteContext('qwer.db', TEST_DB_PATH) as path: vdb1 = VariantsDb.objects.create(name='qwer', filename='qwer.db', location=TEST_DB_PATH, is_active=0) vdb2 = VariantsDb.objects.create(name='qwer', filename='qwer.db', location=TEST_DB_PATH, is_active=1, parent_db_id=vdb1.pk) activate_deactivate_at_gemini_path() db = VariantsDb.objects.get(pk=vdb1.pk) self.assertEqual(db.is_active, 0) db = VariantsDb.objects.get(pk=vdb2.pk) self.assertEqual(db.is_active, 1)
def test_is_source_updated(self): vdb = VariantsDb.objects.create(name='fff', filename='fff.db', location=TEST_DB_PATH, is_active=1) # File does not exist yet self.assertFalse(is_source_updated(vdb)) with TempSqliteContext('fff.db', TEST_DB_PATH) as path: update_time = vdb.updated_at # Ensure that basic conditions pass assert os.path.exists(path) assert isinstance(update_time, datetime.datetime) # There is less than a second difference in creation time self.assertIs(is_source_updated(vdb, path), False) # Now there is 3 seconds vdb.updated_at = update_time - datetime.timedelta(seconds=3) self.assertIsInstance(is_source_updated(vdb, path), datetime.datetime)
def test_copy_VariantsDb_to_settings(self): """Should fill settings.DATABASES with all connections found in VariantsDb.""" VariantsDb.objects.create(name='xxxx', filename='xxxx.db', location=TEST_DB_PATH, is_active=1) VariantsDb.objects.create(name='yyyy', filename='yyyy.db', location=TEST_DB_PATH, is_active=1) with TempSqliteContext('xxxx.db', TEST_DB_PATH): # 'xxxx.db' one exists on disk, 'yyyy.db' does not. copy_VariantsDb_to_settings() self.assertIn('test', settings.DATABASES) self.assertIn('xxxx', settings.DATABASES) self.assertNotIn('yyyy', settings.DATABASES) self.assertIn('test', connections.databases) self.assertIn('xxxx', connections.databases) self.assertNotIn('yyyy', connections.databases)
def test_update_if_db_changed(self): with TempSqliteContext('asdf.db', TEST_DB_PATH): sha1 = sha1sum(os.path.join(TEST_DB_PATH, 'asdf.db')) vdb = VariantsDb.objects.create(name='asdf', filename='asdf.db', location=TEST_DB_PATH, is_active=1, hash=sha1) changed = update_if_db_changed(vdb, warn=True) self.assertFalse(changed) # Changing the update time should not have an effect update_time = vdb.updated_at - datetime.timedelta(seconds=10) vdb.updated_at = update_time # don't vdb.save() or it updates the updated_time... changed = update_if_db_changed(vdb, check_time=True, warn=True) self.assertFalse(changed) # Change the hash should trigger the update # There might have been a save() somewhere else, so set the time back again vdb.hash = '1234' update_time = vdb.updated_at - datetime.timedelta(seconds=100) vdb.updated_at = update_time # don't vdb.save() or it updates the updated_time... changed = update_if_db_changed(vdb, check_time=True, warn=True) self.assertTrue(changed)
def test_scan_dir_for_dbs(self): with TempSqliteContext('aaa.db', TEST_DB_PATH): found = scan_dir_for_dbs(TEST_DB_PATH) self.assertIn(DB_TEST, found) self.assertIn('aaa.db', found) self.assertNotIn('asdf', found)