# -*- coding: utf-8 -*-
# Copyright 2013-2017 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo.tests.common import TransactionCase


class TestDefaultBinder(TransactionCase):
    """ Test the default binder implementation using Components """

    at_install = False
    post_install = True

    def setUp(self):
        super(TestDefaultBinder, self).setUp()

        # create our backend, in case of components,
        # the version would not be required
        self.backend_record = self.env['test.backend'].create(
            {'version': '1', 'name': 'Test'}
        )
        # create a work context with the model we want to work with
        self.work = self.backend_record.work_on('connector.test.binding')
        # get our binder component (for the model in whe work context)
        self.binder = self.work.component(usage='binder')

    def test_default_binder(self):
        """ Small scenario with the default binder """
        test_record = self.env['connector.test.record'].create({})
        test_binding = self.env['connector.test.binding'].create({
            'backend_id': self.backend_record.id,
            'odoo_id': test_record.id,
        })

        # bind the test binding to external id = 99
        self.binder.bind(99, test_binding)
        # find the odoo binding bound to external record 99
        binding = self.binder.to_internal(99)
        self.assertEqual(binding, test_binding)
        # find the odoo record bound to external record 99
        record = self.binder.to_internal(99, unwrap=True)
        self.assertEqual(record, test_record)
        # find the external record bound to odoo binding
        external_id = self.binder.to_external(test_binding)
        self.assertEqual(external_id, 99)
        # find the external record bound to odoo record
        external_id = self.binder.to_external(test_record, wrap=True)
        self.assertEqual(external_id, 99)
        self.assertEqual(self.binder.unwrap_model(), 'connector.test.record')
        # unwrapping the binding should give the same binding
        self.assertEqual(self.binder.unwrap_binding(test_binding), test_record)

@@ -613,24 +613,115 @@
       )%0A
+%0A
   
+  def test_default_binder(self):%0A        %22%22%22 Small scenario with the default binder %22%22%22%0A
       
+  
 # create
@@ -784,19 +784,12 @@
     
-self.work =
+with
  sel
@@ -838,17 +838,30 @@
 inding')
-%0A
+ as work:%0A    
         
@@ -923,32 +923,36 @@
 ontext)%0A        
+    
 self.binder = se
@@ -949,21 +949,16 @@
 inder = 
-self.
 work.com
@@ -984,99 +984,12 @@
 r')%0A
-%0A
     
-def test_default_binder(self):%0A        %22%22%22 Small scenario with the default binder %22%22%22%0A
     
@@ -1051,16 +1051,20 @@
 ate(%7B%7D)%0A
+    
         
@@ -1134,16 +1134,20 @@
         
+ 
+   
 'backend
@@ -1188,16 +1188,20 @@
         
+  
+  
 'odoo_id
@@ -1231,12 +1231,20 @@
     
-%7D)%0A%0A
+    %7D)%0A%0A    
     
@@ -1291,32 +1291,36 @@
 id = 99%0A        
+ 
+   
 self.binder.bind
@@ -1330,32 +1330,36 @@
 , test_binding)%0A
+    
         # find t
@@ -1394,32 +1394,36 @@
 ernal record 99%0A
+    
         binding 
@@ -1452,32 +1452,36 @@
 nal(99)%0A        
+    
 self.assertEqual
@@ -1496,32 +1496,36 @@
 , test_binding)%0A
+    
         # find t
@@ -1575,16 +1575,20 @@
         
+   
+ 
 record =
@@ -1629,32 +1629,36 @@
 p=True)%0A        
+    
 self.assertEqual
@@ -1679,32 +1679,36 @@
 record)%0A        
+ 
+   
 # find the exter
@@ -1740,32 +1740,36 @@
 binding%0A        
+  
+  
 external_id = se
@@ -1796,32 +1796,36 @@
 l(test_binding)%0A
+    
         self.ass
@@ -1850,32 +1850,36 @@
 id, 99)%0A        
+    
 # find the exter
@@ -1910,32 +1910,36 @@
  record%0A        
+ 
+   
 external_id = se
@@ -1984,32 +1984,36 @@
 p=True)%0A        
+    
 self.assertEqual
@@ -2022,32 +2022,36 @@
 xternal_id, 99)%0A
+    
         self.ass
@@ -2086,16 +2086,45 @@
 model(),
+%0A                            
  'connec
@@ -2141,16 +2141,20 @@
 ecord')%0A
+    
         
@@ -2207,32 +2207,36 @@
 binding%0A        
+    
 self.assertEqual
@@ -2277,16 +2277,45 @@
 inding),
+%0A                            
  test_re

Change Collection.work_on() to a context manager