How to use false method of MyModule Package

Best Minitest_ruby code snippet using MyModule.false

test_runtime_basic.rb

Source:test_runtime_basic.rb Github

copy

Full Screen

...5 Dbt.runtime.instance_variable_set('@db', mock)6 database = create_simple_db_definition(create_dir('databases'), 'MyModule', [])7 database.version_hash = 'testing'8 database.version = 29 database.migrations = false10 status = Dbt.runtime.status(database)11 assert_match 'Migration Support: No', status12 assert_match 'Database Version: 2', status13 assert_match 'Database Schema Hash: testing', status14 database.version = 115 database.migrations = true16 status = Dbt.runtime.status(database)17 assert_match 'Migration Support: Yes', status18 assert_match 'Database Version: 1', status19 assert_match 'Database Schema Hash: testing', status20 end21 def test_pre_db_artifacts_loads_repository_xml22 mock = Dbt::DbDriver.new23 Dbt.runtime.instance_variable_set('@db', mock)24 base_dir = create_dir('database')25 database = Dbt.add_database(:default) do |db|26 db.rake_integration = false27 db.search_dirs = [base_dir]28 end29 File.open("#{base_dir}/repository.yml", 'w') do |f|30 f.write Dbt::RepositoryDefinition.new(:modules => ['Core'], :table_map => {'Core' => []}).to_yaml31 end32 repository = Dbt::RepositoryDefinition.new(:modules => ['CodeMetrics'],33 :table_map => {'CodeMetrics' => %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric")})34 database.pre_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)35 Dbt.runtime.load_database_config(database)36 assert_equal %w(CodeMetrics Core), database.repository.modules37 assert_equal %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric"), database.repository.table_ordering('CodeMetrics')38 end39 def test_multiple_pre_db_artifacts_loads_repository_xml40 mock = Dbt::DbDriver.new41 Dbt.runtime.instance_variable_set('@db', mock)42 base_dir = create_dir('database')43 database = Dbt.add_database(:default) do |db|44 db.rake_integration = false45 db.search_dirs = [base_dir]46 end47 File.open("#{base_dir}/repository.yml", 'w') do |f|48 f.write Dbt::RepositoryDefinition.new(:modules => ['Core'], :table_map => {'Core' => []}).to_yaml49 end50 repository = Dbt::RepositoryDefinition.new(:modules => ['CodeMetrics'],51 :table_map => {'CodeMetrics' => %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric")})52 database.pre_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)53 repository = Dbt::RepositoryDefinition.new(:modules => ['Second'],54 :table_map => {'Second' => %w("Second"."tblA" "Second"."tblB")})55 database.pre_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)56 Dbt.runtime.load_database_config(database)57 assert_equal %w(CodeMetrics Second Core), database.repository.modules58 assert_equal %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric"), database.repository.table_ordering('CodeMetrics')59 assert_equal %w("Second"."tblA" "Second"."tblB"), database.repository.table_ordering('Second')60 end61 def test_post_db_artifacts_loads_repository_xml62 mock = Dbt::DbDriver.new63 Dbt.runtime.instance_variable_set('@db', mock)64 base_dir = create_dir('database')65 database = Dbt.add_database(:default) do |db|66 db.rake_integration = false67 db.search_dirs = [base_dir]68 end69 File.open("#{base_dir}/repository.yml", 'w') do |f|70 f.write Dbt::RepositoryDefinition.new(:modules => ['Core'], :table_map => {'Core' => []}).to_yaml71 end72 repository = Dbt::RepositoryDefinition.new(:modules => ['CodeMetrics'],73 :table_map => {'CodeMetrics' => %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric")})74 database.post_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)75 Dbt.runtime.load_database_config(database)76 assert_equal %w(Core CodeMetrics), database.repository.modules77 assert_equal %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric"), database.repository.table_ordering('CodeMetrics')78 end79 def test_multiple_post_db_artifacts_loads_repository_xml80 mock = Dbt::DbDriver.new81 Dbt.runtime.instance_variable_set('@db', mock)82 base_dir = create_dir('database')83 database = Dbt.add_database(:default) do |db|84 db.rake_integration = false85 db.search_dirs = [base_dir]86 end87 File.open("#{base_dir}/repository.yml", 'w') do |f|88 f.write Dbt::RepositoryDefinition.new(:modules => ['Core'], :table_map => {'Core' => []}).to_yaml89 end90 repository = Dbt::RepositoryDefinition.new(:modules => ['CodeMetrics'],91 :table_map => {'CodeMetrics' => %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric")})92 database.post_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)93 repository = Dbt::RepositoryDefinition.new(:modules => ['Second'],94 :table_map => {'Second' => %w("Second"."tblA" "Second"."tblB")})95 database.post_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)96 Dbt.runtime.load_database_config(database)97 assert_equal %w(Core CodeMetrics Second), database.repository.modules98 assert_equal %w("CodeMetrics"."tblCollection" "CodeMetrics"."tblMethodMetric"), database.repository.table_ordering('CodeMetrics')99 assert_equal %w("Second"."tblA" "Second"."tblB"), database.repository.table_ordering('Second')100 end101 def test_pre_and_post_db_artifacts_loads_repository_xml102 mock = Dbt::DbDriver.new103 Dbt.runtime.instance_variable_set('@db', mock)104 base_dir = create_dir('database')105 database = Dbt.add_database(:default) do |db|106 db.rake_integration = false107 db.search_dirs = [base_dir]108 end109 File.open("#{base_dir}/repository.yml", 'w') do |f|110 f.write Dbt::RepositoryDefinition.new(:modules => ['Core'], :table_map => {'Core' => []}).to_yaml111 end112 repository = Dbt::RepositoryDefinition.new(:modules => %w(CodeMetrics), :table_map => {'CodeMetrics' => []})113 database.pre_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)114 repository = Dbt::RepositoryDefinition.new(:modules => %w(Second), :table_map => {'Second' => []})115 database.post_db_artifacts << create_zip('data/repository.yml' => repository.to_yaml)116 Dbt.runtime.load_database_config(database)117 assert_equal ['CodeMetrics', 'Core', 'Second'], database.repository.modules118 end119 def test_query120 mock = Dbt::DbDriver.new121 Dbt.runtime.instance_variable_set('@db', mock)122 config = create_postgres_config()123 db_scripts = create_dir('databases')124 module_name = 'MyModule'125 table_names = %w([MyModule].[foo] [MyModule].[bar] [MyModule].[baz])126 database = create_simple_db_definition(db_scripts, module_name, table_names)127 sql = 'SELECT 42'128 mock.expects(:open).with(config, false).in_sequence(@s)129 mock.expects(:query).with(sql).in_sequence(@s)130 mock.expects(:close).with().in_sequence(@s)131 Dbt.runtime.query(database, sql)132 end133 def test_create134 mock = Dbt::DbDriver.new135 Dbt.runtime.instance_variable_set('@db', mock)136 config = create_postgres_config()137 db_scripts = create_dir('databases')138 module_name = 'MyModule'139 table_names = %w([MyModule].[foo] [MyModule].[bar] [MyModule].[baz])140 database = create_simple_db_definition(db_scripts, module_name, table_names)141 mock.expects(:open).with(config, true).in_sequence(@s)142 mock.expects(:drop).with(database, config).in_sequence(@s)143 mock.expects(:create_database).with(database, config).in_sequence(@s)144 mock.expects(:close).with().in_sequence(@s)145 mock.expects(:open).with(config, false).in_sequence(@s)146 mock.expects(:create_schema).with(module_name).in_sequence(@s)147 mock.expects(:close).with().in_sequence(@s)148 Dbt.runtime.create(database)149 end150 def test_create_by_import151 mock = Dbt::DbDriver.new152 Dbt.runtime.instance_variable_set('@db', mock)153 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))154 db_scripts = create_dir('databases')155 module_name = 'MyModule'156 table_names = %w([MyModule].[foo])157 database = create_simple_db_definition(db_scripts, module_name, table_names)158 database.separate_import_task = true159 import = database.add_import(:default, {})160 mock.expects(:open).with(config, true).in_sequence(@s)161 mock.expects(:drop).with(database, config).in_sequence(@s)162 mock.expects(:create_database).with(database, config).in_sequence(@s)163 mock.expects(:close).with().in_sequence(@s)164 mock.expects(:open).with(config, false).in_sequence(@s)165 mock.expects(:create_schema).with(module_name).in_sequence(@s)166 expect_default_table_import(mock, import, module_name, 'foo')167 mock.expects(:post_data_module_import).with(import, module_name).in_sequence(@s)168 mock.expects(:post_database_import).with(import).in_sequence(@s)169 mock.expects(:close).with().in_sequence(@s)170 Dbt.runtime.create_by_import(import)171 end172 def test_create_with_no_create173 mock = Dbt::DbDriver.new174 Dbt.runtime.instance_variable_set('@db', mock)175 config = create_postgres_config('no_create' => true)176 db_scripts = create_dir('databases')177 module_name = 'MyModule'178 table_names = %w([MyModule].[foo] [MyModule].[bar] [MyModule].[baz])179 database = create_simple_db_definition(db_scripts, module_name, table_names)180 mock.expects(:open).with(config, false).in_sequence(@s)181 mock.expects(:create_schema).with(module_name).in_sequence(@s)182 mock.expects(:close).with().in_sequence(@s)183 Dbt.runtime.create(database)184 end185 def test_create_with_multiple_fixtures186 mock = Dbt::DbDriver.new187 Dbt.runtime.instance_variable_set('@db', mock)188 config = create_postgres_config()189 db_scripts = create_dir('databases')190 module_name = 'MyModule'191 table_names = %w([MyModule].[foo] [MyModule].[bar])192 database = create_simple_db_definition(db_scripts, module_name, table_names)193 create_fixture(module_name, 'foo')194 create_fixture(module_name, 'bar')195 mock.expects(:open).with(config, true).in_sequence(@s)196 mock.expects(:drop).with(database, config).in_sequence(@s)197 mock.expects(:create_database).with(database, config).in_sequence(@s)198 mock.expects(:close).with().in_sequence(@s)199 mock.expects(:open).with(config, false).in_sequence(@s)200 mock.expects(:create_schema).with(module_name).in_sequence(@s)201 expect_delete(mock, module_name, 'bar')202 expect_delete(mock, module_name, 'foo')203 expect_fixture(mock, module_name, 'foo')204 expect_fixture(mock, module_name, 'bar')205 mock.expects(:close).with().in_sequence(@s)206 Dbt.runtime.create(database)207 end208 def test_create_with_unexpected_fixtures209 mock = Dbt::DbDriver.new210 Dbt.runtime.instance_variable_set('@db', mock)211 config = create_postgres_config()212 db_scripts = create_dir('databases')213 module_name = 'MyModule'214 table_names = %w([MyModule].[foo] [MyModule].[bar])215 database = create_simple_db_definition(db_scripts, module_name, table_names)216 create_fixture(module_name, 'baz')217 mock.expects(:open).with(config, true).in_sequence(@s)218 mock.expects(:drop).with(database, config).in_sequence(@s)219 mock.expects(:create_database).with(database, config).in_sequence(@s)220 mock.expects(:close).with().in_sequence(@s)221 mock.expects(:open).with(config, false).in_sequence(@s)222 mock.expects(:create_schema).with(module_name).in_sequence(@s)223 mock.expects(:close).with().in_sequence(@s)224 assert_raises(RuntimeError) do225 Dbt.runtime.create(database)226 end227 end228 def test_create_with_fixtures229 mock = Dbt::DbDriver.new230 Dbt.runtime.instance_variable_set('@db', mock)231 config = create_postgres_config()232 db_scripts = create_dir('databases')233 module_name = 'MyModule'234 table_names = %w([MyModule].[foo] [MyModule].[bar])235 database = create_simple_db_definition(db_scripts, module_name, table_names)236 create_fixture(module_name, 'foo')237 mock.expects(:open).with(config, true).in_sequence(@s)238 mock.expects(:drop).with(database, config).in_sequence(@s)239 mock.expects(:create_database).with(database, config).in_sequence(@s)240 mock.expects(:close).with().in_sequence(@s)241 mock.expects(:open).with(config, false).in_sequence(@s)242 mock.expects(:create_schema).with(module_name).in_sequence(@s)243 expect_delete(mock, module_name, 'foo')244 expect_fixture(mock, module_name, 'foo')245 mock.expects(:close).with().in_sequence(@s)246 Dbt.runtime.create(database)247 end248 def test_create_with_fixtures_including_non_fixture249 mock = Dbt::DbDriver.new250 Dbt.runtime.instance_variable_set('@db', mock)251 config = create_postgres_config()252 db_scripts = create_dir('databases')253 module_name = 'MyModule'254 table_names = ['[foo]', '[bar]']255 database = create_simple_db_definition(db_scripts, module_name, table_names)256 create_file("databases/#{module_name}/fixtures/bar.sql", 'SELECT * FROM tblNotRun')257 mock.expects(:open).with(config, true).in_sequence(@s)258 mock.expects(:drop).with(database, config).in_sequence(@s)259 mock.expects(:create_database).with(database, config).in_sequence(@s)260 mock.expects(:close).with().in_sequence(@s)261 mock.expects(:open).with(config, false).in_sequence(@s)262 mock.expects(:create_schema).with(module_name).in_sequence(@s)263 mock.expects(:close).with().in_sequence(@s)264 assert_raises(RuntimeError) do265 Dbt.runtime.create(database)266 end267 end268 def test_create_with_sql269 mock = Dbt::DbDriver.new270 Dbt.runtime.instance_variable_set("@db", mock)271 config = create_postgres_config()272 db_scripts = create_dir("databases")273 module_name = 'MyModule'274 table_names = ['[MyModule].[foo]']275 database = create_simple_db_definition(db_scripts, module_name, table_names)276 Dbt::Config.default_up_dirs = ['.', 'Dir1', 'Dir2']277 Dbt::Config.default_finalize_dirs = ['Dir3', 'Dir4']278 Dbt::Config.default_fixture_dir_name = 'foo'279 Dbt::Config.default_pre_create_dirs = ['db-pre-create']280 Dbt::Config.default_post_create_dirs = ['db-post-create']281 create_table_sql("db-pre-create", 'preCreate')282 create_table_sql("#{module_name}", 'a')283 create_table_sql("#{module_name}", 'b')284 create_table_sql("#{module_name}/Dir1", 'd')285 create_table_sql("#{module_name}/Dir1", 'c')286 create_table_sql("#{module_name}/Dir2", 'e')287 create_table_sql("#{module_name}/Dir2", 'f')288 create_fixture(module_name, 'foo')289 create_table_sql("#{module_name}/Dir3", 'g')290 create_table_sql("#{module_name}/Dir4", 'h')291 create_table_sql("db-post-create", 'postCreate')292 mock.expects(:open).with(config, true).in_sequence(@s)293 mock.expects(:drop).with(database, config).in_sequence(@s)294 mock.expects(:create_database).with(database, config).in_sequence(@s)295 mock.expects(:close).with().in_sequence(@s)296 mock.expects(:open).with(config, false).in_sequence(@s)297 expect_create_table(mock, '', 'db-pre-create/', 'preCreate')298 mock.expects(:create_schema).with(module_name).in_sequence(@s)299 expect_create_table(mock, module_name, '', 'a')300 expect_create_table(mock, module_name, '', 'b')301 expect_create_table(mock, module_name, 'Dir1/', 'c')302 expect_create_table(mock, module_name, 'Dir1/', 'd')303 expect_create_table(mock, module_name, 'Dir2/', 'e')304 expect_create_table(mock, module_name, 'Dir2/', 'f')305 expect_delete(mock, module_name, 'foo')306 expect_fixture(mock, module_name, 'foo')307 expect_create_table(mock, module_name, 'Dir3/', 'g')308 expect_create_table(mock, module_name, 'Dir4/', 'h')309 expect_create_table(mock, '', 'db-post-create/', 'postCreate')310 mock.expects(:close).with().in_sequence(@s)311 Dbt.runtime.create(database)312 end313 def psn(dir,table_name)314 "data/#{dir}/#{table_name}.sql"315 end316 def test_create_with_sql_from_package_import317 Dbt::Config.default_up_dirs = ['.', 'Dir1', 'Dir2']318 Dbt::Config.default_finalize_dirs = ['Dir3', 'Dir4']319 Dbt::Config.default_fixture_dir_name = 'foo'320 Dbt::Config.default_pre_create_dirs = ['db-pre-create']321 Dbt::Config.default_post_create_dirs = ['db-post-create']322 mock = Dbt::DbDriver.new323 Dbt.runtime.instance_variable_set('@db', mock)324 config = create_postgres_config()325 db_scripts = create_dir('databases')326 module_name = 'MyModule'327 packaged_definition = Dbt::RepositoryDefinition.new(:modules => [module_name],328 :table_map => {module_name => ['[MyModule].[foo]']})329 zipfile = create_zip('data/repository.yml' => packaged_definition.to_yaml,330 psn('db-pre-create', 'preCreate') => ct('preCreate'),331 "data/#{module_name}/#{Dbt::Config.default_fixture_dir_name}/#{module_name}.foo.yml" => "1:\n ID: 1\n",332 psn("#{module_name}", 'a') => ct('a'),333 psn("#{module_name}", 'b') => ct('b'),334 psn("#{module_name}/Dir1", 'd') => ct('d'),335 psn("#{module_name}/Dir1", 'c') => ct('c'),336 psn("#{module_name}/Dir2", 'e') => ct('e'),337 psn("#{module_name}/Dir2", 'f') => ct('f'),338 psn("#{module_name}/Dir3", 'g') => ct('g'),339 psn("#{module_name}/Dir4", 'h') => ct('h'),340 psn('db-post-create', 'postCreate') => ct('postCreate') )341 definition = Dbt::RepositoryDefinition.new(:modules => [], :table_map => {})342 File.open("#{db_scripts}/repository.yml",'w') do |f|343 f.write definition.to_yaml344 end345 database = Dbt.add_database(:default) do |db|346 db.rake_integration = false347 db.post_db_artifacts << zipfile348 db.search_dirs = [db_scripts]349 end350 Dbt.runtime.send(:perform_load_database_config, database)351 mock.expects(:open).with(config, true).in_sequence(@s)352 mock.expects(:drop).with(database, config).in_sequence(@s)353 mock.expects(:create_database).with(database, config).in_sequence(@s)354 mock.expects(:close).with().in_sequence(@s)355 mock.expects(:open).with(config, false).in_sequence(@s)356 expect_create_table(mock, '', 'db-pre-create/', 'preCreate')357 mock.expects(:create_schema).with(module_name).in_sequence(@s)358 expect_create_table(mock, module_name, '', 'a')359 expect_create_table(mock, module_name, '', 'b')360 expect_create_table(mock, module_name, 'Dir1/', 'c')361 expect_create_table(mock, module_name, 'Dir1/', 'd')362 expect_create_table(mock, module_name, 'Dir2/', 'e')363 expect_create_table(mock, module_name, 'Dir2/', 'f')364 expect_delete(mock, module_name, 'foo')365 expect_fixture(mock, module_name, 'foo')366 expect_create_table(mock, module_name, 'Dir3/', 'g')367 expect_create_table(mock, module_name, 'Dir4/', 'h')368 expect_create_table(mock, '', 'db-post-create/', 'postCreate')369 mock.expects(:close).with().in_sequence(@s)370 Dbt.runtime.create(database)371 end372 def test_create_with_sql_and_index_covering_partial373 mock = Dbt::DbDriver.new374 Dbt.runtime.instance_variable_set('@db', mock)375 config = create_postgres_config()376 db_scripts = create_dir('databases')377 module_name = 'MyModule'378 table_names = ['[MyModule].[foo]']379 database = create_simple_db_definition(db_scripts, module_name, table_names)380 Dbt::Config.default_up_dirs = ['Dir1']381 Dbt::Config.index_file_name = 'index2.txt'382 create_file("databases/#{module_name}/Dir1/index2.txt", "d.sql\ne.sql")383 create_table_sql("#{module_name}/Dir1", 'c')384 create_table_sql("#{module_name}/Dir1", 'd')385 create_table_sql("#{module_name}/Dir1", 'e')386 create_table_sql("#{module_name}/Dir1", 'f')387 mock.expects(:open).with(config, true).in_sequence(@s)388 mock.expects(:drop).with(database, config).in_sequence(@s)389 mock.expects(:create_database).with(database, config).in_sequence(@s)390 mock.expects(:close).with().in_sequence(@s)391 mock.expects(:open).with(config, false).in_sequence(@s)392 mock.expects(:create_schema).with(module_name).in_sequence(@s)393 expect_create_table(mock, module_name, 'Dir1/', 'd')394 expect_create_table(mock, module_name, 'Dir1/', 'e')395 expect_create_table(mock, module_name, 'Dir1/', 'c')396 expect_create_table(mock, module_name, 'Dir1/', 'f')397 mock.expects(:close).with().in_sequence(@s)398 Dbt.runtime.create(database)399 end400 def test_create_with_sql_and_index_covering_full401 mock = Dbt::DbDriver.new402 Dbt.runtime.instance_variable_set('@db', mock)403 config = create_postgres_config()404 db_scripts = create_dir('databases')405 module_name = 'MyModule'406 table_names = ['[MyModule].[foo]']407 database = create_simple_db_definition(db_scripts, module_name, table_names)408 Dbt::Config.default_up_dirs = ['Dir1']409 Dbt::Config.index_file_name = 'index2.txt'410 create_file("databases/#{module_name}/Dir1/index2.txt", "d.sql\ne.sql")411 create_table_sql("#{module_name}/Dir1", 'd')412 create_table_sql("#{module_name}/Dir1", 'e')413 mock.expects(:open).with(config, true).in_sequence(@s)414 mock.expects(:drop).with(database, config).in_sequence(@s)415 mock.expects(:create_database).with(database, config).in_sequence(@s)416 mock.expects(:close).with().in_sequence(@s)417 mock.expects(:open).with(config, false).in_sequence(@s)418 mock.expects(:create_schema).with(module_name).in_sequence(@s)419 expect_create_table(mock, module_name, 'Dir1/', 'd')420 expect_create_table(mock, module_name, 'Dir1/', 'e')421 mock.expects(:close).with().in_sequence(@s)422 Dbt.runtime.create(database)423 end424 def test_create_with_sql_and_index_with_additional425 mock = Dbt::DbDriver.new426 Dbt.runtime.instance_variable_set('@db', mock)427 config = create_postgres_config()428 db_scripts = create_dir('databases')429 module_name = 'MyModule'430 table_names = ['[MyModule].[foo]']431 database = create_simple_db_definition(db_scripts, module_name, table_names)432 Dbt::Config.default_up_dirs = ['Dir1']433 Dbt::Config.index_file_name = 'index2.txt'434 create_file("databases/#{module_name}/Dir1/index2.txt", "d.sql\ne.sql\nf.sql")435 create_table_sql("#{module_name}/Dir1", 'd')436 create_table_sql("#{module_name}/Dir1", 'e')437 mock.expects(:open).with(config, true).in_sequence(@s)438 mock.expects(:drop).with(database, config).in_sequence(@s)439 mock.expects(:create_database).with(database, config).in_sequence(@s)440 mock.expects(:close).with().in_sequence(@s)441 mock.expects(:open).with(config, false).in_sequence(@s)442 mock.expects(:create_schema).with(module_name).in_sequence(@s)443 mock.expects(:close).with().in_sequence(@s)444 assert_raises(RuntimeError) do445 Dbt.runtime.create(database)446 end447 end448 def test_drop449 mock = Dbt::DbDriver.new450 Dbt.runtime.instance_variable_set('@db', mock)451 config = create_postgres_config()452 db_scripts = create_dir('databases')453 module_name = 'MyModule'454 table_names = ['[MyModule].[foo]', '[MyModule].[bar]']455 database = create_simple_db_definition(db_scripts, module_name, table_names)456 mock.expects(:open).with(config, true).in_sequence(@s)457 mock.expects(:drop).with(database, config).in_sequence(@s)458 mock.expects(:close).with().in_sequence(@s)459 Dbt.runtime.drop(database)460 end461 def test_import462 mock = Dbt::DbDriver.new463 Dbt.runtime.instance_variable_set('@db', mock)464 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))465 db_scripts = create_dir('databases')466 module_name = 'MyModule'467 table_names = ['[MyModule].[foo]', '[MyModule].[bar]', '[MyModule].[baz]']468 database = create_simple_db_definition(db_scripts, module_name, table_names)469 database.separate_import_task = true470 import = database.add_import(:default, {})471 mock.expects(:open).with(config, false).in_sequence(@s)472 expect_delete_for_table_import(mock, module_name, 'baz')473 expect_delete_for_table_import(mock, module_name, 'bar')474 expect_delete_for_table_import(mock, module_name, 'foo')475 expect_default_table_import(mock, import, module_name, 'foo')476 expect_default_table_import(mock, import, module_name, 'bar')477 expect_default_table_import(mock, import, module_name, 'baz')478 mock.expects(:post_data_module_import).with(import, module_name).in_sequence(@s)479 mock.expects(:post_database_import).with(import).in_sequence(@s)480 mock.expects(:close).with().in_sequence(@s)481 Dbt.runtime.database_import(database.import_by_name(:default), nil)482 end483 def test_import_with_IMPORT_RESUME_AT484 mock = Dbt::DbDriver.new485 Dbt.runtime.instance_variable_set('@db', mock)486 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))487 db_scripts = create_dir('databases')488 module_name = 'MyModule'489 table_names = ['[MyModule].[foo]', '[MyModule].[bar]', '[MyModule].[baz]']490 database = create_simple_db_definition(db_scripts, module_name, table_names)491 database.separate_import_task = true492 import = database.add_import(:default, {})493 mock.expects(:open).with(config, false).in_sequence(@s)494 expect_delete_for_table_import(mock, module_name, 'bar')495 expect_default_table_import(mock, import, module_name, 'bar')496 expect_default_table_import(mock, import, module_name, 'baz')497 mock.expects(:post_data_module_import).with(import, module_name).in_sequence(@s)498 mock.expects(:post_database_import).with(import).in_sequence(@s)499 mock.expects(:close).with()500 ENV['IMPORT_RESUME_AT'] = 'MyModule.bar'501 Dbt.runtime.database_import(database.import_by_name(:default), nil)502 end503 def test_import_by_sql504 mock = Dbt::DbDriver.new505 Dbt.runtime.instance_variable_set('@db', mock)506 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))507 db_scripts = create_dir('databases')508 module_name = 'MyModule'509 table_names = ['[MyModule].[foo]']510 database = create_simple_db_definition(db_scripts, module_name, table_names)511 database.separate_import_task = true512 import = database.add_import(:default, {})513 Dbt::Config.default_import_dir = 'zzzz'514 import_sql = 'INSERT INTO DBT_TEST.[foo]'515 create_file("databases/#{module_name}/zzzz/MyModule.foo.sql", import_sql)516 mock.expects(:open).with(config, false).in_sequence(@s)517 expect_delete_for_table_import(mock, module_name, 'foo')518 expect_pre_table_import(mock, import, module_name, 'foo', 'S')519 mock.expects(:execute).with(import_sql, true).in_sequence(@s)520 expect_post_table_import(mock, import, module_name, 'foo')521 mock.expects(:post_data_module_import).with(import, module_name).in_sequence(@s)522 mock.expects(:post_database_import).with(import).in_sequence(@s)523 mock.expects(:close).with().in_sequence(@s)524 Dbt.runtime.database_import(database.import_by_name(:default), nil)525 end526 def test_import_by_fixture527 mock = Dbt::DbDriver.new528 Dbt.runtime.instance_variable_set('@db', mock)529 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))530 db_scripts = create_dir('databases')531 module_name = 'MyModule'532 table_names = ['[MyModule].[foo]']533 database = create_simple_db_definition(db_scripts, module_name, table_names)534 database.separate_import_task = true535 import = database.add_import(:default, {})536 Dbt::Config.default_import_dir = 'zzzz'537 fixture_data = "1:\n ID: 1\n"538 create_file('databases/MyModule/zzzz/MyModule.foo.yml', fixture_data)539 mock.expects(:open).with(config, false).in_sequence(@s)540 expect_delete_for_table_import(mock, module_name, 'foo')541 expect_pre_table_import(mock, import, module_name, 'foo', 'F')542 mock.expects(:pre_fixture_import).with('[MyModule].[foo]').in_sequence(@s)543 mock.expects(:insert).with('[MyModule].[foo]', 'ID' => 1).in_sequence(@s)544 mock.expects(:post_fixture_import).with('[MyModule].[foo]').in_sequence(@s)545 expect_post_table_import(mock, import, module_name, 'foo')546 mock.expects(:post_data_module_import).with(import, module_name).in_sequence(@s)547 mock.expects(:post_database_import).with(import).in_sequence(@s)548 mock.expects(:close).with().in_sequence(@s)549 Dbt.runtime.database_import(database.import_by_name(:default), nil)550 end551 def test_import_using_pre_post_dirs552 mock = Dbt::DbDriver.new553 Dbt.runtime.instance_variable_set('@db', mock)554 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))555 db_scripts = create_dir('databases')556 module_name = 'MyModule'557 table_names = ['[MyModule].[foo]']558 database = create_simple_db_definition(db_scripts, module_name, table_names)559 database.separate_import_task = true560 import = database.add_import(:default, {})561 Dbt::Config.default_pre_import_dirs = ['a', 'b']562 pre_import_sql = 'SELECT 1'563 create_file('databases/a/yyy.sql', pre_import_sql)564 pre_import_sql_2 = 'SELECT 2'565 create_file('databases/b/qqq.sql', pre_import_sql_2)566 Dbt::Config.default_post_import_dirs = ['c', 'd']567 post_import_sql = 'SELECT 3'568 create_file('databases/c/xxx.sql', post_import_sql)569 post_import_sql_2 = 'SELECT 4'570 create_file('databases/d/zzz.sql', post_import_sql_2)571 mock.expects(:open).with(config, false).in_sequence(@s)572 Dbt.runtime.expects(:info).with(' : a/yyy.sql').in_sequence(@s)573 mock.expects(:execute).with(pre_import_sql, true).in_sequence(@s)574 Dbt.runtime.expects(:info).with(' : b/qqq.sql').in_sequence(@s)575 mock.expects(:execute).with(pre_import_sql_2, true).in_sequence(@s)576 expect_delete_for_table_import(mock, module_name, 'foo')577 expect_default_table_import(mock, import, module_name, 'foo')578 mock.expects(:post_data_module_import).with(import, module_name).in_sequence(@s)579 Dbt.runtime.expects(:info).with(' : c/xxx.sql').in_sequence(@s)580 mock.expects(:execute).with(post_import_sql, true).in_sequence(@s)581 Dbt.runtime.expects(:info).with(' : d/zzz.sql').in_sequence(@s)582 mock.expects(:execute).with(post_import_sql_2, true).in_sequence(@s)583 mock.expects(:post_database_import).with(import).in_sequence(@s)584 mock.expects(:close).with()585 Dbt.runtime.database_import(database.import_by_name(:default), nil)586 end587 def test_migrate588 mock = Dbt::DbDriver.new589 Dbt.runtime.instance_variable_set('@db', mock)590 config = create_postgres_config()591 db_scripts = create_dir('databases')592 module_name = 'MyModule'593 table_names = ['[MyModule].[foo]']594 database = create_simple_db_definition(db_scripts, module_name, table_names)595 database.migrations = true596 Dbt::Config.default_migrations_dir_name = 'migrate22'597 migrate_sql_1 = 'SELECT 1'598 create_file('databases/migrate22/001_x.sql', migrate_sql_1)599 migrate_sql_2 = 'SELECT 2'600 create_file('databases/migrate22/002_x.sql', migrate_sql_2)601 migrate_sql_3 = 'SELECT 3'602 create_file('databases/migrate22/003_x.sql', migrate_sql_3)603 mock.expects(:open).with(config, false).in_sequence(@s)604 expect_should_migrate(mock, 'default', '001_x', true)605 expect_migrate(mock, 'default', '001_x', migrate_sql_1)606 expect_should_migrate(mock, 'default', '002_x', true)607 expect_migrate(mock, 'default', '002_x', migrate_sql_2)608 expect_should_migrate(mock, 'default', '003_x', true)609 expect_migrate(mock, 'default', '003_x', migrate_sql_3)610 mock.expects(:close).with().in_sequence(@s)611 Dbt.runtime.migrate(database)612 end613 def test_migrate_from_major_version614 mock = Dbt::DbDriver.new615 Dbt.runtime.instance_variable_set('@db', mock)616 config = create_postgres_config()617 db_scripts = create_dir('databases')618 module_name = 'MyModule'619 table_names = ['[MyModule].[foo]']620 database = create_simple_db_definition(db_scripts, module_name, table_names)621 database.migrations = true622 database.version = 'Version_1'623 Dbt::Config.default_migrations_dir_name = 'migrate22'624 migrate_sql_1 = 'SELECT 1'625 create_file('databases/migrate22/001_x.sql', migrate_sql_1)626 migrate_sql_2 = 'SELECT 2'627 create_file("databases/migrate22/002_Release-#{database.version}.sql", migrate_sql_2)628 migrate_sql_3 = 'SELECT 3'629 create_file('databases/migrate22/003_z.sql', migrate_sql_3)630 mock.expects(:open).with(config, false).in_sequence(@s)631 expect_should_migrate(mock, 'default', '001_x', true)632 expect_mark_migration_as_run(mock, 'default', '001_x')633 expect_should_migrate(mock, 'default', "002_Release-#{database.version}", true)634 expect_mark_migration_as_run(mock, 'default', "002_Release-#{database.version}")635 expect_should_migrate(mock, 'default', '003_z', true)636 expect_migrate(mock, 'default', '003_z', migrate_sql_3)637 mock.expects(:close).with().in_sequence(@s)638 Dbt.runtime.migrate(database)639 end640 def test_migrate_with_existing_migrations_applied641 mock = Dbt::DbDriver.new642 Dbt.runtime.instance_variable_set('@db', mock)643 config = create_postgres_config()644 db_scripts = create_dir('databases')645 module_name = 'MyModule'646 table_names = ['[MyModule].[foo]']647 database = create_simple_db_definition(db_scripts, module_name, table_names)648 database.migrations = true649 Dbt::Config.default_migrations_dir_name = 'migrate22'650 migrate_sql_1 = 'SELECT 1'651 create_file('databases/migrate22/001_x.sql', migrate_sql_1)652 migrate_sql_2 = 'SELECT 2'653 create_file('databases/migrate22/002_x.sql', migrate_sql_2)654 migrate_sql_3 = 'SELECT 3'655 create_file('databases/migrate22/003_x.sql', migrate_sql_3)656 mock.expects(:open).with(config, false).in_sequence(@s)657 expect_should_migrate(mock, 'default', '001_x', false)658 expect_should_migrate(mock, 'default', '002_x', false)659 expect_should_migrate(mock, 'default', '003_x', true)660 expect_migrate(mock, 'default', '003_x', migrate_sql_3)661 mock.expects(:close).with().in_sequence(@s)662 Dbt.runtime.migrate(database)663 end664 def test_create_with_migrations665 mock = Dbt::DbDriver.new666 Dbt.runtime.instance_variable_set('@db', mock)667 config = create_postgres_config()668 db_scripts = create_dir('databases')669 module_name = 'MyModule'670 table_names = ['[MyModule].[foo]', '[MyModule].[bar]', '[MyModule].[baz]']671 database = create_simple_db_definition(db_scripts, module_name, table_names)672 database.migrations = true673 database.migrations_applied_at_create = false674 Dbt::Config.default_migrations_dir_name = 'migrate22'675 migrate_sql_1 = 'SELECT 1'676 create_file('databases/migrate22/001_x.sql', migrate_sql_1)677 mock.expects(:open).with(config, true).in_sequence(@s)678 mock.expects(:drop).with(database, config).in_sequence(@s)679 mock.expects(:create_database).with(database, config).in_sequence(@s)680 mock.expects(:close).with().in_sequence(@s)681 mock.expects(:open).with(config, false).in_sequence(@s)682 mock.expects(:create_schema).with(module_name).in_sequence(@s)683 mock.expects(:setup_migrations).with().in_sequence(@s)684 expect_migrate(mock, 'default', '001_x', migrate_sql_1)685 mock.expects(:close).with().in_sequence(@s)686 Dbt.runtime.create(database)687 end688 def test_create_with_migrations_already_applied689 mock = Dbt::DbDriver.new690 Dbt.runtime.instance_variable_set('@db', mock)691 config = create_postgres_config()692 db_scripts = create_dir('databases')693 module_name = 'MyModule'694 table_names = ['[MyModule].[foo]', '[MyModule].[bar]', '[MyModule].[baz]']695 database = create_simple_db_definition(db_scripts, module_name, table_names)696 database.migrations = true697 database.migrations_applied_at_create = true698 Dbt::Config.default_migrations_dir_name = 'migrate22'699 migrate_sql_1 = 'SELECT 1'700 create_file('databases/migrate22/001_x.sql', migrate_sql_1)701 mock.expects(:open).with(config, true).in_sequence(@s)702 mock.expects(:drop).with(database, config).in_sequence(@s)703 mock.expects(:create_database).with(database, config).in_sequence(@s)704 mock.expects(:close).with().in_sequence(@s)705 mock.expects(:open).with(config, false).in_sequence(@s)706 mock.expects(:create_schema).with(module_name).in_sequence(@s)707 mock.expects(:setup_migrations).with().in_sequence(@s)708 expect_mark_migration_as_run(mock, 'default', '001_x')709 mock.expects(:close).with().in_sequence(@s)710 Dbt.runtime.create(database)711 end712 def test_load_dataset713 mock = Dbt::DbDriver.new714 Dbt.runtime.instance_variable_set('@db', mock)715 config = create_postgres_config()716 db_scripts = create_dir('databases')717 module_name = 'MyModule'718 database = create_db_definition(db_scripts,719 'MyModule' => ['[MyModule].[foo]', '[MyModule].[bar]'],720 'MyOtherModule' => ['[MyOtherModule].[baz]'])721 Dbt::Config.default_datasets_dir_name = 'mydatasets'722 create_file('databases/MyModule/mydatasets/mydataset/MyModule.foo.yml', "1:\n ID: 1\n")723 create_file('databases/MyModule/mydatasets/mydataset/MyModule.bar.yml', "1:\n ID: 1\n")724 create_file('databases/MyOtherModule/mydatasets/mydataset/MyOtherModule.baz.yml', "1:\n ID: 1\n")725 mock.expects(:open).with(config, false).in_sequence(@s)726 expect_delete(mock, 'MyOtherModule', 'baz')727 expect_delete(mock, module_name, 'bar')728 expect_delete(mock, module_name, 'foo')729 expect_fixture(mock, module_name, 'foo')730 expect_fixture(mock, module_name, 'bar')731 expect_fixture(mock, 'MyOtherModule', 'baz')732 mock.expects(:close).with().in_sequence(@s)733 Dbt.runtime.load_dataset(database, 'mydataset')734 end735 def test_import_with_module_group736 mock = Dbt::DbDriver.new737 Dbt.runtime.instance_variable_set('@db', mock)738 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))739 db_scripts = create_dir('databases')740 database = create_db_definition(db_scripts,741 'MyModule' => ['[MyModule].[foo]', '[MyModule].[bar]'],742 'MyOtherModule' => ['[MyOtherModule].[baz]'],743 'MyThirdModule' => ['[MyThirdModule].[biz]'])744 module_group = database.add_module_group('zz', :modules => ['MyOtherModule','MyThirdModule'], :import_enabled => true)745 assert_equal module_group.modules, ['MyOtherModule', 'MyThirdModule']746 assert_equal module_group.import_enabled?, true747 import = database.add_import(:default, {})748 mock.expects(:open).with(config, false).in_sequence(@s)749 expect_delete_for_table_import(mock, 'MyOtherModule', 'baz')750 expect_default_table_import(mock, import, 'MyOtherModule', 'baz')751 mock.expects(:post_data_module_import).with(import, 'MyOtherModule').in_sequence(@s)752 # TODO: This is wrong behaviour. All of deletes should occur first753 expect_delete_for_table_import(mock, 'MyThirdModule', 'biz')754 expect_default_table_import(mock, import, 'MyThirdModule', 'biz')755 mock.expects(:post_data_module_import).with(import, 'MyThirdModule').in_sequence(@s)756 mock.expects(:post_database_import).with(import).in_sequence(@s)757 mock.expects(:close).with().in_sequence(@s)758 Dbt.runtime.database_import(database.import_by_name(:default), database.module_group_by_name('zz'))759 end760 def test_module_group_up761 mock = Dbt::DbDriver.new762 Dbt.runtime.instance_variable_set('@db', mock)763 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))764 db_scripts = create_dir('databases')765 database = create_db_definition(db_scripts,766 'MyModule' => ['[MyModule].[foo]', '[MyModule].[bar]'],767 'MyOtherModule' => ['[MyOtherModule].[baz]'],768 'MyThirdModule' => ['[MyThirdModule].[biz]'])769 module_group = database.add_module_group('zz', :modules => ['MyOtherModule','MyThirdModule'])770 assert_equal module_group.modules, ['MyOtherModule', 'MyThirdModule']771 Dbt::Config.default_up_dirs = ['.']772 create_table_sql('MyOtherModule', 'a')773 create_table_sql('MyThirdModule', 'b')774 mock.expects(:open).with(config, false).in_sequence(@s)775 mock.expects(:create_schema).with('MyOtherModule').in_sequence(@s)776 expect_create_table(mock, 'MyOtherModule', '', 'a')777 mock.expects(:create_schema).with('MyThirdModule').in_sequence(@s)778 expect_create_table(mock, 'MyThirdModule', '', 'b')779 mock.expects(:close).with().in_sequence(@s)780 Dbt.runtime.up_module_group(database.module_group_by_name('zz'))781 end782 def test_module_group_down783 mock = Dbt::DbDriver.new784 Dbt.runtime.instance_variable_set('@db', mock)785 config = create_postgres_config({}, 'import' => base_postgres_config().merge('database' => 'IMPORT_DB'))786 db_scripts = create_dir('databases')787 database = create_db_definition(db_scripts,788 'MyModule' => ['[MyModule].[foo]', '[MyModule].[bar]'],789 'MyOtherModule' => ['[MyOtherModule].[baz]', '[MyOtherModule].[bark]'],790 'MyThirdModule' => ['[MyThirdModule].[biz]'])791 module_group = database.add_module_group('zz', :modules => ['MyOtherModule', 'MyThirdModule'])792 database.repository.schema_overrides['MyThirdModule'] = 'My3rdSchema'793 assert_equal module_group.modules, ['MyOtherModule', 'MyThirdModule']794 Dbt::Config.default_up_dirs = ['.']795 Dbt::Config.default_down_dirs = ['Down2', 'Down3']796 create_table_sql('MyOtherModule/Down2', 'a')797 create_table_sql('MyThirdModule/Down3', 'b')798 mock.expects(:open).with(config, false).in_sequence(@s)799 expect_create_table(mock, 'MyThirdModule', 'Down3/', 'b')800 mock.expects(:drop_schema).with('My3rdSchema', ['[MyThirdModule].[biz]']).in_sequence(@s)801 expect_create_table(mock, 'MyOtherModule', 'Down2/', 'a')802 mock.expects(:drop_schema).with('MyOtherModule', ['[MyOtherModule].[bark]','[MyOtherModule].[baz]']).in_sequence(@s)803 mock.expects(:close).with().in_sequence(@s)804 Dbt.runtime.down_module_group(database.module_group_by_name('zz'))805 end806 def test_dump_database_to_fixtures807 mock = Dbt::DbDriver.new808 Dbt.runtime.instance_variable_set('@db', mock)809 fixture_dir = create_dir('output_fixtures')810 config = create_postgres_config()811 db_scripts = create_dir('databases')812 table_names = ['[MyModule].[tblTable1]', '[MyModule].[tblTable2]']813 module_name = 'MyModule'814 database = create_simple_db_definition(db_scripts, module_name, table_names)815 Dbt::Config.default_fixture_dir_name = 'fixturesXX'816 expected_table2_sql = 'SELECT * FROM [MyModule].[tblTable2] ORDER BY ID'817 Object.const_set(:DUMP_SQL_FOR_MyModule_tblTable2, expected_table2_sql)818 begin819 mock.expects(:open).with(config, false).in_sequence(@s)820 Dbt.runtime.expects(:info).with('Dumping [MyModule].[tblTable1]').in_sequence(@s)821 mock.expects(:query).with('SELECT * FROM [MyModule].[tblTable1]').returns([{'ID' => 1}, {'ID' => 2}]).in_sequence(@s)822 Dbt.runtime.expects(:info).with('Dumping [MyModule].[tblTable2]').in_sequence(@s)823 mock.expects(:query).with(expected_table2_sql).returns([{'ID' => 1}, {'ID' => 2}]).in_sequence(@s)824 mock.expects(:close).with().in_sequence(@s)825 Dbt.runtime.dump_database_to_fixtures(database, fixture_dir)826 assert_file_exist("#{fixture_dir}/MyModule/fixturesXX/MyModule.tblTable1.yml")827 assert_file_exist("#{fixture_dir}/MyModule/fixturesXX/MyModule.tblTable2.yml")828 ensure829 Object.send(:remove_const, :DUMP_SQL_FOR_MyModule_tblTable2)830 end831 end832 def test_dump_database_to_fixtures_with_data_set833 mock = Dbt::DbDriver.new834 Dbt.runtime.instance_variable_set('@db', mock)835 fixture_dir = create_dir('output_fixtures')836 config = create_postgres_config()837 db_scripts = create_dir('databases')838 table_names = ['[MyModule].[tblTable1]']839 module_name = 'MyModule'840 database = create_simple_db_definition(db_scripts, module_name, table_names)841 data_set = 'foo'842 Dbt::Config.default_datasets_dir_name = 'dataset123'843 mock.expects(:open).with(config, false).in_sequence(@s)844 Dbt.runtime.expects(:info).with('Dumping [MyModule].[tblTable1]').in_sequence(@s)845 mock.expects(:query).with('SELECT * FROM [MyModule].[tblTable1]').returns([{'ID' => 1}, {'ID' => 2}]).in_sequence(@s)846 mock.expects(:close).with().in_sequence(@s)847 Dbt.runtime.dump_database_to_fixtures(database, fixture_dir, :data_set => data_set)848 assert_file_exist("#{fixture_dir}/MyModule/dataset123/foo/MyModule.tblTable1.yml")849 end850 def test_dump_database_to_fixtures_with_filter851 mock = Dbt::DbDriver.new852 Dbt.runtime.instance_variable_set('@db', mock)853 fixture_dir = create_dir('output_fixtures')854 config = create_postgres_config()855 db_scripts = create_dir('databases')856 table_names = ['[MyModule].[tblTable1]', '[MyModule].[tblTable2]']857 module_name = 'MyModule'858 database = create_simple_db_definition(db_scripts, module_name, table_names)859 filter = Proc.new {|t| t == '[MyModule].[tblTable1]'}860 Dbt::Config.default_fixture_dir_name = 'fixturesXX'861 mock.expects(:open).with(config, false).in_sequence(@s)862 Dbt.runtime.expects(:info).with('Dumping [MyModule].[tblTable1]').in_sequence(@s)863 mock.expects(:query).with('SELECT * FROM [MyModule].[tblTable1]').returns([{'ID' => 1}, {'ID' => 2}]).in_sequence(@s)864 mock.expects(:close).with().in_sequence(@s)865 Dbt.runtime.dump_database_to_fixtures(database, fixture_dir, :filter => filter)866 assert_file_exist("#{fixture_dir}/MyModule/fixturesXX/MyModule.tblTable1.yml")867 end868 def test_collect_fileset_for_hash869 db_scripts = create_dir('databases')870 module_name = 'MyModule'871 table_names = ['[MyModule].[foo]']872 database = create_simple_db_definition(db_scripts, module_name, table_names)873 Dbt::Config.default_up_dirs = ['.', 'Dir1', 'Dir2']874 Dbt::Config.default_finalize_dirs = ['Dir3', 'Dir4']875 Dbt::Config.default_fixture_dir_name = 'foo'876 Dbt::Config.default_pre_create_dirs = ['db-pre-create']877 Dbt::Config.default_post_create_dirs = ['db-post-create']878 Dbt::Config.default_post_create_dirs = ['db-post-create']879 files = []880 files << create_table_sql('db-pre-create', 'preCreate')881 files << create_table_sql("#{module_name}", 'a')882 files << create_table_sql("#{module_name}", 'b')883 files << create_table_sql("#{module_name}/Dir1", 'd')884 files << create_table_sql("#{module_name}/Dir1", 'c')885 files << create_table_sql("#{module_name}/Dir2", 'e')886 files << create_table_sql("#{module_name}/Dir2", 'f')887 files << create_fixture(module_name, 'foo')888 files << create_table_sql("#{module_name}/Dir3", 'g')889 files << create_table_sql("#{module_name}/Dir4", 'h')890 files << create_table_sql('db-post-create', 'postCreate')891 database.separate_import_task = true892 import = database.add_import(:default, {})893 import.pre_import_dirs = ['pre-imp1', 'pre-imp2']894 import.post_import_dirs = ['post-imp1', 'post-imp2']895 Dbt::Config.default_import_dir = 'zzzz'896 import_sql = 'INSERT INTO DBT_TEST.[foo]'897 files << create_file('databases/pre-imp1/a.sql', import_sql)898 files << create_file('databases/pre-imp2/a.sql', import_sql)899 files << create_file("databases/#{module_name}/zzzz/MyModule.foo.sql", import_sql)900 files << create_file("databases/#{module_name}/zzzz/MyModule.foo.yml", import_sql)901 files << create_file('databases/post-imp1/a.sql', import_sql)902 files << create_file('databases/post-imp2/a.sql', import_sql)903 database.migrations = true904 Dbt::Config.default_migrations_dir_name = 'migrate22'905 migrate_sql_1 = 'SELECT 1'906 files << create_file('databases/migrate22/001_x.sql', migrate_sql_1)907 # Should not be collected, as in an irrelevant directories908 create_table_sql("#{module_name}/Elsewhere", 'aaa')909 create_table_sql("#{module_name}aa/Dir1", 'aaa')910 # Should not be collected, only imports of sql and yml are included911 create_file("databases/#{module_name}/zzzz/MyModule.foo.ignore", import_sql)912 assert_equal(files.sort, Dbt.runtime.send(:collect_fileset_for_hash, database).map { |f| f.nil? ? 'alert nil' : f.gsub(/\/\.\//, '/') }.sort)913 end914 def test_hash_files_with_no_files_doesnt_crash915 Dbt.runtime.send(:hash_files, nil, [])916 end917 def test_hash_files918 database = create_simple_db_definition(create_dir('databases'), 'MyModule', [])919 create_dir('databases/generated')920 create_file('databases/generated/MyModule/base.sql', 'some')921 create_file('databases/generated/MyModule/types/typeA.sql', 'content')922 create_file('databases/generated/MyModule/views/viewA.sql', 'here')923 create_file('databases/generated/MyModule/views/viewB.sql', 'here')924 hash_1 = Dbt.runtime.send(:hash_files, database, ['databases/generated/MyModule/base.sql',925 'databases/generated/MyModule/types/typeA.sql',926 'databases/generated/MyModule/views/viewA.sql'])927 # Same content, different files928 hash_2 = Dbt.runtime.send(:hash_files, database, ['databases/generated/MyModule/base.sql',929 'databases/generated/MyModule/types/typeA.sql',930 'databases/generated/MyModule/views/viewB.sql'])931 assert_not_equal(hash_1, hash_2)932 create_file('databases/generated/MyModule/types/typeA.sql', 'here')933 create_file('databases/generated/MyModule/views/viewA.sql', 'content')934 # Same files, content switched between files935 hash_3 = Dbt.runtime.send(:hash_files, database, ['databases/generated/MyModule/base.sql',936 'databases/generated/MyModule/types/typeA.sql',937 'databases/generated/MyModule/views/viewA.sql'])938 assert_not_equal(hash_1, hash_3)939 create_file('databases/generated/MyModule/types/typeA.sql', 'content')940 create_file('databases/generated/MyModule/views/viewA.sql', 'here')941 # Same files, recreated942 hash_4 = Dbt.runtime.send(:hash_files, database, ['databases/generated/MyModule/base.sql',943 'databases/generated/MyModule/types/typeA.sql',944 'databases/generated/MyModule/views/viewA.sql'])945 assert_equal(hash_1, hash_4)946 end947 def setup948 super949 @s = sequence('main')950 end951 def create_table_sql(dir, table_name)952 create_file("databases/#{dir}/#{table_name}.sql", ct(table_name))953 end954 def ct(table_name)955 "CREATE TABLE [#{table_name}]"956 end957 def create_fixture(module_name, table_name)958 create_file("databases/#{module_name}/#{Dbt::Config.default_fixture_dir_name}/#{module_name}.#{table_name}.yml", "1:\n ID: 1\n")959 end960 def expect_create_table(mock, module_name, dirname, table_name, seq = true)961 Dbt.runtime.expects(:info).with("#{'%-15s' % module_name}: #{dirname}#{table_name}.sql").in_sequence(@s)962 mock.expects(:execute).with("CREATE TABLE [#{table_name}]", false).in_sequence(@s)963 end964 def expect_delete(mock, module_name, table_name)965 mock.expects(:execute).with("DELETE FROM [#{module_name}].[#{table_name}]", false).in_sequence(@s)966 end967 def expect_fixture(mock, module_name, table_name)968 Dbt.runtime.expects(:info).with("Fixture : #{module_name}.#{table_name}").in_sequence(@s)969 mock.expects(:pre_fixture_import).with("[#{module_name}].[#{table_name}]").in_sequence(@s)970 mock.expects(:insert).with("[#{module_name}].[#{table_name}]", 'ID' => 1).in_sequence(@s)971 mock.expects(:post_fixture_import).with("[#{module_name}].[#{table_name}]").in_sequence(@s)972 end973 def expect_migrate(mock, database_key, migration_name, sql)974 Dbt.runtime.expects(:info).with("Migration: #{migration_name}.sql").in_sequence(@s)975 mock.expects(:execute).with(sql, false).in_sequence(@s)976 expect_mark_migration_as_run(mock, database_key, migration_name)977 end978 def expect_should_migrate(mock, database_key, migration_name, result)979 mock.expects(:'should_migrate?').with(database_key, migration_name).returns(result).in_sequence(@s)980 end981 def expect_mark_migration_as_run(mock, database_key, migration_name)982 mock.expects(:mark_migration_as_run).with(database_key, migration_name).in_sequence(@s)983 end984 def expect_default_table_import(mock, import_definition, module_name, table_name)985 expect_pre_table_import(mock, import_definition, module_name, table_name, 'D')986 mock.expects(:column_names_for_table).with("[#{module_name}].[#{table_name}]").returns(['[ID]']).in_sequence(@s)987 mock.expects(:execute).with("INSERT INTO DBT_TEST.[#{module_name}].[#{table_name}]([ID])\n SELECT [ID] FROM IMPORT_DB.[#{module_name}].[#{table_name}]\n", true).in_sequence(@s)988 expect_post_table_import(mock, import_definition, module_name, table_name)989 end990 def expect_pre_table_import(mock, import_definition, module_name, table_name, import_type)991 mock.expects(:pre_table_import).with(import_definition, "[#{module_name}].[#{table_name}]").in_sequence(@s)992 Dbt.runtime.expects(:info).with("#{'%-15s' % module_name}: Importing #{module_name}.#{table_name} (By #{import_type})").in_sequence(@s)993 end994 def expect_post_table_import(mock, import_definition, module_name, table_name)995 mock.expects(:post_table_import).with(import_definition, "[#{module_name}].[#{table_name}]").in_sequence(@s)996 end997 def expect_delete_for_table_import(mock, module_name, table_name)998 Dbt.runtime.expects(:info).with("Deleting #{module_name}.#{table_name}").in_sequence(@s)999 expect_delete(mock, module_name, table_name)1000 end1001 def create_simple_db_definition(db_scripts, module_name, table_names)1002 Dbt.add_database(:default) do |db|1003 db.rake_integration = false1004 db.repository.modules = [module_name]1005 db.repository.table_map = {module_name => table_names}1006 db.search_dirs = [db_scripts]1007 end1008 end1009 def create_db_definition(db_scripts, table_map)1010 Dbt.add_database(:default) do |db|1011 db.rake_integration = false1012 db.repository.modules = table_map.keys1013 db.repository.table_map = table_map1014 db.search_dirs = [db_scripts]1015 end1016 end1017 def create_postgres_config(config = {}, top_level_config = {})1018 Dbt::Config.driver = 'postgres'1019 Dbt.repository.configuration_data = {1020 Dbt::Config.environment => base_postgres_config(config)1021 }.merge(top_level_config)1022 Dbt.repository.configuration_for_key(Dbt::Config.environment)1023 end1024 def base_postgres_config(config = {})1025 {...

Full Screen

Full Screen

test_runtime_package.rb

Source:test_runtime_package.rb Github

copy

Full Screen

...14 create_file('databases/generated/MyModule/triggers/trgA.sql', '')15 create_file('databases/generated/MyModule/finalize/finA.sql', '')16 create_file('databases/generated/MyModule/down/downA.sql', '')17 database = Dbt.add_database(:default) do |db|18 db.rake_integration = false19 db.repository.modules = ['MyModule']20 db.repository.table_map = {'MyModule' => ['foo', 'bar', 'baz']}21 db.search_dirs = [db_scripts]22 end23 output_dir = create_dir('pkg/out')24 Dbt.runtime.package_database_data(database, output_dir)25 assert_file_exist("#{output_dir}/MyModule/base.sql")26 assert_file_exist("#{output_dir}/MyModule/types/typeA.sql")27 assert_file_exist("#{output_dir}/MyModule/views/viewA.sql")28 assert_file_exist("#{output_dir}/MyModule/functions/functionA.sql")29 assert_file_exist("#{output_dir}/MyModule/stored-procedures/spA.sql")30 assert_file_exist("#{output_dir}/MyModule/misc/spA.sql")31 assert_file_exist("#{output_dir}/MyModule/fixtures/foo.yml")32 assert_file_not_exist("#{output_dir}/MyModule/fixtures/fooShouldNotCopy.yml")33 assert_file_not_exist("#{output_dir}/MyModule/fixtures/bar.sql")34 assert_file_exist("#{output_dir}/MyModule/triggers/trgA.sql")35 assert_file_exist("#{output_dir}/MyModule/finalize/finA.sql")36 assert_file_exist("#{output_dir}/MyModule/down/downA.sql")37 end38 def test_multiple_modules39 db_scripts = create_dir('databases/generated')40 create_file('databases/generated/MyModule/base.sql', '')41 create_file('databases/generated/MyOtherModule/base.sql', '')42 database = Dbt.add_database(:default) do |db|43 db.rake_integration = false44 db.repository.modules = ['MyModule', 'MyOtherModule']45 db.repository.table_map = {'MyModule' => [], 'MyOtherModule' => []}46 db.search_dirs = [db_scripts]47 end48 output_dir = create_dir('pkg/out')49 Dbt.runtime.package_database_data(database, output_dir)50 assert_file_exist("#{output_dir}/MyModule/base.sql")51 assert_file_exist("#{output_dir}/MyOtherModule/base.sql")52 end53 def test_package_through_pre_import54 db_scripts = create_dir('databases/generated')55 packaged_definition = Dbt::RepositoryDefinition.new(:modules => ['MyModule'], :table_map => {'MyModule' => ['foo', 'bar', 'baz']})56 zipfile = create_zip('data/repository.yml' => packaged_definition.to_yaml,57 'data/MyModule/base.sql' => '',58 'data/MyModule/types/typeA.sql' => '',59 'data/MyModule/views/viewA.sql' => '',60 'data/MyModule/functions/functionA.sql' => '',61 'data/MyModule/stored-procedures/spA.sql' => '',62 'data/MyModule/misc/spA.sql' => '',63 'data/MyModule/fixtures/foo.yml' => '',64 'data/MyModule/fixtures/bar.sql' => '',65 'data/MyModule/fixtures/fooShouldNotCopy.yml' => '',66 'data/MyModule/triggers/trgA.sql' => '',67 'data/MyModule/finalize/finA.sql' => '',68 'data/MyModule/down/downA.sql' => '')69 definition = Dbt::RepositoryDefinition.new(:modules => [], :table_map => {})70 File.open("#{db_scripts}/repository.yml",'w') do |f|71 f.write definition.to_yaml72 end73 database = Dbt.add_database(:default) do |db|74 db.rake_integration = false75 db.pre_db_artifacts << zipfile76 db.search_dirs = [db_scripts]77 end78 Dbt.runtime.send(:perform_load_database_config, database)79 output_dir = create_dir('pkg/out')80 Dbt.runtime.package_database_data(database, output_dir)81 assert_file_exist("#{output_dir}/MyModule/base.sql")82 assert_file_exist("#{output_dir}/MyModule/types/typeA.sql")83 assert_file_exist("#{output_dir}/MyModule/views/viewA.sql")84 assert_file_exist("#{output_dir}/MyModule/functions/functionA.sql")85 assert_file_exist("#{output_dir}/MyModule/stored-procedures/spA.sql")86 assert_file_exist("#{output_dir}/MyModule/misc/spA.sql")87 assert_file_exist("#{output_dir}/MyModule/fixtures/foo.yml")88 assert_file_not_exist("#{output_dir}/MyModule/fixtures/fooShouldNotCopy.yml")89 assert_file_not_exist("#{output_dir}/MyModule/fixtures/bar.sql")90 assert_file_exist("#{output_dir}/MyModule/triggers/trgA.sql")91 assert_file_exist("#{output_dir}/MyModule/finalize/finA.sql")92 assert_file_exist("#{output_dir}/MyModule/down/downA.sql")93 end94 def test_package_through_post_import95 db_scripts = create_dir('databases/generated')96 packaged_definition = Dbt::RepositoryDefinition.new(:modules => ['MyModule'], :table_map => {'MyModule' => ['foo', 'bar', 'baz']})97 zipfile = create_zip('data/repository.yml' => packaged_definition.to_yaml,98 'data/MyModule/base.sql' => '',99 'data/MyModule/types/typeA.sql' => '',100 'data/MyModule/views/viewA.sql' => '',101 'data/MyModule/functions/functionA.sql' => '',102 'data/MyModule/stored-procedures/spA.sql' => '',103 'data/MyModule/misc/spA.sql' => '',104 'data/MyModule/fixtures/foo.yml' => '',105 'data/MyModule/fixtures/bar.sql' => '',106 'data/MyModule/fixtures/fooShouldNotCopy.yml' => '',107 'data/MyModule/triggers/trgA.sql' => '',108 'data/MyModule/finalize/finA.sql' => '',109 'data/MyModule/down/downA.sql' => '')110 definition = Dbt::RepositoryDefinition.new(:modules => [], :table_map => {})111 File.open("#{db_scripts}/repository.yml",'w') do |f|112 f.write definition.to_yaml113 end114 database = Dbt.add_database(:default) do |db|115 db.rake_integration = false116 db.post_db_artifacts << zipfile117 db.search_dirs = [db_scripts]118 end119 Dbt.runtime.send(:perform_load_database_config, database)120 output_dir = create_dir('pkg/out')121 Dbt.runtime.package_database_data(database, output_dir)122 assert_file_exist("#{output_dir}/MyModule/base.sql")123 assert_file_exist("#{output_dir}/MyModule/types/typeA.sql")124 assert_file_exist("#{output_dir}/MyModule/views/viewA.sql")125 assert_file_exist("#{output_dir}/MyModule/functions/functionA.sql")126 assert_file_exist("#{output_dir}/MyModule/stored-procedures/spA.sql")127 assert_file_exist("#{output_dir}/MyModule/misc/spA.sql")128 assert_file_exist("#{output_dir}/MyModule/fixtures/foo.yml")129 assert_file_not_exist("#{output_dir}/MyModule/fixtures/fooShouldNotCopy.yml")130 assert_file_not_exist("#{output_dir}/MyModule/fixtures/bar.sql")131 assert_file_exist("#{output_dir}/MyModule/triggers/trgA.sql")132 assert_file_exist("#{output_dir}/MyModule/finalize/finA.sql")133 assert_file_exist("#{output_dir}/MyModule/down/downA.sql")134 end135 def test_multiple_search_dirs136 create_file('databases/MyModule/base.sql', '')137 create_file('databases/generated/MyModule/base2.sql', '')138 database = Dbt.add_database(:default) do |db|139 db.rake_integration = false140 db.repository.modules = ['MyModule']141 db.repository.table_map = {'MyModule' => ['foo', 'bar', 'baz']}142 db.search_dirs = [create_dir('databases'), create_dir('databases/generated')]143 end144 output_dir = create_dir('pkg/out')145 Dbt.runtime.package_database_data(database, output_dir)146 assert_file_exist("#{output_dir}/MyModule/base.sql")147 assert_file_exist("#{output_dir}/MyModule/base2.sql")148 end149 def test_ordering_in_index150 create_file('databases/MyModule/base1.sql', '')151 create_file('databases/MyModule/base2.sql', '')152 create_file('databases/MyModule/base3.sql', '')153 database = Dbt.add_database(:default) do |db|154 db.rake_integration = false155 db.repository.modules = ['MyModule']156 db.repository.table_map = {'MyModule' => []}157 db.search_dirs = [create_dir('databases')]158 end159 Dbt::Config.index_file_name = 'myindex.txt'160 output_dir = create_dir('pkg/out')161 Dbt.runtime.package_database_data(database, output_dir)162 assert_file_exist("#{output_dir}/MyModule/base1.sql")163 assert_file_exist("#{output_dir}/MyModule/base2.sql")164 assert_file_exist("#{output_dir}/MyModule/base3.sql")165 assert_file_exist("#{output_dir}/MyModule/myindex.txt")166 index = IO.readlines("#{output_dir}/MyModule/myindex.txt")167 assert_equal index.size, 3168 assert_equal index[0].strip, 'base1.sql'169 assert_equal index[1].strip, 'base2.sql'170 assert_equal index[2].strip, 'base3.sql'171 end172 def test_ordering_in_index_with_partial_index_supplied173 Dbt::Config.index_file_name = 'myindex.txt'174 create_file('databases/MyModule/myindex.txt', "base3.sql\nbase2.sql\n")175 create_file('databases/MyModule/base1.sql', '')176 create_file('databases/MyModule/base2.sql', '')177 create_file('databases/MyModule/base3.sql', '')178 create_file('databases/MyModule/base4.sql', '')179 database = Dbt.add_database(:default) do |db|180 db.rake_integration = false181 db.repository.modules = ['MyModule']182 db.repository.table_map = {'MyModule' => []}183 db.search_dirs = [create_dir('databases')]184 end185 output_dir = create_dir('pkg/out')186 Dbt.runtime.package_database_data(database, output_dir)187 assert_file_exist("#{output_dir}/MyModule/base1.sql")188 assert_file_exist("#{output_dir}/MyModule/base2.sql")189 assert_file_exist("#{output_dir}/MyModule/base3.sql")190 assert_file_exist("#{output_dir}/MyModule/base4.sql")191 assert_file_exist("#{output_dir}/MyModule/myindex.txt")192 index = IO.readlines("#{output_dir}/MyModule/myindex.txt")193 assert_equal index.size, 4194 assert_equal index[0].strip, 'base3.sql'195 assert_equal index[1].strip, 'base2.sql'196 assert_equal index[2].strip, 'base1.sql'197 assert_equal index[3].strip, 'base4.sql'198 end199 def test_ordering_in_index_with_full_index_supplied200 Dbt::Config.index_file_name = 'myindex.txt'201 create_file('databases/MyModule/myindex.txt', "base3.sql\nbase1.sql\nbase2.sql\n")202 create_file('databases/MyModule/base1.sql', '')203 create_file('databases/MyModule/base2.sql', '')204 create_file('databases/MyModule/base3.sql', '')205 database = Dbt.add_database(:default) do |db|206 db.rake_integration = false207 db.repository.modules = ['MyModule']208 db.repository.table_map = {'MyModule' => []}209 db.search_dirs = [create_dir('databases')]210 end211 output_dir = create_dir('pkg/out')212 Dbt.runtime.package_database_data(database, output_dir)213 assert_file_exist("#{output_dir}/MyModule/base1.sql")214 assert_file_exist("#{output_dir}/MyModule/base2.sql")215 assert_file_exist("#{output_dir}/MyModule/base3.sql")216 assert_file_exist("#{output_dir}/MyModule/myindex.txt")217 index = IO.readlines("#{output_dir}/MyModule/myindex.txt")218 assert_equal index.size, 3219 assert_equal index[0].strip, 'base3.sql'220 assert_equal index[1].strip, 'base1.sql'221 assert_equal index[2].strip, 'base2.sql'222 end223 def test_data_sets_copied224 db_scripts = create_dir('databases/generated')225 create_file('databases/generated/MyModule/zang/bing/foo.yml', '')226 database = Dbt.add_database(:default) do |db|227 db.rake_integration = false228 db.repository.modules = ['MyModule']229 db.repository.table_map = {'MyModule' => ['foo', 'bar', 'baz']}230 db.search_dirs = [db_scripts]231 db.datasets = ['bing']232 end233 Dbt::Config.default_datasets_dir_name = 'zang'234 output_dir = create_dir('pkg/out')235 Dbt.runtime.package_database_data(database, output_dir)236 assert_file_exist("#{output_dir}/MyModule/zang/bing/foo.yml")237 end238 def test_imports_copied239 db_scripts = create_dir('databases/generated')240 create_file('databases/generated/MyModule/import1/foo.yml', '')241 database = Dbt.add_database(:default) do |db|242 db.rake_integration = false243 db.repository.modules = ['MyModule']244 db.repository.table_map = {'MyModule' => ['foo', 'bar', 'baz']}245 db.search_dirs = [db_scripts]246 db.add_import(:default, {})247 end248 Dbt::Config.default_import_dir = 'import1'249 output_dir = create_dir('pkg/out')250 Dbt.runtime.package_database_data(database, output_dir)251 assert_file_exist("#{output_dir}/MyModule/import1/foo.yml")252 end253end...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Minitest_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful