Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions orm_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,34 @@ def _check_removed_columns(self, cr, log=False):
pass

def name_get(self, cr, user, ids, context=None):
result = self.read(cr, user, ids, [self._rec_name])
return [(x['id'], x[self._rec_name]) for x in result]
if not context:
context = {}
if not ids:
return []
if isinstance(ids, integer_types):
ids = [ids]
return [(r['id'], tools.ustr(r[self._rec_name])) for r in self.read(cr, user, ids,
[self._rec_name], context, load='_classic_write')]

def name_search(self, cr, user, name='', args=None, operator='ilike',
context=None, limit=100):
if args is None:
args = []
if context is None:
context = {}

# Build the search domain
search_domain = list(args)

# Add the name filter if name is provided
if name:
search_domain.append((self._rec_name, operator, name))

# Search for matching records
ids = self.search(cr, user, search_domain, limit=limit, context=context)

# Return results in name_get format
return self.name_get(cr, user, ids, context=context)

def perm_read(self, cr, user, ids, context=None, details=True):

Expand Down
96 changes: 96 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,109 @@ def test_name_get(self):
[(mmt_id, 'Foo')]
)

# Test with single id (integer)
result = mmt_obj.name_get(cursor, uid, mmt_id)
self.assertListEqual(
result,
[(mmt_id, 'Foo')]
)

# Test with empty ids
result = mmt_obj.name_get(cursor, uid, [])
self.assertListEqual(result, [])

# Changing the rec_name should use other field
MongoModelTest._rec_name = 'other_name'
result = mmt_obj.name_get(cursor, uid, [mmt_id])
self.assertListEqual(
result,
[(mmt_id, 'Bar')]
)
# Reset _rec_name to default
MongoModelTest._rec_name = 'name'

def test_name_search(self):
self.create_model()
cursor = self.txn.cursor
uid = self.txn.user
mmt_obj = self.openerp.pool.get(MongoModelTest._name)

# Save original _rec_name
original_rec_name = MongoModelTest._rec_name

# Create test records
mmt_id1 = mmt_obj.create(cursor, uid, {
'name': 'Apple Product',
'other_name': 'Product A',
'boolean_field': True
})
mmt_id2 = mmt_obj.create(cursor, uid, {
'name': 'Banana Product',
'other_name': 'Product B',
'boolean_field': True
})
mmt_id3 = mmt_obj.create(cursor, uid, {
'name': 'Cherry Product',
'other_name': 'Product C',
'boolean_field': False
})

# Test basic name search with ilike operator (default)
result = mmt_obj.name_search(cursor, uid, 'Apple')
self.assertIn((mmt_id1, 'Apple Product'), result)
self.assertEqual(len(result), 1)

# Test case-insensitive search
result = mmt_obj.name_search(cursor, uid, 'apple')
self.assertIn((mmt_id1, 'Apple Product'), result)
self.assertEqual(len(result), 1)

# Test partial match
result = mmt_obj.name_search(cursor, uid, 'Product')
self.assertEqual(len(result), 3)
result_ids = [r[0] for r in result]
self.assertIn(mmt_id1, result_ids)
self.assertIn(mmt_id2, result_ids)
self.assertIn(mmt_id3, result_ids)

# Test with args (domain filters)
result = mmt_obj.name_search(cursor, uid, 'Product',
args=[('boolean_field', '=', True)])
self.assertEqual(len(result), 2)
result_ids = [r[0] for r in result]
self.assertIn(mmt_id1, result_ids)
self.assertIn(mmt_id2, result_ids)
self.assertNotIn(mmt_id3, result_ids)

# Test with empty name (should return all records or filtered by args)
result = mmt_obj.name_search(cursor, uid, '',
args=[('boolean_field', '=', False)])
self.assertEqual(len(result), 1)
self.assertIn((mmt_id3, 'Cherry Product'), result)

# Test with limit
result = mmt_obj.name_search(cursor, uid, 'Product', limit=2)
self.assertEqual(len(result), 2)

# Test with exact match operator
result = mmt_obj.name_search(cursor, uid, 'Apple Product', operator='=')
self.assertEqual(len(result), 1)
self.assertIn((mmt_id1, 'Apple Product'), result)

# Test with like operator
result = mmt_obj.name_search(cursor, uid, 'Banana%', operator='like')
self.assertEqual(len(result), 1)
self.assertIn((mmt_id2, 'Banana Product'), result)

# Test with different _rec_name
try:
MongoModelTest._rec_name = 'other_name'
result = mmt_obj.name_search(cursor, uid, 'Product A')
self.assertIn((mmt_id1, 'Product A'), result)
self.assertEqual(len(result), 1)
finally:
# Always restore original _rec_name
MongoModelTest._rec_name = original_rec_name

def test_boolean(self):
self.create_model()
Expand Down
Loading