-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_main_cli.py
More file actions
657 lines (578 loc) · 22.2 KB
/
test_main_cli.py
File metadata and controls
657 lines (578 loc) · 22.2 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import re
import shutil
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
from typer.testing import CliRunner
from buildstock_fetch.cli.main import app
def strip_ansi_codes(text: str) -> str:
"""Remove ANSI escape codes from text."""
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
return ansi_escape.sub("", text)
@pytest.fixture(scope="function")
def cleanup_downloads():
# Setup - clean up any existing files before test
data_dir = Path("data")
test_output_dir = Path("test_output")
if data_dir.exists():
shutil.rmtree(data_dir)
if test_output_dir.exists():
shutil.rmtree(test_output_dir)
yield
# Teardown - clean up downloaded files after test
if data_dir.exists():
shutil.rmtree(data_dir)
if test_output_dir.exists():
shutil.rmtree(test_output_dir)
# Create a test runner
runner = CliRunner()
@pytest.mark.vcr
@pytest.mark.network
@patch("questionary.confirm")
@patch("questionary.press_any_key_to_continue")
@patch("questionary.path")
@patch("questionary.select")
@patch("questionary.checkbox")
@patch("questionary.text")
def test_interactive_mode(
mock_text, mock_checkbox, mock_select, mock_path, mock_press_any_key_to_continue, mock_confirm, cleanup_downloads
):
"""Test interactive mode with mocked questionary."""
# Mock the questionary responses
mock_select.return_value.ask.side_effect = ["resstock", "2021", "tmy3", "1", "Download a sample"]
mock_checkbox.return_value.ask.side_effect = [["0"], ["CA", "MA"], ["metadata"]]
mock_path.return_value.ask.return_value = str(Path.cwd() / "test_output")
mock_confirm.return_value.ask.return_value = True
# Mock the text input for sample size (not used in this test case but needed)
mock_text.return_value.ask.return_value = "5"
mock_press_any_key_to_continue.ask.return_value = None
result = runner.invoke(app, [])
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 0
assert "BuildStock Fetch Interactive CLI" in result.stdout
assert "Welcome to the BuildStock Fetch CLI!" in result.stdout
assert "Please select the release information and file type you would like to fetch:" in result.stdout
# Check that the result contains the expected values but with dynamic path and new print format
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2021" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['CA', 'MA']" in result.stdout
assert "File type: ['metadata']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
# Test abort path: confirmation returns False
mock_select.return_value.ask.side_effect = ["resstock", "2021", "tmy3", "1", "Download a sample"]
mock_checkbox.return_value.ask.side_effect = [["0"], ["CA", "MA"], ["metadata"]]
mock_path.return_value.ask.return_value = str(Path.cwd() / "test_output")
mock_confirm.return_value.ask.return_value = False
result = runner.invoke(app, [])
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 1
# Mock the questionary responses for another valid run
mock_select.return_value.ask.side_effect = ["comstock", "2024", "amy2018", "1", "Download a sample"]
mock_checkbox.return_value.ask.side_effect = [["7"], ["CA", "TX"], ["load_curve_15min", "metadata"]]
mock_path.return_value.ask.return_value = str(Path.cwd() / "test_output")
mock_confirm.return_value.ask.return_value = True
result = runner.invoke(app, [])
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 0
assert "BuildStock Fetch Interactive CLI" in result.stdout
assert "Welcome to the BuildStock Fetch CLI!" in result.stdout
assert "Please select the release information and file type you would like to fetch:" in result.stdout
# Check that the result contains the expected values but with dynamic path and new print format
assert "Downloading data for:" in result.stdout
assert "Product: comstock" in result.stdout
assert "Release year: 2024" in result.stdout
assert "Weather file: amy2018" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['CA', 'TX']" in result.stdout
assert "File type: ['metadata', 'load_curve_15min']" in result.stdout
assert "Upgrade ids: ['7']" in result.stdout
assert "test_output" in result.stdout
# Mock the questionary responses for another valid run
mock_select.return_value.ask.side_effect = ["resstock", "2024", "amy2018", "2", "Download a sample"]
mock_checkbox.return_value.ask.side_effect = [["7"], ["CA", "TX"], ["metadata", "load_curve_monthly"]]
mock_path.return_value.ask.return_value = str(Path.cwd() / "test_output")
mock_confirm.return_value.ask.return_value = True
result = runner.invoke(app, [])
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 0
assert "BuildStock Fetch Interactive CLI" in result.stdout
assert "Welcome to the BuildStock Fetch CLI!" in result.stdout
assert "Please select the release information and file type you would like to fetch:" in result.stdout
# Check that the result contains the expected values but with dynamic path and new print format
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2024" in result.stdout
assert "Weather file: amy2018" in result.stdout
assert "Release version: 2" in result.stdout
assert "States: ['CA', 'TX']" in result.stdout
assert "File type: ['metadata', 'load_curve_monthly']" in result.stdout
assert "Upgrade ids: ['7']" in result.stdout
assert "test_output" in result.stdout
# Test weather file selection
mock_select.return_value.ask.side_effect = ["resstock", "2022", "amy2012", "1", "Download a sample"]
mock_checkbox.return_value.ask.side_effect = [["0"], ["NY"], ["weather"]]
mock_path.return_value.ask.return_value = str(Path.cwd() / "test_output")
mock_confirm.return_value.ask.return_value = True
result = runner.invoke(app, [])
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 0
assert "BuildStock Fetch Interactive CLI" in result.stdout
assert "Welcome to the BuildStock Fetch CLI!" in result.stdout
assert "Please select the release information and file type you would like to fetch:" in result.stdout
# Check that the result contains the expected values but with dynamic path and new print format
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2022" in result.stdout
assert "Weather file: amy2012" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['NY']" in result.stdout
assert "File type: ['weather']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
@pytest.mark.vcr
@pytest.mark.network
@patch("questionary.confirm")
@patch("questionary.press_any_key_to_continue")
@patch("questionary.path")
@patch("questionary.select")
@patch("questionary.checkbox")
@patch("questionary.text")
def test_interactive_mode_sample_download(
mock_text, mock_checkbox, mock_select, mock_path, mock_press_any_key_to_continue, mock_confirm, cleanup_downloads
):
"""Test interactive mode with sample download option."""
# Mock the questionary responses for sample download
mock_select.return_value.ask.side_effect = ["resstock", "2021", "tmy3", "1", "Download a sample"]
mock_checkbox.return_value.ask.side_effect = [["0"], ["CA"], ["metadata"]]
mock_path.return_value.ask.return_value = str(Path.cwd() / "test_output")
mock_press_any_key_to_continue.ask.return_value = None
mock_confirm.return_value.ask.return_value = True
# Mock the text input for sample size (for upgrade 0)
mock_text.return_value.ask.return_value = "3"
result = runner.invoke(app, [])
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 0
assert "BuildStock Fetch Interactive CLI" in result.stdout
assert "Welcome to the BuildStock Fetch CLI!" in result.stdout
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2021" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['CA']" in result.stdout
assert "File type: ['metadata']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
# Check for sample download messages
assert "files for this release" in result.stdout
assert "Selected 3 buildings for State CA, Upgrade 0" in result.stdout
@patch("questionary.press_any_key_to_continue", new=Mock())
@patch("questionary.confirm")
@patch("questionary.path")
@patch("questionary.select")
@patch("questionary.checkbox")
@patch("questionary.text")
def test_interactive_mode_zero_sample(
mock_text, mock_checkbox, mock_select, mock_path, mock_confirm, cleanup_downloads
):
"""Test interactive mode with zero sample download option."""
# Mock the questionary responses for zero sample download
mock_select.return_value.ask.side_effect = ["resstock", "2021", "tmy3", "1", "Download a sample"]
mock_checkbox.return_value.ask.side_effect = [["0"], ["CA"], ["metadata"]]
mock_path.return_value.ask.return_value = str(Path.cwd() / "test_output")
mock_confirm.return_value.ask.return_value = True
# Mock the text input for sample size (0 for upgrade 0)
mock_text.return_value.ask.return_value = "0"
result = runner.invoke(app, [])
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 0
assert "BuildStock Fetch Interactive CLI" in result.stdout
assert "Welcome to the BuildStock Fetch CLI!" in result.stdout
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2021" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['CA']" in result.stdout
assert "File type: ['metadata']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
# Check for zero sample download messages
assert "files for this release" in result.stdout
assert "No files will be downloaded for State CA, Upgrade 0" in result.stdout
assert "No files selected for download" in result.stdout
@pytest.mark.vcr
@pytest.mark.network
def test_cli_direct_arguments(cleanup_downloads):
"""Test CLI with direct command line arguments (non-interactive mode)."""
# Test with direct arguments for metadata download
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2021",
"--weather_file",
"tmy3",
"--release_version",
"1",
"--states",
"CA",
"--file_type",
"metadata",
"--upgrade_id",
"0",
"--output_directory",
"test_output",
],
)
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2021" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['CA']" in result.stdout
assert "File type: ['metadata']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2021",
"--weather_file",
"tmy3",
"--release_version",
"1",
"--states",
"CA",
"--file_type",
"load_curve_15min",
"--upgrade_id",
"0",
"--output_directory",
"test_output",
"--sample",
"3",
],
)
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2021" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['CA']" in result.stdout
assert "File type: ['load_curve_15min']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2024",
"--weather_file",
"tmy3",
"--release_version",
"2",
"--states",
"CA",
"--file_type",
"load_curve_15min",
"--upgrade_id",
"0 1 2",
"--output_directory",
"test_output",
"--sample",
"2",
],
)
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2024" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 2" in result.stdout
assert "States: ['CA']" in result.stdout
assert "File type: ['load_curve_15min']" in result.stdout
assert "Upgrade ids: ['0', '1', '2']" in result.stdout
assert "test_output" in result.stdout
@pytest.mark.vcr
@pytest.mark.network
def test_cli_multiple_file_types(cleanup_downloads):
"""Test CLI with multiple file types."""
result = runner.invoke(
app,
[
"--product",
"comstock",
"--release_year",
"2023",
"--weather_file",
"amy2018",
"--release_version",
"2",
"--states",
"CA TX",
"--file_type",
"load_curve_15min metadata",
"--upgrade_id",
"7",
"--output_directory",
"test_output",
],
)
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert "Downloading data for:" in result.stdout
assert "Product: comstock" in result.stdout
assert "Release year: 2023" in result.stdout
assert "Weather file: amy2018" in result.stdout
assert "Release version: 2" in result.stdout
assert "States: ['CA', 'TX']" in result.stdout
assert "File type: ['metadata', 'load_curve_15min']" in result.stdout
assert "Upgrade ids: ['7']" in result.stdout
@pytest.mark.vcr
@pytest.mark.network
def test_cli_multiple_upgrade_ids(cleanup_downloads):
"""Test CLI with multiple upgrade IDs."""
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2022",
"--weather_file",
"tmy3",
"--release_version",
"1",
"--states",
"NY",
"--file_type",
"hpxml schedule",
"--upgrade_id",
"0 1 2",
"--output_directory",
"test_output",
],
)
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2022" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 1" in result.stdout
assert "States: ['NY']" in result.stdout
assert "File type: ['hpxml', 'schedule']" in result.stdout
assert "Upgrade ids: ['0', '1', '2']" in result.stdout
def test_cli_invalid_arguments(cleanup_downloads):
"""Test CLI with invalid arguments."""
# Test with invalid product
result = runner.invoke(
app,
[
"--product",
"invalid_product",
"--release_year",
"2021",
"--weather_file",
"tmy3",
"--release_version",
"1",
"--states",
"CA",
"--file_type",
"metadata",
"--output_directory",
"test_output",
],
)
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 2 # Invalid product should exit with code 2
# Test with invalid release year
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2000", # Invalid year
"--weather_file",
"tmy3",
"--release_version",
"1",
"--states",
"CA",
"--file_type",
"metadata",
"--output_directory",
"test_output",
],
)
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 2
# Test with invalid state
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2021",
"--weather_file",
"tmy3",
"--release_version",
"1",
"--states",
"ABCDEFG", # Invalid state
"--file_type",
"metadata",
"--output_directory",
"test_output",
],
)
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert result.exit_code == 2
assert "Invalid value for states" in result.stderr
def test_cli_help():
"""Test CLI help functionality."""
# Test help command
result = runner.invoke(app, ["--help"])
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
# Strip ANSI escape codes for easier text checking
clean_output = strip_ansi_codes(result.stdout)
assert result.exit_code == 0
assert "--product" in clean_output
assert "--release_year" in clean_output
assert "--weather_file" in clean_output
assert "--release_version" in clean_output
assert "--states" in clean_output
assert "--file_type" in clean_output
assert "--upgrade_id" in clean_output
assert "--output_directory" in clean_output
@pytest.mark.vcr
@pytest.mark.network
def test_cli_direct_arguments_trip_schedules(cleanup_downloads):
"""Test CLI with direct command line arguments (non-interactive mode)."""
# Test with direct arguments for metadata download
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2024",
"--weather_file",
"tmy3",
"--release_version",
"2",
"--states",
"NY",
"--file_type",
"trip_schedules",
"--upgrade_id",
"0",
"--output_directory",
"test_output",
],
)
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2024" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 2" in result.stdout
assert "States: ['NY']" in result.stdout
assert "File type: ['trip_schedules']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
"""Test CLI with direct command line arguments (non-interactive mode)."""
# Test with direct arguments for metadata download
result = runner.invoke(
app,
[
"--product",
"resstock",
"--release_year",
"2024",
"--weather_file",
"tmy3",
"--release_version",
"2",
"--states",
"WY",
"--file_type",
"trip_schedules",
"--upgrade_id",
"0",
"--output_directory",
"test_output",
],
)
# Debug output
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")
assert "Downloading data for:" in result.stdout
assert "Product: resstock" in result.stdout
assert "Release year: 2024" in result.stdout
assert "Weather file: tmy3" in result.stdout
assert "Release version: 2" in result.stdout
assert "States: ['WY']" in result.stdout
assert "File type: ['trip_schedules']" in result.stdout
assert "Upgrade ids: ['0']" in result.stdout
assert "test_output" in result.stdout
assert "The following state is not available for trip schedules: WY" in result.stdout