mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-28 13:28:11 +08:00
+87
@@ -0,0 +1,87 @@
|
||||
import string
|
||||
import random
|
||||
|
||||
|
||||
from sqlalchemy import func
|
||||
|
||||
from server.db.cellxgene_orm import CellxGeneUser, CellxGeneDataset, Annotation, Base
|
||||
from server.db.create_db import create_db
|
||||
from server.db.db_utils import DbUtils
|
||||
|
||||
|
||||
class TestDatabase:
|
||||
def __init__(self):
|
||||
local_db_uri = "postgresql://postgres:test_pw@localhost:5432"
|
||||
create_db(local_db_uri)
|
||||
self.db = DbUtils(local_db_uri)
|
||||
self._populate_test_data()
|
||||
self._populate_test_data_many()
|
||||
|
||||
def _populate_test_data(self):
|
||||
self._create_test_user()
|
||||
self._create_test_dataset()
|
||||
self._create_test_annotation()
|
||||
|
||||
def _populate_test_data_many(self):
|
||||
self._create_test_users()
|
||||
self._create_test_datasets()
|
||||
self._create_test_annotations()
|
||||
|
||||
def _create_test_user(self):
|
||||
user = CellxGeneUser(id="test_user_id")
|
||||
self.db.session.add(user)
|
||||
self.db.session.commit()
|
||||
|
||||
def _create_test_dataset(self):
|
||||
dataset = CellxGeneDataset(
|
||||
id="test_dataset_id",
|
||||
name="test_dataset",
|
||||
)
|
||||
self.db.session.add(dataset)
|
||||
self.db.session.commit()
|
||||
|
||||
def _create_test_annotation(self):
|
||||
annotation = Annotation(
|
||||
id="test_annotation_id",
|
||||
tiledb_uri="tiledb_uri",
|
||||
user_id="test_user_id",
|
||||
dataset_id="test_dataset_id"
|
||||
)
|
||||
self.db.session.add(annotation)
|
||||
self.db.session.commit()
|
||||
|
||||
@staticmethod
|
||||
def get_random_string():
|
||||
letters = string.ascii_lowercase
|
||||
return ''.join(random.choice(letters) for i in range(12))
|
||||
|
||||
def _create_test_users(self, user_count: int = 10):
|
||||
users = []
|
||||
for i in range(user_count):
|
||||
users.append(CellxGeneUser(id=self.get_random_string()))
|
||||
self.db.session.add_all(users)
|
||||
self.db.session.commit()
|
||||
|
||||
def _create_test_datasets(self, dataset_count: int = 10):
|
||||
datasets = []
|
||||
for i in range(dataset_count):
|
||||
datasets.append(CellxGeneDataset(id=self.get_random_string(), name=self.get_random_string()))
|
||||
self.db.session.add_all(datasets)
|
||||
self.db.session.commit()
|
||||
|
||||
def order_by_random(self, table: Base):
|
||||
return self.db.session.query(table).order_by(func.random()).first()
|
||||
|
||||
def _create_test_annotations(self, annotation_count: int = 10):
|
||||
annotations = []
|
||||
for i in range(annotation_count):
|
||||
dataset = self.order_by_random(CellxGeneDataset)
|
||||
user = self.order_by_random(CellxGeneUser)
|
||||
annotations.append(Annotation(
|
||||
id=self.get_random_string(),
|
||||
tiledb_uri=self.get_random_string(),
|
||||
user_id=user.id,
|
||||
dataset_id=dataset.id
|
||||
))
|
||||
self.db.session.add_all(annotations)
|
||||
self.db.session.commit()
|
||||
@@ -0,0 +1,34 @@
|
||||
import unittest
|
||||
from server.db.cellxgene_orm import CellxGeneUser, CellxGeneDataset, Annotation
|
||||
from server.db.db_utils import DbUtils
|
||||
from server.test.fixtures.database import TestDatabase
|
||||
|
||||
|
||||
class AppConfigTest(unittest.TestCase):
|
||||
db = DbUtils("postgresql://postgres:test_pw@localhost:5432")
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
TestDatabase()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls) -> None:
|
||||
del cls.db
|
||||
|
||||
def test_user_creation(self):
|
||||
one_user = self.db.get(table=CellxGeneUser, entity_id='test_user_id')
|
||||
self.assertEqual(one_user.id, 'test_user_id')
|
||||
user_count = self.db.session.query(CellxGeneUser).count()
|
||||
self.assertGreater(user_count, 10)
|
||||
|
||||
def test_dataset_creation(self):
|
||||
one_dataset = self.db.get(table=CellxGeneDataset, entity_id='test_dataset_id')
|
||||
self.assertEqual(one_dataset.id, 'test_dataset_id')
|
||||
dataset_count = self.db.session.query(CellxGeneDataset).count()
|
||||
self.assertGreater(dataset_count, 10)
|
||||
|
||||
def test_annotation_creation(self):
|
||||
one_annotation = self.db.get(table=Annotation, entity_id='test_annotation_id')
|
||||
self.assertEqual(one_annotation.id, 'test_annotation_id')
|
||||
annotation_count = self.db.session.query(Annotation).count()
|
||||
self.assertGreater(annotation_count, 10)
|
||||
Reference in New Issue
Block a user