Skip to content

Commit c766427

Browse files
committed
feat: Add FsFileType.data
1 parent 8021a20 commit c766427

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

graphene_mongo/advanced_types.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ def _resolve_type_coordinates(self, info):
1111

1212
def _resolve_data(self, info):
1313
v = getattr(self.instance, self.key)
14-
data = v.read()
15-
return base64.b64encode(data)
14+
return base64.b64encode(v.read())
1615

1716

1817
class FsFile(mongoengine.Document):
@@ -30,7 +29,17 @@ class FsFileType(MongoengineObjectType):
3029
class Meta:
3130
model = FsFile
3231

33-
data = graphene.String(resolver=_resolve_data)
32+
# data = graphene.String(
33+
# resolver=lambda self, *args, **kwargs: self.resolve_data())
34+
35+
data = graphene.String(
36+
resolver=_resolve_data)
37+
38+
def resolve_data(self):
39+
v = getattr(self.instance, self.key)
40+
data = v.read()
41+
print(type(data))
42+
return base64.b64encode(data)
3443

3544

3645
class _TypeField(graphene.ObjectType):

graphene_mongo/tests/setup.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
current_dirname = os.path.dirname(os.path.abspath(__file__))
1313

14+
15+
@pytest.fixture()
16+
def fixtures_dirname():
17+
return os.path.join(current_dirname, 'fixtures')
18+
19+
1420
@pytest.fixture(scope='module')
1521
def fixtures():
1622
Publisher.drop_collection()
@@ -31,13 +37,6 @@ def fixtures():
3137
editor1.avatar.put(f, content_type='image/jpeg')
3238
editor1.save()
3339

34-
e = Editor.objects(first_name='Penny').first()
35-
avatar = e.avatar.read()
36-
print(e.avatar.content_type)
37-
print(e.avatar.chunk_size)
38-
print(e.avatar.md5)
39-
print(type(avatar))
40-
4140
editor2 = Editor(
4241
id='2',
4342
first_name='Grant',

graphene_mongo/tests/test_query.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import base64
2+
import os
13
import json
2-
34
import graphene
45

5-
from .setup import fixtures
6+
from .setup import fixtures, fixtures_dirname
67
from .models import (
78
Child, Editor, Player, Reporter, ProfessorVector, Parent, CellTower
89
)
@@ -11,7 +12,7 @@
1112
)
1213

1314

14-
def test_should_query_editor(fixtures):
15+
def test_should_query_editor(fixtures, fixtures_dirname):
1516

1617
class Query(graphene.ObjectType):
1718

@@ -37,7 +38,7 @@ def resolve_editors(self, *args, **kwargs):
3738
chunkSize,
3839
length,
3940
md5,
40-
chunk
41+
data
4142
}
4243
}
4344
editors {
@@ -46,6 +47,11 @@ def resolve_editors(self, *args, **kwargs):
4647
}
4748
}
4849
'''
50+
51+
avator_filename = os.path.join(fixtures_dirname, 'image.jpg')
52+
with open(avator_filename, 'rb') as f:
53+
data = base64.b64encode(f.read())
54+
4955
expected = {
5056
'editor': {
5157
'firstName': 'Penny',
@@ -55,7 +61,7 @@ def resolve_editors(self, *args, **kwargs):
5561
'chunkSize': 261120,
5662
'length': 46928,
5763
'md5': 'f3c657fd472fdc4bc2ca9056a1ae6106',
58-
'chunk': 'abc'
64+
'data': str(data)
5965
}
6066
},
6167
'editors': [{

0 commit comments

Comments
 (0)