2018-09-06 15:34:56 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-07 14:06:08 +02:00
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/utils"
|
2018-09-06 15:34:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMeasurementWorkflow(t *testing.T) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "dbtest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "oonitest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
sess, err := Connect(tmpfile.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-09-07 14:06:08 +02:00
|
|
|
|
|
|
|
location := utils.LocationInfo{
|
|
|
|
ASN: 0,
|
|
|
|
CountryCode: "IT",
|
|
|
|
NetworkName: "Unknown",
|
|
|
|
}
|
|
|
|
network, err := CreateNetwork(sess, &location)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := CreateResult(sess, tmpdir, "websites", network.ID)
|
2018-09-06 15:34:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-09-07 12:55:27 +02:00
|
|
|
reportID := sql.NullString{String: "", Valid: false}
|
|
|
|
testName := "antani"
|
|
|
|
resultID := result.ID
|
|
|
|
reportFilePath := tmpdir
|
|
|
|
urlID := sql.NullInt64{Int64: 0, Valid: false}
|
|
|
|
|
|
|
|
m1, err := CreateMeasurement(sess, reportID, testName, resultID, reportFilePath, urlID)
|
2018-09-06 15:34:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m2 Measurement
|
|
|
|
err = sess.Collection("measurements").Find("id", m1.ID).One(&m2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if m2.ResultID != m1.ResultID {
|
|
|
|
t.Error("result_id mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|