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

Generating unique values #73

Open
jonaslagoni opened this issue Nov 2, 2020 · 2 comments
Open

Generating unique values #73

jonaslagoni opened this issue Nov 2, 2020 · 2 comments

Comments

@jonaslagoni
Copy link

Hey!

I am trying to generate unique ids however I am having some troubles with it. I have tried adding a list to which ids have already been generated and then discard any that matches. questionIdGenOptions.alreadyGeneratedQuestions is an array of strings. Utils.Contains is a function which returns true if the given array contains the corresponding string otherwise false.

func QuestionIdGen(questionIdGenOptions *QuestionIdGenOptions) gopter.Gen{
	if questionIdGenOptions != nil && questionIdGenOptions.alreadyGeneratedQuestions == nil {
		var newAlreadyGeneratedQuestions = []string{}
		questionIdGenOptions.alreadyGeneratedQuestions = &newAlreadyGeneratedQuestions
	}
	pgen := gen.PtrOf(gen.AnyString())
	if questionIdGenOptions != nil {
		pgen = pgen.SuchThat(
			func(generatedId *string) bool {
				//Some characters look the same however their bytes are not i.e. it is therefore unique.
				if questionIdGenOptions.GenUniqueId != nil && *questionIdGenOptions.GenUniqueId == true && (generatedId == nil || Utils.Contains(*questionIdGenOptions.alreadyGeneratedQuestions, *generatedId)) {
					return false
				}

				newAlreadyGeneratedQuestions := append(*questionIdGenOptions.alreadyGeneratedQuestions, *generatedId)
				questionIdGenOptions.alreadyGeneratedQuestions = &newAlreadyGeneratedQuestions
				return true
			})
	}
	return pgen
}

However atm. with this if statement, the wrapper generator which uses this function fails to generate anything... And I am unsure why almost like something internal error is happening. If I remove it everything works fine except the ids are not unique. Is there another way of doing it or?

@jonaslagoni jonaslagoni changed the title Generating unique ids Generating unique values Nov 2, 2020
@jonaslagoni
Copy link
Author

Soo I test simply give up after (fixed any underlying problems), however the usecase are still relevant, do you want any functionality which can do this in a reproducing manner? Or should I find external libraries for this?

@untoldwind
Copy link
Collaborator

Sorry for the long wait ... (I guess the situation is kind of messed up all around the world).

Obviously you are doing a sort of stateful testing, i.e. the function under test is not a pure-function since the outcome depends on the previous calls to it. As a rule of thumb one should use command-based tests for this kind of situation.

Maybe there is a somewhat "simple" example that could be added to the library reflecting a use-case like this.

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