Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date and datetime conversions #56

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
31 changes: 31 additions & 0 deletions lib/fulfil/conversions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Fulfil
class Conversions
swanny85 marked this conversation as resolved.
Show resolved Hide resolved
class << self
def update_date_and_datetime_fields(domain)
domain.each do |params|
params.map! do |param|
swanny85 marked this conversation as resolved.
Show resolved Hide resolved
case param
when Date
date_as_object(param)
when DateTime
datetime_as_object(param)
else
param
end
end
end
end

def datetime_as_object(datetime)
{
__class__: "datetime",
iso_string: datetime.new_offset(0).iso8601
}
end

def date_as_object(date)
datetime_as_object(date.to_datetime)
end
end
end
end
11 changes: 11 additions & 0 deletions lib/fulfil/model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'fulfil/query'
require 'fulfil/conversions'

module Fulfil
class Model
Expand Down Expand Up @@ -28,6 +29,8 @@ def search(
offset: nil,
sort: nil
)
domain = convert_date_and_datetime_fields(domain)

@client.search(
model: model,
domain: domain,
Expand All @@ -39,6 +42,8 @@ def search(
end

def count(domain:)
domain = convert_date_and_datetime_fields(domain)

@client.count(model: model_name, domain: domain)
end

Expand Down Expand Up @@ -84,5 +89,11 @@ def fetch_associated(models:, association_name:, source_key:, fields:)
end
end
end

private

def convert_date_and_datetime_fields(domain)
swanny85 marked this conversation as resolved.
Show resolved Hide resolved
Fulfil::Conversions.update_date_and_datetime_fields(domain)
end
end
end
77 changes: 77 additions & 0 deletions test/fulfil/client_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'test_helper'
require 'date'

module Fulfil
class ClientTest < Minitest::Test
Expand Down Expand Up @@ -68,5 +69,81 @@ def test_do_not_retry_when_retry_is_disabled
end
end
end

def test_date_conversion_in_search
date = Date.new(2022, 1, 1)
datetime = date.to_datetime
utc_datetime = datetime.new_offset(0)
expected_formatted_date = {
__class__: 'datetime',
iso_string: utc_datetime.iso8601
}

# Convert the expected hash's symbol keys to strings for comparison
expected_formatted_date_with_string_keys = expected_formatted_date.transform_keys(&:to_s)

stub_request(:put, fulfil_url_for('sale.sale/search_read'))
.to_return(status: 200, body: [].to_json, headers: { 'Content-Type' => 'application/json' })

client = Fulfil::Client.new
fulfil_model = Fulfil::Model.new(client: client, model_name: 'sale.sale')
fulfil_model.search(domain: [['create_date', '>=', date]])

assert_requested(:put, fulfil_url_for('sale.sale/search_read'), times: 1) do |request|
actual_formatted_date = JSON.parse(request.body).dig(0, 0, 2)

assert_equal expected_formatted_date_with_string_keys, actual_formatted_date
end
end

def test_datetime_conversion_in_search
datetime = DateTime.new(2022, 1, 1, 12, 0, 0)
utc_datetime = datetime.new_offset(0)
expected_formatted_date = {
__class__: 'datetime',
iso_string: utc_datetime.iso8601
}

# Convert the expected hash's symbol keys to strings for comparison
expected_formatted_date_with_string_keys = expected_formatted_date.transform_keys(&:to_s)

stub_request(:put, fulfil_url_for('sale.sale/search_read'))
.to_return(status: 200, body: [].to_json, headers: { 'Content-Type' => 'application/json' })

client = Fulfil::Client.new
fulfil_model = Fulfil::Model.new(client: client, model_name: 'sale.sale')
fulfil_model.search(domain: [['create_date', '>=', datetime]])

assert_requested(:put, fulfil_url_for('sale.sale/search_read'), times: 1) do |request|
actual_formatted_date = JSON.parse(request.body).dig(0, 0, 2)

assert_equal expected_formatted_date_with_string_keys, actual_formatted_date
end
end

def test_datetime_conversion_in_count
datetime = DateTime.new(2022, 1, 1, 12, 0, 0)
utc_datetime = datetime.new_offset(0)
expected_formatted_date = {
__class__: 'datetime',
iso_string: utc_datetime.iso8601
}

# Convert the expected hash's symbol keys to strings for comparison
expected_formatted_date_with_string_keys = expected_formatted_date.transform_keys(&:to_s)

stub_request(:put, fulfil_url_for('sale.sale/search_count'))
.to_return(status: 200, body: [].to_json, headers: { 'Content-Type' => 'application/json' })

client = Fulfil::Client.new
fulfil_model = Fulfil::Model.new(client: client, model_name: 'sale.sale')
fulfil_model.count(domain: [['create_date', '>=', datetime]])

assert_requested(:put, fulfil_url_for('sale.sale/search_count'), times: 1) do |request|
actual_formatted_date = JSON.parse(request.body).dig(0, 0, 2)

assert_equal expected_formatted_date_with_string_keys, actual_formatted_date
end
end
end
end
Loading