Files
simply_based/spec/include_relations_spec.rb
GregandClaude Opus 4.8 86299b8827 SimplyBased 0.1.0 — Couchbase-native ActiveModel ORM
A from-scratch fork of simply_couch's model layer that runs on Couchbase
natively: `include SimplyBased::Model` instead of `include SimplyCouch::Model`.

- SimplyBased::Store replaces the CouchDB layer: KV documents, N1QL/SQL++
  finders + lazily-created GSIs, CAS mapped onto _rev. No design documents or
  JavaScript map/reduce — the hand-written association views become N1QL
  (belongs_to/has_many/has_one scalar FK; habtm via ANY ... SATISFIES + array
  index, or KV multi-get on the storing side).
- Redis replacements on the same cluster: Counter (atomic increment/decrement),
  Cache (ActiveSupport::Cache::Store over KV+TTL, drop-in Rails.cache/ActiveJob),
  Lock (insert-if-absent + TTL + CAS-checked release).
- Runs the simply_couch test suite against live Couchbase: 378/432 applicable
  examples green (CouchDB-mechanic specs tagged :couchdb_only and excluded).
- Remaining: ancestry (tree) + embedded_in reverse-lookups (recursive/embedded
  N1QL) and a few pagination/lifecycle edges.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-20 13:58:47 -05:00

44 lines
1.5 KiB
Ruby

require 'spec_helper'
describe "Include relation, aggregate many queries to one simplified one for performance" do
context "initialized posts" do
before do
@user = User.create :title => 'Include relation posts user'
@post1 = Post.create(:user => @user)
@post2 = Post.create(:user => @user)
@user.reload
@post1 = Post.find(@post1.id)
@post2 = Post.find(@post2.id)
end
it "have proper relations without include_relation" do
@user.posts.should match_array [@post1, @post2]
end
it "have proper relations with include_relation" do
[@user].include_relation(:posts)
#assert_equal [@post1, @post2].sort_by(&:id), @user.posts.sort_by(&:id)
@user.posts.should match_array [@post1, @post2]
end
it "have proper relations with include_relation called twice" do
[@user].include_relation(:posts)
[@user].include_relation(:posts)
@user.posts.should match_array [@post1, @post2]
end
end
context '...' do
it "includes has_many relations and sets the reverse relation on has many" do
user1 = User.create title: "User1"
user2 = User.create title: "User2"
Post.create user: user1
Post.create user: user1
Post.create user: user2
Post.create user: user2
users = nil
expect{ users = User.all.include_relation(:posts) }.not_to exceed_query_limit 2 # users.all and adding posts to both users
expect{ users.map(&:posts).flatten.map(&:user) }.not_to perform_any_queries # reverse relation user must be set on each post
end
end
end