Launchpad automatic translations update.
[odoo/odoo.git] / openerp / addons / base / tests / test_ir_attachment.py
1 import hashlib
2 import os
3
4 import unittest2
5
6 import openerp
7 import openerp.tests.common
8
9 class test_ir_attachment(openerp.tests.common.TransactionCase):
10
11     def test_00_attachment_flow(self):
12         registry, cr, uid = self.registry, self.cr, self.uid
13         root_path = openerp.tools.config['root_path']
14         ira = registry('ir.attachment')
15
16         # Blob1
17         blob1 = 'blob1'
18         blob1_b64 = blob1.encode('base64')
19         blob1_hash = hashlib.sha1(blob1).hexdigest()
20         blob1_fname = blob1_hash[:3] + '/' + blob1_hash
21
22         # Blob2
23         blob2 = 'blob2'
24         blob2_b64 = blob2.encode('base64')
25         blob2_hash = hashlib.sha1(blob2).hexdigest()
26         blob2_fname = blob2_hash[:3] + '/' + blob2_hash
27
28         # 'ir_attachment.location' is undefined test database storage
29         a1 = ira.create(cr, uid, {'name': 'a1', 'datas': blob1_b64})
30         a1_read = ira.read(cr, uid, [a1], ['datas'])
31         self.assertEqual(a1_read[0]['datas'], blob1_b64)
32
33         cr.execute("select id,db_datas from ir_attachment where id = %s", (a1,) )
34         a1_db_datas = str(cr.fetchall()[0][1])
35         self.assertEqual(a1_db_datas, blob1_b64)
36
37         # define a location for filestore
38         registry('ir.config_parameter').set_param(cr, uid, 'ir_attachment.location', 'file:///filestore')
39
40         # Test file storage
41         a2 = ira.create(cr, uid, {'name': 'a2', 'datas': blob1_b64})
42         a2_read = ira.read(cr, uid, [a2], ['datas'])
43         self.assertEqual(a2_read[0]['datas'], blob1_b64)
44
45         cr.execute("select id,store_fname from ir_attachment where id = %s", (a2,) )
46         a2_store_fname = cr.fetchall()[0][1]
47         self.assertEqual(a2_store_fname, blob1_fname)
48
49         a2_fn = os.path.join(root_path, 'filestore', cr.dbname, blob1_hash[:3], blob1_hash)
50         fc = file(a2_fn).read()
51         self.assertEqual(fc, blob1)
52
53         # create a3 with same blob
54         a3 = ira.create(cr, uid, {'name': 'a3', 'datas': blob1_b64})
55         a3_read = ira.read(cr, uid, [a3], ['datas'])
56         self.assertEqual(a3_read[0]['datas'], blob1_b64)
57
58         cr.execute("select id,store_fname from ir_attachment where id = %s", (a3,) )
59         a3_store_fname = cr.fetchall()[0][1]
60         self.assertEqual(a3_store_fname, a2_store_fname)
61
62         # create a4 blob2
63         a4 = ira.create(cr, uid, {'name': 'a4', 'datas': blob2_b64})
64         a4_read = ira.read(cr, uid, [a4], ['datas'])
65         self.assertEqual(a4_read[0]['datas'], blob2_b64)
66
67         a4_fn = os.path.join(root_path, 'filestore', cr.dbname, blob2_hash[:3], blob2_hash)
68         self.assertTrue(os.path.isfile(a4_fn))
69
70         # delete a3 but file stays
71         ira.unlink(cr, uid, [a3])
72         self.assertTrue(os.path.isfile(a2_fn))
73
74         # delete a2 it is unlinked
75         ira.unlink(cr, uid, [a2])
76         self.assertFalse(os.path.isfile(a2_fn))
77
78         # update a4 blob2 by blob1
79         ira.write(cr, uid, [a4], {'datas': blob1_b64})
80         a4_read = ira.read(cr, uid, [a4], ['datas'])
81         self.assertEqual(a4_read[0]['datas'], blob1_b64)
82
83         # file of a4 disapear and a2 reappear
84         self.assertFalse(os.path.isfile(a4_fn))
85         self.assertTrue(os.path.isfile(a2_fn))
86
87         # everybody applause
88
89