Skip to content

Commit 15da3ca

Browse files
committed
#128 WIP more tests
1 parent 36858b4 commit 15da3ca

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

tests/test_filewriter.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_testfile_content(self):
7171
f"expected {v} '{k}' document(s)")
7272

7373

74-
class Test_newfile(unittest.TestCase):
74+
class Test_SpecWriterCallback(unittest.TestCase):
7575

7676
def setUp(self):
7777
self.tempdir = tempfile.mkdtemp()
@@ -112,6 +112,8 @@ def test_writer_filename(self):
112112
self.assertTrue(len(self.db) > 0, "test data ready")
113113

114114
testfile = os.path.join(self.tempdir, "tune_mr.dat")
115+
if os.path.exists(testfile):
116+
os.remove(testfile)
115117
specwriter = SpecWriterCallback(filename=testfile)
116118

117119
self.assertIsInstance(
@@ -130,7 +132,10 @@ def test_writer_filename(self):
130132

131133
def test_newfile_exists(self):
132134
testfile = os.path.join(self.tempdir, "tune_mr.dat")
135+
if os.path.exists(testfile):
136+
os.remove(testfile)
133137
specwriter = SpecWriterCallback(filename=testfile)
138+
134139
write_stream(specwriter, self.db["tune_mr"])
135140
self.assertTrue(os.path.exists(testfile), "data file created")
136141

@@ -142,12 +147,74 @@ def test_newfile_exists(self):
142147
raised = True
143148
finally:
144149
self.assertTrue(raised, "file exists")
150+
151+
def test__rebuild_scan_command(self):
152+
from apstools.filewriters import _rebuild_scan_command
153+
154+
self.assertTrue(len(self.db) > 0, "test data ready")
155+
156+
start_docs = []
157+
for header in self.db["tune_mr"]:
158+
tag, doc = header
159+
if tag == "start":
160+
start_docs.append(doc)
161+
self.assertEqual(len(start_docs), 1, "unique start doc found")
162+
163+
doc = start_docs[0]
164+
expected = "108 tune_mr()"
165+
result = _rebuild_scan_command(doc)
166+
self.assertEqual(result, expected, "rebuilt #S line")
167+
168+
def test_spec_comment(self):
169+
from apstools.filewriters import spec_comment
170+
171+
# spec_comment(comment, doc=None, writer=None)
172+
testfile = os.path.join(self.tempdir, "spec_comment.dat")
173+
if os.path.exists(testfile):
174+
os.remove(testfile)
175+
specwriter = SpecWriterCallback(filename=testfile)
176+
177+
# insert comments with every document
178+
spec_comment(
179+
"TESTING: Should appear within start doc",
180+
doc=None,
181+
writer=specwriter)
182+
183+
for idx, document in enumerate(self.db["tune_mr"]):
184+
tag, doc = document
185+
msg = f"TESTING: document {idx+1}: '{tag}' %s specwriter.receiver"
186+
spec_comment(
187+
msg % "before",
188+
doc=tag,
189+
writer=specwriter)
190+
specwriter.receiver(tag, doc)
191+
if tag == "stop":
192+
# since stop doc was received, this appears in the next scan
193+
spec_comment(
194+
str(msg % "before") + " (appears at END of next scan)",
195+
doc=tag,
196+
writer=specwriter)
197+
else:
198+
spec_comment(
199+
msg % "after",
200+
doc=tag,
201+
writer=specwriter)
202+
203+
# since stop doc was received, this appears in the next scan
204+
spec_comment(
205+
"TESTING: Appears at END of next scan",
206+
doc="stop",
207+
writer=specwriter)
208+
write_stream(specwriter, self.db["tune_ar"])
209+
210+
pass # TODO: test the file for the comments
211+
145212

146213

147214
def suite(*args, **kw):
148215
test_list = [
149216
Test_Data_is_Readable,
150-
Test_newfile,
217+
Test_SpecWriterCallback,
151218
]
152219
test_suite = unittest.TestSuite()
153220
for test_case in test_list:

0 commit comments

Comments
 (0)