forked from akiellor/coder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrepository_spec.rb
60 lines (44 loc) · 1.56 KB
/
repository_spec.rb
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
require 'json'
require 'minitest/autorun'
require 'rack/test'
require './repository'
DB = Sequel.connect 'sqlite://test.db'
Sequel.extension :migration
Sequel::IntegerMigrator.new(DB, 'migrations').run
module TransactionalTestCase
def run *args
[].tap do |rv|
DB.transaction(rollback: :always) { rv << super(*args) }
end.pop
end
end
describe Repository do
include Rack::Test::Methods
include TransactionalTestCase
let(:app) { Repository }
let(:forks) { DB[:forks] }
let(:stub_fork) { {'abc' => '123'} }
let(:stub_fork_2) { {'123' => 'abc'} }
before { @fork_id = forks.insert(data: stub_fork.to_json) }
it 'should reply with default data' do
Dir.mktmpdir do |tmpdir|
Dir.mkdir File.join(tmpdir, 'spec')
File.open(File.join(tmpdir, 'thing.js'), 'w') { |f| f << 'abc' }
File.open(File.join(tmpdir, 'spec', 'thing_spec.js'), 'w') { |f| f << '123' }
get '/v1/forks/new', {}, {'repository.path' => tmpdir}
assert last_response.ok?, 'Failed to get new fork'
JSON(last_response.body).must_equal 'thing' => {'src' => 'abc', 'spec' => '123'}
end
end
it 'should create new forks' do
post '/v1/forks/new', stub_fork.to_json
assert last_response.successful?, 'Failed to create a new fork'
new_id = JSON(last_response.body)['id']
forks[id: new_id][:data].must_equal stub_fork.to_json
end
it 'should reply with the data for a fork' do
get "/v1/forks/#{@fork_id}"
assert last_response.ok?, "Failed to get fork: #{@fork_id}"
JSON(last_response.body).must_equal stub_fork
end
end