Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3990 Add csv export tests for read_events feature toggle enabled #4260

Merged

Conversation

danielabar
Copy link
Contributor

Resolves #3990

Description

Based on issue discussion, it was determined that when the read_events feature toggle is enabled, the storage location csv export is correct. In that a storage location with an item that's not present in any of the other storage locations will have the inventory for that item located in the correct column for that item. And for cells with none of the items, a 0 is displayed.

So it's only when read_events feature toggle is disabled, then the code uses the generate_csv from the Exportable concern, with incorrect input which leads to the bug.

It was decided rather than spending time to fix the path the code takes when read_events is off, to just add tests to cover the csv exporting when read_events is on. This PR adds tests for both header, and data rows generation.

For reference these comments in the issue have further analysis details:

  1. [BUG] Storage Location Export doesn't format product quantity columns for each storage location properly. #3990 (comment)
  2. [BUG] Storage Location Export doesn't format product quantity columns for each storage location properly. #3990 (comment)

Type of change

  • Bug fix - sort of, adds tests.

How Has This Been Tested?

  • With the Rails server running bin/start, navigate to http://localhost:3000/flipper and sign in with the admin account, then add toggle read_events and enable for all.
  • Then as an org admin, navigate to http://localhost:3000/diaper_bank/storage_locations, and add a new storage location
  • Go to Inventory -> Items & Inventory and add a new item
  • Add the new item to the new storage location by recording a new donation. At this point, you should have one storage location with an item that is not present in any of the other locations.
  • Click Export Storage Locations, and inspect the downloaded csv
  • There should be a column for the new item you added, and the inventory quantity should be populated in that column only for the new storage location. All the other rows should have 0 in this column.

Run the tests that verify this behaviour: bin/rspec spec/requests/storage_locations_requests_spec.rb:69

Note when I run the full test suite, get 3 failing system tests, but this branch doesn't have any code changes:

Failed examples:

rspec ./spec/system/purchase_system_spec.rb:227 # Purchases while signed in as a normal user When creating a new purchase via barcode entry a user can add items via scanning them in by barcode
rspec ./spec/system/audit_system_spec.rb:42 # Audit management while signed in as an organization admin when starting a new audit does not display quantities in line-item drop down selector
rspec ./spec/system/distributions_by_county_system_spec.rb:18 # Distributions by County handles time ranges properly works for all time

@cielf cielf requested a review from dorner April 9, 2024 15:15
@cielf
Copy link
Collaborator

cielf commented Apr 9, 2024

@danielabar (nods) We have recently started a concentrated effort on improving our flakey tests. Calling the above list out to @elasticspoon, since he's been doing a lot of work in that area.


it "generates header with Storage Location fields followed by alphabetized item names" do
get storage_locations_path(default_params.merge(format: response_format))
expect(response.body.split("\n")[0]).to eq([StorageLocation.csv_export_headers, item1.name, item2.name, item3.name, item4.name].join(','))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to reference item1.nameetc. when we actually know what the name is (A, B etc.).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

5
].join(","))
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually for CSVs, I check the results something like this:

csv_data = <<~CSV
header1,header2,header3
r1cell1,r1cell2,r1cell3
#{dynamic_value1},r2cell2,r2cell3
CSV

expect(results).to eq(csv_data)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can update the tests to use this technique (actually with this, can probably get all of it into just one test).

Only tricky thing could be one of the csv rows is for a storage location that comes from either seeds or fixtures, not data explicitly generated by the test. For example, the Smithsonian Conservation Center is not from the test:

Name,Address,Square Footage,Warehouse Type,Total Inventory,A,B,C,D
Smithsonian Conservation Center,"1500 Remount Road, Front Royal, VA 22630",100,Residential space used,0,0,0,0,0
Storage Location with Duplicate Items,"1500 Remount Road, Front Royal, VA 22630",100,Residential space used,1,0,0,1,0
Storage Location with Items,"1500 Remount Road, Front Royal, VA 22630",100,Residential space used,3,1,1,1,0
Storage Location with Unique Items,"1500 Remount Road, Front Royal, VA 22630",100,Residential space used,5,0,0,0,5

I can add an assertion for it, but if the seeds/fixtures should change in the future, this test would break.

Thoughts on how to deal with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another observation: That storage location that's not generated explicitly by the test uses the StorageLocation::WAREHOUSE_TYPES.sample for warehouse_type, resulting in a different csv output each time which would make this test flaky.

I actually ran into that earlier with my own factory created storage locations, which I fixed by always selecting a specific warehouse type when creating, eg:

let(:storage_location_with_items) { create(:storage_location, name: "Storage Location with Items", warehouse_type: StorageLocation::WAREHOUSE_TYPES.first) }

Potential Solutions:

  1. Figure out where that additional storage location is coming from and try to remove it from fixtures or seeds (but this might break other tests).
  2. Process the response.body to remove that row before using it in test expectation. But it might not always be guaranteed to be the first data row, so this could result in flakiness.
  3. Use my current technique of finding each specific csv row to verify by storage location name, and assert on those independently. (but this either results in multiple tests, or I could collapse it into a single test but then it would have multiple assertions - is that ok?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just realized that extra storage location actually is created by the test, just not the new test(s) I added. It's created in thebefore block for describe "GET #index". In this case, I think it could be modified to always have the same warehouse type, and then the csv test can have consistent data to verify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the update to use a heredoc to verify generated csv contents in a single test.

1. Use actual values of item names when verifying csv header.
2. Use heredoc to verify contents of generated csv in a single test.
@danielabar danielabar requested a review from dorner April 11, 2024 13:26
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - thanks!

We are working towards not using the sample data in tests precisely because of confusion like this. Means the tests will overall run more slowly, but will be a lot more straightforward to understand.

@dorner dorner merged commit 615b313 into rubyforgood:main Apr 11, 2024
19 checks passed
@danielabar danielabar deleted the 3990-storage-location-export-format branch April 11, 2024 19:49
Copy link
Contributor

@danielabar: Your PR #3990 Add csv export tests for read_events feature toggle enabled is part of today's Human Essentials production release: 2024.04.14.
Thank you very much for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Storage Location Export doesn't format product quantity columns for each storage location properly.
3 participants