1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2010 OpenERP S.A. http://www.openerp.com
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.
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.
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/>.
20 ##############################################################################
23 from openerp.osv.query import Query
26 class QueryTestCase(unittest.TestCase):
28 def test_basic_query(self):
30 query.tables.extend(['"product_product"', '"product_template"'])
31 query.where_clause.append("product_product.template_id = product_template.id")
32 query.add_join(("product_template", "product_category", "categ_id", "id", "categ_id"), implicit=False, outer=False) # add normal join
33 query.add_join(("product_product", "res_user", "user_id", "id", "user_id"), implicit=False, outer=True) # outer join
34 self.assertEquals(query.get_sql()[0].strip(),
35 """"product_product" LEFT JOIN "res_user" as "product_product__user_id" ON ("product_product"."user_id" = "product_product__user_id"."id"),"product_template" JOIN "product_category" as "product_template__categ_id" ON ("product_template"."categ_id" = "product_template__categ_id"."id") """.strip())
36 self.assertEquals(query.get_sql()[1].strip(), """product_product.template_id = product_template.id""".strip())
38 def test_query_chained_explicit_joins(self):
40 query.tables.extend(['"product_product"', '"product_template"'])
41 query.where_clause.append("product_product.template_id = product_template.id")
42 query.add_join(("product_template", "product_category", "categ_id", "id", "categ_id"), implicit=False, outer=False) # add normal join
43 query.add_join(("product_template__categ_id", "res_user", "user_id", "id", "user_id"), implicit=False, outer=True) # CHAINED outer join
44 self.assertEquals(query.get_sql()[0].strip(),
45 """"product_product","product_template" JOIN "product_category" as "product_template__categ_id" ON ("product_template"."categ_id" = "product_template__categ_id"."id") LEFT JOIN "res_user" as "product_template__categ_id__user_id" ON ("product_template__categ_id"."user_id" = "product_template__categ_id__user_id"."id")""".strip())
46 self.assertEquals(query.get_sql()[1].strip(), """product_product.template_id = product_template.id""".strip())
48 def test_mixed_query_chained_explicit_implicit_joins(self):
50 query.tables.extend(['"product_product"', '"product_template"'])
51 query.where_clause.append("product_product.template_id = product_template.id")
52 query.add_join(("product_template", "product_category", "categ_id", "id", "categ_id"), implicit=False, outer=False) # add normal join
53 query.add_join(("product_template__categ_id", "res_user", "user_id", "id", "user_id"), implicit=False, outer=True) # CHAINED outer join
54 query.tables.append('"account.account"')
55 query.where_clause.append("product_category.expense_account_id = account_account.id") # additional implicit join
56 self.assertEquals(query.get_sql()[0].strip(),
57 """"product_product","product_template" JOIN "product_category" as "product_template__categ_id" ON ("product_template"."categ_id" = "product_template__categ_id"."id") LEFT JOIN "res_user" as "product_template__categ_id__user_id" ON ("product_template__categ_id"."user_id" = "product_template__categ_id__user_id"."id"),"account.account" """.strip())
58 self.assertEquals(query.get_sql()[1].strip(), """product_product.template_id = product_template.id AND product_category.expense_account_id = account_account.id""".strip())
60 def test_raise_missing_lhs(self):
62 query.tables.append('"product_product"')
63 self.assertRaises(AssertionError, query.add_join, ("product_template", "product_category", "categ_id", "id", "categ_id"), implicit=False, outer=False)
66 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: