Skip to content

Commit

Permalink
Add timezone cases (#112)
Browse files Browse the repository at this point in the history
* Add tryToExecute() to wrap execute() in tests

* Add tests that retrive datetime from vertex/edge

* Add comments

* Fix error
  • Loading branch information
Aiee authored Jul 1, 2021
1 parent 7e856b0 commit a0577f6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 35 deletions.
118 changes: 95 additions & 23 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,23 @@ func TestConfigs(t *testing.T) {
}
defer session.Release()
// Excute a query
resp, err := session.Execute("SHOW HOSTS;")
resp, err := tryToExecute(session, "SHOW HOSTS;")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResSetResp(t, "show hosts", resp)
// Create a new space
resp, err = session.Execute("CREATE SPACE client_test(partition_num=1024, replica_factor=1, vid_type = FIXED_STRING(30));")
resp, err = tryToExecute(
session,
"CREATE SPACE client_test(partition_num=1024, replica_factor=1, vid_type = FIXED_STRING(30));")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResSetResp(t, "create space", resp)

resp, err = session.Execute("DROP SPACE client_test;")
resp, err = tryToExecute(session, "DROP SPACE client_test;")
if err != nil {
t.Fatalf(err.Error())
return
Expand Down Expand Up @@ -264,16 +266,16 @@ func TestServiceDataIO(t *testing.T) {
"first_out_city timestamp, hobby string); " +
"CREATE TAG IF NOT EXISTS student(name string); " +
"CREATE EDGE IF NOT EXISTS like(likeness double); " +
"CREATE EDGE IF NOT EXISTS friend(start_year int, end_year int); " +
"CREATE EDGE IF NOT EXISTS friend(start_Datetime datetime, end_Datetime datetime); " +
"CREATE TAG INDEX IF NOT EXISTS person_name_index ON person(name(8));"
resultSet, err := session.Execute(createSchema)
resultSet, err := tryToExecute(session, createSchema)
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(createSchema, resultSet)
}
time.Sleep(10 * time.Second)
time.Sleep(5 * time.Second)

// Load data
{
Expand All @@ -295,7 +297,7 @@ func TestServiceDataIO(t *testing.T) {
"'John':('John', 10, 3, 10, 100, datetime('2010-09-10T10:08:02'), " +
"date('2017-09-10'), time('07:10:00'), " +
"1000.0, false, \"Hello World!\", 100.0, 1111)"
resultSet, err := session.Execute(query)
resultSet, err := tryToExecute(session, query)
if err != nil {
t.Fatalf(err.Error())
return
Expand All @@ -306,7 +308,7 @@ func TestServiceDataIO(t *testing.T) {
"INSERT VERTEX student(name) VALUES " +
"'Bob':('Bob'), 'Lily':('Lily'), " +
"'Tom':('Tom'), 'Jerry':('Jerry'), 'John':('John')"
resultSet, err = session.Execute(query)
resultSet, err = tryToExecute(session, query)
if err != nil {
t.Fatalf(err.Error())
return
Expand All @@ -320,21 +322,21 @@ func TestServiceDataIO(t *testing.T) {
"'Jerry'->'Lily':(84.0)," +
"'Tom'->'Jerry':(68.3), " +
"'Bob'->'John':(97.2)"
resultSet, err = session.Execute(query)
resultSet, err = tryToExecute(session, query)
if err != nil {
t.Fatalf(err.Error())
return
}
checkResultSet(query, resultSet)

query =
"INSERT EDGE friend(start_year, end_year) VALUES " +
"'Bob'->'Lily':(2018, 2020), " +
"'Bob'->'Tom':(2018, 2020), " +
"'Jerry'->'Lily':(2018, 2020)," +
"'Tom'->'Jerry':(2018, 2020), " +
"'Bob'->'John':(2018, 2020)"
resultSet, err = session.Execute(query)
"INSERT EDGE friend(start_Datetime, end_Datetime) VALUES " +
"'Bob'->'Lily':(datetime('2008-09-10T10:08:02'), datetime('2010-09-10T10:08:02')), " +
"'Bob'->'Tom':(datetime('2008-09-10T10:08:02'), datetime('2010-09-10T10:08:02')), " +
"'Jerry'->'Lily':(datetime('2008-09-10T10:08:02'), datetime('2010-09-10T10:08:02')), " +
"'Tom'->'Jerry':(datetime('2008-09-10T10:08:02'), datetime('2010-09-10T10:08:02')), " +
"'Bob'->'John':(datetime('2008-09-10T10:08:02'), datetime('2010-09-10T10:08:02'))"
resultSet, err = tryToExecute(session, query)
if err != nil {
t.Fatalf(err.Error())
return
Expand All @@ -350,7 +352,7 @@ func TestServiceDataIO(t *testing.T) {
"person.start_school, person.morning, " +
"person.property, person.is_girl, person.child_name, " +
"person.expend, person.first_out_city, person.hobby"
resp, err := session.Execute(query)
resp, err := tryToExecute(session, query)
if err != nil {
t.Fatalf(err.Error())
return
Expand Down Expand Up @@ -429,9 +431,10 @@ func TestServiceDataIO(t *testing.T) {
expected := nebula.Time{23, 10, 0, 0}
assert.Equal(t, expected, *UTCTime)
}

// test node
{
resp, err := session.Execute("MATCH (v:person {name: \"Bob\"}) RETURN v")
resp, err := tryToExecute(session, "MATCH (v:person {name: \"Bob\"}) RETURN v")
if err != nil {
t.Fatalf(err.Error())
return
Expand Down Expand Up @@ -461,11 +464,68 @@ func TestServiceDataIO(t *testing.T) {
"morning: 07:10:00.000000, name: \"Bob\", "+
"property: 1000.0, start_school: 2017-09-10})",
node.String())
props, _ := node.Properties("person")
datetime := props["birthday"]
dtWrapper, err := datetime.AsDateTime()
if err != nil {
t.Fatalf(err.Error())
return
}
utcTime := dtWrapper.getRawDateTime()
expected := nebula.DateTime{2010, 9, 10, 2, 8, 2, 0}
assert.Equal(t, expected, *utcTime)

localTime, _ := dtWrapper.getLocalDateTime()
expected = nebula.DateTime{2010, 9, 10, 10, 8, 2, 0}
assert.Equal(t, expected, *localTime)
}

// test edge
{
resp, err := tryToExecute(session, "MATCH (:person{name: \"Bob\"}) -[e:friend]-> (:person{name: \"Lily\"}) RETURN e")
if err != nil {
t.Fatalf(err.Error())
return
}
assert.Equal(t, 1, resp.GetRowSize())
record, err := resp.GetRowValuesByIndex(0)
if err != nil {
t.Fatalf(err.Error())
return
}
valWrap, err := record.GetValueByIndex(0)
if err != nil {
t.Fatalf(err.Error())
return
}
relationship, err := valWrap.AsRelationship()
if err != nil {
t.Fatalf(err.Error())
return
}
assert.Equal(t,
"[:friend \"Bob\"->\"Lily\" @0 {end_Datetime: 2010-09-10T10:08:02.000000, start_Datetime: 2008-09-10T10:08:02.000000}]",
relationship.String())
props := relationship.Properties()
datetime := props["end_Datetime"]
dtWrapper, err := datetime.AsDateTime()
if err != nil {
t.Fatalf(err.Error())
return
}
utcTime := dtWrapper.getRawDateTime()
expected := nebula.DateTime{2010, 9, 10, 2, 8, 2, 0}
assert.Equal(t, expected, *utcTime)

localTime, _ := dtWrapper.getLocalDateTime()
expected = nebula.DateTime{2010, 9, 10, 10, 8, 2, 0}
assert.Equal(t, expected, *localTime)
}

// Drop space
{
query := "DROP SPACE test_data;"
_, err := session.Execute(query)
_, err := tryToExecute(session, query)
if err != nil {
t.Fatalf(err.Error())
return
Expand Down Expand Up @@ -501,21 +561,21 @@ func TestPool_SingleHost(t *testing.T) {
}
defer session.Release()
// Excute a query
resp, err := session.Execute("SHOW HOSTS;")
resp, err := tryToExecute(session, "SHOW HOSTS;")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResSetResp(t, "show hosts", resp)
// Create a new space
resp, err = session.Execute("CREATE SPACE client_test(partition_num=1024, replica_factor=1, vid_type = FIXED_STRING(30));")
resp, err = tryToExecute(session, "CREATE SPACE client_test(partition_num=1024, replica_factor=1, vid_type = FIXED_STRING(30));")
if err != nil {
t.Fatalf(err.Error())
return
}
checkResSetResp(t, "create space", resp)

resp, err = session.Execute("DROP SPACE client_test;")
resp, err = tryToExecute(session, "DROP SPACE client_test;")
if err != nil {
t.Fatalf(err.Error())
return
Expand Down Expand Up @@ -570,7 +630,7 @@ func TestPool_MultiHosts(t *testing.T) {
assert.Equal(t, 0, pool.idleConnectionQueue.Len())
assert.Equal(t, 3, pool.activeConnectionQueue.Len())

resp, err := newSession.Execute("SHOW HOSTS;")
resp, err := tryToExecute(newSession, "SHOW HOSTS;")
if err != nil {
t.Fatalf(err.Error())
return
Expand Down Expand Up @@ -825,3 +885,15 @@ func startContainer(t *testing.T, containerName string) {
t.Fatalf("failed to start container, name: %s, error code: %s", containerName, err.Error())
}
}

func tryToExecute(session *Session, query string) (*ResultSet, error) {
var err error
for i := 3; i > 0; i-- {
resp, err := session.Execute(query)
if err == nil && resp.IsSucceed() {
return resp, nil
}
time.Sleep(2 * time.Second)
}
return nil, err
}
27 changes: 17 additions & 10 deletions result_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,30 +429,30 @@ func (record Record) hasColName(colName string) bool {
return false
}

// Returns a list of row vid
// getRawID returns a list of row vid
func (node Node) getRawID() *nebula.Value {
return node.vertex.GetVid()
}

// Returns a list of vid of node
// GetID returns a list of vid of node
func (node Node) GetID() ValueWrapper {
return ValueWrapper{node.vertex.GetVid(), node.timezoneInfo}
}

// Returns a list of tag names of node
// GetTags returns a list of tag names of node
func (node Node) GetTags() []string {
return node.tags
}

// Check if node contains given label
// HasTag checks if node contains given label
func (node Node) HasTag(label string) bool {
if _, ok := node.tagNameIndexMap[label]; ok {
return true
}
return false
}

// Returns all properties of a tag
// Properties returns all properties of a tag
func (node Node) Properties(tagName string) (map[string]*ValueWrapper, error) {
kvMap := make(map[string]*ValueWrapper)
// Check if label exists
Expand All @@ -466,7 +466,7 @@ func (node Node) Properties(tagName string) (map[string]*ValueWrapper, error) {
return kvMap, nil
}

// Returns all prop names of the given tag name
// Keys returns all prop names of the given tag name
func (node Node) Keys(tagName string) ([]string, error) {
if !node.HasTag(tagName) {
return nil, fmt.Errorf("failed to get properties: Tag name %s does not exsist in the Node", tagName)
Expand All @@ -479,7 +479,7 @@ func (node Node) Keys(tagName string) ([]string, error) {
return propNameList, nil
}

// Returns all prop values of the given tag name
// Values returns all prop values of the given tag name
func (node Node) Values(tagName string) ([]*ValueWrapper, error) {
if !node.HasTag(tagName) {
return nil, fmt.Errorf("failed to get properties: Tag name %s does not exsist in the Node", tagName)
Expand All @@ -492,6 +492,7 @@ func (node Node) Values(tagName string) ([]*ValueWrapper, error) {
return propValList, nil
}

// String returns a string representing node
// Node format: ("VertexID" :tag1{k0: v0,k1: v1}:tag2{k2: v2})
func (node Node) String() string {
var keyList []string
Expand Down Expand Up @@ -556,6 +557,7 @@ func (relationship Relationship) GetRanking() int64 {
return int64(relationship.edge.Ranking)
}

// Properties returns a map where the key is property name and the value is property name
func (relationship Relationship) Properties() map[string]*ValueWrapper {
kvMap := make(map[string]*ValueWrapper)
var (
Expand All @@ -573,7 +575,7 @@ func (relationship Relationship) Properties() map[string]*ValueWrapper {
return kvMap
}

// Returns a list of keys
// Keys returns a list of keys
func (relationship Relationship) Keys() []string {
var keys []string
for key := range relationship.edge.GetProps() {
Expand All @@ -582,7 +584,7 @@ func (relationship Relationship) Keys() []string {
return keys
}

// Returns a list of values wrapped as ValueWrappers
// Values returns a list of values wrapped as ValueWrappers
func (relationship Relationship) Values() []*ValueWrapper {
var values []*ValueWrapper
for _, value := range relationship.edge.GetProps() {
Expand All @@ -591,6 +593,7 @@ func (relationship Relationship) Values() []*ValueWrapper {
return values
}

// String returns a string representing relationship
// Relationship format: [:edge src->dst @ranking {props}]
func (relationship Relationship) String() string {
edge := relationship.edge
Expand Down Expand Up @@ -680,7 +683,11 @@ func (path *PathWrapper) GetEndNode() (*Node, error) {
return path.segments[len(path.segments)-1].endNode, nil
}

// Path format: <("VertexID" :tag1{k0: v0,k1: v1})-[:TypeName@ranking {edgeProps}]->("VertexID2" :tag1{k0: v0,k1: v1} :tag2{k2: v2})-[:TypeName@ranking {edgeProps}]->("VertexID3" :tag1{k0: v0,k1: v1})>
// Path format: <("VertexID" :tag1{k0: v0,k1: v1})
// -[:TypeName@ranking {edgeProps}]->
// ("VertexID2" :tag1{k0: v0,k1: v1} :tag2{k2: v2})
// -[:TypeName@ranking {edgeProps}]->
// ("VertexID3" :tag1{k0: v0,k1: v1})>
func (pathWrap *PathWrapper) String() string {
path := pathWrap.path
src := path.Src
Expand Down
5 changes: 3 additions & 2 deletions result_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ func TestAsDate(t *testing.T) {

func TestAsTime(t *testing.T) {
value := nebula.Value{TVal: &nebula.Time{13, 12, 25, 29}}
valWrap := ValueWrapper{&value, testTimezone}
timezoneInfo := timezoneInfo{8 * 3600, []byte("+08:00")}
valWrap := ValueWrapper{&value, timezoneInfo}
assert.Equal(t, true, valWrap.IsTime())
assert.Equal(t, "13:12:25.000029", valWrap.String())
assert.Equal(t, "21:12:25.000029", valWrap.String())

// test timezone conversion
timeWrapper, err := valWrap.AsTime()
Expand Down

0 comments on commit a0577f6

Please sign in to comment.