markbates / mack-more

All the extra stuff you could want for the Mack Framework.

This URL has Read+Write access

mack-more / mack-active_record / spec / lib / db_structure_spec.rb
100644 146 lines (116 sloc) 3.836 kb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
 
describe Mack::Database do
  
  class House < ActiveRecord::Base
    # include DataMapper::Resource
    #
    # property :id, Serial
    # property :color, String
    # property :address, Text, :nullable => true
  end
  
  class Cottage < ActiveRecord::Base
    # include DataMapper::Resource
    #
    # property :id, Serial
    # property :created_at, DateTime
  end
  
  class HouseMigration < ActiveRecord::Migration
    def self.up
      create_table :houses do |t|
        t.column :color, :string
        t.column :address, :text
      end
    end
    def self.down
      drop_table :houses
    end
  end
  
  class CottageMigration < ActiveRecord::Migration
    def self.up
      create_table :cottages do |t|
      end
    end
    def self.down
      drop_table :cottages
    end
  end
  
  before(:each) do
    @dump = File.join(Mack.root, "db", "development_schema_structure.sql")
  end
  
  after(:each) do
    FileUtils.rm_rf(@dump)
  end
  
  describe "load_structure" do
    
    describe "MySQL" do
      include Spec::CreateAndDropTask::Helper::MySQL
      
      it "should reconstructe a db from a .sql file" do
        config_db(:mysql) do
          Mack::Database.recreate
          Mack::Database.establish_connection(Mack.env)
          House.should_not be_table_exists
          Cottage.should_not be_table_exists
          Mack::Database.load_structure(fixture_location("mysql_schema_structure.sql"), "test")
          House.should be_table_exists
          Cottage.should be_table_exists
        end
      end
      
    end
    
    describe "Postgres" do
      include Spec::CreateAndDropTask::Helper::PostgreSQL
      
      it "should reconstructe a db from a .sql file" do
        config_db(:postgresql) do
          Mack::Database.recreate("development")
          Mack::Database.recreate("test")
          Mack::Database.establish_connection(Mack.env)
          House.should_not be_table_exists
          Cottage.should_not be_table_exists
          Mack::Database.load_structure(fixture_location("postgres_schema_structure.sql"), "test")
          House.should be_table_exists
          Cottage.should be_table_exists
        end
      end
      
    end
    
    describe "SQLite3" do
      
      it "should reconstructe a db from a .sql file" do
        pending("All yours Darsono")
      end
      
    end
    
  end
  
  describe "dump_structure" do
  
    describe "MySQL" do
      include Spec::CreateAndDropTask::Helper::MySQL
            
      it "should write a .sql that represents the db structure" do
        config_db(:mysql) do
          Mack::Database.recreate
          Mack::Database.establish_connection(Mack.env)
          HouseMigration.up
          CottageMigration.up
          File.should_not be_exists(@dump)
          Mack::Database.dump_structure(Mack.env)
          File.should be_exists(@dump)
          File.read(@dump).should == fixture("mysql_schema_structure.sql")
        end
      end
    
    end
      
    describe "Postgres" do
      include Spec::CreateAndDropTask::Helper::PostgreSQL
      
      it "should write a .sql that represents the db structure" do
        config_db(:postgresql) do
          Mack::Database.recreate
          Mack::Database.establish_connection(Mack.env)
          HouseMigration.up
          CottageMigration.up
          File.should_not be_exists(@dump)
          Mack::Database.dump_structure(Mack.env)
          File.should be_exists(@dump)
          File.read(@dump).should == fixture("postgres_schema_structure.sql")
        end
      end
    
    end
      
    describe "SQLite3" do
    
      it "should write a .sql that represents the db structure" do
        pending("All yours Darsono")
      end
    
    end
    
  end
  
end