Skip to content

Instantly share code, notes, and snippets.

Joakim Kolsjö joakimk

Block or report user

Report or block joakimk

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@joakimk
joakimk / gist:8703877
Last active Aug 29, 2015
Support unit testing AR models without loading all of rails
View gist:8703877
require "active_record"
# Also highly recommended:
# require "active_support/dependencies"
# ActiveSupport::Dependencies.autoload_paths += [ "app/models" ]
# ..so you don't need to do manual requires, found it to be just as fast.
# Support unit testing AR models without loading all of rails.
connection_info = YAML.load(ERB.new(File.read("config/database.yml")).result)["test"]
ActiveRecord::Base.establish_connection(connection_info)
@joakimk
joakimk / app_mappers_application_mapper.rb
Created Feb 18, 2014
Using the app/records organization method with minimapper.
View app_mappers_application_mapper.rb
require "minimapper/mapper"
class ApplicationMapper < Minimapper::Mapper
def record_class
record_name = self.class.name.gsub(/Mapper/, "Record")
record_name.constantize
end
end
View 00_blog.md

I just read http://re-factor.com/blog/2013/09/27/slow-tests-are-the-symptom-not-the-cause/.

I fully agree that the testing speed can often be a result of improving the code design.

This is probably how I'd implement a similar feature in barsoom.se's apps.

I'm mostly of the opinion that you should use the right tool for each job. Integrated tests to test integration. Unit tests to test logic and message passing.

I prefer to keep things simple and avoid dependency injection until there is a need for it outside of tests.

@joakimk
joakimk / test_helper.exs
Created Aug 12, 2014
Starting a redis server when running elixir tests
View test_helper.exs
Code.require_file "support/test_redis.exs", __DIR__
ExUnit.start
TestRedis.start
@joakimk
joakimk / gist:5efe8d04e526d521e8c6
Last active Aug 29, 2015
Alternate heroku setup for circle
View gist:5efe8d04e526d521e8c6
echo -e "machine api.heroku.com\n login $HEROKU_USER\n password $HEROKU_API_KEY\nmachine code.heroku.com\n login $HEROKU_USER\n password $HEROKU_API_KEY" > ~/.netrc && chmod 0600 /home/ubuntu/.netrc # We want to simply configure the API key, this seems easier than using the regular UI as that requires you to log in as a github user and assign yourself, etc.
@joakimk
joakimk / jocke_aliases_and_functions.sh
Created May 28, 2015
The terminal aliases and functions I somewhat regularly use (gist for easy use when pairing)
View jocke_aliases_and_functions.sh
# Rails
alias sc='bundle exec rails console'
alias ss="bundle exec rails server"
alias rs='rake spec'
alias rsu='rake spec:unit'
alias rsua='rake spec:unit:all'
alias rsa='rake spec:all'
alias mig='rake db:migrate'
alias rsp='rake testbot:spec'
alias ref='script/refresh'
@joakimk
joakimk / tip.md
Last active Aug 29, 2015
Nginx "Bad Request" 400 error because of % in the body text?
View tip.md

If you have a body like this:

payload={"data": "5 %"}

Then nginx will most likely give you a 400 bad request error.

To get around this, encode the json with "x-www-urlencoded" encoding.

A web framework like rails will transparently decode "x-www-urlencoded" for you.

@joakimk
joakimk / code.exs
Last active Aug 29, 2015
Simple example of using GenEvent to pass on messages
View code.exs
# In this example we're running jobs of some kind, and waiting for events if they finish okay or fail.
defmodule JobEvent do
def start_link do
{:ok, pid} = GenEvent.start_link(name: __MODULE__)
end
defmodule MessageForwarder do
use GenEvent
View gist:89810
#!/usr/bin/env ruby
require 'net/http'
require 'rexml/document'
@cc_server = 'your-cc-server'
include REXML
def build_green?
result = Net::HTTP.start(@cc_server, 8000) { |http| http.get('/XmlStatusReport.aspx') }
@joakimk
joakimk / Testbot stats (for rspec)
Created Jan 26, 2010
Example test runtimes using testbot
View Testbot stats (for rspec)
1 computers - 1 cpus: 29 minutes (rake spec)
5 computers - 13 cpus: 4 minutes (rake testbot:spec)
10 computers - 25 cpus: 2 minutes (rake testbot:spec) (about 60% cpu efficiency)
The testbot communication overhead is about 2-5 seconds per run. Fetching the code
and setting up the test environment takes a bit longer. The jobs are not being
perfectly balanced. There is still plenty of room for optimizations.
You can’t perform that action at this time.