Skip to content

Commit 876d62a

Browse files
improve tests for adapted attributes
1 parent 4a56d42 commit 876d62a

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

tests/schema_adapted.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datajoint as dj
22
import networkx as nx
3+
import json
34
from pathlib import Path
45
import tempfile
56
from datajoint import errors
@@ -11,8 +12,8 @@
1112
S3_CONN_INFO,
1213
protocol='s3',
1314
location='adapted/repo',
14-
stage=tempfile.mkdtemp())
15-
}
15+
stage=tempfile.mkdtemp())}
16+
1617
dj.config['stores'] = stores_config
1718

1819
schema_name = PREFIX + '_test_custom_datatype'
@@ -53,37 +54,36 @@ class Connectivity(dj.Manual):
5354
errors._switch_filepath_types(True)
5455

5556

56-
class Filepath2GraphAdapter(dj.AttributeAdapter):
57+
class LayoutToFilepath(dj.AttributeAdapter):
58+
"""
59+
An adapted data type that saves a graph layout into fixed filepath
60+
"""
5761

5862
attribute_type = 'filepath@repo_s3'
5963

6064
@staticmethod
61-
def get(obj):
62-
s = open(obj, "r").read()
63-
return nx.spring_layout(
64-
nx.lollipop_graph(4, 2), seed=int(s))
65+
def get(path):
66+
with open(path, "r") as f:
67+
return json.load(f)
6568

6669
@staticmethod
67-
def put(obj):
68-
path = Path(
69-
dj.config['stores']['repo_s3']['stage'], 'sample.txt')
70-
71-
f = open(path, "w")
72-
f.write(str(obj*obj))
73-
f.close()
74-
70+
def put(layout):
71+
path = Path(dj.config['stores']['repo_s3']['stage'], 'layout.json')
72+
with open(str(path), "w") as f:
73+
json.dump(layout, f)
7574
return path
7675

7776

78-
file2graph = Filepath2GraphAdapter()
77+
layout_to_filepath = LayoutToFilepath()
7978

8079

8180
@schema
82-
class Position(dj.Manual):
81+
class Layout(dj.Manual):
8382
definition = """
84-
pos_id : int
83+
# stores graph layout
84+
-> Connectivity
8585
---
86-
seed_root: <file2graph>
86+
layout: <layout_to_filepath>
8787
"""
8888

8989
errors._switch_filepath_types(False)

tests/test_adapted_attributes.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import datajoint as dj
22
import networkx as nx
33
from itertools import zip_longest
4-
from nose.tools import assert_true, assert_equal
4+
from nose.tools import assert_true, assert_equal, assert_dict_equal
55
from . import schema_adapted as adapted
6-
from .schema_adapted import graph, file2graph
6+
from .schema_adapted import graph, layout_to_filepath
77

88

99
def test_adapted_type():
@@ -22,17 +22,25 @@ def test_adapted_type():
2222

2323
def test_adapted_filepath_type():
2424
# https://github.com/datajoint/datajoint-python/issues/684
25+
2526
dj.errors._switch_adapted_types(True)
2627
dj.errors._switch_filepath_types(True)
27-
c = adapted.Position()
28-
Position.insert([{'pos_id': 0, 'seed_root': 3}])
29-
result = (Position & 'pos_id=0').fetch1('seed_root')
3028

31-
assert_true(isinstance(result, dict))
32-
assert_equal(0.3761992090175474, result[1][0])
33-
assert_true(6 == len(result))
29+
c = adapted.Connectivity()
30+
c.delete()
31+
c.insert1((0, nx.lollipop_graph(4, 2)))
3432

33+
layout = nx.spring_layout(c.fetch1('conn_graph'))
34+
# make json friendly
35+
layout = {str(k): [round(r, ndigits=4) for r in v] for k, v in layout.items()}
36+
t = adapted.Layout()
37+
t.insert1((0, layout))
38+
result = t.fetch1('layout')
39+
assert_dict_equal(result, layout)
40+
41+
t.delete()
3542
c.delete()
43+
3644
dj.errors._switch_filepath_types(False)
3745
dj.errors._switch_adapted_types(False)
3846

0 commit comments

Comments
 (0)