Files
tryton/trytond/tests/test_modelsingleton.py
T

134 lines
4.1 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
2016-02-06 19:10:35 +01:00
from trytond.tests.test_tryton import install_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):
2013-09-16 11:38:11 +02:00
install_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'])
self.assert_(singleton['name'] == 'test')
self.assert_(singleton['id'] == 1)
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singleton, = Singleton.read([1], ['name'])
self.assert_(singleton['name'] == 'test')
self.assert_(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)
self.assertEqual(singleton['create_uid.rec_name'], 'Administrator')
self.assert_(isinstance(singleton['create_date'], datetime))
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'}])
self.assert_(singleton)
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([])
self.assertEqual(map(int, singletons), [1])
2010-03-04 14:02:27 +01:00
2016-02-06 19:10:35 +01:00
singletons = Singleton.search([])
self.assertEqual(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
2012-04-01 23:08:15 +02:00
2010-03-04 14:02:27 +01:00
def suite():
return unittest.TestLoader().loadTestsFromTestCase(ModelSingletonTestCase)