Skip to content

Commit e8e04fb

Browse files
committed
first implementation of csv load function. Failing
1 parent f81a281 commit e8e04fb

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

runtests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def start():
3131
sys.argv.pop(1)
3232

3333
if pulsar:
34+
from pulsar.apps.test import TestSuite
35+
from pulsar.apps.test.plugins import bench
36+
3437
os.environ['stdnet_test_suite'] = 'pulsar'
3538
suite = TestSuite(
3639
description = 'Stdnet Asynchronous test suite',

stdnet/orm/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import sys
3-
from csv import DictWriter
3+
from csv import DictWriter, DictReader
44
from inspect import isclass
55

66
from stdnet.utils import StringIO
@@ -83,7 +83,8 @@ def serialize(self, qs):
8383
def write(self, stream = None):
8484
raise NotImplementedError()
8585

86-
def load(self, stream):
86+
def load(self, stream, model = None):
87+
'''Load a stream of data into the database.'''
8788
raise NotImplementedError()
8889

8990

@@ -108,7 +109,7 @@ def write(self, stream = None):
108109
stream.write(line)
109110
return stream
110111

111-
def load(self, stream):
112+
def load(self, stream, model = None):
112113
data = json.loads(stream, **self.options)
113114
for model_data in data:
114115
model = get_model_from_hash(model_data['hash'])
@@ -152,6 +153,14 @@ def write(self, stream = None):
152153
for row in data:
153154
w.writerow(row)
154155
return stream
156+
157+
def load(self, stream, model = None):
158+
if not model:
159+
raise ValueError('Model is required when loading from csv file')
160+
r = DictReader(stream, **self.options)
161+
with model.objects.transaction() as t:
162+
for item_data in r:
163+
t.add(model.from_base64_data(**item_data))
155164

156165

157166
register_serializer('json', JsonSerializer)

stdnet/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __call__(self, result=None):
9595
################################################################################
9696
try:
9797
import pulsar
98-
from pulsar.apps.test import TestSuite, TestOptionPlugin
98+
from pulsar.apps.test import TestOptionPlugin
9999
from pulsar.apps.test.plugins import bench
100100

101101

tests/regression/serialize/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ def testWrite(self):
3030

3131
def testLoad(self):
3232
s = self.testDump()
33+
qs = self.model.objects.query().sort_by('id').all()
3334
data = s.write().getvalue()
3435
self.model.objects.flush()
35-
s.load(data)
36+
s.load(data, self.model)
37+
qs2 = self.model.objects.query().sort_by('id').all()
38+
self.assertEqual(qs,qs2)
3639

3740

3841
class DummySerializer(orm.Serializer):

0 commit comments

Comments
 (0)