|
| 1 | +require 'database_cleaner/sequel/transaction' |
| 2 | +require 'database_cleaner/spec' |
| 3 | +require 'sequel' |
| 4 | +require 'database_cleaner/spec/database_helper' |
| 5 | +require 'ostruct' |
| 6 | + |
| 7 | +if RUBY_PLATFORM == 'java' |
| 8 | + require_relative '../../support/postgresql-42.3.0.jar' |
| 9 | + |
| 10 | + class SequelHelper < DatabaseCleaner::Spec::DatabaseHelper |
| 11 | + private |
| 12 | + |
| 13 | + def establish_connection(_config = default_config) |
| 14 | + @connection = ::Sequel.connect( |
| 15 | + # 'postgres://database_cleaner:database_cleaner@127.0.0.1:5433/database_cleaner_test' |
| 16 | + 'jdbc:postgresql://127.0.0.1:5433/database_cleaner_test?user=database_cleaner&password=database_cleaner' |
| 17 | + ) |
| 18 | + end |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +module DatabaseCleaner |
| 23 | + module Sequel |
| 24 | + RSpec.describe Transaction, :with_jdbc do |
| 25 | + it_should_behave_like "a database_cleaner strategy" |
| 26 | + |
| 27 | + SequelHelper.with_all_dbs do |helper| |
| 28 | + next unless helper.db == :postgres |
| 29 | + |
| 30 | + context "using a #{helper.db} connection" do |
| 31 | + around do |example| |
| 32 | + helper.setup |
| 33 | + example.run |
| 34 | + helper.teardown |
| 35 | + end |
| 36 | + |
| 37 | + let(:connection) { helper.connection } |
| 38 | + |
| 39 | + before { subject.db = connection } |
| 40 | + |
| 41 | + context "cleaning" do |
| 42 | + it "should clean database" do |
| 43 | + subject.cleaning do |
| 44 | + connection[:users].insert |
| 45 | + expect(connection[:users].count).to eq(1) |
| 46 | + end |
| 47 | + |
| 48 | + expect(connection[:users]).to be_empty |
| 49 | + end |
| 50 | + |
| 51 | + it "should work with nested transaction" do |
| 52 | + subject.cleaning do |
| 53 | + begin |
| 54 | + connection.transaction do |
| 55 | + connection[:users].insert |
| 56 | + connection[:users].insert |
| 57 | + |
| 58 | + expect(connection[:users].count).to eq(2) |
| 59 | + raise ::Sequel::Rollback |
| 60 | + end |
| 61 | + rescue |
| 62 | + end |
| 63 | + |
| 64 | + connection[:users].insert |
| 65 | + expect(connection[:users].count).to eq(1) |
| 66 | + end |
| 67 | + |
| 68 | + expect(connection[:users]).to be_empty |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context "start/clean" do |
| 73 | + it "should clean database" do |
| 74 | + subject.start |
| 75 | + |
| 76 | + connection[:users].insert |
| 77 | + expect(connection[:users].count).to eq(1) |
| 78 | + |
| 79 | + subject.clean |
| 80 | + |
| 81 | + expect(connection[:users]).to be_empty |
| 82 | + end |
| 83 | + |
| 84 | + it "should work with nested transaction" do |
| 85 | + subject.start |
| 86 | + begin |
| 87 | + connection.transaction do |
| 88 | + connection[:users].insert |
| 89 | + connection[:users].insert |
| 90 | + |
| 91 | + expect(connection[:users].count).to eq(2) |
| 92 | + raise ::Sequel::Rollback |
| 93 | + end |
| 94 | + rescue |
| 95 | + end |
| 96 | + |
| 97 | + connection[:users].insert |
| 98 | + expect(connection[:users].count).to eq(1) |
| 99 | + |
| 100 | + subject.clean |
| 101 | + |
| 102 | + expect(connection[:users]).to be_empty |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + describe "start" do |
| 109 | + it "should start a transaction" |
| 110 | + end |
| 111 | + |
| 112 | + describe "clean" do |
| 113 | + it "should finish a transaction" |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments