-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8249553
commit 2deca1f
Showing
2 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |