Skip to content

Commit

Permalink
Use github.com/tkuchiki/parsetime for parsing time string
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzolo committed Sep 4, 2019
1 parent 2d3553a commit 20f260f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 10 deletions.
35 changes: 33 additions & 2 deletions fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,39 @@ func TestUseFixtureWithDataSetMerge(t *testing.T) {
}
}

func commonSuccessCheck(t *testing.T, age interface{}) {
t.Helper()
func TestUseFixtureYAMLFormat(t *testing.T) {
defer mongotest.Reconfigure(mongotest.Config{
FixtureFormat: mongotest.FixtureFormatYAML,
})()
err := mongotest.UseFixture("yaml/admin_users")
if err != nil {
t.Error(err)
}
cnt, err := mongotest.CountInt("users")
if err != nil {
t.Error(err)
}
if cnt != 2 {
t.Errorf("saved user count is invalid (want: %d, got: %d)", cnt, 2)
}

saved, err := mongotest.Find("users", "admin1")
if err != nil {
t.Error(err)
}
want := map[string]interface{}{
"_id": "admin1",
"name": "admin user1",
"email": "[email protected]",
"admin": true,
"company": "foo",
"age": int32(30),
"note": "abc",
"created_at": createdAtPrimitive,
}
if !reflect.DeepEqual(saved, want) {
t.Errorf("saved user is invalid. (%v)", diffMap(saved, want))
}
}

func TestUseFixtureJSONFormat(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.12
require (
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/tkuchiki/go-timezone v0.1.4 // indirect
github.com/tkuchiki/parsetime v0.0.0-20160517140325-bece749572a8
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.1.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/tkuchiki/go-timezone v0.1.4 h1:bqdCyQmMPO2vxCobv9nqHWgmkMksGZ/tNhROr2DKxPI=
github.com/tkuchiki/go-timezone v0.1.4/go.mod h1:b1Ean9v2UXtxSq4TZF0i/TU9NuoWa9hOzOKoGCV2zqY=
github.com/tkuchiki/parsetime v0.0.0-20160517140325-bece749572a8 h1:D/JRgW+fqBVZKYlnDtUfSoYjeDvRJmMsOtcPKwWQfpM=
github.com/tkuchiki/parsetime v0.0.0-20160517140325-bece749572a8/go.mod h1:lyFzkHaB7gqrI+ron+B8biAk0dke6Tvbpw/tfDjQ+r4=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v1.0.0 h1:d9X0esnoa3dFsV0FG35rAT0RIhYFlPq7MiP+DW89La0=
Expand Down
9 changes: 7 additions & 2 deletions mongotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package mongotest

import (
"context"
"time"

"github.com/tkuchiki/parsetime"

"go.mongodb.org/mongo-driver/bson"
)
Expand Down Expand Up @@ -78,7 +79,11 @@ func SimpleConvertTime(collectionName, fieldName string) PreInsertFunc {
if !ok {
return value, nil
}
t, err := time.Parse("2006-01-02T15:04:05Z", sv)
p, err := parsetime.NewParseTime()
if err != nil {
return nil, err
}
t, err := p.Parse(sv)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/admin_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ users:
email: [email protected]
admin: true
company: bar
created_at: 2019-01-02T12:34:56Z
created_at: 2019/01/02 12:34:56
companies:
foo:
name: foo company
Expand Down
2 changes: 1 addition & 1 deletion testdata/foo_users.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"admin": false,
"company": "foo",
"age": 25,
"created_at": "2019-01-02T12:34:56Z"
"created_at": "2019/01/02 12:34:56"
}
},
"companies": {
Expand Down
2 changes: 1 addition & 1 deletion testdata/json/admin_users.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"email": "[email protected]",
"admin": true,
"company": "bar",
"created_at": "2019-01-02T12:34:56Z"
"created_at": "2019/01/02 12:34:56"
}
},
"companies": {
Expand Down
2 changes: 1 addition & 1 deletion testdata/json/foo_users.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"admin": false,
"company": "foo",
"age": 25,
"created_at": "2019-01-02T12:34:56Z"
"created_at": "2019/01/02 12:34:56"
}
},
"companies": {
Expand Down
2 changes: 1 addition & 1 deletion testdata/yaml/admin_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ users:
email: [email protected]
admin: true
company: bar
created_at: 2019-01-02T12:34:56Z
created_at: 2019/01/02 12:34:56
companies:
foo:
name: foo company
Expand Down
2 changes: 1 addition & 1 deletion testdata/yaml/foo_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ users:
admin: false
company: foo
age: 25
created_at: 2019-01-02T12:34:56Z
created_at: 2019/01/02 12:34:56
companies:
foo:
name: foo company
Expand Down

0 comments on commit 20f260f

Please sign in to comment.