Skip to content

Commit

Permalink
🎇 add filter. fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
perillaroc committed Feb 6, 2019
1 parent 8249553 commit 2deca1f
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
22 changes: 22 additions & 0 deletions filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hpcmodel

type Filter struct {
Conditions []FilterCondition
}

func (f *Filter) Filter(items []Item) []Item {
var targetItems []Item
for _, item := range items {
isFit := true
for _, condition := range f.Conditions {
if !condition.IsFit(&item) {
isFit = false
break
}
}
if isFit {
targetItems = append(targetItems, item)
}
}
return targetItems
}
163 changes: 163 additions & 0 deletions filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package hpcmodel_test

import (
. "github.com/perillaroc/nwpc-hpc-model-go"
"testing"
"time"
)

func TestFilter_Filter(t *testing.T) {
items := []Item{
Item{
Props: []Property{
&StringProperty{
Category: QueryCategory{
ID: "squeue.jobid",
},
Data: "1",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.account",
},
Data: "nwp_xp",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.partition",
},
Data: "serial",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.state",
},
Data: "RUNNING",
},
&DateTimeProperty{
Category: QueryCategory{
ID: "squeue.submit_time",
},
Data: time.Date(2019, time.February, 6, 18, 00, 00, 00, time.UTC),
},
},
},
Item{
Props: []Property{
&StringProperty{
Category: QueryCategory{
ID: "squeue.jobid",
},
Data: "2",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.account",
},
Data: "nwp",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.partition",
},
Data: "serial_op",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.state",
},
Data: "RUNNING",
},
&DateTimeProperty{
Category: QueryCategory{
ID: "squeue.submit_time",
},
Data: time.Date(2019, time.February, 6, 20, 00, 00, 00, time.UTC),
},
},
},
Item{
Props: []Property{
&StringProperty{
Category: QueryCategory{
ID: "squeue.jobid",
},
Data: "3",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.account",
},
Data: "windroc",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.partition",
},
Data: "serial_op",
},
&StringProperty{
Category: QueryCategory{
ID: "squeue.state",
},
Data: "PENDING",
},
&DateTimeProperty{
Category: QueryCategory{
ID: "squeue.submit_time",
},
Data: time.Date(2019, time.February, 6, 19, 00, 00, 00, time.UTC),
},
},
},
}

tests := []struct {
filter Filter
result []string
}{
{
filter: Filter{
Conditions: []FilterCondition{
&StringPropertyFilterCondition{
ID: "squeue.account",
Checker: &StringEqualValueChecker{
ExpectedValue: "nwp_xp",
},
},
},
},
result: []string{"1"},
},
{
filter: Filter{
Conditions: []FilterCondition{
&StringPropertyFilterCondition{
ID: "squeue.account",
Checker: &StringContainChecker{
ExpectedValue: "nwp",
},
},
&StringPropertyFilterCondition{
ID: "squeue.partition",
Checker: &StringEqualValueChecker{
ExpectedValue: "serial_op",
},
},
},
},
result: []string{"2"},
},
}

for _, test := range tests {
targetItems := test.filter.Filter(items)
for index, id := range test.result {
targetItem := targetItems[index]
prop := targetItem.GetProperty("squeue.jobid").(*StringProperty)
if prop.Data != id {
t.Errorf("target id %s != requred id %s", prop.Data, id)
}
}
}
}

0 comments on commit 2deca1f

Please sign in to comment.