Skip to content

Commit

Permalink
removed storer.kata_exists() and storer.avatar_exists() methods; kata…
Browse files Browse the repository at this point in the history
…_controller.run_tests() now does one less service round-trip
  • Loading branch information
JonJagger committed Dec 19, 2016
1 parent f9a0c95 commit d279437
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 108 deletions.
22 changes: 20 additions & 2 deletions app/controllers/forker_controller.rb
Expand Up @@ -5,12 +5,12 @@ def fork
result = { forked: false }
error = false

if !error && kata.nil?
if !error && bad_kata_id?
error = true
result[:reason] = "dojo(#{id})"
end

if !error && avatar.nil?
if !error && bad_avatar_name?
error = true
result[:reason] = "avatar(#{avatar_name})"
end
Expand Down Expand Up @@ -71,5 +71,23 @@ def fork
include TimeNow
include UniqueId

def bad_kata_id?
begin
kata.created
return false
rescue StandardError
return true
end
end

def bad_avatar_name?
begin
avatar.lights
return false
rescue StandardError
return true
end
end

end

2 changes: 0 additions & 2 deletions app/controllers/kata_controller.rb
Expand Up @@ -12,9 +12,7 @@ def edit
end

def run_tests
fail "sorry, we can't do that" if kata.nil? || avatar.nil?
@avatar = avatar

incoming = params[:file_hashes_incoming]
outgoing = params[:file_hashes_outgoing]
delta = FileDeltaMaker.make_delta(incoming, outgoing)
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/setup_custom_start_point_controller.rb
Expand Up @@ -7,7 +7,8 @@ def show
@id = id
@title = 'create'
custom_names = display_names_of(dojo.custom)
index = choose_language(custom_names, dojo.katas[id])
kata = (id != nil) ? dojo.katas[id] : nil
index = choose_language(custom_names, kata)
@start_points = ::DisplayNamesSplitter.new(custom_names, index)
end

Expand Down
6 changes: 4 additions & 2 deletions app/controllers/setup_default_start_point_controller.rb
Expand Up @@ -9,7 +9,8 @@ def show_languages
@id = id
@title = 'create'
languages_names = display_names_of(languages)
index = choose_language(languages_names, dojo.katas[id])
kata = (id != nil) ? dojo.katas[id] : nil
index = choose_language(languages_names, kata)
@start_points = ::DisplayNamesSplitter.new(languages_names, index)
@max_seconds = runner.max_seconds
end
Expand All @@ -20,7 +21,8 @@ def show_exercises
@language = params[:language]
@test = params[:test]
@exercises_names,@exercises = read_exercises
@initial_index = choose_exercise(@exercises_names, dojo.katas[id])
kata = (id != nil) ? dojo.katas[id] : nil
@initial_index = choose_exercise(@exercises_names, kata)
end

def save
Expand Down
41 changes: 19 additions & 22 deletions app/lib/fake_storer.rb
Expand Up @@ -38,10 +38,6 @@ def completions(outer_dir)

# - - - - - - - - - - - - - - - - - - - - - - - -

def kata_exists?(id)
valid?(id) && kata_dir(id).exists?
end

def create_kata(manifest)
dir = kata_dir(manifest[:id])
dir.make
Expand Down Expand Up @@ -73,10 +69,6 @@ def kata_start_avatar(id, avatar_names)

# - - - - - - - - - - - - - - - - - - - - - - - -

def avatar_exists?(id, name)
avatar_dir(id, name).exists?
end

def avatar_increments(id, name)
[tag0(id)] + increments(id, name)
end
Expand Down Expand Up @@ -160,14 +152,6 @@ def increments(id, name)

# - - - - - - - - - - - - - - - -

def valid?(id)
id.class.name == 'String' &&
id.length == 10 &&
id.chars.all? { |char| hex?(char) }
end

# - - - - - - - - - - - - - - - -

def write_avatar_increments(id, name, increments)
dir = avatar_dir(id, name)
dir.write_json(increments_filename, increments)
Expand All @@ -181,10 +165,6 @@ def write_tag_manifest(id, name, tag, files)

# - - - - - - - - - - - - - - - -

def hex?(char)
'0123456789ABCDEF'.include?(char)
end

def increments_filename
'increments.json'
end
Expand All @@ -193,7 +173,24 @@ def manifest_filename
'manifest.json'
end

include NearestAncestors
def env_var; nearest_ancestors(:env_var); end
# - - - - - - - - - - - - - - - -

def valid?(id)
id.class.name == 'String' &&
id.length == 10 &&
id.chars.all? { |char| hex?(char) }
end

def hex?(char)
'0123456789ABCDEF'.include?(char)
end

def kata_exists?(id)
valid?(id) && kata_dir(id).exists?
end

def avatar_exists?(id, name)
avatar_dir(id, name).exists?
end

end
17 changes: 0 additions & 17 deletions app/lib/storer_service.rb
Expand Up @@ -11,15 +11,6 @@ def initialize(parent)

# - - - - - - - - - - - -

def kata_exists?(kata_id)
kata_exists(kata_id)
end
def avatar_exists?(kata_id, avatar_name)
avatar_exists(kata_id, avatar_name)
end

# - - - - - - - - - - - -

def path
get(__method__)
end
Expand All @@ -42,10 +33,6 @@ def completions(id)

# - - - - - - - - - - - -

def kata_exists(kata_id)
get(__method__, kata_id)
end

def kata_manifest(kata_id)
get(__method__, kata_id)
end
Expand All @@ -60,10 +47,6 @@ def kata_started_avatars(kata_id)

# - - - - - - - - - - - -

def avatar_exists(kata_id, avatar_name)
get(__method__, kata_id, avatar_name)
end

def avatar_increments(kata_id, avatar_name)
get(__method__, kata_id, avatar_name)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/avatar.rb
Expand Up @@ -47,7 +47,7 @@ def active?
# instructions. I don't want these avatars appearing on the
# dashboard. When forking a new kata you can enter as one
# animal to sanity check it is ok (but not press [test])
storer.avatar_exists?(kata.id, name) && !lights.empty?
!lights.empty?
end

def tags
Expand Down
2 changes: 1 addition & 1 deletion app/models/katas.rb
Expand Up @@ -23,7 +23,7 @@ def completed(id)
end

def [](id)
storer.kata_exists?(id) ? Kata.new(self, id) : nil
Kata.new(self, id)
end

# modifiers
Expand Down
5 changes: 4 additions & 1 deletion test.sh
@@ -1,11 +1,12 @@
#!/bin/bash
set -e
#Don't do [set -e] because we want to get coverage stats out

storer_cid=`docker ps --all --quiet --filter "name=cyber-dojo-storer"`
docker exec ${storer_cid} sh -c "rm -rf /tmp/cyber-dojo/katas/*"

web_cid=`docker ps --all --quiet --filter "name=cyber-dojo-web"`
docker exec ${web_cid} sh -c "cd test && ./run.sh ${*}"
status=$?

# copy coverage stats out of container
my_dir="$( cd "$( dirname "${0}" )" && pwd )"
Expand All @@ -14,3 +15,5 @@ mkdir -p ${my_dir}/coverage
src=${web_cid}:/tmp/cyber-dojo/coverage/.
dst=${my_dir}/coverage/
docker cp ${src} ${dst}

exit ${status}
48 changes: 0 additions & 48 deletions test/app_lib/fake_storer_test.rb
Expand Up @@ -26,7 +26,6 @@ def storer
'but symbol-keys have become string-keys' do
manifest = make_manifest(kata_id = '603E8BAEDF')
storer.create_kata(manifest)
assert kata_exists?(kata_id)
expected = manifest
actual = kata_manifest(kata_id)
assert_equal expected.keys.size, actual.keys.size
Expand All @@ -35,39 +34,6 @@ def storer
end
end

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# kata_exists?(id)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

test '9D3DE636',
'kata_exists?(id) for id that is not a 10-digit string is false' do
not_string = Object.new
refute kata_exists?(not_string)
nine = 'DE6369A32'
assert_equal 9, nine.length
refute kata_exists?(nine)
end

test '9D3DB6ED',
'kata.exists?(id) for 10 digit id with non hex-chars is false' do
has_a_g = '123456789G'
assert_equal 10, has_a_g.length
refute kata_exists?(has_a_g)
end

test '9D3CF9F2',
'kata.exists?(id) for non-existing id is false' do
kata_id = '9D3CF9F23D'
assert_equal 10, kata_id.length
refute kata_exists?(kata_id)
end

test '9D3DFB05',
'kata.exists?(id) if kata with existing id is true' do
create_kata(kata_id = '9D3DFB0532')
assert kata_exists?(kata_id)
end

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# completions(id)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -161,21 +127,15 @@ def storer
test '9D381C02',
'unstarted avatar does not exist' do
create_kata(kata_id = '9D381C0200')
refute avatar_exists?(kata_id, lion)
assert_equal [], started_avatars(kata_id)
end

test '9D316F7B',
'started avatars exist' do
create_kata(kata_id = '9D316F7B00')
assert_equal lion, start_avatar(kata_id, [lion])
assert avatar_exists?(kata_id, lion)
refute avatar_exists?(kata_id, salmon)
assert_equal [lion], started_avatars(kata_id)

assert_equal salmon, start_avatar(kata_id, [lion,salmon])
assert avatar_exists?(kata_id, lion)
assert avatar_exists?(kata_id, salmon)
assert_equal [lion,salmon].sort, started_avatars(kata_id).sort
end

Expand Down Expand Up @@ -268,10 +228,6 @@ def create_kata(kata_id)
storer.create_kata(manifest)
end

def kata_exists?(kata_id)
storer.kata_exists?(kata_id)
end

def kata_manifest(kata_id)
storer.kata_manifest(kata_id)
end
Expand All @@ -280,10 +236,6 @@ def completed(id)
storer.completed(id)
end

def avatar_exists?(kata_id, avatar_name)
storer.avatar_exists?(kata_id, avatar_name)
end

def start_avatar(kata_id, avatar_names)
storer.kata_start_avatar(kata_id, avatar_names)
end
Expand Down
4 changes: 0 additions & 4 deletions test/app_lib/storer_service_test.rb
Expand Up @@ -23,13 +23,11 @@ class StorerServiceTest < AppLibTestBase

assert_equal '/tmp/cyber-dojo/katas', storer.path

refute storer.kata_exists?(kata_id)
refute all_ids.include? kata_id

manifest = make_manifest(kata_id)
storer.create_kata(manifest)

assert storer.kata_exists?(kata_id)
assert all_ids.include? kata_id

expected = manifest
Expand All @@ -39,10 +37,8 @@ class StorerServiceTest < AppLibTestBase
assert_equal value, actual[key.to_s]
end
assert_equal kata_id, storer.completed(kata_id[0..5])
refute storer.avatar_exists?(kata_id, lion)
assert_equal [], storer.kata_started_avatars(kata_id)
assert_equal lion, storer.kata_start_avatar(kata_id, [lion])
assert storer.avatar_exists?(kata_id, lion)
assert_equal [lion], storer.kata_started_avatars(kata_id)
files0 = storer.kata_manifest(kata_id)['visible_files']
assert_equal files0, storer.tag_visible_files(kata_id, lion, tag=0)
Expand Down
10 changes: 4 additions & 6 deletions test/app_models/katas_test.rb
Expand Up @@ -12,7 +12,7 @@ def setup
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

test 'F3B8B1',
'katas[bad-id] is nil' do
'katas[bad-id] is not nil but any access to storer service raises' do
bad_ids = [
nil, # not string
Object.new, # not string
Expand All @@ -22,11 +22,9 @@ def setup
'123456789S' # not 0-9A-F
]
bad_ids.each do |bad_id|
begin
kata = katas[bad_id]
assert_nil kata
rescue StandardError
end
kata = katas[bad_id]
refute_nil kata
assert_raises { kata.age }
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/print_coverage_summary.rb
Expand Up @@ -179,7 +179,7 @@ def gather_done(stats, totals)
[ 'total assertions per sec > 50', totals[:assertions_per_sec] > 50 ]
]
done << coverage(stats, 'app_helpers') if modules.include? 'app_helpers'
done << coverage(stats, 'app_lib') if modules.include? 'app_lib'
done << coverage(stats, 'app_lib', 99) if modules.include? 'app_lib'
done << coverage(stats, 'app_models') if modules.include? 'app_models'
done << coverage(stats, 'lib') if modules.include? 'lib'
done << coverage(stats, 'app_controllers', 98) if modules.include? 'app_controllers'
Expand Down

0 comments on commit d279437

Please sign in to comment.