Skip to content

Commit

Permalink
Adding specs to confirm #906. Seems to be fine. Closes #906
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink committed Aug 5, 2023
1 parent d584864 commit f57d91c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions db/migrations/20230805212959_add_next_id_to_tokens.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddNextIdToTokens::V20230805212959 < Avram::Migrator::Migration::V1
def migrate
alter table_for(Token) do
add next_id : Int32, default: 0
end
end

def rollback
alter table_for(Token) do
remove :next_id
end
end
end
15 changes: 15 additions & 0 deletions spec/avram/operations/save_operation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ private class UpsertUserOperation < User::SaveOperation
upsert_lookup_columns :name, :nickname
end

private class UpsertToken < Token::SaveOperation
upsert_lookup_columns :next_id
end

private class OverrideDefaults < ModelWithDefaultValues::SaveOperation
permit_columns :greeting, :drafted_at, :published_at, :admin, :age, :money
end
Expand Down Expand Up @@ -234,6 +238,17 @@ describe "Avram::SaveOperation" do
end
end

it "updates the record using the same default value" do
TokenFactory.create(&.name("special").scopes(["name"]).next_id(4))

UpsertToken.upsert(next_id: 4, name: "Secret", scopes: ["red", "blue"]) do |op, token|
op.valid?.should eq(true)
token.should_not eq(nil)
token.as(Token).name.should eq("Secret")
token.as(Token).scopes.should eq(["red", "blue"])
end
end

it "creates a new record if match one doesn't exist" do
user_with_different_nickname =
UserFactory.create &.name("Rich").nickname(nil).age(20)
Expand Down
7 changes: 7 additions & 0 deletions spec/support/factories/token_factory.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class TokenFactory < BaseFactory
def initialize
name "Secret"
scopes ["email"]
next_id 0
end
end
1 change: 1 addition & 0 deletions spec/support/models/token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Token < BaseModel
table do
column name : String = "Secret"
column scopes : Array(String) = ["email"]
column next_id : Int32 = 0
end
end

Expand Down

0 comments on commit f57d91c

Please sign in to comment.