7 import openerp.tests.common
9 class test_ir_attachment(openerp.tests.common.TransactionCase):
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')
18 blob1_b64 = blob1.encode('base64')
19 blob1_hash = hashlib.sha1(blob1).hexdigest()
20 blob1_fname = blob1_hash[:3] + '/' + blob1_hash
24 blob2_b64 = blob2.encode('base64')
25 blob2_hash = hashlib.sha1(blob2).hexdigest()
26 blob2_fname = blob2_hash[:3] + '/' + blob2_hash
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)
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)
37 # define a location for filestore
38 registry('ir.config_parameter').set_param(cr, uid, 'ir_attachment.location', 'file:///filestore')
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)
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)
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)
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)
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)
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)
67 a4_fn = os.path.join(root_path, 'filestore', cr.dbname, blob2_hash[:3], blob2_hash)
68 self.assertTrue(os.path.isfile(a4_fn))
70 # delete a3 but file stays
71 ira.unlink(cr, uid, [a3])
72 self.assertTrue(os.path.isfile(a2_fn))
74 # delete a2 it is unlinked
75 ira.unlink(cr, uid, [a2])
76 self.assertFalse(os.path.isfile(a2_fn))
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)
83 # file of a4 disapear and a2 reappear
84 self.assertFalse(os.path.isfile(a4_fn))
85 self.assertTrue(os.path.isfile(a2_fn))