Skip to content

Commit

Permalink
Tests passing on api specs with database handling
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyaray committed Oct 29, 2017
1 parent 8c59afa commit 2155a9f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ end
group :development, :test do
gem 'sqlite3'

gem 'database_cleaner'

gem 'pry'
gem 'rerun'

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ GEM
concurrent-ruby (1.0.5)
crack (0.4.3)
safe_yaml (~> 1.0.0)
database_cleaner (1.6.1)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
docile (1.1.5)
Expand Down Expand Up @@ -143,6 +144,7 @@ PLATFORMS
ruby

DEPENDENCIES
database_cleaner
dry-struct
dry-types
econfig
Expand Down
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ namespace :db do
Sequel::Migrator.run(app.DB, 'infrastructure/db/migrations')
end

desc 'Drop all tables'
task :drop do
require_relative 'config/environment.rb'
# drop according to dependencies
app.DB.drop_table :repos_contributors
app.DB.drop_table :repos
app.DB.drop_table :collaborators
app.DB.drop_table :schema_info
end

desc 'Reset all database tables'
task reset: [:drop, :migrate]

desc 'Delete dev or test database file'
task :wipe do
if app.environment == :production
Expand Down
18 changes: 9 additions & 9 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ class Api < Roda
routing.on 'api' do
# /api/v0.1 branch
routing.on 'v0.1' do
# /api/v0.1/:ownername/:repo_name branch
routing.on 'repo', String, String do |ownername, repo_name|
# GET /api/v0.1/repo/:ownername/:repo_name request
routing.is do
# /api/v0.1/:ownername/:reponame branch
routing.on 'repo', String, String do |ownername, reponame|
# GET /api/v0.1/repo/:ownername/:reponame request
routing.get do
repo = Database::ORM[Entity::Repo]
.find_full_name(ownername, repo_name)
.find_full_name(ownername, reponame)

routing.halt(404, error: 'Repository not found') unless repo
repo.to_h
end

# post '/api/v0.1/repo/:ownername/:repo_name
# POST '/api/v0.1/repo/:ownername/:reponame
routing.post do
github_repo = Github::RepoMapper.new(app.config)
begin
repo = github_repo.load(ownername, repo_name)
github_repo = Github::RepoMapper.new(app.config)
repo = github_repo.load(ownername, reponame)
rescue StandardError
routing.halt(404, error: 'Repository not found')
routing.halt(404, error: "Repo not found")
end

stored_repo = Database::ORM[Entity::Repo].find_or_create(repo)
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/db/orm/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'sequel'

Dir.glob("#{File.dirname(__FILE__)}/*.rb").each do |file|
Dir.glob("#{File.dirname(__FILE__)}/*_orm.rb").each do |file|
require file
end

Expand Down
24 changes: 14 additions & 10 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@
end

describe 'Repo information' do
it 'HAPPY: should provide correct repo attributes' do
get "#{API_VER}/repo/#{USERNAME}/#{REPO_NAME}"
before do
# DatabaseCleaner.clean
Rake::Task['db:reset'].invoke
end

it 'HAPPY: should retrieve and store repo and collaborators' do
post "#{API_VER}/repo/#{USERNAME}/#{REPO_NAME}"
_(last_response.status).must_equal 200
repo_data = JSON.parse last_response.body
_(repo_data.size).must_be :>, 0
end

it 'SAD: should raise exception on incorrect repo' do
get "#{API_VER}/repo/#{USERNAME}/bad_repo"
_(last_response.status).must_equal 404
body = JSON.parse last_response.body
_(body.keys).must_include 'error'
end
end
it 'HAPPY: should find stored repo and collaborators' do
post "#{API_VER}/repo/#{USERNAME}/#{REPO_NAME}"

describe 'Collaborator information' do
get "#{API_VER}/repo/#{USERNAME}/#{REPO_NAME}"
_(last_response.status).must_equal 200
repo_data = JSON.parse last_response.body
_(repo_data.size).must_be :>, 0
end
end
end
16 changes: 7 additions & 9 deletions spec/repo_spec.rb → spec/gh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Econfig.env = 'development'
Econfig.root = '.'

GH_TOKEN = config.gh_token
# GH_TOKEN = config.gh_token
CORRECT = YAML.safe_load(File.read('spec/fixtures/gh_results.yml'))
CASSETTE_FILE = 'github_api'.freeze

Expand All @@ -24,34 +24,32 @@

describe 'Repo information' do
it 'HAPPY: should provide correct repo attributes' do
api = CodePraise::Github::Api.new(GH_TOKEN)
repo_mapper = CodePraise::Github::RepoMapper.new(api)
repo_mapper = CodePraise::Github::RepoMapper.new(app.config)
repo = repo_mapper.load(USERNAME, REPO_NAME)
_(repo.size).must_equal CORRECT['size']
_(repo.git_url).must_equal CORRECT['git_url']
end

it 'SAD: should raise exception on incorrect repo' do
proc do
api = CodePraise::Github::Api.new(GH_TOKEN)
repo_mapper = CodePraise::Github::RepoMapper.new(api)
repo_mapper = CodePraise::Github::RepoMapper.new(app.config)
repo_mapper.load(USERNAME, 'sad_repo_name')
end.must_raise CodePraise::Github::Api::Errors::NotFound
end

it 'SAD: should raise exception when unauthorized' do
proc do
sad_api = CodePraise::Github::Api.new('sad_token')
repo_mapper = CodePraise::Github::RepoMapper.new(sad_api)
require 'ostruct'
sad_config = OpenStruct.new(gh_token: 'sad_token')
repo_mapper = CodePraise::Github::RepoMapper.new(sad_config)
repo_mapper.load(USERNAME, REPO_NAME)
end.must_raise CodePraise::Github::Api::Errors::Unauthorized
end
end

describe 'Collaborator information' do
before do
api = CodePraise::Github::Api.new(GH_TOKEN)
repo_mapper = CodePraise::Github::RepoMapper.new(api)
repo_mapper = CodePraise::Github::RepoMapper.new(app.config)
@repo = repo_mapper.load(USERNAME, REPO_NAME)
end

Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

require_relative 'test_load_all'

load 'Rakefile'
Rake::Task['db:reset'].invoke


USERNAME = 'soumyaray'.freeze
REPO_NAME = 'YPBT-app'.freeze
CASSETTES_FOLDER = 'spec/fixtures/cassettes'.freeze
Expand All @@ -26,3 +30,7 @@
c.filter_sensitive_data('<GITHUB_TOKEN>') { github_token }
c.filter_sensitive_data('<GITHUB_TOKEN_ESC>') { CGI.escape(github_token) }
end

# DB = app.DB
# require 'database_cleaner'
# DatabaseCleaner.strategy = :truncation

0 comments on commit 2155a9f

Please sign in to comment.