File tree Expand file tree Collapse file tree 3 files changed +122
-0
lines changed
Expand file tree Collapse file tree 3 files changed +122
-0
lines changed Original file line number Diff line number Diff line change 1+ require " ../pg_spec"
2+ require " ../../repository_spec"
3+
4+ describe " Repository(Postgres)#db=" do
5+ repo = repo(:postgresql )
6+
7+ context " with DB::Database" do
8+ it " sets the db to the given database" do
9+ old_db = repo.db
10+
11+ DB .open(ENV [" POSTGRESQL_URL" ]) do |db |
12+ repo.db = db
13+ new_db = repo.db
14+
15+ new_db.should_not be old_db
16+ new_db.should be_a(::DB ::Database )
17+ new_db.should be db
18+ end
19+
20+ repo.db = old_db
21+ end
22+ end
23+
24+ context " with DB::Connection" do
25+ it " sets the db to the given connection" do
26+ old_db = repo.db
27+
28+ DB .connect(ENV [" POSTGRESQL_URL" ]) do |connection |
29+ repo.db = connection
30+ new_db = repo.db
31+
32+ new_db.should_not be old_db
33+ new_db.should be_a(::DB ::Connection )
34+ new_db.should be connection
35+ end
36+
37+ repo.db = old_db
38+ end
39+ end
40+
41+ context " with DB::Transaction" do
42+ it " sets the db to the given transaction's connection" do
43+ old_db = repo.db
44+
45+ DB .open(ENV [" POSTGRESQL_URL" ]) do |db |
46+ db.transaction do |tx |
47+ repo.db = tx
48+ new_db = repo.db
49+
50+ new_db.should_not be old_db
51+ new_db.should be_a(::DB ::Connection )
52+ new_db.should be tx.connection
53+ end
54+ end
55+
56+ repo.db = old_db
57+ end
58+ end
59+ end
Original file line number Diff line number Diff line change 1+ require " ../sqlite3_spec"
2+ require " ../../repository_spec"
3+
4+ describe " Repository(Sqlite3)#db=" do
5+ repo = repo(:sqlite3 )
6+
7+ context " with DB::Database" do
8+ it " sets the db to the given database" do
9+ old_db = repo.db
10+
11+ DB .open(ENV [" SQLITE3_URL" ]) do |db |
12+ repo.db = db
13+ new_db = repo.db
14+
15+ new_db.should_not be old_db
16+ new_db.should be_a(::DB ::Database )
17+ new_db.should be db
18+ end
19+
20+ repo.db = old_db
21+ end
22+ end
23+
24+ context " with DB::Connection" do
25+ it " sets the db to the given connection" do
26+ old_db = repo.db
27+
28+ DB .connect(ENV [" SQLITE3_URL" ]) do |connection |
29+ repo.db = connection
30+ new_db = repo.db
31+
32+ new_db.should_not be old_db
33+ new_db.should be_a(::DB ::Connection )
34+ new_db.should be connection
35+ end
36+
37+ repo.db = old_db
38+ end
39+ end
40+
41+ context " with DB::Transaction" do
42+ it " sets the db to the given transaction's connection" do
43+ old_db = repo.db
44+
45+ DB .open(ENV [" SQLITE3_URL" ]) do |db |
46+ db.transaction do |tx |
47+ repo.db = tx
48+ new_db = repo.db
49+
50+ new_db.should_not be old_db
51+ new_db.should be_a(::DB ::Connection )
52+ new_db.should be tx.connection
53+ end
54+ end
55+
56+ repo.db = old_db
57+ end
58+ end
59+ end
Original file line number Diff line number Diff line change @@ -32,6 +32,10 @@ module Onyx::SQL
3232 def initialize (@db : ::DB ::Database | ::DB ::Connection , @logger : Logger = Logger ::Standard .new)
3333 end
3434
35+ def db = (transaction : ::DB ::Transaction )
36+ self .db = transaction.connection
37+ end
38+
3539 protected def postgresql?
3640 {% if Object .all_subclasses.any? { |sc | sc.stringify == " PG::Driver" } % }
3741 return db.is_a?(PG ::Driver )
You can’t perform that action at this time.
0 commit comments