Skip to content
Permalink
Browse files

Added testing more

  • Loading branch information...
ikeikeikeike committed Oct 28, 2018
1 parent 34e2328 commit b872633eaf745f4767152aba7fb42f1344047a5e
Showing with 298 additions and 1 deletion.
  1. +1 −0 .travis.yml
  2. +1 −1 README.md
  3. +6 −0 stm/_adapter_s3.go
  4. +1 −0 stm/adapter.go
  5. +29 −0 stm/adapter_buffer.go
  6. +6 −0 stm/adapter_file.go
  7. +254 −0 stm/sitemap_test.go
@@ -7,6 +7,7 @@ go:
- 1.8
- 1.9
- "1.10"
- "1.11"
- tip

install:
@@ -331,7 +331,7 @@ $ go get github.com/clbanning/mxj
Do testing

```console
$ go test -v -cover ./...
$ go test -v -cover -race ./...
```

#### Inspired by [sitemap_generator](http://github.com/kjvarga/sitemap_generator)
@@ -20,6 +20,12 @@ type S3Adapter struct {
Creds *credentials.Credentials
}

// Bytes gets written content.
func (adp *S3Adapter) Bytes() [][]byte {
// TODO
return nil
}

// Write will create sitemap xml file into the s3.
func (adp *S3Adapter) Write(loc *Location, data []byte) {
var reader io.Reader = bytes.NewReader(data)
@@ -8,4 +8,5 @@ var GzipPtn = regexp.MustCompile(".gz$")
// Adapter provides interface for writes some kind of sitemap.
type Adapter interface {
Write(loc *Location, data []byte)
Bytes() [][]byte
}
@@ -0,0 +1,29 @@
package stm

import "bytes"

// NewBufferAdapter returns the created the BufferAdapter's pointer
func NewBufferAdapter() *BufferAdapter {
adapter := &BufferAdapter{}
return adapter
}

// BufferAdapter provides implementation for the Adapter interface.
type BufferAdapter struct {
bufs []*bytes.Buffer // TODO: contains with filename
}

// Bytes gets written content.
func (adp *BufferAdapter) Bytes() [][]byte {
bufs := make([][]byte, len(adp.bufs))

for i, buf := range adp.bufs {
bufs[i] = buf.Bytes()
}
return bufs
}

// Write will create sitemap xml file into the file systems.
func (adp *BufferAdapter) Write(loc *Location, data []byte) {
adp.bufs = append(adp.bufs, bytes.NewBuffer(data))
}
@@ -15,6 +15,12 @@ func NewFileAdapter() *FileAdapter {
// FileAdapter provides implementation for the Adapter interface.
type FileAdapter struct{}

// Bytes gets written content.
func (adp *FileAdapter) Bytes() [][]byte {
// TODO
return nil
}

// Write will create sitemap xml file into the file systems.
func (adp *FileAdapter) Write(loc *Location, data []byte) {
dir := loc.Directory()
@@ -0,0 +1,254 @@
package stm

import (
"bytes"
"reflect"
"testing"

"github.com/clbanning/mxj"
)

func TestSitemapGenerator(t *testing.T) {
buf := BufferAdapter{}

sm := NewSitemap()
sm.SetPretty(true)
sm.SetVerbose(false)
sm.SetAdapter(&buf)

sm.Create()
for i := 1; i <= 10; i++ {
sm.Add(URL{"loc": "home", "changefreq": "always", "mobile": true, "lastmod": "2018-10-28T17:56:02+09:00"})
sm.Add(URL{"loc": "readme", "lastmod": "2018-10-28T17:56:02+09:00"})
sm.Add(URL{"loc": "aboutme", "priority": 0.1, "lastmod": "2018-10-28T17:56:02+09:00"})
}
sm.Finalize()

buffers := buf.Bytes()

data := buffers[len(buffers)-1]
expect := []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.example.com/sitemaps//sitemap1.xml.gz</loc>
<lastmod>2018-10-28T17:37:21+09:00</lastmod>
</sitemap>
</sitemapindex>`)

mdata, _ := mxj.NewMapXml(data)
mexpect, _ := mxj.NewMapXml(expect)
mdata.Remove("sitemapindex.sitemap.lastmod")
mexpect.Remove("sitemapindex.sitemap.lastmod")

if !reflect.DeepEqual(mdata, mexpect) {
t.Error(`Failed to generate sitemapindex`)
}

bufs := bytes.Buffer{}
for _, buf := range buffers[:len(buffers)-1] {
bufs.Write(buf)
}
data = bufs.Bytes()
expect = []byte(`
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:geo="http://www.google.com/geo/schemas/sitemap/1.0" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:pagemap="http://www.google.com/schemas/sitemap-pagemap/1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml" ><url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://www.example.com/home</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>always</changefreq>
<priority>0.5</priority>
<mobile:mobile/>
</url>
<url>
<loc>http://www.example.com/readme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.example.com/aboutme</loc>
<lastmod>2018-10-28T17:56:02+09:00</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
</urlset>
`)

mdata, _ = mxj.NewMapXml(data)
mexpect, _ = mxj.NewMapXml(expect)

if !reflect.DeepEqual(mdata, mexpect) {
t.Error(`Failed to generate dataindex`)
}

}

0 comments on commit b872633

Please sign in to comment.
You can’t perform that action at this time.