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

fix: override SearchRecommendations methods #99

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/target/
.env
/.run/ThreshrClientTest.run.xml
.iml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,33 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import io.micronaut.serde.annotation.Serdeable;

import java.util.Arrays;
import java.util.Objects;

@Serdeable
public record SearchRecommendations(
@JsonProperty("category_id")
String categoryId,
@JsonProperty("related_categories")
RelatedCategory[] relatedCategories
) {}
RelatedCategory[] relatedCategories) {
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
SearchRecommendations castObject = (SearchRecommendations) object;
return Objects.equals(categoryId, castObject.categoryId()) &&
Arrays.equals(relatedCategories, castObject.relatedCategories);
}
@Override
public int hashCode() {
int hash = 31 * Objects.hash(categoryId);
hash += Arrays.hashCode(relatedCategories);
return hash;
}
@Override
public String toString() {
return String.format("SearchRecommendations[categoryId=%s, relatedCategories=%s]",
categoryId, Arrays.toString(relatedCategories));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.graqr.threshr.model.redsky.product

import groovy.sql.Sql
import io.micronaut.context.annotation.Value
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Shared
import spock.lang.Specification

import java.util.stream.Collectors

@MicronautTest
class SearchRecommendationsSpec extends Specification {

@Shared
@Value('${test.datasources.default.url}')
String url

@Shared
Sql sql

@Shared
RelatedCategory[] testCategories

void setupSpec() {
url = null != url ? url : System.getenv("TEST_DATASOURCES_DEFAULT_URL")
sql = Sql.newInstance(url)
testCategories = sql.rows('select id, category_name from test_target_categories limit 5')
.stream().map(it ->
new RelatedCategory(it.get('id') as String, it.get('category_name') as String))
}

void "equals function validates equality with multiple RelatedCategory objects field"() {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
recommendations == new SearchRecommendations(testCategories.first().title(), testCategories)
}

void "equals function fails appropriately with single field differences"() {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
recommendations != new SearchRecommendations(title, categories as RelatedCategory[])

where:
title | categories
testCategories.first().title() | testCategories[0..3].toArray(RelatedCategory[]::new) //bad categories
"I'm a bad title" | testCategories
"we're all wrong" | testCategories[2..3].toArray(RelatedCategory[]::new)

}

def "HashCode matches with multiple RelatedCategory objects field"() {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
recommendations.hashCode() == new SearchRecommendations(testCategories.first().title(), testCategories).hashCode()
}

void "HashCode method fails appropriately with singe field differences"() {
given:
SearchRecommendations recommendations = new SearchRecommendations(testCategories.first().title(), testCategories)

expect:
recommendations != new SearchRecommendations(title, categories as RelatedCategory[])

where:
title | categories
testCategories.first().title() | testCategories[0..3].toArray(RelatedCategory[]::new) //bad categories
"I'm a bad title" | testCategories
"we're all wrong" | testCategories[2..3].toArray(RelatedCategory[]::new)
}


def "ToString"() {
given:
String expectedTitle = testCategories.first().title()
SearchRecommendations recommendations = new SearchRecommendations(expectedTitle, categories)
String expectedCategories = Arrays.stream(categories).map { it ->
return "RelatedCategory[url=${((RelatedCategory) it).url()}, title=${((RelatedCategory) it).title()}]"
}
.collect(Collectors.joining(", "))

expect:
recommendations.toString() == "SearchRecommendations[categoryId=${expectedTitle}, relatedCategories=[${expectedCategories}]]"


where:
title | categories
testCategories.first().title() | testCategories[0..3].toArray(RelatedCategory[]::new)
testCategories.last().title() | new RelatedCategory[]{testCategories[0]}
}
}
Loading