fixed tests and added full watch test with db/trigger
This commit is contained in:
parent
cfbd4d0ff0
commit
82ae6352d8
2 changed files with 137 additions and 7 deletions
|
@ -649,8 +649,10 @@ func storeFilterResult(filter *Filter, db *gorm.DB, debug bool) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(filterOutputs) > 0 {
|
||||||
db.Create(&filterOutputs)
|
db.Create(&filterOutputs)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getFilterResultConditionDiff(filter *Filter, db *gorm.DB) {
|
func getFilterResultConditionDiff(filter *Filter, db *gorm.DB) {
|
||||||
var previousOutput FilterOutput
|
var previousOutput FilterOutput
|
||||||
|
@ -790,7 +792,7 @@ func getFilterResultConditionHigherLast(filter *Filter, db *gorm.DB) {
|
||||||
func getFilterResultConditionHighest(filter *Filter, db *gorm.DB) {
|
func getFilterResultConditionHighest(filter *Filter, db *gorm.DB) {
|
||||||
var previousOutputs []FilterOutput
|
var previousOutputs []FilterOutput
|
||||||
db.Model(&FilterOutput{}).Where("watch_id = ? AND name = ?", filter.WatchID, filter.Var2).Find(&previousOutputs)
|
db.Model(&FilterOutput{}).Where("watch_id = ? AND name = ?", filter.WatchID, filter.Var2).Find(&previousOutputs)
|
||||||
highest := math.MaxFloat64
|
highest := -math.MaxFloat64
|
||||||
if previousOutputs != nil {
|
if previousOutputs != nil {
|
||||||
for _, previousOutput := range previousOutputs {
|
for _, previousOutput := range previousOutputs {
|
||||||
number, err := strconv.ParseFloat(previousOutput.Value, 64)
|
number, err := strconv.ParseFloat(previousOutput.Value, 64)
|
||||||
|
@ -802,7 +804,6 @@ func getFilterResultConditionHighest(filter *Filter, db *gorm.DB) {
|
||||||
highest = number
|
highest = number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, parent := range filter.Parents {
|
for _, parent := range filter.Parents {
|
||||||
|
|
141
scraping_test.go
141
scraping_test.go
|
@ -870,9 +870,9 @@ func TestFilterLowerThan(t *testing.T) {
|
||||||
Parents: []*Filter{
|
Parents: []*Filter{
|
||||||
{
|
{
|
||||||
Results: test.Input,
|
Results: test.Input,
|
||||||
|
},
|
||||||
|
},
|
||||||
Var2: &test.Threshold,
|
Var2: &test.Threshold,
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
getFilterResultConditionLowerThan(
|
getFilterResultConditionLowerThan(
|
||||||
&filter,
|
&filter,
|
||||||
|
@ -1131,11 +1131,11 @@ func TestFilterHigherThan(t *testing.T) {
|
||||||
Parents: []*Filter{
|
Parents: []*Filter{
|
||||||
{
|
{
|
||||||
Results: test.Input,
|
Results: test.Input,
|
||||||
|
},
|
||||||
|
},
|
||||||
Var2: &test.Threshold,
|
Var2: &test.Threshold,
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
getFilterResultConditionLowerThan(
|
getFilterResultConditionHigherThan(
|
||||||
&filter,
|
&filter,
|
||||||
)
|
)
|
||||||
if (filter.Results != nil && test.Want != nil) && !reflect.DeepEqual(test.Want, filter.Results) {
|
if (filter.Results != nil && test.Want != nil) && !reflect.DeepEqual(test.Want, filter.Results) {
|
||||||
|
@ -1234,7 +1234,6 @@ end`
|
||||||
|
|
||||||
func TestFilterLuaLibs(t *testing.T) {
|
func TestFilterLuaLibs(t *testing.T) {
|
||||||
regex := `
|
regex := `
|
||||||
sdfsdfsefs
|
|
||||||
local regexp = require("regexp")
|
local regexp = require("regexp")
|
||||||
local inspect = require("inspect")
|
local inspect = require("inspect")
|
||||||
|
|
||||||
|
@ -1512,6 +1511,136 @@ func TestSimpleDebugWatch(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSimpleTriggeredWatch(t *testing.T) {
|
||||||
|
db := getTestDB()
|
||||||
|
watch := Watch{
|
||||||
|
Name: "Test",
|
||||||
|
}
|
||||||
|
db.Create(&watch)
|
||||||
|
filters := []Filter{
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "Schedule",
|
||||||
|
Type: "cron",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "Echo",
|
||||||
|
Type: "echo",
|
||||||
|
Var1: HTML_STRING,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "XPath",
|
||||||
|
Type: "xpath",
|
||||||
|
Var1: "//td[@class='price']",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "Replace",
|
||||||
|
Type: "replace",
|
||||||
|
Var1: "[^0-9]",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "Min",
|
||||||
|
Type: "math",
|
||||||
|
Var1: "min",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "Minimum",
|
||||||
|
Type: "store",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "Max",
|
||||||
|
Type: "math",
|
||||||
|
Var1: "max",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
Name: "Maximum",
|
||||||
|
Type: "store",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
db.Create(&filters)
|
||||||
|
scheduleFilter := &filters[0]
|
||||||
|
echoFilter := &filters[1]
|
||||||
|
xpathFilter := &filters[2]
|
||||||
|
replaceFilter := &filters[3]
|
||||||
|
minFilter := &filters[4]
|
||||||
|
storeMinFilter := &filters[5]
|
||||||
|
maxFilter := &filters[6]
|
||||||
|
storeMaxFilter := &filters[7]
|
||||||
|
|
||||||
|
log.Println(scheduleFilter)
|
||||||
|
|
||||||
|
connections := []FilterConnection{
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
OutputID: scheduleFilter.ID,
|
||||||
|
InputID: echoFilter.ID,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
OutputID: echoFilter.ID,
|
||||||
|
InputID: xpathFilter.ID,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
OutputID: xpathFilter.ID,
|
||||||
|
InputID: replaceFilter.ID,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
OutputID: replaceFilter.ID,
|
||||||
|
InputID: minFilter.ID,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
OutputID: minFilter.ID,
|
||||||
|
InputID: storeMinFilter.ID,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
OutputID: replaceFilter.ID,
|
||||||
|
InputID: maxFilter.ID,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
WatchID: watch.ID,
|
||||||
|
OutputID: maxFilter.ID,
|
||||||
|
InputID: storeMaxFilter.ID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
db.Create(&connections)
|
||||||
|
|
||||||
|
log.Println(connections[0])
|
||||||
|
|
||||||
|
triggerSchedule(watch.ID, &Web{db: db}, &scheduleFilter.ID)
|
||||||
|
|
||||||
|
var filterOutputs []FilterOutput
|
||||||
|
db.Model(&FilterOutput{}).Find(&filterOutputs, fmt.Sprintf("watch_id = %d", watch.ID))
|
||||||
|
|
||||||
|
for _, filterOutput := range filterOutputs {
|
||||||
|
if filterOutput.Name == "Maximum" {
|
||||||
|
if filterOutput.Value != "400.000000" {
|
||||||
|
t.Errorf("Minimum filter value 400.000000 != %s", filterOutput.Value)
|
||||||
|
}
|
||||||
|
} else if filterOutput.Name == "Minimum" {
|
||||||
|
if filterOutput.Value != "100.000000" {
|
||||||
|
t.Errorf("Minimum filter value 100.000000 != %s", filterOutput.Value)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Errorf("Unknown filter name: %s", filterOutput.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err := os.Remove("./test.db")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Could not remove test db:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDontAllowMultipleCronOnSingleFilter(t *testing.T) {
|
func TestDontAllowMultipleCronOnSingleFilter(t *testing.T) {
|
||||||
filters := []Filter{
|
filters := []Filter{
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue