Skip to content

Commit

Permalink
partial implementation of testkit tests for temporal types
Browse files Browse the repository at this point in the history
  • Loading branch information
klobuczek committed Oct 8, 2023
1 parent 4f09f1c commit a03e1a5
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
ruby: [ jruby, ruby ]
neo4j: [ 4.4.20, 5.7.0 ]
neo4j: [ 4.4.26, 5.12.0 ]
include:
- ruby: jruby
java-version: 17
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ HOE = Class.new(Hoe) do
end.spec 'neo4j-ruby-driver' do
developer 'Heinrich Klobuczek', '[email protected]'

dependency 'activesupport', '>= 0'
dependency 'activesupport', '< 7.1'
# dependency 'async-rspec', '>= 0', :dev
dependency 'ffaker', '>= 0', :dev
dependency 'hoe', '>= 0', :dev
Expand All @@ -41,7 +41,7 @@ end.spec 'neo4j-ruby-driver' do
dependency 'jar-dependencies', '>= 0.4.1'
dependency 'ruby-maven', '>= 0', :dev

spec_extras[:requirements] = ->(requirements) { requirements << 'jar org.neo4j.driver, neo4j-java-driver-all, 5.8.0' }
spec_extras[:requirements] = ->(requirements) { requirements << 'jar org.neo4j.driver, neo4j-java-driver-all, 5.13.0' }
spec_extras[:platform] = 'java'
else
require_ruby_version '>= 3.1'
Expand Down
2 changes: 1 addition & 1 deletion jruby/neo4j/driver/ext/ruby_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def as_ruby_object
date = as_local_date
Date.new(date.year, date.month_value, date.day_of_month)
when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::DURATION
ActiveSupport::Duration.parse(as_iso_duration.to_s)
Driver::Internal::DurationNormalizer.create(*%i[months days seconds nanoseconds].map(&as_object.method(:send)))
when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::POINT
point = as_point
Types::Point.new(srid: point.srid, x: point.x, y: point.y, z: nullable(point.z))
Expand Down
5 changes: 5 additions & 0 deletions lib/neo4j/driver/internal/duration_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def milliseconds(duration)
duration&.in_milliseconds&.round
end

def create(months, days, seconds, nanoseconds)
{ months:, days:, seconds: seconds + (nanoseconds.zero? ? 0 : nanoseconds * BigDecimal('1e-9')) }
.sum { |key, value| ActiveSupport::Duration.send(key, value) }
end

private

def divmod(number, factor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ def unpack_date_time_with_zone_id
end

def unpack_duration
ActiveSupport::Duration.months(unpack) +
ActiveSupport::Duration.days(unpack) +
ActiveSupport::Duration.seconds(unpack) +
ActiveSupport::Duration.seconds(unpack * BigDecimal('1e-9'))
DurationNormalizer.create(*4.times.map { unpack })
end

def unpack_point2_d
Expand Down
2 changes: 1 addition & 1 deletion testkit-backend/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install
bundle update

# Do any other automated setup that you need to do here
1 change: 1 addition & 0 deletions testkit-backend/lib/testkit/backend.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "active_support/core_ext/module/attribute_accessors"
require 'active_support/inflector'
require 'async/io'
require 'bigdecimal'
require 'neo4j/driver'
require 'nio'
require 'testkit/backend/loader'
Expand Down
18 changes: 18 additions & 0 deletions testkit-backend/lib/testkit/backend/messages/conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def to_testkit(object)
else
value_entity('CypherString', object)
end
when Date
named_entity('CypherDate', **map_of(object, :year, :month, :day))
when Neo4j::Driver::Types::OffsetTime
named_entity('CypherTime', hour: object.hour, minute: object.min, second: object.sec, nanosecond: object.nsec, 'utc_offset_s' => object.utc_offset)
when Neo4j::Driver::Types::LocalTime
named_entity('CypherTime', hour: object.hour, minute: object.min, second: object.sec, nanosecond: object.nsec)
when Neo4j::Driver::Types::LocalDateTime
named_entity('CypherDateTime', **map_of(object, :year, :month, :day, :hour), minute: object.min, second: object.sec, nanosecond: object.nsec)
when Time
named_entity('CypherDateTime', **map_of(object, :year, :month, :day, :hour), minute: object.min, second: object.sec, nanosecond: object.nsec, 'utc_offset_s' => object.utc_offset, 'timezone_id' => object.try(:time_zone)&.name)
when ActiveSupport::Duration
named_entity('CypherDuration', **%i[months days seconds nanoseconds].zip(Neo4j::Driver::Internal::DurationNormalizer.normalize(object)).to_h)
when Symbol
to_testkit(object.to_s)
when Neo4j::Driver::Types::Path
Expand Down Expand Up @@ -50,6 +62,12 @@ def float_encode(f)
f
end
end

private

def map_of(object, *keys)
keys.to_h { |key| [key, object.send(key)] }
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion testkit-backend/lib/testkit/backend/messages/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def reference(name)

def named_entity(name, **hash)
{ name: name }.tap do |entity|
entity[:data] = hash.transform_keys { |key| key.to_s.camelize(:lower) } unless hash.empty?
entity[:data] = hash.transform_keys { |key| key.is_a?(String) ? key : key.to_s.camelize(:lower) } unless hash.empty?
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Testkit::Backend::Messages
module Requests
class CypherDate < Request
def to_object
Date.new(year, month, day)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Testkit::Backend::Messages
module Requests
class CypherDateTime < Request
def to_object
if timezone_id
Time.new(year, month, day, hour, minute, second + nanosecond * 1e-9, utc_offset_s).in_time_zone(TZInfo::Timezone.get(timezone_id))
elsif utc_offset_s
Time.new(year, month, day, hour, minute, second + nanosecond * 1e-9, utc_offset_s)
else
Neo4j::Driver::Types::LocalDateTime.new(Time.new(year, month, day, hour, minute, second + nanosecond * 1e-9))
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Testkit::Backend::Messages
module Requests
class CypherDuration < Request
def to_object
Neo4j::Driver::Internal::DurationNormalizer.create(months, days, seconds, nanoseconds)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Testkit::Backend::Messages
module Requests
class CypherTime < Request
def to_object
if utc_offset_s
Neo4j::Driver::Types::OffsetTime.new(Time.new(1, 1, 1, hour, minute, second + nanosecond * 1e-9, utc_offset_s))
else
Neo4j::Driver::Types::LocalTime.new(Time.new(1, 1, 1, hour, minute, second + nanosecond * 1e-9))
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GetFeatures < Request
'Feature:API:SSLConfig' => 'ja',
'Feature:API:SSLSchemes' => 'ja',
'Feature:API:Type.Spatial' => '',
'Feature:API:Type.Temporal' => 'a',
'Feature:API:Type.Temporal' => 'a', # Most tests pass. Implement SubTests to skip some e.g. old System timezone
'Feature:Auth:Bearer' => 'jar',
'Feature:Auth:Custom' => 'jar',
'Feature:Auth:Kerberos' => 'jar',
Expand Down
2 changes: 1 addition & 1 deletion testkit-backend/testkit-backend.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'async-io'
spec.add_dependency 'nio4r'
spec.add_dependency 'zeitwerk'
spec.add_dependency 'activesupport'
spec.add_dependency 'activesupport', '< 7.1'
spec.add_development_dependency 'rspec'
# spec.add_development_dependency "async-rspec", "~> 1.10"
end

0 comments on commit a03e1a5

Please sign in to comment.