Skip to content
This repository
tree: 06aca741df
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 154 lines (128 sloc) 5.307 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 147 148 149 150 151 152 153
require File.join(File.dirname(__FILE__), '_lib.rb')
require 'mosql/cli'

class MoSQL::Test::Functional::CLITest < MoSQL::Test::Functional
  TEST_MAP = <<EOF
---
mosql_test:
collection:
:meta:
:table: sqltable
:columns:
- _id: TEXT
- var: INTEGER
renameid:
:meta:
:table: sqltable2
:columns:
- id:
:source: _id
:type: TEXT
- goats: INTEGER
EOF

  def fake_cli
    # This is a hack. We should refactor cli.rb to be more testable.
    MoSQL::CLI.any_instance.expects(:setup_signal_handlers)
    cli = MoSQL::CLI.new([])
    cli.instance_variable_set(:@mongo, mongo)
    cli.instance_variable_set(:@schemamap, @map)
    cli.instance_variable_set(:@sql, @adapter)
    cli.instance_variable_set(:@options, {})
    cli
  end

  before do
    @map = MoSQL::Schema.new(YAML.load(TEST_MAP))
    @adapter = MoSQL::SQLAdapter.new(@map, sql_test_uri)

    @sequel.drop_table?(:sqltable)
    @sequel.drop_table?(:sqltable2)
    @map.create_schema(@sequel)

    @cli = fake_cli
  end

  it 'handle "u" ops without _id' do
    o = { '_id' => BSON::ObjectId.new, 'var' => 17 }
    @adapter.upsert_ns('mosql_test.collection', o)

    @cli.handle_op({ 'ns' => 'mosql_test.collection',
                     'op' => 'u',
                     'o2' => { '_id' => o['_id'] },
                     'o' => { 'var' => 27 }
                   })
    assert_equal(27, sequel[:sqltable].where(:_id => o['_id'].to_s).select.first[:var])
  end

  it 'handle "d" ops with BSON::ObjectIds' do
    o = { '_id' => BSON::ObjectId.new, 'var' => 17 }
    @adapter.upsert_ns('mosql_test.collection', o)

    @cli.handle_op({ 'ns' => 'mosql_test.collection',
                     'op' => 'd',
                     'o' => { '_id' => o['_id'] },
                   })
    assert_equal(0, sequel[:sqltable].where(:_id => o['_id'].to_s).count)
  end

  it 'handle "u" ops with $set and BSON::ObjectIDs' do
    o = { '_id' => BSON::ObjectId.new, 'var' => 17 }
    @adapter.upsert_ns('mosql_test.collection', o)

    # $set's are currently a bit of a hack where we read the object
    # from the db, so make sure the new object exists in mongo
    connect_mongo['mosql_test']['collection'].insert(o.merge('var' => 100),
                                                     :w => 1)

    @cli.handle_op({ 'ns' => 'mosql_test.collection',
                     'op' => 'u',
                     'o2' => { '_id' => o['_id'] },
                     'o' => { '$set' => { 'var' => 100 } },
                   })
    assert_equal(100, sequel[:sqltable].where(:_id => o['_id'].to_s).select.first[:var])
  end

  it 'handle "u" ops with $set and a renamed _id' do
    o = { '_id' => BSON::ObjectId.new, 'goats' => 96 }
    @adapter.upsert_ns('mosql_test.renameid', o)

    # $set's are currently a bit of a hack where we read the object
    # from the db, so make sure the new object exists in mongo
    connect_mongo['mosql_test']['renameid'].insert(o.merge('goats' => 0),
                                                   :w => 1)

    @cli.handle_op({ 'ns' => 'mosql_test.renameid',
                     'op' => 'u',
                     'o2' => { '_id' => o['_id'] },
                     'o' => { '$set' => { 'goats' => 0 } },
                   })
    assert_equal(0, sequel[:sqltable2].where(:id => o['_id'].to_s).select.first[:goats])
  end

  it 'handles "d" ops with a renamed id' do
    o = { '_id' => BSON::ObjectId.new, 'goats' => 1 }
    @adapter.upsert_ns('mosql_test.renameid', o)

    @cli.handle_op({ 'ns' => 'mosql_test.renameid',
                     'op' => 'd',
                     'o' => { '_id' => o['_id'] },
                   })
    assert_equal(0, sequel[:sqltable2].where(:id => o['_id'].to_s).count)
  end

  describe '.bulk_upsert' do
    it 'inserts multiple rows' do
      objs = [
              { '_id' => BSON::ObjectId.new, 'var' => 0 },
              { '_id' => BSON::ObjectId.new, 'var' => 1 },
              { '_id' => BSON::ObjectId.new, 'var' => 3 },
             ].map { |o| @map.transform('mosql_test.collection', o) }

      @cli.bulk_upsert(sequel[:sqltable], 'mosql_test.collection',
                       objs)

      assert(sequel[:sqltable].where(:_id => objs[0].first, :var => 0).count)
      assert(sequel[:sqltable].where(:_id => objs[1].first, :var => 1).count)
      assert(sequel[:sqltable].where(:_id => objs[2].first, :var => 3).count)
    end

    it 'upserts' do
      _id = BSON::ObjectId.new
      objs = [
              { '_id' => _id, 'var' => 0 },
              { '_id' => BSON::ObjectId.new, 'var' => 1 },
              { '_id' => BSON::ObjectId.new, 'var' => 3 },
             ].map { |o| @map.transform('mosql_test.collection', o) }

      @cli.bulk_upsert(sequel[:sqltable], 'mosql_test.collection',
                       objs)

      newobjs = [
                 { '_id' => _id, 'var' => 117 },
                 { '_id' => BSON::ObjectId.new, 'var' => 32 },
                ].map { |o| @map.transform('mosql_test.collection', o) }
      @cli.bulk_upsert(sequel[:sqltable], 'mosql_test.collection',
                       newobjs)


      assert(sequel[:sqltable].where(:_id => newobjs[0].first, :var => 117).count)
      assert(sequel[:sqltable].where(:_id => newobjs[1].first, :var => 32).count)
    end
  end
end
Something went wrong with that request. Please try again.