From 7e75ab4304abcb2034fbaa80176a92f9fa0f44fd Mon Sep 17 00:00:00 2001 From: Jisin0 Date: Sat, 1 Jun 2024 20:15:38 +0100 Subject: [PATCH 1/2] Add TestXpath Create sample.html --- encode/sample.html | 25 +++++++++++++++++++++++++ encode/xpath_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 encode/sample.html create mode 100644 encode/xpath_test.go diff --git a/encode/sample.html b/encode/sample.html new file mode 100644 index 0000000..9309be6 --- /dev/null +++ b/encode/sample.html @@ -0,0 +1,25 @@ + + +
+ + + + Data 1 + Data 2 + + Data 3 + + + +

+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec non tortor in orci sagittis vehicula accumsan vel magna. Fusce et nulla nec eros egestas ullamcorper. Phasellus consequat euismod efficitur. Fusce nec bibendum diam, eu mollis turpis. Sed nisi tellus, euismod ut viverra vitae, efficitur et magna. Proin dictum tellus ac augue semper, quis pharetra mauris ullamcorper. Duis quis ullamcorper metus. Morbi eu leo et augue aliquam faucibus. +

+ +

+
+ + \ No newline at end of file diff --git a/encode/xpath_test.go b/encode/xpath_test.go new file mode 100644 index 0000000..0ef914e --- /dev/null +++ b/encode/xpath_test.go @@ -0,0 +1,37 @@ +package encode_test + +import ( + "testing" + + "github.com/Jisin0/filmigo/encode" + "github.com/Jisin0/filmigo/types" + "github.com/antchfx/htmlquery" +) + +func TestXpath(t *testing.T) { + doc, err := htmlquery.LoadDoc("sample.html") + if err != nil { + t.Errorf("failed to open sample file : %v", err) + t.FailNow() + } + + if doc != nil { + type sampleData struct { + InnerText string `xpath:"//p[contains(@class, 'substring')]"` + Attribute string `xpath:"//p[last()]|attr_my-attr"` + LinkList types.Links `xpath:"//span[@class='sample']"` + StringList []string `xpath:"//ul"` + } + + res, ok := encode.Xpath(doc, sampleData{}).(sampleData) + if !ok { + t.Errorf("unknown type returned") + t.FailNow() + } + + if res.Attribute == "" || res.InnerText == "" || len(res.LinkList) < 3 || len(res.StringList) < 3 { + t.Errorf("xpath failed with output : %+v", res) + t.FailNow() + } + } +} From 459bf70ef7cf58d37b568580bcc764b8b48f3a2f Mon Sep 17 00:00:00 2001 From: Jisin0 Date: Sun, 2 Jun 2024 14:05:45 +0100 Subject: [PATCH 2/2] Add TestCache --- cache/cache_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 cache/cache_test.go diff --git a/cache/cache_test.go b/cache/cache_test.go new file mode 100644 index 0000000..7487648 --- /dev/null +++ b/cache/cache_test.go @@ -0,0 +1,45 @@ +package cache_test + +import ( + "os" + "testing" + "time" + + "github.com/Jisin0/filmigo/cache" +) + +func TestCache(t *testing.T) { + directoryName := "testcache" + c := cache.NewCache(directoryName, 1*time.Hour) + + type sampleType struct { + String string `json:"string"` + Integer int `json:"int"` + } + + inputData := sampleType{ + String: "foo", + Integer: 3, + } + + err := c.Save("uid", inputData) + if err != nil { + t.Error(err) + } + + var outputData sampleType + + err = c.Load("uid", &outputData) + if err != nil { + t.Error(err) + } + + if outputData != inputData { + t.Logf("Input: %+v\nOutput: %+v", inputData, outputData) + } + + err = os.RemoveAll(directoryName) + if err != nil { + t.Error(err) + } +}