# -*- coding: utf-8 -*-
import unittest2

from erppeek import issearchdomain, searchargs


class TestUtils(unittest2.TestCase):

    def test_issearchdomain(self):
        self.assertFalse(issearchdomain(None))
        self.assertFalse(issearchdomain(42))
        self.assertFalse(issearchdomain('42'))
        self.assertFalse(issearchdomain([1, 42]))
        self.assertFalse(issearchdomain(['1', '42']))

        self.assertTrue(issearchdomain([('name', '=', 'mushroom'),
                                        ('state', '!=', 'draft')]))
        self.assertTrue(issearchdomain(['name = mushroom', 'state != draft']))
        self.assertTrue(issearchdomain([]))
        self.assertTrue(issearchdomain('state != draft'))
        self.assertTrue(issearchdomain(('state', '!=', 'draft')))

    def test_searchargs(self):
        domain = [('name', '=', 'mushroom'), ('state', '!=', 'draft')]

        self.assertEqual(searchargs(([],)), ([],))
        self.assertEqual(searchargs((domain,)), (domain,))
        self.assertEqual(searchargs((['name = mushroom', 'state != draft'],)),
                         (domain,))
        # XXX catch warnings
        # self.assertEqual(searchargs(('state != draft',)),
        #                  ([('state', '!=', 'draft')],))
        # self.assertEqual(searchargs((('state', '!=', 'draft'),)),
        #                  ([('state', '!=', 'draft')],))

        self.assertEqual(searchargs((['status=Running'],)),
                         ([('status', '=', 'Running')],))
        self.assertEqual(searchargs((['state="in_use"'],)),
                         ([('state', '=', 'in_use')],))
        self.assertEqual(searchargs((['spam.ham in(1, 2)'],)),
                         ([('spam.ham', 'in', (1, 2))],))
        self.assertEqual(searchargs((['spam in(1, 2)'],)),
                         ([('spam', 'in', (1, 2))],))

        # Standard comparison operators
        self.assertEqual(searchargs((['ham=2'],)), ([('ham', '=', 2)],))
        self.assertEqual(searchargs((['ham!=2'],)), ([('ham', '!=', 2)],))
        self.assertEqual(searchargs((['ham>2'],)), ([('ham', '>', 2)],))
        self.assertEqual(searchargs((['ham>=2'],)), ([('ham', '>=', 2)],))
        self.assertEqual(searchargs((['ham<2'],)), ([('ham', '<', 2)],))
        self.assertEqual(searchargs((['ham<=2'],)), ([('ham', '<=', 2)],))

        # Operators rarely used
        self.assertEqual(searchargs((['status =like Running'],)),
                         ([('status', '=like', 'Running')],))
        self.assertEqual(searchargs((['status=like Running'],)),
                         ([('status', '=like', 'Running')],))
        self.assertEqual(searchargs((['status =ilike Running'],)),
                         ([('status', '=ilike', 'Running')],))
        self.assertEqual(searchargs((['status =? Running'],)),
                         ([('status', '=?', 'Running')],))
        self.assertEqual(searchargs((['status=?Running'],)),
                         ([('status', '=?', 'Running')],))

        self.assertRaises(ValueError, searchargs, (['spam.hamin(1, 2)'],))
        self.assertRaises(ValueError, searchargs, (['spam.hamin (1, 2)'],))
        self.assertRaises(ValueError, searchargs, (['spamin (1, 2)'],))
        self.assertRaises(ValueError, searchargs, (['[id = 1540]'],))

@@ -33,16 +33,28 @@
 nittest2
+%0Aimport mock
 %0A%0Afrom e
@@ -1124,24 +1124,25 @@
 main,))%0A
+%0A
         
 # XXX ca
@@ -1137,28 +1137,33 @@
     
-# XXX c
+with mock.p
 atch
- 
+('
 warnings
 %0A   
@@ -1158,16 +1158,37 @@
 warnings
+.warn') as mock_warn:
 %0A       
@@ -1180,33 +1180,35 @@
 k_warn:%0A        
-#
+   
  self.assertEqua
@@ -1242,33 +1242,35 @@
 ft',)),%0A        
-#
+   
                 
@@ -1310,17 +1310,164 @@
         
+   
-#
+ mock_warn.assert_called_once_with(%0A                %22Domain should be a list: %5B'state != draft'%5D%22)%0A            mock_warn.reset_mock()%0A           
  self.as
@@ -1525,17 +1525,18 @@
         
-#
+  
         
@@ -1537,32 +1537,33 @@
                 
+ 
 (%5B('state', '!='
@@ -1572,24 +1572,142 @@
 'draft')%5D,))
+%0A            mock_warn.assert_called_once_with(%0A                %22Domain should be a list: %5B('state', '!=', 'draft')%5D%22)
 %0A%0A        se

enable warning tests