Changeset 221
- Timestamp:
- 07/11/06 18:01:29
- Files:
-
- trunk/arenas.py (modified) (2 diffs)
- trunk/storage/storeado.py (modified) (3 diffs)
- trunk/test/test_storemsaccess.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/arenas.py
r220 r221 1 1 2 2 import ConfigParser 3 from types import ClassType 3 4 4 5 from containers import Graph … … 70 71 if isinstance(store, basestring): 71 72 store = xray.classes(store)(name, self, options or {}) 73 74 elif isinstance(store, (type, ClassType)): 75 store = store(name, self, options or {}) 72 76 73 77 self.stores[name] = store trunk/storage/storeado.py
r220 r221 121 121 return datetime.time(hour, minute, second) 122 122 123 def float_from_com(com_currency): 124 """Convert a value from a Currency field to a float value. 125 126 Courtesy of Alex Martelli from: 127 http://groups.google.com/group/comp.lang.python/browse_frm/thread/fed03c64735c9e9c 128 """ 129 return float((long(com_currency[1])&0xFFFFFFFFL) | 130 (long(com_currency[0]) << 32)) / 1e4 131 132 def decimal_from_com(com_currency): 133 """Convert a value from a Currency field to a Decimal value.""" 134 135 return decimal.Decimal(str(float_from_com(com_currency))) 136 123 137 124 138 class AdapterFromADO(db.AdapterFromDB): … … 163 177 # See coerce_datetime 164 178 return time_from_com(value) 179 180 def coerce_decimal_Decimal(self, value, coltype): 181 if coltype == 0x06: 182 # Currency 183 if isinstance(value, tuple): 184 value = decimal_from_com(value) 185 else: 186 value = decimal.Decimal(str(value)) 187 return value.quantize(decimal.Decimal('.01')) 165 188 166 189 def coerce_fixedpoint_FixedPoint(self, value, coltype): 167 190 if coltype == 0x06: 168 191 # Currency 169 value = value[1] / 10000.0 192 if isinstance(value, tuple): 193 value = float_from_com(value) 170 194 return fixedpoint.FixedPoint(value) 171 195 … … 173 197 if coltype == 0x06: 174 198 # Currency 175 value = value[1] / 10000.0 199 if isinstance(value, tuple): 200 value = float_from_com(value) 176 201 return float(value) 177 202 trunk/test/test_storemsaccess.py
r220 r221 18 18 "The MSAccess test will not be run.") 19 19 else: 20 21 class CurrencyAdapter(storeado.FieldTypeAdapter_MSAccess): 22 """Stores Decimal and FixedPoint objects as CURRENCY.""" 23 24 def coerce_decimal_Decimal(self, cls, key): 25 return "CURRENCY" 26 27 def coerce_fixedpoint_FixedPoint(self, cls, key): 28 return "CURRENCY" 29 20 30 SM_class = "dejavu.storage.storeado.StorageManagerADO_MSAccess" 21 31 opts = {u'Connect': "PROVIDER=MICROSOFT.JET.OLEDB.4.0;" … … 25 35 def run(): 26 36 import zoo_fixture 37 38 # lists to keep track of the test_currency runs 39 standard_runs = [] 40 altered_runs = [] 41 42 def test_currency(obj): 43 sm = zoo_fixture.arena.stores['testSM'] 44 fta = sm.typeAdapter.__class__.__name__ 45 46 clsnames = ('Exhibit', 'Zoo') 47 tblnames = [sm.table_name(cn).strip('[]') for cn in clsnames] 48 colnames = ('Acreage', 'Admission') 49 targets = dict(zip(tblnames, colnames)) 50 data, _ = sm.fetch(storeado.adSchemaColumns, conn=None, 51 schema=True) 52 for row in data: 53 match = targets.get(row[2]) 54 if not match: 55 continue 56 if match == row[3]: 57 ## print row[2], row[3], row[11] 58 dt = row[11] 59 60 if fta in ("CurrencyAdapter",): 61 obj.assertEqual(dt, storeado.adCurrency) 62 else: 63 obj.assertEqual(dt, storeado.adDouble) 64 obj.assertEqual(len(standard_runs), 0) 65 66 27 67 # Isolate schema changes from one test to the next. 28 68 reload(zoo_fixture) 69 70 # test the standard MS Access setup where Decimal and FixedPoint 71 # objects are stored in the database as INTEGERS, LONGS or DOUBLES 72 print 73 print "Standard MSAccess test." 74 zoo_fixture.ZooTests.test_currency = test_currency 29 75 zoo_fixture.init() 30 76 zoo_fixture.run(SM_class, opts) 77 standard_runs.append(True) 78 79 # plugin the adapter for testing CURRENCY columns 80 storeado.StorageManagerADO_MSAccess.typeAdapter = CurrencyAdapter() 81 82 reload(zoo_fixture) 83 84 # test storing and recalling Decimal values to the database using 85 # CURRENCY columns - the (current) default behavior of pythoncom 86 # is to return CURRENCY values as a tuple 87 print 88 print "MSAccess test - CURRENCY returned as tuple." 89 zoo_fixture.ZooTests.test_currency = test_currency 90 zoo_fixture.init() 91 zoo_fixture.run(SM_class, opts) 92 altered_runs.append(True) 93 94 # set pythoncom to return CURRENCY values as Decimal objects 95 pythoncom.__future_currency__ = True 96 97 reload(zoo_fixture) 98 99 # test storing and recalling Decimal values to the database using 100 # CURRENCY columns - CURRENCY values are now returned as Decimal 101 # objects 102 print 103 print "MSAccess test - CURRENCY returned as Decimal." 104 zoo_fixture.ZooTests.test_currency = test_currency 105 zoo_fixture.init() 106 zoo_fixture.run(SM_class, opts) 107 altered_runs.append(True) 31 108 32 109 if __name__ == "__main__":
