@@ -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