Files
tryton/trytond/tests/test_modelsingleton.py
T

152 lines
4.8 KiB
Python
Raw Normal View History

2010-03-04 14:02:27 +01:00
# -*- coding: utf-8 -*-
2014-12-02 12:54:01 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2010-03-04 14:02:27 +01:00
import unittest
from datetime import datetime
from trytond.tests.test_tryton import activate_module, with_transaction
2010-08-19 22:01:12 +02:00
from trytond.transaction import Transaction
2016-02-06 19:10:35 +01:00
from trytond.pool import Pool
2010-03-04 14:02:27 +01:00
class ModelSingletonTestCase(unittest.TestCase):
2014-01-12 19:58:22 +01:00
'Test ModelSingleton'
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
@classmethod
def setUpClass(cls):
activate_module('tests')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
@with_transaction()
def test_read(self):
2014-01-12 19:58:22 +01:00
'Test read method'
2016-02-06 19:10:35 +01:00
pool = Pool()
Singleton = pool.get('test.singleton')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton, = Singleton.read([1], ['name'])
2018-06-26 19:11:19 +02:00
self.assertTrue(singleton['name'] == 'test')
self.assertTrue(singleton['id'] == 1)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton, = Singleton.read([1], ['name'])
2018-06-26 19:11:19 +02:00
self.assertTrue(singleton['name'] == 'test')
self.assertTrue(singleton['id'] == 1)
2016-02-06 19:10:35 +01:00
singleton, = Singleton.read([1], [
'create_uid',
'create_uid.rec_name',
'create_date',
'write_uid',
'write_date',
])
self.assertEqual(singleton['create_uid'], Transaction().user)
2018-12-21 23:41:56 +01:00
self.assertEqual(singleton['create_uid.']['rec_name'], 'Administrator')
2018-06-26 19:11:19 +02:00
self.assertTrue(isinstance(singleton['create_date'], datetime))
2016-02-06 19:10:35 +01:00
self.assertEqual(singleton['write_uid'], None)
self.assertEqual(singleton['write_date'], None)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
@with_transaction()
def test_create(self):
2014-01-12 19:58:22 +01:00
'Test create method'
2016-02-06 19:10:35 +01:00
pool = Pool()
Singleton = pool.get('test.singleton')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton, = Singleton.create([{'name': 'bar'}])
2018-06-26 19:11:19 +02:00
self.assertTrue(singleton)
2016-02-06 19:10:35 +01:00
self.assertEqual(singleton.name, 'bar')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton2, = Singleton.create([{'name': 'foo'}])
self.assertEqual(singleton2, singleton)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
self.assertEqual(singleton.name, 'foo')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singletons = Singleton.search([])
self.assertEqual(singletons, [singleton])
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
@with_transaction()
def test_copy(self):
2014-01-12 19:58:22 +01:00
'Test copy method'
2016-02-06 19:10:35 +01:00
pool = Pool()
Singleton = pool.get('test.singleton')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton, = Singleton.search([])
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton2, = Singleton.copy([singleton])
self.assertEqual(singleton2, singleton)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singletons = Singleton.search([])
self.assertEqual(len(singletons), 1)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton3, = Singleton.copy([singleton], {'name': 'bar'})
self.assertEqual(singleton3, singleton)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singletons = Singleton.search([])
self.assertEqual(len(singletons), 1)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
@with_transaction()
def test_default_get(self):
2014-01-12 19:58:22 +01:00
'Test default_get method'
2016-02-06 19:10:35 +01:00
pool = Pool()
Singleton = pool.get('test.singleton')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
default = Singleton.default_get(['name'])
self.assertEqual(default, {'name': 'test'})
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
default = Singleton.default_get(['create_uid'])
self.assertEqual(len(default), 2)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
default = Singleton.default_get(['create_uid'],
with_rec_name=False)
self.assertEqual(len(default), 1)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
Singleton.create([{'name': 'bar'}])
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
default = Singleton.default_get(['name'])
self.assertEqual(default, {'name': 'bar'})
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
default = Singleton.default_get(['create_uid'])
self.assertEqual(len(default), 2)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
default = Singleton.default_get(['create_uid'],
with_rec_name=False)
self.assertEqual(len(default), 1)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
@with_transaction()
def test_search(self):
2014-01-12 19:58:22 +01:00
'Test search method'
2016-02-06 19:10:35 +01:00
pool = Pool()
Singleton = pool.get('test.singleton')
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singletons = Singleton.search([])
2018-06-26 19:11:19 +02:00
self.assertEqual(list(map(int, singletons)), [1])
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singletons = Singleton.search([])
2018-06-26 19:11:19 +02:00
self.assertEqual(list(map(int, singletons)), [1])
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
count = Singleton.search([], count=True)
self.assertEqual(count, 1)
Singleton.create([{'name': 'foo'}])
singleton, = Singleton.search([('name', '=', 'foo')])
self.assertEqual(singleton.name, 'foo')
singletons = Singleton.search([('name', '=', 'bar')])
self.assertEqual(singletons, [])
2010-03-04 14:02:27 +01:00
@with_transaction()
def test_all_cache_cleared(self):
"Test all cache cleared"
pool = Pool()
Singleton = pool.get('test.singleton')
singleton, = Singleton.create([{'name': 'foo'}])
singleton.name # Fill the cache
singleton2 = Singleton(singleton.id + 1) # Use a different id
singleton2.name # Fill the cache
Singleton.write([singleton], {'name': 'bar'})
singleton2 = Singleton(singleton2.id) # Clear local cache
self.assertEqual(singleton2.name, 'bar')
Singleton.delete([singleton])
self.assertEqual(singleton2.name, 'test')
2012-04-01 23:08:15 +02:00
2010-03-04 14:02:27 +01:00
def suite():
return unittest.TestLoader().loadTestsFromTestCase(ModelSingletonTestCase)