def test_db_for_read(self): router = ShardedPerTenantRouter() obj = TShardedModel(partner_id=PID) db_alias = router.db_for_read(TShardedModel, instance=obj) self.assertEqual(db_alias, 'test2__{}'.format(obj.partner_id)) self.assertFalse(router._write_mode)
def test_get_shard_for_instance_read_mod(self): instance = TShardedModel(partner_id=PID) router = ShardedPerTenantRouter() router._write_mode = False db_alias = router._get_shard_for_instance(instance) self.assertEqual(db_alias, 'test2__{}'.format(instance.partner_id))
def test_get_shard_with_instance_read_mode(self): router = ShardedPerTenantRouter() router._write_mode = False instance = TShardedModel(partner_id=PID) hints = {'instance': instance} db_alias = router._get_shard(TShardedModel, **hints) self.assertEqual(db_alias, 'test2__{}'.format(instance.partner_id))
def test_allow_relation(self): router = ShardedPerTenantRouter() obj1 = TShardedModel(partner_id=PID) obj2 = TShardedModel(partner_id=obj1.partner_id) result = router.allow_relation(obj1, obj2) self.assertTrue(result) self.assertTrue(router._write_mode)
def test_get_shard_read_mode(self): partner_id = PID router = ShardedPerTenantRouter() router._write_mode = False hints = {'exact_lookups': {TShardedModel.sharded_field: partner_id}} db_alias = router._get_shard(TShardedModel, **hints) self.assertEqual(db_alias, 'test2__{}'.format(partner_id))
def test__build_db_alias_without_shard_value(self): router = ShardedPerTenantRouter() router._write_mode = True db_alias = router._build_db_alias(None, TShardedModel) self.assertEqual(db_alias, 'test1')
def test__build_db_alias_with_config(self): router = ShardedPerTenantRouter() router._write_mode = True db_alias = router._build_db_alias(1, TShardedModel) self.assertEqual(db_alias, 'test1__1')
def test_get_db_group_for_default(self): router = ShardedPerTenantRouter() router._write_mode = True db_group = router._get_db_group(TShardedModel) self.assertEqual(db_group, 'test1')
def test_get_db_group_for_read(self): router = ShardedPerTenantRouter() router._write_mode = False db_group = router._get_db_group(TShardedModel) self.assertEqual(db_group, 'test2')
def test_if_alias_not_in_DATABASES_then_return_db_group(self): router = ShardedPerTenantRouter() router._write_mode = True db_alias = router._build_db_alias(1, TShardedModel) self.assertEqual(db_alias, 'test1')
def test_db_for_read_for_non_sharded_model(self): router = ShardedPerTenantRouter() self.assertIsNone(router.db_for_write(TNoneShardedModel))
def test_is_sharded_model(self): router = ShardedPerTenantRouter() self.assertTrue(router._is_sharded_model(TShardedModel)) self.assertFalse(router._is_sharded_model(TNoneShardedModel))