Skip to content

Commit 5ad1e9c

Browse files
Added: Complex test case for multiple switch_db and switch_collection scenarios
1 parent a9cc4ce commit 5ad1e9c

2 files changed

Lines changed: 178 additions & 0 deletions

File tree

tests/asynchronous/document/test_instance.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,6 +3210,80 @@ class Group(Document):
32103210
g0 = await Group.aobjects.first()
32113211
assert "hello - default" == g0.name
32123212

3213+
async def test_switch_multiple_db_and_multiple_collection_same_time(self):
3214+
await async_register_connection("tenantA", "mongoenginetest2")
3215+
await async_register_connection("tenantB", "mongoenginetest2")
3216+
3217+
class User(Document):
3218+
name = StringField()
3219+
3220+
class Post(Document):
3221+
title = StringField()
3222+
3223+
# Clean defaults
3224+
await User.adrop_collection()
3225+
await Post.adrop_collection()
3226+
3227+
# Clean switched targets (two different db+collection combos)
3228+
async with switch_db(User, "tenantA"), switch_collection(User, "users_A"):
3229+
await User.adrop_collection()
3230+
3231+
async with switch_db(Post, "tenantB"), switch_collection(Post, "posts_B"):
3232+
await Post.adrop_collection()
3233+
3234+
# Seed defaults (default DB + default collections)
3235+
await User(name="user-default").asave()
3236+
await Post(title="post-default").asave()
3237+
3238+
assert 1 == await User.aobjects.count()
3239+
assert 1 == await Post.aobjects.count()
3240+
3241+
# Write to BOTH overrides in the SAME context block
3242+
async with switch_db(User, "tenantA"), switch_collection(User, "users_A"), \
3243+
switch_db(Post, "tenantB"), switch_collection(Post, "posts_B"):
3244+
await User(name="user-A").asave()
3245+
await Post(title="post-B").asave()
3246+
3247+
assert 1 == await User.aobjects.count()
3248+
assert 1 == await Post.aobjects.count()
3249+
3250+
u = await User.aobjects.first()
3251+
p = await Post.aobjects.first()
3252+
assert u.name == "user-A"
3253+
assert p.title == "post-B"
3254+
3255+
# Verify defaults are unchanged after leaving the block
3256+
u0 = await User.aobjects.first()
3257+
p0 = await Post.aobjects.first()
3258+
assert u0.name == "user-default"
3259+
assert p0.title == "post-default"
3260+
3261+
# Verify switched locations still have their own data (independently)
3262+
async with switch_db(User, "tenantA"), switch_collection(User, "users_A"):
3263+
assert 1 == await User.aobjects.count()
3264+
u = await User.aobjects.first()
3265+
assert u.name == "user-A"
3266+
3267+
async with switch_db(Post, "tenantB"), switch_collection(Post, "posts_B"):
3268+
assert 1 == await Post.aobjects.count()
3269+
p = await Post.aobjects.first()
3270+
assert p.title == "post-B"
3271+
3272+
# Cleanup only switched targets (defaults remain)
3273+
async with switch_db(User, "tenantA"), switch_collection(User, "users_A"):
3274+
await User.adrop_collection()
3275+
assert 0 == await User.aobjects.count()
3276+
3277+
async with switch_db(Post, "tenantB"), switch_collection(Post, "posts_B"):
3278+
await Post.adrop_collection()
3279+
assert 0 == await Post.aobjects.count()
3280+
3281+
# Defaults still intact
3282+
assert 1 == await User.aobjects.count()
3283+
assert 1 == await Post.aobjects.count()
3284+
assert (await User.aobjects.first()).name == "user-default"
3285+
assert (await Post.aobjects.first()).title == "post-default"
3286+
32133287
async def test_load_undefined_fields(self):
32143288
class User(Document):
32153289
name = StringField()

tests/synchronous/document/test_instance.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,6 +3226,110 @@ class Group(Document):
32263226
g0 = Group.objects.first()
32273227
assert "hello - default" == g0.name
32283228

3229+
def test_switch_multiple_db_and_multiple_collection_same_time(self):
3230+
register_connection("testdb-a", "mongoenginetest2")
3231+
register_connection("testdb-b", "mongoenginetest2")
3232+
3233+
class User(Document):
3234+
name = StringField()
3235+
3236+
class Post(Document):
3237+
title = StringField()
3238+
3239+
# Clean default + switched locations
3240+
User.drop_collection()
3241+
Post.drop_collection()
3242+
3243+
with switch_db(User, "testdb-a"):
3244+
with switch_collection(User, "users_alt"):
3245+
User.drop_collection()
3246+
3247+
with switch_db(Post, "testdb-b"):
3248+
with switch_collection(Post, "posts_alt"):
3249+
Post.drop_collection()
3250+
3251+
# Seed default (default DB + default collection)
3252+
User(name="user - default").save()
3253+
Post(title="post - default").save()
3254+
assert 1 == User.objects.count()
3255+
assert 1 == Post.objects.count()
3256+
3257+
# Switch instances to db+collection and save there
3258+
u0 = User.objects.first()
3259+
p0 = Post.objects.first()
3260+
3261+
u0.switch_db("testdb-a")
3262+
u0.switch_collection("users_alt")
3263+
u0.name = "user - testdb-a/users_alt"
3264+
u0.save()
3265+
3266+
p0.switch_db("testdb-b")
3267+
p0.switch_collection("posts_alt")
3268+
p0.title = "post - testdb-b/posts_alt"
3269+
p0.save()
3270+
3271+
# Read back from switched db+collection (BOTH at same time)
3272+
with switch_db(User, "testdb-a"), switch_collection(User, "users_alt"), \
3273+
switch_db(Post, "testdb-b"), switch_collection(Post, "posts_alt"):
3274+
u = User.objects.first()
3275+
p = Post.objects.first()
3276+
assert "user - testdb-a/users_alt" == u.name
3277+
assert "post - testdb-b/posts_alt" == p.title
3278+
3279+
# Default still unchanged
3280+
u_def = User.objects.first()
3281+
p_def = Post.objects.first()
3282+
assert "user - default" == u_def.name
3283+
assert "post - default" == p_def.title
3284+
3285+
# Update only in switched db+collection (same object_id assumption)
3286+
u_def.switch_db("testdb-a")
3287+
u_def.switch_collection("users_alt")
3288+
u_def.update(set__name="user - update")
3289+
3290+
p_def.switch_db("testdb-b")
3291+
p_def.switch_collection("posts_alt")
3292+
p_def.update(set__title="post - update")
3293+
3294+
with switch_db(User, "testdb-a"), switch_collection(User, "users_alt"), \
3295+
switch_db(Post, "testdb-b"), switch_collection(Post, "posts_alt"):
3296+
u = User.objects.first()
3297+
p = Post.objects.first()
3298+
assert "user - update" == u.name
3299+
assert "post - update" == p.title
3300+
3301+
# cleanup switched targets only
3302+
User.drop_collection()
3303+
Post.drop_collection()
3304+
assert 0 == User.objects.count()
3305+
assert 0 == Post.objects.count()
3306+
3307+
# Default still intact after dropping switched collections
3308+
u_def = User.objects.first()
3309+
p_def = Post.objects.first()
3310+
assert "user - default" == u_def.name
3311+
assert "post - default" == p_def.title
3312+
3313+
# Delete in switched target only (same object_id assumption)
3314+
u_def.switch_db("testdb-a")
3315+
u_def.switch_collection("users_alt")
3316+
u_def.delete()
3317+
3318+
p_def.switch_db("testdb-b")
3319+
p_def.switch_collection("posts_alt")
3320+
p_def.delete()
3321+
3322+
with switch_db(User, "testdb-a"), switch_collection(User, "users_alt"), \
3323+
switch_db(Post, "testdb-b"), switch_collection(Post, "posts_alt"):
3324+
assert 0 == User.objects.count()
3325+
assert 0 == Post.objects.count()
3326+
3327+
# Default still intact
3328+
u_def = User.objects.first()
3329+
p_def = Post.objects.first()
3330+
assert "user - default" == u_def.name
3331+
assert "post - default" == p_def.title
3332+
32293333
def test_load_undefined_fields(self):
32303334
class User(Document):
32313335
name = StringField()

0 commit comments

Comments
 (0)