@@ -807,6 +807,63 @@ def test_create_cron(self):
807807 self .assertEqual (cron .code , "answer = 42" )
808808
809809
810+ class TestField (UnitTestCase ):
811+ def test_invert_boolean_field (self ):
812+ cr = self .env .cr
813+
814+ with self .assertRaises (ValueError ):
815+ util .invert_boolean_field (cr , "res.partner" , "name" , "nom" )
816+
817+ fltr = self .env ["ir.filters" ].create (
818+ {"name" : "test" , "model_id" : "ir.model.data" , "domain" : "[('noupdate', '=', True)]" }
819+ )
820+
821+ query = """
822+ SELECT {0}, count(*)
823+ FROM ir_model_data
824+ GROUP BY {0}
825+ """
826+
827+ cr .execute (util .format_query (cr , query , "noupdate" ))
828+ initial_repartition = dict (cr .fetchall ())
829+
830+ # util.parallel_execute will `commit` the cursor and create new ones
831+ # as we are in a test, we should not commit as we are in a subtransaction
832+ with mock .patch .object (cr , "commit" , lambda : ...):
833+ util .invert_boolean_field (cr , "ir.model.data" , "noupdate" , "yesupdate" )
834+
835+ util .invalidate (fltr )
836+ self .assertEqual (literal_eval (fltr .domain ), ["!" , ("yesupdate" , "=" , True )])
837+
838+ cr .execute (util .format_query (cr , query , "yesupdate" ))
839+ inverted_repartition = dict (cr .fetchall ())
840+
841+ self .assertEqual (inverted_repartition [False ], initial_repartition [True ])
842+ self .assertEqual (inverted_repartition [True ], initial_repartition [False ] + initial_repartition .get (None , 0 ))
843+ self .assertEqual (inverted_repartition .get (None , 0 ), 0 )
844+
845+ # rename back
846+ with mock .patch .object (cr , "commit" , lambda : ...):
847+ util .rename_field (cr , "ir.model.data" , "yesupdate" , "noupdate" )
848+
849+ util .invalidate (fltr )
850+ self .assertEqual (literal_eval (fltr .domain ), ["!" , ("noupdate" , "=" , True )])
851+
852+ # invert with same name; will invert domains and data
853+ with mock .patch .object (cr , "commit" , lambda : ...):
854+ util .invert_boolean_field (cr , "ir.model.data" , "noupdate" , "noupdate" )
855+
856+ util .invalidate (fltr )
857+ self .assertEqual (literal_eval (fltr .domain ), ["!" , "!" , ("noupdate" , "=" , True )])
858+
859+ cr .execute (util .format_query (cr , query , "noupdate" ))
860+ back_repartition = dict (cr .fetchall ())
861+
862+ # merge None into False in the initial repartition
863+ initial_repartition [False ] += initial_repartition .pop (None , 0 )
864+ self .assertEqual (back_repartition , initial_repartition )
865+
866+
810867class TestHelpers (UnitTestCase ):
811868 def test_model_table_convertion (self ):
812869 cr = self .env .cr
0 commit comments