[IMP] Added missing vim mode lines
[odoo/odoo.git] / openerp / test / test_osv.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010 OpenERP S.A. http://www.openerp.com
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import unittest
23 from openerp.osv.query import Query
24
25 class QueryTestCase(unittest.TestCase):
26
27     def test_basic_query(self):
28         query = Query()
29         query.tables.extend(['"product_product"','"product_template"'])
30         query.where_clause.append("product_product.template_id = product_template.id")
31         query.join(("product_template", "product_category", "categ_id", "id"), outer=False) # add normal join
32         query.join(("product_product", "res_user", "user_id", "id"), outer=True) # outer join
33         self.assertEquals(query.get_sql()[0].strip(),
34             """"product_product" LEFT JOIN "res_user" ON ("product_product"."user_id" = "res_user"."id"),"product_template" JOIN "product_category" ON ("product_template"."categ_id" = "product_category"."id") """.strip())
35         self.assertEquals(query.get_sql()[1].strip(), """product_product.template_id = product_template.id""".strip())
36
37     def test_query_chained_explicit_joins(self):
38         query = Query()
39         query.tables.extend(['"product_product"','"product_template"'])
40         query.where_clause.append("product_product.template_id = product_template.id")
41         query.join(("product_template", "product_category", "categ_id", "id"), outer=False) # add normal join
42         query.join(("product_category", "res_user", "user_id", "id"), outer=True) # CHAINED outer join
43         self.assertEquals(query.get_sql()[0].strip(),
44             """"product_product","product_template" JOIN "product_category" ON ("product_template"."categ_id" = "product_category"."id") LEFT JOIN "res_user" ON ("product_category"."user_id" = "res_user"."id")""".strip())
45         self.assertEquals(query.get_sql()[1].strip(), """product_product.template_id = product_template.id""".strip())
46
47     def test_mixed_query_chained_explicit_implicit_joins(self):
48         query = Query()
49         query.tables.extend(['"product_product"','"product_template"'])
50         query.where_clause.append("product_product.template_id = product_template.id")
51         query.join(("product_template", "product_category", "categ_id", "id"), outer=False) # add normal join
52         query.join(("product_category", "res_user", "user_id", "id"), outer=True) # CHAINED outer join
53         query.tables.append('"account.account"')
54         query.where_clause.append("product_category.expense_account_id = account_account.id") # additional implicit join
55         self.assertEquals(query.get_sql()[0].strip(),
56             """"product_product","product_template" JOIN "product_category" ON ("product_template"."categ_id" = "product_category"."id") LEFT JOIN "res_user" ON ("product_category"."user_id" = "res_user"."id"),"account.account" """.strip())
57         self.assertEquals(query.get_sql()[1].strip(), """product_product.template_id = product_template.id AND product_category.expense_account_id = account_account.id""".strip())
58
59
60     def test_raise_missing_lhs(self):
61         query = Query()
62         query.tables.append('"product_product"')
63         self.assertRaises(AssertionError, query.join, ("product_template", "product_category", "categ_id", "id"), outer=False)
64
65
66 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: