FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
diffpy.utils/tests/test_serialization.py at 3.7.2 · diffpy/diffpy.utils · GitHub
diffpy
diffpy.utils
Repository navigation
Code
Issues
23
(23)
Pull requests
3
(3)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
diffpy.utils
/
tests
/
test_serialization.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
145 lines (131 loc) · 4.69 KB
Breadcrumbs
diffpy.utils
/
tests
/
test_serialization.py
Copy path
File metadata and controls
145 lines (131 loc) · 4.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from
pathlib
import
Path
import
numpy
import
pytest
from
diffpy
.
utils
.
parsers
import
load_data
from
diffpy
.
utils
.
parsers
.
custom_exceptions
import
(
ImproperSizeError
,
UnsupportedTypeError
,
)
from
diffpy
.
utils
.
parsers
.
serialization
import
deserialize_data
,
serialize_data
def
test_load_multiple
(
tmp_path
,
datafile
):
# Load test data
targetjson
=
datafile
(
"targetjson.json"
)
generatedjson
=
tmp_path
/
"generated_serialization.json"
dbload_dir
=
datafile
(
"dbload"
)
tlm_list
=
sorted
(
dbload_dir
.
glob
(
"*.gr"
))
generated_data
=
None
for
headerfile
in
tlm_list
:
# gather data using load_data
hdata
=
load_data
(
headerfile
,
headers
=
True
)
data_table
=
load_data
(
headerfile
)
# check path extraction
generated_data
=
serialize_data
(
headerfile
,
hdata
,
data_table
,
dt_colnames
=
[
"r"
,
"gr"
],
show_path
=
True
,
)
assert
Path
(
headerfile
).
resolve
()
==
Path
(
generated_data
[
headerfile
.
name
].
pop
(
"path"
)
)
# rerun without path information and save to file
generated_data
=
serialize_data
(
headerfile
,
hdata
,
data_table
,
dt_colnames
=
[
"r"
,
"gr"
],
show_path
=
False
,
serial_file
=
generatedjson
,
)
# compare to target
target_data
=
deserialize_data
(
targetjson
)
assert
target_data
==
generated_data
# ensure file saved properly
assert
target_data
==
deserialize_data
(
generatedjson
,
filetype
=
".json"
)
def
test_exceptions
(
datafile
):
# Load test data
wrongtype
=
datafile
(
"wrong.type"
)
loadfile
=
datafile
(
"loadfile.txt"
)
warningfile
=
datafile
(
"generatewarnings.txt"
)
nodt
=
datafile
(
"loaddatawithheaders.txt"
)
hdata
=
load_data
(
loadfile
,
headers
=
True
)
data_table
=
load_data
(
loadfile
)
# improper file types
with
pytest
.
raises
(
UnsupportedTypeError
):
serialize_data
(
loadfile
,
hdata
,
data_table
,
serial_file
=
wrongtype
)
with
pytest
.
raises
(
UnsupportedTypeError
):
deserialize_data
(
wrongtype
)
# various dt_colnames inputs
with
pytest
.
raises
(
ImproperSizeError
):
serialize_data
(
loadfile
,
hdata
,
data_table
,
dt_colnames
=
[
"one"
,
"two"
,
"three is too many"
],
)
# check proper output
normal
=
serialize_data
(
loadfile
,
hdata
,
data_table
,
dt_colnames
=
[
"r"
,
"gr"
]
)
data_name
=
list
(
normal
.
keys
())[
0
]
r_list
=
normal
[
data_name
][
"r"
]
gr_list
=
normal
[
data_name
][
"gr"
]
# three equivalent ways to denote no column names
missing_parameter
=
serialize_data
(
loadfile
,
hdata
,
data_table
,
show_path
=
False
)
empty_parameter
=
serialize_data
(
loadfile
,
hdata
,
data_table
,
show_path
=
False
,
dt_colnames
=
[]
)
none_entry_parameter
=
serialize_data
(
loadfile
,
hdata
,
data_table
,
show_path
=
False
,
dt_colnames
=
[
None
,
None
]
)
# check equivalence
assert
missing_parameter
==
empty_parameter
assert
missing_parameter
==
none_entry_parameter
assert
numpy
.
allclose
(
missing_parameter
[
data_name
][
"data table"
],
data_table
)
# extract a single column
r_extract
=
serialize_data
(
loadfile
,
hdata
,
data_table
,
show_path
=
False
,
dt_colnames
=
[
"r"
]
)
gr_extract
=
serialize_data
(
loadfile
,
hdata
,
data_table
,
show_path
=
False
,
dt_colnames
=
[
None
,
"gr"
]
)
incorrect_r_extract
=
serialize_data
(
loadfile
,
hdata
,
data_table
,
show_path
=
False
,
dt_colnames
=
[
None
,
"r"
]
)
# check proper columns extracted
assert
numpy
.
allclose
(
gr_extract
[
data_name
][
"gr"
],
incorrect_r_extract
[
data_name
][
"r"
]
)
assert
"r"
not
in
gr_extract
[
data_name
]
assert
(
"gr"
not
in
r_extract
[
data_name
]
and
"gr"
not
in
incorrect_r_extract
[
data_name
]
)
# check correct values extracted
assert
numpy
.
allclose
(
r_extract
[
data_name
][
"r"
],
r_list
)
assert
numpy
.
allclose
(
gr_extract
[
data_name
][
"gr"
],
gr_list
)
# no datatable
nodt_hdata
=
load_data
(
nodt
,
headers
=
True
)
nodt_dt
=
load_data
(
nodt
)
no_dt
=
serialize_data
(
nodt
,
nodt_hdata
,
nodt_dt
,
show_path
=
False
)
nodt_data_name
=
list
(
no_dt
.
keys
())[
0
]
assert
numpy
.
allclose
(
no_dt
[
nodt_data_name
][
"data table"
],
nodt_dt
)
# ensure user is warned when columns are overwritten
hdata
=
load_data
(
warningfile
,
headers
=
True
)
data_table
=
load_data
(
warningfile
)
with
pytest
.
warns
(
RuntimeWarning
)
as
record
:
serialize_data
(
warningfile
,
hdata
,
data_table
,
show_path
=
False
,
dt_colnames
=
[
"c1"
,
"c2"
,
"c3"
],
)
assert
len
(
record
)
==
4
for
msg
in
record
:
assert
"overwritten"
in
msg
.
message
.
args
[
0
]
Back
|
FazBrowse Home
|
New Git URL