spec/integration/relation_schema_spec.rb in rom-sql-1.3.5 vs spec/integration/relation_schema_spec.rb in rom-sql-2.0.0.beta1
- old
+ new
@@ -17,22 +17,46 @@
expect { container.users }.to raise_error(NoMethodError)
end
end
context 'defining associations', seeds: false do
+ let(:config) { TestConfiguration.new(:sql, conn) }
+ let(:container) { ROM.container(config) }
+
+ let(:user_associations) do
+ config.relation(:accounts) { schema(infer: true) }
+ config.relation(:cards) { schema(infer: true) }
+ config.register_relation(Test::Users)
+ container.relations[:users].associations
+ end
+
+ let(:post_associations) do
+ config.relation(:tags) { schema(infer: true) }
+ config.relation(:posts_tags) { schema(infer: true) }
+ config.register_relation(Test::Posts)
+ container.relations[:posts].associations
+ end
+
+ let(:tag_associations) do
+ config.relation(:posts) { schema(infer: true) }
+ config.relation(:posts_tags) { schema(infer: true) }
+ config.register_relation(Test::Tags)
+ container.relations[:tags].associations
+ end
+
it "allows defining a one-to-many" do
class Test::Posts < ROM::Relation[:sql]
schema(:posts) do
associations do
one_to_many :tags
end
end
end
- assoc = ROM::SQL::Association::OneToMany.new(:posts, :tags)
+ assoc = ROM::Associations::Definitions::OneToMany.new(:posts, :tags)
- expect(Test::Posts.associations[:tags]).to eql(assoc)
+ expect(post_associations[:tags].definition).to eql(assoc)
end
it "allows defining a one-to-many using has_many shortcut" do
class Test::Posts < ROM::Relation[:sql]
schema(:posts) do
@@ -40,13 +64,13 @@
has_many :tags
end
end
end
- assoc = ROM::SQL::Association::OneToMany.new(:posts, :tags)
+ assoc = ROM::Associations::Definitions::OneToMany.new(:posts, :tags)
- expect(Test::Posts.associations[:tags]).to eql(assoc)
+ expect(post_associations[:tags].definition).to eql(assoc)
end
it "allows defining a one-to-one" do
class Test::Users < ROM::Relation[:sql]
schema(:users) do
@@ -54,13 +78,13 @@
one_to_one :accounts
end
end
end
- assoc = ROM::SQL::Association::OneToOne.new(:users, :accounts)
+ assoc = ROM::Associations::Definitions::OneToOne.new(:users, :accounts)
- expect(Test::Users.associations[:accounts]).to eql(assoc)
+ expect(user_associations[:accounts].definition).to eql(assoc)
end
it "allows defining a one-to-one using has_one shortcut" do
class Test::Users < ROM::Relation[:sql]
schema(:users) do
@@ -68,14 +92,14 @@
has_one :account
end
end
end
- assoc = ROM::SQL::Association::OneToOne.new(:users, :accounts, as: :account)
+ assoc = ROM::Associations::Definitions::OneToOne.new(:users, :accounts, as: :account)
- expect(Test::Users.associations[:account]).to eql(assoc)
- expect(Test::Users.associations[:account].target).to be_aliased
+ expect(user_associations[:account].definition).to eql(assoc)
+ expect(user_associations[:account].definition.target).to be_aliased
end
it "allows defining a one-to-one using has_one shortcut with an alias" do
class Test::Users < ROM::Relation[:sql]
schema(:users) do
@@ -83,14 +107,14 @@
has_one :account, as: :user_account
end
end
end
- assoc = ROM::SQL::Association::OneToOne.new(:users, :accounts, as: :user_account)
+ assoc = ROM::Associations::Definitions::OneToOne.new(:users, :accounts, as: :user_account)
- expect(Test::Users.associations[:user_account]).to eql(assoc)
- expect(Test::Users.associations[:user_account].target).to be_aliased
+ expect(user_associations[:user_account].definition).to eql(assoc)
+ expect(user_associations[:user_account].definition.target).to be_aliased
end
it "allows defining a one-to-one-through" do
class Test::Users < ROM::Relation[:sql]
schema(:users) do
@@ -98,13 +122,13 @@
one_to_one :cards, through: :accounts
end
end
end
- assoc = ROM::SQL::Association::OneToOneThrough.new(:users, :cards, through: :accounts)
+ assoc = ROM::Associations::Definitions::OneToOneThrough.new(:users, :cards, through: :accounts)
- expect(Test::Users.associations[:cards]).to eql(assoc)
+ expect(user_associations[:cards].definition).to eql(assoc)
end
it "allows defining a many-to-one" do
class Test::Tags < ROM::Relation[:sql]
schema(:tags) do
@@ -112,13 +136,13 @@
many_to_one :posts
end
end
end
- assoc = ROM::SQL::Association::ManyToOne.new(:tags, :posts)
+ assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts)
- expect(Test::Tags.associations[:posts]).to eql(assoc)
+ expect(tag_associations[:posts].definition).to eql(assoc)
end
it "allows defining a many-to-one using belongs_to shortcut" do
class Test::Tags < ROM::Relation[:sql]
schema(:tags) do
@@ -126,13 +150,13 @@
belongs_to :post
end
end
end
- assoc = ROM::SQL::Association::ManyToOne.new(:tags, :posts, as: :post)
+ assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts, as: :post)
- expect(Test::Tags.associations[:post]).to eql(assoc)
+ expect(tag_associations[:post].definition).to eql(assoc)
end
it "allows defining a many-to-one using belongs_to shortcut" do
class Test::Tags < ROM::Relation[:sql]
schema(:tags) do
@@ -140,13 +164,13 @@
belongs_to :post, as: :post_tag
end
end
end
- assoc = ROM::SQL::Association::ManyToOne.new(:tags, :posts, as: :post_tag)
+ assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts, as: :post_tag)
- expect(Test::Tags.associations[:post_tag]).to eql(assoc)
+ expect(tag_associations[:post_tag].definition).to eql(assoc)
end
it "allows defining a many-to-many" do
class Test::Posts < ROM::Relation[:sql]
schema(:posts) do
@@ -154,26 +178,26 @@
one_to_many :tags, through: :posts_tags
end
end
end
- assoc = ROM::SQL::Association::ManyToMany.new(:posts, :tags, through: :posts_tags)
+ assoc = ROM::Associations::Definitions::ManyToMany.new(:posts, :tags, through: :posts_tags)
- expect(Test::Posts.associations[:tags]).to eql(assoc)
+ expect(post_associations[:tags].definition).to eql(assoc)
end
it "allows defining a many-to-one with a custom name" do
class Test::Tags < ROM::Relation[:sql]
schema(:tags) do
associations do
- many_to_one :published_posts, relation: :posts
+ many_to_one :posts, as: :published_posts
end
end
end
- assoc = ROM::SQL::Association::ManyToOne.new(:tags, :published_posts, relation: :posts)
+ assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts, as: :published_posts)
- expect(Test::Tags.associations[:published_posts]).to eql(assoc)
+ expect(tag_associations[:published_posts].definition).to eql(assoc)
end
end
end
end