-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #108 - Added some behave tests for the CLI.
- Loading branch information
Rob Barry
committed
Aug 27, 2021
1 parent
1554590
commit 794bbbd
Showing
5 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Feature: Test the csvqb Command Line Interface. | ||
|
||
Scenario: The csvqb build command should take an info.json and CSV and output CSV-Ws into the default './out' directory. | ||
Given the existing test-case file "configloaders/data.csv" | ||
And the existing test-case file "configloaders/info.json" | ||
When the csvqb CLI is run with "build --config configloaders/info.json configloaders/data.csv" | ||
Then the csvqb CLI should succeed | ||
And the csvqb CLI should print "Creating output directory" | ||
And the file at "out/ons-international-trade-in-services-by-subnational-areas-of-the-uk.csv" should exist | ||
And the file at "out/ons-international-trade-in-services-by-subnational-areas-of-the-uk.csv-metadata.json" should exist | ||
And the file at "out/validation-errors.json" should not exist | ||
|
||
Scenario: The csvqb build command should not output CSV-Ws when validation errors occur | ||
Given the existing test-case file "configloaders/validation-error/data.csv" | ||
And the existing test-case file "configloaders/validation-error/info.json" | ||
When the csvqb CLI is run with "build --config configloaders/validation-error/info.json configloaders/validation-error/data.csv" | ||
Then the csvqb CLI should fail with status code 1 | ||
And the csvqb CLI should print "Validation Error" | ||
And the csvqb CLI should print "Column 'Measure Type' not found in data provided" | ||
And the file at "out/validation-error-output.csv" should not exist | ||
And the file at "out/validation-error-output.csv-metadata.json" should not exist | ||
And the file at "out/validation-errors.json" should not exist | ||
|
||
Scenario: The csvqb build command should output validation errors to file | ||
Given the existing test-case file "configloaders/validation-error/data.csv" | ||
And the existing test-case file "configloaders/validation-error/info.json" | ||
When the csvqb CLI is run with "build --validation-errors-to-file --config configloaders/validation-error/info.json configloaders/validation-error/data.csv" | ||
Then the csvqb CLI should fail with status code 1 | ||
And the file at "out/validation-errors.json" should exist | ||
And the validation-errors.json file in the "out" directory should contain | ||
""" | ||
Column 'Measure Type' not found in data provided | ||
""" | ||
|
||
Scenario: The csvqb build command should still output CSV-Ws if the user overrides validation errors | ||
Given the existing test-case file "configloaders/validation-error/data.csv" | ||
And the existing test-case file "configloaders/validation-error/info.json" | ||
When the csvqb CLI is run with "build --ignore-validation-errors --config configloaders/validation-error/info.json configloaders/validation-error/data.csv" | ||
Then the csvqb CLI should succeed | ||
And the file at "out/validation-error-output.csv" should exist | ||
And the file at "out/validation-error-output.csv-metadata.json" should exist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from behave import when, then | ||
import subprocess | ||
from typing import Tuple | ||
from devtools.behave.temporarydirectory import get_context_temp_dir_path | ||
|
||
|
||
@when('the csvqb CLI is run with "{arguments}"') | ||
def step_impl(context, arguments: str): | ||
command: str = f"csvqb {arguments.strip()}" | ||
(status_code, response) = run_command_in_temp_dir(context, command) | ||
context.csvqb_cli_result = (status_code, response) | ||
|
||
|
||
@then("the csvqb CLI should succeed") | ||
def step_impl(context): | ||
(status_code, response) = context.csvqb_cli_result | ||
assert status_code == 0, status_code | ||
assert "Build Complete" in response, response | ||
|
||
|
||
@then("the csvqb CLI should fail with status code {status_code}") | ||
def step_impl(context, status_code: str): | ||
(status_code, response) = context.csvqb_cli_result | ||
assert status_code == int(status_code), status_code | ||
|
||
|
||
@then('the csvqb CLI should print "{printed_text}"') | ||
def step_impl(context, printed_text: str): | ||
(status_code, response) = context.csvqb_cli_result | ||
assert printed_text in response, response | ||
|
||
|
||
@then('the validation-errors.json file in the "{out_dir}" directory should contain') | ||
def step_impl(context, out_dir: str): | ||
tmp_dir_path = get_context_temp_dir_path(context) | ||
expected_text_contents: str = context.text.strip() | ||
validation_errors_file = tmp_dir_path / out_dir / "validation-errors.json" | ||
assert validation_errors_file.exists() | ||
|
||
with open(validation_errors_file, "r") as f: | ||
file_contents = f.read() | ||
|
||
assert expected_text_contents in file_contents, file_contents | ||
|
||
|
||
def run_command_in_temp_dir(context, command: str) -> Tuple[int, str]: | ||
tmp_dir_path = get_context_temp_dir_path(context) | ||
process = subprocess.Popen( | ||
command, | ||
shell=True, | ||
cwd=tmp_dir_path, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
status_code = process.wait() | ||
response = process.stdout.read().decode("utf-8") + process.stderr.read().decode( | ||
"utf-8" | ||
) | ||
return status_code, response |
2 changes: 2 additions & 0 deletions
2
csvqb/csvqb/tests/test-cases/configloaders/validation-error/data.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Period,Location,Flow,Industry Grouping,Marker,Value,Undefined Column | ||
2020-01,West Midlands,Imports,Barbed Wire,,20,Undefined Column Value |
42 changes: 42 additions & 0 deletions
42
csvqb/csvqb/tests/test-cases/configloaders/validation-error/info.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"id": "validation-error-output", | ||
"title": "Validation Error Output", | ||
"publisher": "HM Revenue & Customs", | ||
"description": "All bulletins provide details on percentage of one litre or less & more than one litre bottles. This information is provided on a yearly basis.", | ||
"landingPage": "https://www.gov.uk/government/statistics/bottles-bulletin", | ||
"datasetNotes": [ | ||
"\"UK bottles-bulletin Tables\" Excel file, latest version on page" | ||
], | ||
"published": "2019-02-28", | ||
"families": [ | ||
"Trade" | ||
], | ||
"extract": { | ||
"source": "XLS", | ||
"stage": "Done" | ||
}, | ||
"transform": { | ||
"airtable": "recys4OhEtE0gE14P", | ||
"columns": { | ||
"Period": { | ||
"parent": "http://purl.org/linked-data/sdmx/2009/dimension#refPeriod", | ||
"value": "http://reference.data.gov.uk/id/{+period}" | ||
}, | ||
"Measure Type": { | ||
"dimension": "http://purl.org/linked-data/cube#measureType", | ||
"value": "http://gss-data.org.uk/def/x/{+measure_type}", | ||
"types": ["one-litre-and-less", "more-than-one-litre", "number-of-bottles"] | ||
}, | ||
"Unit": { | ||
"attribute": "http://purl.org/linked-data/sdmx/2009/attribute#unitMeasure", | ||
"value": "http://gss-data.org.uk/def/concept/measurement-units/{+unit}" | ||
}, | ||
"Value": { | ||
"datatype": "integer" | ||
} | ||
}, | ||
"main_issue": 67 | ||
}, | ||
"sizingNotes": "", | ||
"notes": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters