From ca505dfff324ea51e736d975ea52b140b0b68a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 7 Sep 2018 15:29:16 +0200 Subject: [PATCH] Write a unittest for the URL creation function --- internal/database/actions_test.go | 39 +++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/internal/database/actions_test.go b/internal/database/actions_test.go index 8c7e770..0da42c5 100644 --- a/internal/database/actions_test.go +++ b/internal/database/actions_test.go @@ -6,7 +6,6 @@ import ( "os" "testing" - "github.com/apex/log" "github.com/ooni/probe-cli/utils" ) @@ -15,8 +14,7 @@ func TestMeasurementWorkflow(t *testing.T) { if err != nil { t.Fatal(err) } - log.Infof("%s", tmpfile.Name()) - //defer os.Remove(tmpfile.Name()) + defer os.Remove(tmpfile.Name()) tmpdir, err := ioutil.TempDir("", "oonitest") if err != nil { @@ -26,7 +24,7 @@ func TestMeasurementWorkflow(t *testing.T) { sess, err := Connect(tmpfile.Name()) if err != nil { - t.Error(err) + t.Fatal(err) } location := utils.LocationInfo{ @@ -84,3 +82,36 @@ func TestMeasurementWorkflow(t *testing.T) { t.Error("network_type should be wifi") } } + +func TestURLCreation(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.Fatal(err) + } + + newID1, err := CreateOrUpdateURL(sess, "https://google.com", "SRCH", "XX") + if err != nil { + t.Fatal(err) + } + + newID2, err := CreateOrUpdateURL(sess, "https://google.com", "GMB", "XX") + if err != nil { + t.Fatal(err) + } + + if newID2 != newID1 { + t.Error("inserting the same URL with different category code should produce the same result") + } +}