/
test_filedata.py
145 lines (118 loc) · 5.5 KB
/
test_filedata.py
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
import json
import os
import unittest
from shutil import copyfile
import pandas as pd
from pyiron_base import FileHDFio, ProjectHDFio
from pyiron_base.storage.filedata import FileData, load_file
from pyiron_base.project.generic import Project
from pyiron_base._tests import PyironTestCase
class TestLoadFile(PyironTestCase):
@classmethod
def setUpClass(cls):
cls.current_dir = os.path.dirname(os.path.abspath(__file__)).replace("\\", "/")
hdf5 = FileHDFio(file_name=cls.current_dir + "/test_data.h5")
with hdf5.open("content") as hdf:
hdf['key'] = "value"
copyfile(cls.current_dir + "/test_data.h5", cls.current_dir + "/test_data")
with open(cls.current_dir + '/test_data.txt', 'w') as f:
f.write("some text")
with open(cls.current_dir + '/test_data2', 'w') as f:
f.write("some text")
with open(cls.current_dir + '/test_data.csv', 'w') as f:
f.write("id,status,chemicalformula")
f.write("13,aborted,Al108")
with open(cls.current_dir + '/test_data.json', 'w') as f:
json.dump({'x': [0, 1]}, f)
@classmethod
def tearDownClass(cls):
cls.current_dir = os.path.dirname(os.path.abspath(__file__)).replace("\\", "/")
for file in ['/test_data', "/test_data.h5", "/test_data.txt",
"/test_data.csv", "/test_data.json", "/test_data2"]:
os.remove(cls.current_dir + file)
def test_load_file_txt(self):
txt = load_file(self.current_dir+'/test_data.txt')
with open(self.current_dir+'/test_data.txt') as f:
content = f.readlines()
self.assertEqual(txt, content)
def test_load_file_stream_txt(self):
with open(self.current_dir + '/test_data.txt') as f:
content = f.readlines()
with open(self.current_dir + '/test_data.txt') as f:
txt = load_file(f)
self.assertEqual(txt, content)
def test_load_file_csv(self):
csv = load_file(self.current_dir+'/test_data.csv')
content = pd.read_csv(self.current_dir+'/test_data.csv')
self.assertTrue(content.equals(csv))
def test_load_file_hdf(self):
hdf = load_file(self.current_dir+'/test_data.h5')
self.assertIsInstance(hdf, FileHDFio)
self.assertEqual(hdf['content/key'], 'value')
def test_load_file_ProjectHDF(self):
pr = Project(self.current_dir + '/test_pr')
pr_hdf = load_file(self.current_dir + '/test_data.h5', project=pr)
self.assertIsInstance(pr_hdf, ProjectHDFio)
self.assertEqual(pr_hdf['content/key'], 'value')
pr.remove(enable=True)
def test_load_file_default(self):
"""Test default load for text file and h5 file without extension."""
filename = self.current_dir + '/test_data'
self.assertRaises(IOError, load_file, filename)
filename = self.current_dir + '/test_data2'
default = load_file(filename)
self.assertEqual(default, ["some text"])
def test_filetype_option(self):
filename = self.current_dir + '/test_data'
hdf = load_file(filename, filetype="hdf")
self.assertIsInstance(hdf, FileHDFio)
self.assertEqual(hdf['content/key'], 'value')
def test_load_file_json(self):
json_dict = load_file(self.current_dir+'/test_data.json')
self.assertEqual(json_dict, {'x': [0, 1]})
class TestFileData(PyironTestCase):
@classmethod
def setUpClass(cls):
cls.current_dir = os.path.dirname(os.path.abspath(__file__)).replace("\\", "/")
cls.filepath = os.path.join(cls.current_dir, 'test_data.txt').replace("\\", "/")
with open(cls.filepath, 'w') as f:
f.write("some text")
cls.data = FileData(file=cls.filepath)
@classmethod
def tearDownClass(cls):
cls.current_dir = os.path.dirname(os.path.abspath(__file__)).replace("\\", "/")
filepath = os.path.join(cls.current_dir, 'test_data.txt').replace("\\", "/")
os.remove(filepath)
def test___init__(self):
"""Test init of data class."""
# Test if init from setUpClass is as expected
self.assertFalse(self.data._hasdata)
self.assertEqual(self.data.filename, "test_data.txt")
self.assertEqual(self.data.filetype, "txt")
self.assertRaises(TypeError, FileData)
data = FileData(file=self.filepath, metadata={"some": "dict"})
self.assertFalse(data._hasdata)
self.assertEqual(data.filename, "test_data.txt")
self.assertEqual(data.filetype, "txt")
self.assertEqual(data.metadata["some"], "dict")
with open(self.filepath) as f:
some_data = f.readlines()
self.assertRaises(TypeError, FileData, data=some_data)
data = FileData(data=some_data, file="test_data.dat")
self.assertTrue(data._hasdata)
self.assertEqual(data.filetype, "dat")
data = FileData(data=some_data, file="test_data.dat", filetype="txt")
self.assertEqual(data.filetype, "txt")
data = FileData(data=some_data, file='foo')
self.assertTrue(data.filetype is None)
def test_data(self):
"""Test data property of FileData."""
with open(self.filepath) as f:
some_data = f.readlines()
self.assertEqual(self.data.data, some_data)
data = FileData(data=b'some string', file='foo.txt')
self.assertEqual(data.data, b'some string')
data = FileData(self.filepath, filetype='.txt')
self.assertEqual(data.data, some_data)
if __name__ == '__main__':
unittest.main()