Files
tryton/trytond/tests/test_modelsingleton.py
T

128 lines
4.5 KiB
Python
Raw Normal View History

2010-03-04 14:02:27 +01:00
# -*- coding: utf-8 -*-
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import unittest
from datetime import datetime
2010-08-19 22:01:12 +02:00
from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT, \
install_module
from trytond.transaction import Transaction
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
def setUp(self):
2013-09-16 11:38:11 +02:00
install_module('tests')
2010-03-04 14:02:27 +01:00
self.singleton = POOL.get('test.singleton')
def test0010read(self):
2014-01-12 19:58:22 +01:00
'Test read method'
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
2012-09-24 22:16:53 +02:00
singleton, = self.singleton.read([1], ['name'])
2010-08-19 22:01:12 +02:00
self.assert_(singleton['name'] == 'test')
self.assert_(singleton['id'] == 1)
2010-03-04 14:02:27 +01:00
singleton, = self.singleton.read([1], ['name'])
2010-08-19 22:01:12 +02:00
self.assert_(singleton['name'] == 'test')
self.assert_(singleton['id'] == 1)
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
singleton, = self.singleton.read([1], [
'create_uid',
'create_uid.rec_name',
'create_date',
'write_uid',
'write_date',
])
self.assertEqual(singleton['create_uid'], USER)
self.assertEqual(singleton['create_uid.rec_name'], 'Administrator')
self.assert_(isinstance(singleton['create_date'], datetime))
2012-12-16 23:54:41 +01:00
self.assertEqual(singleton['write_uid'], None)
self.assertEqual(singleton['write_date'], None)
2010-08-19 22:01:12 +02:00
transaction.cursor.rollback()
2010-03-04 14:02:27 +01:00
def test0020create(self):
2014-01-12 19:58:22 +01:00
'Test create method'
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
2013-01-06 22:18:13 +01:00
singleton, = self.singleton.create([{'name': 'bar'}])
2012-09-24 22:16:53 +02:00
self.assert_(singleton)
self.assertEqual(singleton.name, 'bar')
2010-03-04 14:02:27 +01:00
2013-01-06 22:18:13 +01:00
singleton2, = self.singleton.create([{'name': 'foo'}])
2012-09-24 22:16:53 +02:00
self.assertEqual(singleton2, singleton)
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
self.assertEqual(singleton.name, 'foo')
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
singletons = self.singleton.search([])
self.assertEqual(singletons, [singleton])
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
transaction.cursor.rollback()
2010-03-04 14:02:27 +01:00
def test0030copy(self):
2014-01-12 19:58:22 +01:00
'Test copy method'
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
2012-09-24 22:16:53 +02:00
singleton, = self.singleton.search([])
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
singleton2, = self.singleton.copy([singleton])
self.assertEqual(singleton2, singleton)
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
singletons = self.singleton.search([])
2012-09-28 18:06:27 +02:00
self.assertEqual(len(singletons), 1)
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
singleton3, = self.singleton.copy([singleton], {'name': 'bar'})
self.assertEqual(singleton3, singleton)
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
singletons = self.singleton.search([])
2012-09-28 18:06:27 +02:00
self.assertEqual(len(singletons), 1)
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
transaction.cursor.rollback()
2010-03-04 14:02:27 +01:00
def test0040default_get(self):
2014-01-12 19:58:22 +01:00
'Test default_get method'
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
2010-08-19 22:01:12 +02:00
default = self.singleton.default_get(['name'])
2012-09-24 22:16:53 +02:00
self.assertEqual(default, {'name': 'test'})
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
default = self.singleton.default_get(['create_uid'])
2012-09-24 22:16:53 +02:00
self.assertEqual(len(default), 2)
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
default = self.singleton.default_get(['create_uid'],
with_rec_name=False)
2012-09-24 22:16:53 +02:00
self.assertEqual(len(default), 1)
2010-03-04 14:02:27 +01:00
2013-01-06 22:18:13 +01:00
self.singleton.create([{'name': 'bar'}])
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
default = self.singleton.default_get(['name'])
2012-09-24 22:16:53 +02:00
self.assertEqual(default, {'name': 'bar'})
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
default = self.singleton.default_get(['create_uid'])
2012-09-24 22:16:53 +02:00
self.assertEqual(len(default), 2)
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
default = self.singleton.default_get(['create_uid'],
with_rec_name=False)
2012-09-24 22:16:53 +02:00
self.assertEqual(len(default), 1)
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
transaction.cursor.rollback()
2010-03-04 14:02:27 +01:00
def test0050search(self):
2014-01-12 19:58:22 +01:00
'Test search method'
with Transaction().start(DB_NAME, USER,
context=CONTEXT) as transaction:
2012-09-24 22:16:53 +02:00
singletons = self.singleton.search([])
self.assertEqual(map(int, singletons), [1])
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
singletons = self.singleton.search([])
self.assertEqual(map(int, singletons), [1])
2010-03-04 14:02:27 +01:00
2012-09-24 22:16:53 +02:00
count = self.singleton.search([], count=True)
self.assertEqual(count, 1)
2010-03-04 14:02:27 +01:00
2010-08-19 22:01:12 +02:00
transaction.cursor.rollback()
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)