| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 | module ReferentialHelper
  def first_referential
    Referential.find_by!(:slug => "first")
  end
  def first_organisation
    Organisation.find_by!(code: "first")
  end
  def self.included(base)
    base.class_eval do
      extend ClassMethods
      alias_method :referential, :first_referential
      alias_method :organisation, :first_organisation
    end
  end
  module ClassMethods
    def assign_referential
      before(:each) do
        assign :referential, referential
      end
    end
    def assign_organisation
      before(:each) do
        assign :organisation, referential.organisation
      end
    end
  end
end
RSpec.configure do |config|
  config.include ReferentialHelper
  config.before(:suite) do
    # Clean all tables to start
    DatabaseCleaner.clean_with :truncation, except: %w[spatial_ref_sys]
    # Truncating doesn't drop schemas, ensure we're clean here, first *may not* exist
    Apartment::Tenant.drop('first') rescue nil
    # Create the default tenant for our tests
    organisation = Organisation.create!(code: "first", name: "first")
    line_referential = LineReferential.find_or_create_by(name: "first") do |referential|
      referential.objectid_format = "stif_codifligne"
      referential.add_member organisation, owner: true
    end
    stop_area_referential = StopAreaReferential.find_or_create_by(name: "first") do |referential|
      referential.objectid_format = "stif_reflex"
      referential.add_member organisation, owner: true
    end
    workbench = FactoryGirl.create(
      :workbench,
      name: "Gestion de l'offre",
      organisation: organisation,
      line_referential: line_referential,
      stop_area_referential: stop_area_referential
    )
    referential = FactoryGirl.create(
      :referential,
      prefix: "first",
      name: "first",
      slug: "first",
      organisation: organisation,
      workbench: workbench,
      objectid_format: "stif_netex"
    )
  end
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    # Switch into the default tenant
    first_referential.switch
  end
  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation, { except: %w[spatial_ref_sys] }
  end
  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    # Reset tenant back to `public`
    Apartment::Tenant.reset
    # Rollback transaction
    DatabaseCleaner.clean
  end
end
 |