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

Unclear on how to use Format.toCsv(FakeCollection<T> collection) #278

Closed
ghost opened this issue Aug 6, 2022 · 8 comments
Closed

Unclear on how to use Format.toCsv(FakeCollection<T> collection) #278

ghost opened this issue Aug 6, 2022 · 8 comments

Comments

@ghost
Copy link

ghost commented Aug 6, 2022

This is neither a bug nor a feature request, that's why I'm writing in free form, I hope that's okay.

I'm trying to do the following:

  1. Generate valid CSV data (using datafaker)
  2. Give CSV string to a parser (the one under test)
  3. Compare parsed entities with the generated CSV data

How could one go about doing this? I saw that there is a Format.toCSV() overload that accepts a collection but it's unclear to me how you'd use that method.

@bodiam
Copy link
Contributor

bodiam commented Aug 6, 2022

No worries at all. I'm happy to help you more, but as a starting point, did you have a look at this documentation?

https://www.datafaker.net/documentation/file-formats/#csv

If so, is there anything specially unclear?

@ghost
Copy link
Author

ghost commented Aug 7, 2022

The documentation is a bit unclear on how to use a collection builder for generating CSV files. An example like the test shown here would have been nice:

void differentNumberOfHeadersAndColumns() {
.

I've basically done the following right now:

  1. Create a custom class that has methods for each faked value (e.g. one for generating uuids, one for generating dates)
  2. Create a custom faker that uses that custom class and initializes the Faker-superclass with the same random seed each time
  3. Use two different custom faker instances: one builds a collection for comparing the parsed entities and the other one for generating the CSV file

I'd be willing to submit a PR if you'd like. Three PRs, actually. One explaining how to use a collection builder for generating CSV files, another one for an end-to-end example on how to compare the custom generated entities with domain objects. The third PR would have a certain nullRate to inject errors. What do you think?

I've also noticed that generating UUIDs ignores the Faker's random seed, thus generating a "truly" random UUID each time. What do you think of changing that? A simple hack could be to simply call UUID.nameUUIDFromBytes() and passing a randomly generated number each time, using the RandomService / user provided random seed.

Edit:

  • I just saw that there CSV + Collections example in the README. Adding it to the main website would help out greatly
  • Adding a internet().uuidv3() would perhaps works? Leaving the existing uuid() one unchanged.

@bodiam
Copy link
Contributor

bodiam commented Aug 7, 2022

I really like all your ideas, and I'd accept any PR if you'd be willing to submit one. Thanks for the great feedback, that's really useful!

@ghost
Copy link
Author

ghost commented Aug 7, 2022

No problem, will do. Would you like me to create new issues for all the things I've just mentioned or shall I simply reference this issue in each PR?

@snuyanzin
Copy link
Collaborator

just fyi: there is also a separate doc about collections https://www.datafaker.net/documentation/collections/
may be the issue is missing link between collection's and csv's doc pages?

@bodiam
Copy link
Contributor

bodiam commented Aug 7, 2022

No problem, will do. Would you like me to create new issues for all the things I've just mentioned or shall I simply reference this issue in each PR?

Thanks again! And I have no preference, whatever works best for you.

@ghost
Copy link
Author

ghost commented Aug 9, 2022

just fyi: there is also a separate doc about collections https://www.datafaker.net/documentation/collections/ may be the issue is missing link between collection's and csv's doc pages?

Basically, yes.

@ghost
Copy link
Author

ghost commented Aug 11, 2022

I can't think of another good documentation example and the uuid randomness issue has been fixed, I'll close this issue.

@ghost ghost closed this as completed Aug 11, 2022
This issue was closed.
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

No branches or pull requests

2 participants