-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
306 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "time" | ||
|
||
module LunchMoney | ||
module Validators | ||
include Kernel | ||
|
||
sig { params(value: String, valid_values: T::Array[String]).returns(String) } | ||
def validate_one_of!(value, valid_values) | ||
return value unless LunchMoney.validate_object_attributes? | ||
|
||
if valid_values.exclude?(value) | ||
raise(InvalidObjectAttribute, "#{value} is invalid, must be one of #{valid_values.join(", ")}") | ||
end | ||
|
||
value | ||
end | ||
|
||
sig { params(value: String).returns(String) } | ||
def validate_iso8601!(value) | ||
return value unless LunchMoney.validate_object_attributes? | ||
|
||
raise(InvalidObjectAttribute, "#{value} is not a valid ISO 8601 string") unless valid_iso8601_string?(value) | ||
|
||
value | ||
end | ||
|
||
private | ||
|
||
sig { params(time_string: String).returns(T::Boolean) } | ||
def valid_iso8601_string?(time_string) | ||
Time.iso8601(time_string) | ||
true | ||
rescue ArgumentError => error | ||
raise unless error.message.match?("invalid xmlschema format") | ||
|
||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module ConfigurationStubs | ||
private | ||
|
||
sig { void } | ||
def should_validate_object_attributes | ||
LunchMoney.configuration.expects(:validate_object_attributes).returns(true).at_least_once | ||
end | ||
|
||
sig { void } | ||
def should_not_validate_object_attributes | ||
LunchMoney.configuration.expects(:validate_object_attributes).returns(false).at_least_once | ||
end | ||
|
||
sig { void } | ||
def remove_validate_object_attributes_expectation | ||
LunchMoney.configuration.unstub(:validate_object_attributes) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class AssetTest < ActiveSupport::TestCase | ||
test "type_name can be set to known valid types" do | ||
LunchMoney::Asset::VALID_TYPE_NAMES.each do |type_name| | ||
assert_nothing_raised do | ||
create_asset(type_name:) | ||
end | ||
end | ||
end | ||
|
||
# test "type_name can not be set in an invalid type" do | ||
# error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
# create_asset(type_name: "invalid_type_name") | ||
# end | ||
|
||
# assert_match(/is invalid, must be one of/, error.message) | ||
# end | ||
|
||
# test "subtype_name can be set to known valid types" do | ||
# LunchMoney::Asset::VALID_SUBTYPE_NAMES.each do |subtype_name| | ||
# assert_nothing_raised do | ||
# create_asset(subtype_name:) | ||
# end | ||
# end | ||
# end | ||
|
||
# test "subtype_name can not be set in an invalid type" do | ||
# error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
# create_asset(subtype_name: "invalid_subtype_name") | ||
# end | ||
|
||
# assert_match(/is invalid, must be one of/, error.message) | ||
# end | ||
|
||
# test "balance_as_of can be set to a valid timestamp" do | ||
# assert_nothing_raised do | ||
# create_asset(balance_as_of: "2023-01-01T01:01:01.000Z") | ||
# end | ||
# end | ||
|
||
# test "balance_as_of can not be set to an invalid timestamp" do | ||
# error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
# create_asset(balance_as_of: "2023-01-01") | ||
# end | ||
|
||
# assert_match(/is not a valid ISO 8601 string/, error.message) | ||
# end | ||
|
||
# test "created_at can be set to a valid timestamp" do | ||
# assert_nothing_raised do | ||
# create_asset(created_at: "2023-01-01T01:01:01.000Z") | ||
# end | ||
# end | ||
|
||
# test "created_at can not be set to an invalid timestamp" do | ||
# error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
# create_asset(created_at: "2023-01-01") | ||
# end | ||
|
||
# assert_match(/is not a valid ISO 8601 string/, error.message) | ||
# end | ||
|
||
sig do | ||
params( | ||
type_name: String, | ||
subtype_name: String, | ||
balance_as_of: String, | ||
created_at: String, | ||
).returns(LunchMoney::Asset) | ||
end | ||
def create_asset(type_name: "cash", subtype_name: "retirement", balance_as_of: "2023-01-01T01:01:01.000Z", | ||
created_at: "2023-01-01T01:01:01.000Z") | ||
LunchMoney::Asset.new( | ||
"id": 1, | ||
"type_name": type_name, | ||
"subtype_name": subtype_name, | ||
"name": "Test Asset 1", | ||
"balance": "1201.0100", | ||
"balance_as_of": balance_as_of, | ||
"currency": "cad", | ||
"institution_name": "Bank of Me", | ||
"exclude_transactions": false, | ||
"created_at": created_at, | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class ValidatorsTest < ActiveSupport::TestCase | ||
include ConfigurationStubs | ||
include LunchMoney::Validators | ||
|
||
setup do | ||
should_validate_object_attributes | ||
end | ||
|
||
test "validate_one_of validates values if validate_object_attributes is enabled" do | ||
error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
validate_one_of!("bad_value", ["good_value"]) | ||
end | ||
|
||
assert_match(/is invalid, must be one of/, error.message) | ||
end | ||
|
||
test "validate_one_of does not validate values if validate_object_attributes is disabled" do | ||
remove_validate_object_attributes_expectation | ||
should_not_validate_object_attributes | ||
|
||
assert_nothing_raised do | ||
validate_one_of!("bad_value", ["good_value"]) | ||
end | ||
end | ||
|
||
test "validate_one_of does not raise an error when set to a valid value" do | ||
assert_nothing_raised do | ||
validate_one_of!("good_value", ["good_value"]) | ||
end | ||
end | ||
|
||
test "validate_one_of raises an error when set to an invalid value" do | ||
error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
validate_one_of!("bad_value", ["good_value"]) | ||
end | ||
|
||
assert_match(/is invalid, must be one of/, error.message) | ||
end | ||
|
||
test "validate_iso8601 validates time if validate_object_attributes is enabled" do | ||
error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
validate_iso8601!("2023-01-01") | ||
end | ||
|
||
assert_match(/is not a valid ISO 8601 string/, error.message) | ||
end | ||
|
||
test "validate_iso8601 does not validate values if validate_object_attributes is disabled" do | ||
remove_validate_object_attributes_expectation | ||
should_not_validate_object_attributes | ||
|
||
assert_nothing_raised do | ||
validate_iso8601!("2023-01-01") | ||
end | ||
end | ||
|
||
test "validate_iso8601 does not raise an error when set to a valid value" do | ||
assert_nothing_raised do | ||
validate_iso8601!("2023-01-01T01:01:01.000Z") | ||
end | ||
end | ||
|
||
test "validate_iso8601 raises an error when set to an invalid value" do | ||
error = assert_raises(LunchMoney::InvalidObjectAttribute) do | ||
validate_iso8601!("2023-01-01") | ||
end | ||
|
||
assert_match(/is not a valid ISO 8601 string/, error.message) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters