To test the feature of your data labeling application using Cucumber (Gherkin), you need to write a feature file that describes the behavior of the application in a human-readable format, while also ensuring that the underlying functionality (e.g., executing SQL queries and generating tables for labeling) is tested. Below, I'll guide you through the process of writing a feature file, structuring the scenarios, and providing some implementation considerations for the step definitions.
Step 1: Understand the Feature
The feature you are testing involves:
- Allowing users to input a SQL query (raw or simplified) to filter data from a pipeline output table (
pipeline_output.cc_pipeline_results_2024_h1_0721).
- Creating a new table based on the query results.
- Enabling labelers to use the new table to suggest transaction resolutions for merchants.
Step 2: Write the Feature File
Below is an example of a Gherkin feature file that tests this functionality. The feature file is written in a way that is independent of the specific SQL query, making it reusable and maintainable. You can parameterize the SQL query to test different variations.
Feature: Filter Transaction Data for Labeling Using SQL Queries
As a data labeler
I want to filter transaction data using SQL queries
So that I can create a table of results to suggest transaction resolutions for merchants
Background:
Given the pipeline output table "pipeline_output.cc_pipeline_results_2024_h1_0721" exists
And the table contains transaction data
Scenario: Create a new table using a raw SQL query
Given I am on the data filtering page
When I enter the following raw SQL query:
"""
select "descriptor", descriptor_to_sanitize_cleaned, channel, card_acceptor_id, amount, taxonomy_root, city, state_location, mcc, count(*) as trans_count
from pipeline_output.cc_pipeline_results_2024_h1_0721 cpr
where process = 'Sanitization'
group by "descriptor", descriptor_to_sanitize_cleaned, channel, card_acceptor_id, amount, taxonomy_root, city, state_location, mcc
order by trans_count desc
limit 50
"""
And I submit the query
Then a new table should be created with the query results
And the table should contain the following columns:
| descriptor |
| descriptor_to_sanitize_cleaned |
| channel |
| card_acceptor_id |
| amount |
| taxonomy_root |
| city |
| state_location |
| mcc |
| trans_count |
And the table should have exactly 50 rows
And the rows should be sorted by "trans_count" in descending order
Scenario: Suggest transaction resolutions using the filtered table
Given a filtered table has been created with the following raw SQL query:
"""
select "descriptor", descriptor_to_sanitize_cleaned, channel, card_acceptor_id, amount, taxonomy_root, city, state_location, mcc, count(*) as trans_count
from pipeline_output.cc_pipeline_results_2024_h1_0721 cpr
where process = 'Sanitization'
group by "descriptor", descriptor_to_sanitize_cleaned, channel, card_acceptor_id, amount, taxonomy_root, city, state_location, mcc
order by trans_count desc
limit 50
"""
When I navigate to the labeling interface for the filtered table
Then I should see the filtered table with transaction data
And I should be able to suggest a transaction resolution for each merchant
Scenario: Handle invalid SQL query
Given I am on the data filtering page
When I enter the following raw SQL query:
"""
select invalid_column from pipeline_output.cc_pipeline_results_2024_h1_0721
"""
And I submit the query
Then I should see an error message indicating the query is invalid
Step 3: Explanation of the Feature File
- Feature: Describes the high-level functionality being tested.
- Background: Sets up common preconditions for all scenarios, such as ensuring the pipeline output table exists and contains data.
- Scenario 1 (Create a new table using a raw SQL query): Tests the core functionality of entering a SQL query, submitting it, and verifying the results. It checks:
- The table is created.
- The table has the expected columns.
- The table has the correct number of rows (50, as per the
limit 50 in the query).
- The rows are sorted correctly (by
trans_count in descending order).
- Scenario 2 (Suggest transaction resolutions): Tests the labeling interface, ensuring labelers can use the filtered table to suggest resolutions.
- Scenario 3 (Handle invalid SQL query): Tests error handling for invalid queries, ensuring the application provides meaningful feedback.
Step 4: Implementation Considerations for Step Definitions
To implement the step definitions (the code that executes the Gherkin steps), you'll need to use a testing framework compatible with Cucumber, such as Cucumber-Java, Cucumber-Ruby, or Cucumber-JS, depending on your tech stack. Below are some general considerations for implementing the step definitions:
1. Database Setup
- Ensure you have a test database with a copy of the
pipeline_output.cc_pipeline_results_2024_h1_0721 table, populated with test data.
- Use a database testing library (e.g.,
ActiveRecord in Ruby, JDBC in Java, or Sequelize in JavaScript) to interact with the database.
2. Step Definitions for Scenario 1
Here’s an example of how the step definitions might look in Ruby (using Cucumber-Ruby):
Given(/^the pipeline output table "([^"]*)" exists$/) do |table_name|
# Ensure the table exists in the test database
# You might use a database migration or setup script to create the table
end
Given(/^the table contains transaction data$/) do
# Insert test data into the table
# Example: Insert 100 rows of transaction data with varying descriptors, amounts, etc.
end
Given(/^I am on the data filtering page$/) do
# Navigate to the data filtering page in your application
# Example: Use a browser automation tool like Selenium or Capybara
end
When(/^I enter the following raw SQL query:$/) do |query|
# Enter the SQL query into the text form on the data filtering page
# Example: Use Capybara to fill in a textarea
end
When(/^I submit the query$/) do
# Click the submit button on the data filtering page
# Example: Use Capybara to click a button
end
Then(/^a new table should be created with the query results$/) do
# Verify that a new table exists in the database
# Example: Query the database to check for the existence of the table
end
Then(/^the table should contain the following columns:$/) do |table|
expected_columns = table.raw.flatten
# Query the database to get the actual columns of the new table
# Compare actual columns with expected_columns
end
Then(/^the table should have exactly (\d+) rows$/) do |expected_row_count|
# Query the database to count the rows in the new table
# Compare actual row count with expected_row_count
end
Then(/^the rows should be sorted by "([^"]*)" in descending order$/) do |column_name|
# Query the database to fetch the rows of the new table
# Verify that the values in column_name are in descending order
end
3. Step Definitions for Scenario 2
For the labeling interface, you’ll need to simulate user interactions with the UI and verify that the filtered table is displayed correctly. This might involve:
- Using a browser automation tool (e.g., Selenium, Capybara) to navigate to the labeling interface.
- Verifying that the table is displayed with the expected data.
- Simulating user input to suggest transaction resolutions and verifying that the suggestions are saved correctly.
4. Step Definitions for Scenario 3
For error handling, you’ll need to:
- Simulate entering an invalid SQL query.
- Verify that the application displays an error message (e.g., by checking the UI or API response).
5. Test Data
- Use a test dataset that mimics the structure and content of the real
pipeline_output.cc_pipeline_results_2024_h1_0721 table.
- Ensure the test data includes enough variety to test edge cases (e.g., duplicate descriptors, missing values, etc.).
Step 5: Additional Testing Considerations
- Parameterized Queries: If your application supports simplified queries (e.g., a UI-based query builder), add scenarios to test those as well. For example:
Scenario: Create a new table using a simplified query
Given I am on the data filtering page
When I use the simplified query builder to filter transactions where process is "Sanitization"
And I group the results by "descriptor", "channel", and "mcc"
And I sort the results by transaction count in descending order
And I limit the results to 50 rows
And I submit the query
Then a new table should be created with the query results
- Performance Testing: If performance is a concern, consider adding scenarios to test the time it takes to execute the query and create the table, especially with large datasets.
- Security Testing: Ensure that the application sanitizes SQL queries to prevent SQL injection attacks. Add scenarios to test this, such as:
Scenario: Prevent SQL injection attacks
Given I am on the data filtering page
When I enter the following raw SQL query:
"""
select * from pipeline_output.cc_pipeline_results_2024_h1_0721; DROP TABLE users; --
"""
And I submit the query
Then I should see an error message indicating the query is invalid
And the "users" table should still exist
Step 6: Running the Tests
- Use the Cucumber command-line tool to run your feature file and verify that the tests pass.
- Example command:
cucumber features/filter_transaction_data.feature
Step 7: Continuous Integration
- Integrate your Cucumber tests into your CI/CD pipeline (e.g., Jenkins, GitHub Actions) to ensure the feature is tested automatically on every code change.
By following this approach, you can thoroughly test the feature of filtering transaction data using SQL queries in your data labeling application, ensuring it works as expected for labelers and handles edge cases appropriately.