Ruby on Rails - Testing the contents of a CSV file - ruby-on-rails

So I have a link on my rails web page that generates a CSV file. How would I go about writing a functional test to test that the CSV file is created properly and check the contents of it?

There are probably many ways. Here's a version that won't make you jump through browser hoops
Extract the creation of a CSV to another non-controller class
In your controller test, assert your CreatesCSV receives the right parameters
Test your CreatesCSV class by writing its output to a file, then reading it in and verifying the hash is correct.

Related

Rails PDF generation in model

I am writing and invoicing application in Ruby On Rails, which is supposed to generate a PDF from a model so called out_invoice to an in_invoice under certain conditions.
The PDF for the out_invoice is already separately generated on demand using wicked_pdf gem basically having a HTML template and sending back the PDF inside the action and downloading it.
I have successfully programmed the duplication inside the out_invoice model which works fine for all values of the out_invoice. Including an attached PDF in a separate attribute.
But instead of generating the PDF and attaching it to the in_invoice. It fails. Some Controller/Model MVC thing I assume!
The PDF for the out_invoice is currently generated separately in a controller action so I tried copying the code: I tried rendering the template inside the model which seems to work fine, but "sending" is not defined for Rails models. If I just return the WickedPDF without sending it I get an error: ActiveSupport::MessageVerifier::InvalidSignature
Must be something super simple I am forgetting + I have no clue what is the reason for the error. I am not a front-end guy so don't want to use something else as the already existing HTML template for rendering. But worst case I write the template newly in another engine. Looked at Prawn but this means rewriting everything.
Can somebody help? Thanks!

How to properly include code in .rb files into Rails architecture?

I am currently coding an application in Ruby that does some simple external API calls to Soundcloud's API.
I have developed a bunch of code inside a single .rb file and want to put this into the rails architecture. This Ruby file has the following classes:
class SoundcloudUser
class SoundcloudQuery
class SoundcloudFollowers
Currently I understand that I can put these classes into seperate .rb files, and then just put them into the /models/ folder which then gives me the ability to call these classes from elsewhere in my rails application (using require/include).
My question is simply, is this the correct way to go about this? I am familiar with rails, but I am new to transferring a Ruby developed project into the rails format. I tried searching best practices for this in the Ruby style guide but I didn't really find anything.
On a side note - I wanted to also create another class that acts as a ?service? wherein in checks my local database if an entry already exists in the database, and if not, then it will query new data. My side-question here would be similar - where would this .rb file for this 'service' live?
I hope I explained my question clearly enough, if not, I am happy to add some clarifications. Thank you for your time!
If in Rails, you can put them in either lib/ or somewhere in the main app directory. For example, you can create app/services and put them inside there, and when you restart the Rails server you should be able to call SoundcloudUser (provided you name them app/services/soundcloud_user.rb.
I always look at the Gitlab source code for this. It's a gigantic Rails app but look at this file: https://github.com/gitlabhq/gitlabhq/blob/master/app/services/gravatar_service.rb. Because it's inside an app/services (any name actually), GravatarService can be called from anywhere in Rails. If you want to have some namespacing, you have to put it in app/services/soundcloud/user.rb or lib/soundcloud/user.rb and name the class Soundcloud::User.
For the class that acts as a service, it seems like it orchestrates the logic of "check if (song?) exists, else scrape. Some people put it in a model class, I'd probably put it in a service class a la the Gitlab source code. Hope I helped.

Location for XSD file used for validation during RSpec test

I am building XML files inside my application and I would like to validate the generated XML's format against an XSD file. I am not sure where shall I put this schema.xsd file so that it can be easily retrieved inside an RSpec example.
I was thinking about putting it inside spec/support, but I am not sure about it.
I would suggest spec/fixtures, since it is part of your test suite, it's somewhat static data and it can be accessed easily via fixture_path (you can set any other *_path helper in your spec_helper file if you wish to).
I've ended up putting my .xsd file in spec/support/schemas/; it felt more natural for me, since it is a support file.

Cucumber to test Email Parsing?

this is my first stackoverflow question.
I am building an app that gets posted an email from SendGrid, which I then want to parse in a delayed job.
My concern is how do I QA this. I have been reading about Cucumber and that sounds like a good solution but I can't figure out the end to end test flow.
Here's where I am so far.
I want to have a large list of TXT files that include various types of email body's
I then iterate through each txt file, and make sure that when passed to a method in my lib directory /mailingjob.rb, that what is returned matches something defined in cucumber.
So what I have so far is something like:
expected = File.open('???/mail1.txt', 'r') do |f|
f.read
end
That's where I'm starting. So if you can, please help me understand the following:
Where should all these TXT files be located in the rails project directory?
One e2e example showing how to grab a local text file, pass it to a method in the /lib directory, and then make sure what is returned equals what is set for that file path.
Thank you thank you for any help you can provide
I would probably store these files somewhere in your test or spec directory (depending on what testing framework you use; you mentioned Cucumber, which I'm not super familiar with, but I think it uses a directory called features). Really, you could put them anywhere you want, but some subdirectory of your test directory makes sense.
If you're not familiar with testing with Cucumber, I recommend Railscast episode 155 and episode 159 to get you started. To answer your direct question, you would read the email from the text file as such
email_text = File.read("#{Rails.root}/test/path/to/email.txt")
Rails.root always refers to the root directory for your project, and makes it easy to build paths to other files or folders.

A one-time generated CSV file in RoR

Is it possible to create a CSV file right out of the command line in IRB or elsewhere with a one-time use on it. Say, I just need a CSV file with all my user's first name on it.
Can I create that without setting up any architecture?
Yes, use Ruby's csv library (it's part of the standard lib):
http://ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV/Writer.html
What database are you using? It seems doubtful that you'd need to use Rails at all. For example:
How to output MySQL query results in CSV format?

Resources