-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
primary auto: false
works as a natural key
- Loading branch information
Showing
11 changed files
with
187 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% for adapter in GraniteExample::ADAPTERS %} | ||
module {{adapter.capitalize.id}} | ||
describe "{{ adapter.id }} .new" do | ||
it "works when the primary is defined as `auto: true`" do | ||
Parent.new | ||
end | ||
|
||
it "works when the primary is defined as `auto: false`" do | ||
Kvs.new | ||
end | ||
end | ||
|
||
describe "{{ adapter.id }} .new(primary_key: value)" do | ||
it "ignores the value in default" do | ||
Parent.new(id: 1).id?.should eq(nil) | ||
end | ||
|
||
it "sets the value when the primary is defined as `auto: false`" do | ||
Kvs.new(k: "foo").k?.should eq("foo") | ||
end | ||
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,81 @@ | ||
require "../../spec_helper" | ||
|
||
{% for adapter in GraniteExample::ADAPTERS %} | ||
module {{adapter.capitalize.id}} | ||
describe "(Natural Key){{ adapter.id }} #save" do | ||
it "fails when a primary key is not set" do | ||
kv = Kvs.new | ||
kv.save.should be_false | ||
kv.errors.first.message.should eq "Primary key('k') cannot be null" | ||
end | ||
|
||
it "creates a new object when a primary key is given" do | ||
kv = Kvs.new | ||
kv.k = "foo" | ||
kv.save.should be_true | ||
|
||
kv = Kvs.find("foo").not_nil! | ||
kv.k.should eq("foo") | ||
end | ||
|
||
it "updates an existing object" do | ||
kv = Kvs.new | ||
kv.k = "foo" | ||
kv.v = "1" | ||
kv.save.should be_true | ||
|
||
kv.v = "2" | ||
kv.save.should be_true | ||
kv.k.should eq("foo") | ||
kv.v.should eq("2") | ||
end | ||
end | ||
|
||
describe "(Natural Key){{ adapter.id }} usecases" do | ||
it "CRUD" do | ||
Kvs.clear | ||
|
||
## Create | ||
port = Kvs.new(k: "mysql_port", v: "3306") | ||
port.new_record?.should be_true | ||
port.save.should be_true | ||
port.v.should eq("3306") | ||
Kvs.count.should eq(1) | ||
|
||
## Read | ||
port = Kvs.find("mysql_port") | ||
port.v.should eq("3306") | ||
port.new_record?.should be_false | ||
|
||
## Update | ||
port.v = "3307" | ||
port.new_record?.should be_false | ||
port.save.should be_true | ||
port.v.should eq("3307") | ||
Kvs.count.should eq(1) | ||
|
||
## Delete | ||
port.destroy.should be_true | ||
Kvs.count.should eq(0) | ||
end | ||
|
||
it "creates a new record twice" do | ||
Kvs.clear | ||
|
||
# create a new record | ||
port = Kvs.new(k: "mysql_port", v: "3306") | ||
port.new_record?.should be_true | ||
port.save.should be_true | ||
port.v.should eq("3306") | ||
Kvs.count.should eq(1) | ||
|
||
# create a new record again | ||
port = Kvs.new(k: "mysql_port", v: "3306") | ||
port.new_record?.should be_true | ||
port.save.should be_true | ||
port.v.should eq("3306") | ||
Kvs.count.should eq(2) | ||
end | ||
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
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
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