Implement search criteria filtering

This commit is contained in:
The Magician 2024-06-11 12:47:55 +01:00
parent 2968462011
commit a6f1587612
2 changed files with 451 additions and 1 deletions

View File

@ -10,11 +10,52 @@ import (
"strings"
)
type Triadic int
const (
True Triadic = iota
False
Either
)
type SearchCriteria struct {
SetCode string
Foil Triadic
Promo Triadic
Language string
}
// The SearchOptions type is a map of `string` keys (which can be searched using fzf)
// to `string` values (which represent a primary key in the CardPrintings table)
type SearchOptions map[string]string
func GetAllSearchOptions(db *sql.DB) (SearchOptions, error) {
func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteria) bool {
if searchCriteria.SetCode != "" && printing.SetCode != searchCriteria.SetCode {
return true
}
if searchCriteria.Foil == False && printing.IsFoil {
return true
}
if searchCriteria.Foil == True && !printing.IsFoil {
return true
}
if searchCriteria.Promo == False && printing.IsPromo {
return true
}
if searchCriteria.Promo == True && !printing.IsPromo {
return true
}
if searchCriteria.Language != "" && printing.Language != searchCriteria.Language {
return true
}
return false
}
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptions, error) {
var searchOptions SearchOptions = make(map[string]string)
cardPrintings, err := database.GetAllCardPrintings(db)
@ -23,6 +64,13 @@ func GetAllSearchOptions(db *sql.DB) (SearchOptions, error) {
}
for _, printing := range cardPrintings {
// Filter based on search criteria
filter := filterPrinting(printing, searchCriteria)
if filter {
continue
}
// Construct search option string
searchString := fmt.Sprintf("%s (%s %s) [%s]", printing.Name, printing.SetCode, printing.CollectorNumber, printing.Language)
if printing.IsFoil {

View File

@ -0,0 +1,402 @@
package logic
import (
"sevenkeys/database"
"testing"
)
func TestFilterPrinting_ReturnsTrue_IfSetCodeDoesNotMatch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "otj",
Foil: False,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != true {
t.Errorf("filter was false")
}
}
func TestFilterPrinting_ReturnsFalse_IfSetCodeDoesMatch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfSetCodeNotSet(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "",
Foil: False,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsTrue_IfFoilCardInNonFoilSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: true,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != true {
t.Errorf("filter was false")
}
}
func TestFilterPrinting_ReturnsTrue_IfNonFoilCardInFoilSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: True,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != true {
t.Errorf("filter was false")
}
}
func TestFilterPrinting_ReturnsFalse_IfNonFoilCardInNonFoilSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfFoilCardInFoilSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: true,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: True,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfFoilCardInEitherFoilSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: true,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: Either,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfNonFoilCardInEitherFoilSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: Either,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsTrue_IfPromoCardInNonPromoSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: true,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != true {
t.Errorf("filter was false")
}
}
func TestFilterPrinting_ReturnsTrue_IfNonPromoCardInPromoSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: True,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != true {
t.Errorf("filter was false")
}
}
func TestFilterPrinting_ReturnsFalse_IfNonPromoCardInNonPromoSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfPromoCardInPromoSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: true,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: True,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfPromoCardInEitherPromoSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: true,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: Either,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfNonPromoCardInEitherPromoSearch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: Either,
Language: "en",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsTrue_IfLanguageDoesNotMatch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "de",
}
filter := filterPrinting(printing, searchCriteria)
if filter != true {
t.Errorf("filter was false")
}
}
func TestFilterPrinting_ReturnsFalse_IfLanguageDoesMatch(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "de",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "de",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}
func TestFilterPrinting_ReturnsFalse_IfLanguageNotSet(t *testing.T) {
printing := database.CardPrinting{
SetCode: "rtr",
IsFoil: false,
IsPromo: false,
Language: "en",
}
searchCriteria := SearchCriteria{
SetCode: "rtr",
Foil: False,
Promo: False,
Language: "",
}
filter := filterPrinting(printing, searchCriteria)
if filter != false {
t.Errorf("filter was true")
}
}