ackbar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,169 @@
1
+ require 'test_helper'
2
+
3
+ class KBSchemaTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ # recreate_kb_database(false)
7
+ @adapter = ActiveRecord::Base.connection
8
+ end
9
+
10
+ def tear_down
11
+ end
12
+
13
+ def test_migrated_schema
14
+ # load the schme.rb file and migration files
15
+ recreate_kb_database(true)
16
+
17
+ # check that all tables are in:
18
+ assert_equal [:authors, :authors_books, :books, :errata, :pages, :primary_key_tests, :publishers, :schema_info],
19
+ $db.tables.sort_by{|t| t.to_s }
20
+ assert_equal $db.tables.map{|t| t.to_s}, @adapter.tables
21
+
22
+ # check books table
23
+ assert_equal [:recno, :name, :published, :publisher_id], $db.get_table(:books).field_names
24
+ assert_equal [:Integer, :String, :Date, :Integer ], $db.get_table(:books).field_types
25
+ assert_equal [nil, "Index->1", nil, nil ], $db.get_table(:books).field_indexes
26
+ assert_equal [false, true, true, false ], $db.get_table(:books).field_requireds
27
+ assert_equal [nil, nil, nil, nil ], $db.get_table(:books).field_defaults
28
+ assert_equal [{}, {}, {}, {} ], $db.get_table(:books).field_extras
29
+
30
+ # check authors table
31
+ assert_equal [:recno, :name ], $db.get_table(:authors).field_names
32
+ assert_equal [:Integer, :String ], $db.get_table(:authors).field_types
33
+ assert_equal [nil, "Index->1"], $db.get_table(:authors).field_indexes
34
+ assert_equal [false, true ], $db.get_table(:authors).field_requireds
35
+ assert_equal [nil, nil ], $db.get_table(:authors).field_defaults
36
+ assert_equal [{}, {} ], $db.get_table(:authors).field_extras
37
+
38
+ # check authors_books table
39
+ assert_equal [:recno, :author_id, :book_id], $db.get_table(:authors_books).field_names
40
+ assert_equal [:Integer, :Integer, :Integer], $db.get_table(:authors_books).field_types
41
+ assert_equal [nil, nil, nil ], $db.get_table(:authors_books).field_indexes
42
+ assert_equal [false, false, false ], $db.get_table(:authors_books).field_requireds
43
+ assert_equal [nil, nil, nil ], $db.get_table(:authors_books).field_defaults
44
+ assert_equal [{}, {}, {} ], $db.get_table(:authors_books).field_extras
45
+
46
+ # check pages table
47
+ assert_equal [:recno, :book_id, :page_num, :content], $db.get_table(:pages).field_names
48
+ assert_equal [:Integer, :Integer, :Integer, :String ], $db.get_table(:pages).field_types
49
+ assert_equal [nil, nil, nil, nil ], $db.get_table(:pages).field_indexes
50
+ assert_equal [false, false, false, false ], $db.get_table(:pages).field_requireds
51
+ assert_equal [nil, nil, nil, nil ], $db.get_table(:pages).field_defaults
52
+ assert_equal [{}, {}, {}, {} ], $db.get_table(:pages).field_extras
53
+
54
+ # check publishers table
55
+ assert_equal [:recno, :name, :address], $db.get_table(:publishers).field_names
56
+ assert_equal [:Integer, :String, :String ], $db.get_table(:publishers).field_types
57
+ assert_equal [nil, "Index->1", nil ], $db.get_table(:publishers).field_indexes
58
+ assert_equal [false, false, false ], $db.get_table(:publishers).field_requireds
59
+ assert_equal [nil, nil, nil ], $db.get_table(:publishers).field_defaults
60
+ assert_equal [{}, {}, {} ], $db.get_table(:publishers).field_extras
61
+
62
+ # check errata table
63
+ assert_equal [:recno, :book_id, :contents], $db.get_table(:errata).field_names
64
+ assert_equal [:Integer, :Integer, :String ], $db.get_table(:errata).field_types
65
+ assert_equal [nil, nil, nil ], $db.get_table(:errata).field_indexes
66
+ assert_equal [false, false, false ], $db.get_table(:errata).field_requireds
67
+ assert_equal [nil, nil, nil ], $db.get_table(:errata).field_defaults
68
+ assert_equal [{}, {}, {} ], $db.get_table(:errata).field_extras
69
+ end
70
+
71
+ def x_test_create_table
72
+ flunk
73
+ end
74
+
75
+ def x_test_rename_table
76
+ flunk
77
+ end
78
+
79
+ def x_test_indexes
80
+ breakpoint
81
+ i = ActiveRecord::ConnectionAdapters::IndexDefinition.new(:authors, '1', true, [:name])
82
+ assert_equal [i], Author.indexes
83
+ i = ActiveRecord::ConnectionAdapters::IndexDefinition.new(:books, '1', true, [:name])
84
+ assert_equal [i], Book.indexes
85
+ assert_equal [], Page.indexes
86
+ i = ActiveRecord::ConnectionAdapters::IndexDefinition.new(:publishers, '1', true, [:name])
87
+ assert_equal [], Publisher.indexes
88
+ end
89
+
90
+ def x_test_drop_table
91
+ flunk
92
+ end
93
+
94
+ def x_test_add_column
95
+ flunk
96
+ end
97
+
98
+ def x_test_change_column
99
+ flunk
100
+ end
101
+
102
+ def x_test_change_column_default
103
+ flunk
104
+ end
105
+
106
+ def x_test_rename_column
107
+ flunk
108
+ end
109
+
110
+ def x_test_remove_column
111
+ flunk
112
+ end
113
+
114
+ def x_test_add_index
115
+ flunk
116
+ end
117
+
118
+ def x_test_remove_index
119
+ flunk
120
+ end
121
+
122
+ def x_test_tables
123
+ flunk
124
+ end
125
+
126
+ def test_columns
127
+ columns = Book.columns
128
+
129
+ assert_equal 4, columns.size
130
+ assert_equal ["id", "name", "published", "publisher_id"], columns.map {|col| col.name }
131
+ assert_equal [:integer, :string, :date, :integer ], columns.map {|col| col.type }
132
+ assert_equal [true, false, false, true ], columns.map {|col| col.null }
133
+ assert_equal [nil, nil, nil, nil ], columns.map {|col| col.default }
134
+
135
+ assert !columns[0].text?
136
+ assert columns[1].text?
137
+ assert !columns[2].text?
138
+ assert !columns[3].text?
139
+
140
+ assert columns.all? { |col| col.default.nil? }
141
+
142
+ assert columns[0].number?
143
+ assert !columns[1].number?
144
+ assert !columns[2].number?
145
+ assert columns[3].number?
146
+ end
147
+
148
+ def test_indexes
149
+ @adapter.create_table "index_tests", :force => true do |t|
150
+ t.column "indy_1", :integer
151
+ t.column "indy_2", :string
152
+ t.column "indy_3", :text
153
+ end
154
+ assert_nothing_raised { @adapter.add_index "index_tests", ["indy_1"], :name => "names_are_of_no_consequence" }
155
+ assert_nothing_raised { @adapter.add_index "index_tests", ["indy_2", "indy_3"], :name => "names_are_of_no_consequence_2" }
156
+ indices = @adapter.indexes("index_tests")
157
+ assert_equal 2, indices.size
158
+ assert_equal [["indy_1"], ["indy_2", "indy_3"]], indices.map{|ind| ind.columns}.sort
159
+ assert_equal ["index_tests_indy_1_index", "index_tests_indy_2_index"], indices.map{|ind| ind.name}.sort
160
+ end
161
+
162
+ def test_primary_key
163
+ pak = PrimaryKeyTest.create :name => 'first'
164
+ assert_equal 1, pak.id
165
+ assert_equal 1, pak.pk
166
+
167
+ end
168
+
169
+ end
@@ -0,0 +1,79 @@
1
+ require 'test_helper'
2
+
3
+ ## This class checks only the translate_sql_to_code method
4
+ class KBSqlToCodeTest < Test::Unit::TestCase
5
+
6
+ def test_basic
7
+ assert_equal "rec.name == ?", Book.translate_sql_to_code("name = ?")
8
+ assert_equal "rec.name == 'Andy Hunt'", Book.translate_sql_to_code("name = 'Andy Hunt'")
9
+ assert_equal "rec.parent_id == 1", Book.translate_sql_to_code("parent_id = 1")
10
+ assert_equal "rec.name == ?", Book.translate_sql_to_code("name = ?")
11
+ assert_equal "rec.name == ?", Book.translate_sql_to_code("name = :name")
12
+ assert_equal "rec.name == ?", Book.translate_sql_to_code("name = '%s'")
13
+ assert_equal "true", Book.translate_sql_to_code("1 = 1")
14
+ assert_equal "rec.type == 'Client'", Book.translate_sql_to_code("type = 'Client'")
15
+ end
16
+
17
+ def test_comparison_operators
18
+ assert_equal "rec.salary > 90000", Book.translate_sql_to_code("salary > 90000")
19
+ assert_equal "rec.salary < 90000", Book.translate_sql_to_code("salary < 90000")
20
+ assert_equal "rec.salary >= 90000", Book.translate_sql_to_code("salary >= 90000")
21
+ assert_equal "rec.salary <= 90000", Book.translate_sql_to_code("salary <= 90000")
22
+ assert_equal "rec.salary != 90000", Book.translate_sql_to_code("salary <> 90000")
23
+ assert_equal "rec.recno > 5", Book.translate_sql_to_code("id > 5")
24
+ assert_equal "rec.recno < 5", Book.translate_sql_to_code("id < 5")
25
+ assert_equal "rec.recno >= 5", Book.translate_sql_to_code("id >= 5")
26
+ assert_equal "rec.recno <= 5", Book.translate_sql_to_code("id <= 5")
27
+ assert_equal "rec.recno != 5", Book.translate_sql_to_code("id <> 5")
28
+ end
29
+
30
+ def test_id_to_recno
31
+ assert_equal "rec.recno == ?", Book.translate_sql_to_code("id = ?")
32
+ assert_equal "rec.recno == ? and rec.name == ?", Book.translate_sql_to_code("id=? AND name = ?")
33
+ assert_equal "rec.recno == ?", Book.translate_sql_to_code("id=?")
34
+ assert_equal "rec.recno == ?", Book.translate_sql_to_code("id = %d")
35
+ assert_equal "rec.recno > 3", Book.translate_sql_to_code("id > 3")
36
+ assert_equal "rec.recno > 3", Book.translate_sql_to_code("rec.recno > 3")
37
+ assert_equal "rec.recno > ?", Book.translate_sql_to_code("id > ?")
38
+ end
39
+
40
+ def test_and_or
41
+ assert_equal "rec.book_id == ? and rec.content == ?", Book.translate_sql_to_code("book_id = ? AND content = ?")
42
+ assert_equal "rec.name == 'Dave' and rec.num_books == 1", Book.translate_sql_to_code("name = 'Dave' AND num_books = 1")
43
+
44
+ assert_equal "rec.book_id == ? or rec.content == ?", Book.translate_sql_to_code("book_id = ? OR content = ?")
45
+ assert_equal "rec.name == 'Dave' or rec.num_books == 1", Book.translate_sql_to_code("name = 'Dave' OR num_books = 1")
46
+
47
+ assert_equal "rec.book_id == ? and (rec.name == ? or rec.content == ?)", Book.translate_sql_to_code("book_id = ? AND (name = ? OR content = ?)")
48
+ assert_equal "rec.recno > ? or (rec.name == ? and rec.content == ?)", Book.translate_sql_to_code("id > ? OR (name = ? AND content = ?)")
49
+
50
+ assert_equal "rec.recno == ? and rec.name == ?", Book.translate_sql_to_code("id=:id and name=:name")
51
+ end
52
+
53
+ def test_is
54
+ assert_equal "rec.last_read == ?", Book.translate_sql_to_code("last_read IS ?")
55
+ assert_equal "rec.last_read == ? and rec.author_name == ?", Book.translate_sql_to_code("last_read IS ? and author_name = ?")
56
+ end
57
+
58
+ def test_null
59
+ assert_equal "rec.last_read == nil", Book.translate_sql_to_code("last_read IS NULL")
60
+ assert_equal "rec.last_read != nil", Book.translate_sql_to_code("last_read IS NOT NULL")
61
+ end
62
+
63
+ def test_in
64
+ assert_equal "rec.title.in(?)", Book.translate_sql_to_code("title IN (?)")
65
+ assert_equal "rec.recno.in(1, 2, 3)", Book.translate_sql_to_code("id IN (1, 2, 3)")
66
+ assert_equal "rec.recno.in(1,2, 3 )", Book.translate_sql_to_code("id IN (1,2, 3 )")
67
+ assert_equal "rec.name.in('me', 'you')", Book.translate_sql_to_code("name IN ('me', 'you')")
68
+ end
69
+
70
+ def test_preserve_rec
71
+ assert_equal "rec.name == ?", Book.translate_sql_to_code("rec.name == ?")
72
+ assert_equal "rec.name == 'John'", Book.translate_sql_to_code("rec.name == 'John'")
73
+ assert_equal "rec.recno > 3", Book.translate_sql_to_code("rec.recno > 3")
74
+ assert_equal "rec.recno < 3", Book.translate_sql_to_code("rec.recno < 3")
75
+ assert_equal "rec.recno >= 3", Book.translate_sql_to_code("rec.recno >= 3")
76
+ assert_equal "rec.recno <= 3", Book.translate_sql_to_code("rec.recno <= 3")
77
+ end
78
+
79
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ ## This class checks only the translate_sql_to_code method
4
+ class KBStdlibExtensionsTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @num_array = [2,4,6,3,5,1]
8
+ @string_array = ['az', 'cx', 'by', 'dw']
9
+ @rec_array = [['c', 2], ['a', 2], ['d', 1], ['b', 1]]
10
+ end
11
+
12
+ def test_array_sort_by
13
+ assert_equal [1,2,3,4,5,6], @num_array.sort_by(:to_i)
14
+ assert_equal [1,2,3,4,5,6], @num_array.sort_by{|x| x.to_i}
15
+ assert_equal ['dw', 'cx', 'by', 'az'], @string_array.sort_by(:reverse)
16
+ assert_equal ['az', 'by', 'cx', 'dw'], @string_array.sort_by{|x| x[0..0]}
17
+ end
18
+
19
+ def test_array_sort_by!
20
+ @num_array.sort_by!(:to_i)
21
+ assert_equal [1,2,3,4,5,6], @num_array
22
+ end
23
+
24
+ def test_array_stable_sort
25
+ assert_equal [['a', 2], ['b', 1], ['c', 2], ['d', 1]], @rec_array.stable_sort
26
+ end
27
+
28
+ def test_array_stable_sort_by
29
+ assert_equal [['d', 1], ['b', 1], ['c', 2], ['a', 2]], @rec_array.stable_sort_by(:last)
30
+ end
31
+
32
+ def test_object_in
33
+ assert 1.in(@num_array)
34
+ assert !1.in(@string_array)
35
+ assert 1.in(1)
36
+ assert 1.in(1,2,3)
37
+ end
38
+ end
@@ -0,0 +1,27 @@
1
+ class Book < ActiveRecord::Base
2
+ belongs_to :publisher
3
+ has_many :pages, :dependent => true
4
+ has_one :errata
5
+ has_and_belongs_to_many :author
6
+ end
7
+
8
+ class Page < ActiveRecord::Base
9
+ belongs_to :book
10
+ end
11
+
12
+ class Author < ActiveRecord::Base
13
+ has_and_belongs_to_many :book
14
+ end
15
+
16
+ class Publisher < ActiveRecord::Base
17
+ has_many :books
18
+ end
19
+
20
+ class Errata < ActiveRecord::Base
21
+ set_table_name 'errata'
22
+ belongs_to :book
23
+ end
24
+
25
+ class PrimaryKeyTest < ActiveRecord::Base
26
+ set_primary_key :pk
27
+ end
@@ -0,0 +1,41 @@
1
+ ###############################################################################
2
+ # DB Schema for testing ActiveKirby
3
+
4
+ ActiveRecord::Schema.define() do
5
+
6
+ create_table "books", :force => true do |t|
7
+ t.column "name", :string, :null => false
8
+ t.column "published", :date, :null => false
9
+ t.column "publisher_id", :integer
10
+ end
11
+
12
+ add_index "books", ["name"], :name => "book_names_index"
13
+
14
+ create_table "authors", :force => true do |t|
15
+ t.column "name", :string, :null => false
16
+ end
17
+
18
+ add_index "authors", ["name"], :name => "author_names_index"
19
+
20
+ create_table "authors_books", :force => true do |t|
21
+ t.column "author_id", :integer
22
+ t.column "book_id", :integer
23
+ end
24
+
25
+ create_table "publishers", :force => true do |t|
26
+ t.column "name", :text
27
+ end
28
+
29
+ create_table 'delete_me_in_migration', :force => true do |t|
30
+ t.column "junk", :text, :dafault => 'food'
31
+ t.column "more_junk", :integer
32
+ t.column "junk_yard", :datetime
33
+ end
34
+ add_index "delete_me_in_migration", ["junk"], :name => "junk_index"
35
+
36
+ create_table 'primary_key_tests', :force => true do |t|
37
+ t.column 'pk', :primary_key
38
+ t.column 'name', :string
39
+ end
40
+
41
+ end
@@ -0,0 +1,49 @@
1
+
2
+ require 'test/unit'
3
+
4
+ require_gem 'rails'
5
+ require 'active_record'
6
+ require 'active_record/migration'
7
+ require 'active_record/fixtures'
8
+ require 'active_support/breakpoint'
9
+ require 'kirbybase_adapter'
10
+ require 'model'
11
+
12
+ ActiveRecord::Base.logger = Logger.new("test.log")
13
+
14
+ ###############################################################################
15
+ # Database Utilities
16
+ $TEST_DB_LOCATION = File.join(File.dirname(__FILE__), 'db')
17
+
18
+ def recreate_kb_database(load_schema = true)
19
+ FileUtils.rm_rf $TEST_DB_LOCATION rescue nil
20
+ FileUtils.mkdir_p $TEST_DB_LOCATION
21
+
22
+ ActiveRecord::Base.establish_connection(
23
+ :adapter => "kirbybase",
24
+ :connection_type => "local",
25
+ :dbpath => $TEST_DB_LOCATION
26
+ )
27
+ ActiveRecord::Base.clear_connection_cache!
28
+ $db = ActiveRecord::Base.connection.db # use the same KirbyBase object that the ActiveRecord adapter uses
29
+
30
+ if load_schema
31
+ load File.join(File.dirname(__FILE__), 'schema.rb')
32
+ ActiveRecord::Migrator.migrate(File.expand_path(File.dirname(__FILE__)) + "/", 1)
33
+ end
34
+ end
35
+
36
+ ###############################################################################
37
+ # Start tests: This will do 3 things:
38
+ # Create the DB path, establish connection and load the test schema
39
+ # Load the schema
40
+ # The schema.rb file contains basic schema instructions, and is the functional
41
+ # testing of basic AR::Schema compatibility. # Migrate the schema
42
+ # This test will load the migration files. These files both test the integration
43
+ # and support of ActiveRecord::Migrations, and more advanced schema operations
44
+ #
45
+ # All AR::Schema functionality to test goes in the schema.rb file or in the migration
46
+ # file, as appropriate.
47
+ #
48
+ puts "Recreating Database..."
49
+ recreate_kb_database
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: ackbar
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-02-15
8
+ summary: ActiveRecord KirbyBase Adapter
9
+ require_paths:
10
+ - "."
11
+ email: assaph@gmail.com
12
+ homepage: http://ackbar.rubyforge.org
13
+ rubyforge_project: ackbar
14
+ description: An adapter for Rails::ActiveRecord ORM to the KirbyBase pure-ruby DBMS
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Assaph Mehr
29
+ files:
30
+ - kirbybase_adapter.rb
31
+ - Rakefile
32
+ - CHANGELOG
33
+ - README
34
+ - TODO
35
+ - test/001_schema_migration_test.rb
36
+ - test/ar_base_tests_runner.rb
37
+ - test/ar_model_adaptation.rb
38
+ - test/connection.rb
39
+ - test/create_dbs_for_ar_tests.rb
40
+ - test/kb_associations_test.rb
41
+ - test/kb_basics_test.rb
42
+ - test/kb_schema_test.rb
43
+ - test/kb_sql_to_code_test.rb
44
+ - test/kb_stdlib_extensions_test.rb
45
+ - test/model.rb
46
+ - test/schema.rb
47
+ - test/test_helper.rb
48
+ - test/fixtures/authors.yml
49
+ - test/fixtures/authors_books.yml
50
+ - test/fixtures/books.yml
51
+ - test/fixtures/pages.yml
52
+ - test/fixtures/publishers.yml
53
+ test_files: []
54
+ rdoc_options:
55
+ - "--title"
56
+ - "Ackbar -- ActiveRecord Adapter for KirbyBase"
57
+ - "--main"
58
+ - README
59
+ - "--exclude"
60
+ - test
61
+ - "--line-numbers"
62
+ extra_rdoc_files:
63
+ - README
64
+ - CHANGELOG
65
+ - TODO
66
+ executables: []
67
+ extensions: []
68
+ requirements: []
69
+ dependencies:
70
+ - !ruby/object:Gem::Dependency
71
+ name: KirbyBase
72
+ version_requirement:
73
+ version_requirements: !ruby/object:Gem::Version::Requirement
74
+ requirements:
75
+ -
76
+ - "="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.5.2
79
+ version:
80
+ - !ruby/object:Gem::Dependency
81
+ name: activerecord
82
+ version_requirement:
83
+ version_requirements: !ruby/object:Gem::Version::Requirement
84
+ requirements:
85
+ -
86
+ - "="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.13.2
89
+ version: