fixie 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,6 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ log
19
+ *.swp
20
+ *.swo
data/README.md CHANGED
@@ -18,7 +18,81 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- Coming Soon!
21
+ To use Fixie, you first create some fixture files in a directory called `fixtures` somewhere in your load path, typically in the `test` directory:
22
+
23
+ test/
24
+ └── fixtures
25
+ ├── cities.yml
26
+ └── countries.yml
27
+
28
+ Fixie use Sequel to load the data into the database. Fixie will work even if you aren't using Sequel in your application. You must configure the Fixie database and then include the Fixie module in your test class to use it. Your test helper might look like this:
29
+
30
+ ``` ruby
31
+ Fixie.db = Sequel.sqlite
32
+
33
+ class Test::Unit::TestCase
34
+ include Fixie
35
+ end
36
+ ```
37
+
38
+ Now all the fixtures will be loaded into the test database, and you can access them from within a test like this:
39
+
40
+ ``` ruby
41
+ def test_something
42
+ assert_equal "US", countries(:us)
43
+ end
44
+ ```
45
+
46
+ You can also access the fixtures in any context once they have been loaded like this:
47
+
48
+ ``` ruby
49
+ Fixie.countries(:us)
50
+ ```
51
+
52
+ Fixtures are defined in YAML files like this:
53
+
54
+ ``` yaml
55
+ us:
56
+ id: 1
57
+ name: United States
58
+ ```
59
+
60
+ If left out, the value for the `id` attribute will be automatically generated based on the name of the fixture:
61
+
62
+ ``` yaml
63
+ us:
64
+ name: United States
65
+ ```
66
+
67
+ You can then use it in other fixtures to reference the other record by name instead of id:
68
+
69
+ ``` yaml
70
+ baltimore:
71
+ name: Baltimore
72
+ country: us
73
+ ```
74
+
75
+ You can also use ERB in the YAML files:
76
+
77
+ ``` yaml
78
+ baltimore:
79
+ name: Baltimore
80
+ country: us
81
+ created_at: <%= Time.now %>
82
+ ```
83
+
84
+ The ERB is evaluated in the context of the Fixie module, so if there is anything else you want to make available in that context, just mix the module into Fixie:
85
+
86
+ ``` ruby
87
+ Fixie.extend(FastGettext::Translation)
88
+ ```
89
+
90
+ ``` yaml
91
+ baltimore:
92
+ name: <%=_ "Baltimore" %>
93
+ country: us
94
+ created_at: <%= Time.now %>
95
+ ```
22
96
 
23
97
  ## Contributing
24
98
 
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs = ["lib"]
6
+ t.test_files = FileList["test/*_test.rb"]
7
+ end
8
+
9
+ task :default => :test
data/fixie.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "fixie"
5
- gem.version = "0.0.1"
5
+ gem.version = "0.1.0"
6
6
  gem.authors = ["Paul Barry"]
7
7
  gem.email = ["mail@paulbarry.com"]
8
8
  gem.description = %q{A standalone library for managing test fixture data}
@@ -13,4 +13,8 @@ Gem::Specification.new do |gem|
13
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
14
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
15
  gem.require_paths = ["lib"]
16
+
17
+ gem.add_runtime_dependency "activesupport"
18
+ gem.add_runtime_dependency "sequel"
19
+ gem.add_development_dependency "sqlite3"
16
20
  end
data/lib/fixie/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fixie
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/fixie.rb CHANGED
@@ -1,5 +1,87 @@
1
- require "fixie/version"
1
+ require 'fixie/version'
2
+ require 'active_support/core_ext'
3
+ require 'erb'
4
+ require 'yaml'
5
+ require 'zlib'
2
6
 
3
7
  module Fixie
4
- # Your code goes here...
8
+ MAX_ID = 2 ** 30 - 1
9
+
10
+ def self.identify(label)
11
+ Zlib.crc32(label.to_s) % MAX_ID
12
+ end
13
+
14
+ def self.version
15
+ VERSION
16
+ end
17
+
18
+ class << self
19
+ attr_accessor :db, :dir, :fixtures
20
+ end
21
+
22
+ def self.dir
23
+ @dir ||= begin
24
+ dir = $LOAD_PATH.detect{|p| Dir.exists?(File.join(p, "fixtures")) }
25
+ if dir
26
+ File.join(dir, "fixtures")
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.included(cls)
32
+ fixtures # load the fixtures
33
+ end
34
+
35
+ def self.fixtures
36
+ @fixtures ||= begin
37
+ all_fixtures = {}
38
+
39
+ unless Dir.exists?(Fixie.dir)
40
+ raise "There is no directory in the $LOAD_PATH with a 'fixtures' directory in it"
41
+ end
42
+
43
+ # First pass, load all the fixtures
44
+ Dir[File.join(Fixie.dir, "**/*.yml")].each do |file|
45
+ fixture_name = File.basename(file, '.yml')
46
+ fixture_class = fixture_name.singularize.classify.constantize
47
+
48
+ fixtures = YAML.load(ERB.new(IO.read(file)).result(binding)).symbolize_keys
49
+
50
+ fixtures.each do |name, data|
51
+ data["id"] ||= identify(name)
52
+ end
53
+
54
+ unless respond_to?(fixture_name)
55
+ define_method(fixture_name) do |fixture|
56
+ fixture_class.new(fixtures[fixture])
57
+ end
58
+ end
59
+
60
+ all_fixtures[fixture_name.to_sym] = fixtures
61
+ end
62
+
63
+ # Do a second pass to resolve associations
64
+ all_fixtures.each do |fixture_name, fixtures|
65
+ fixture_class = fixture_name.to_s.singularize.classify.constantize
66
+ fixtures.each do |name, data|
67
+ data.keys.each do |attr|
68
+ associated_fixtures = all_fixtures[attr.to_s.pluralize.to_sym]
69
+ if associated_fixtures && fixture_class.method_defined?("#{attr}_id=")
70
+ associated_fixture = associated_fixtures[data[attr].to_sym]
71
+ if associated_fixture
72
+ data["#{attr}_id"] = associated_fixture['id']
73
+ data.delete(attr)
74
+ end
75
+ end
76
+ end
77
+ db[fixture_name].insert(data)
78
+ end
79
+
80
+ end
81
+
82
+ all_fixtures
83
+ end
84
+ end
85
+
86
+ extend self
5
87
  end
@@ -0,0 +1,87 @@
1
+ require 'test/unit'
2
+ require 'logger'
3
+ require 'sequel'
4
+ require 'sqlite3'
5
+
6
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
7
+ $LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
8
+
9
+ require 'fixie'
10
+
11
+ class Model
12
+ attr_accessor :id
13
+
14
+ def initialize(attrs={})
15
+ attrs.each do |attr, value|
16
+ send("#{attr}=", value)
17
+ end
18
+ end
19
+ end
20
+
21
+ class Country < Model
22
+ attr_accessor :name, :code
23
+ end
24
+
25
+ class City < Model
26
+ attr_accessor :name, :country, :country_id, :created_at
27
+ end
28
+
29
+ Fixie.db = Sequel.sqlite(logger: Logger.new("log/test.log"))
30
+
31
+ Fixie.db.create_table :countries do
32
+ primary_key :id
33
+ String :name
34
+ String :code
35
+ end
36
+
37
+ Fixie.db.create_table :cities do
38
+ primary_key :id
39
+ Integer :country_id
40
+ String :name
41
+ String :nick_name
42
+ Time :created_at
43
+ end
44
+
45
+ module FakeGetText
46
+ def _(key)
47
+ key.to_s.titleize
48
+ end
49
+ end
50
+
51
+ Fixie.extend(FakeGetText)
52
+
53
+ class FixieTest < MiniTest::Unit::TestCase
54
+ include Fixie
55
+
56
+ def test_explicit_id
57
+ assert_equal "United States", countries(:us).name
58
+ assert_equal 1, countries(:us).id
59
+ end
60
+
61
+ def test_implicity_id
62
+ assert_equal "Canada", countries(:canada).name
63
+ assert_equal 842554592, countries(:canada).id
64
+ end
65
+
66
+ def test_association
67
+ assert_equal countries(:us).id, cities(:baltimore).country_id
68
+ # TODO: make this work
69
+ # assert_equal countries(:us), cities(:baltimore).country
70
+ end
71
+
72
+ def test_get
73
+ assert_equal "US", Fixie.countries(:us).code
74
+ end
75
+
76
+ def test_loaded_in_db
77
+ assert_equal ["CA","US"], Fixie.db[:countries].all.map{|c| c[:code] }.sort
78
+ end
79
+
80
+ def test_erb
81
+ assert_equal Time, cities(:baltimore).created_at.class
82
+ end
83
+
84
+ def test_erb_is_evaled_in_context_of_fixie
85
+ assert_equal "Baltimore", cities(:baltimore).name
86
+ end
87
+ end
@@ -0,0 +1,9 @@
1
+ baltimore:
2
+ name: <%=_ :baltimore %>
3
+ country: us
4
+ created_at: <%= Time.now %>
5
+
6
+ montreal:
7
+ name: Montreal
8
+ country: canada
9
+ created_at: <%= Time.now %>
@@ -0,0 +1,8 @@
1
+ us:
2
+ id: 1
3
+ name: United States
4
+ code: US
5
+
6
+ canada:
7
+ name: Canada
8
+ code: CA
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,55 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-04-12 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sequel
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
14
62
  description: A standalone library for managing test fixture data
15
63
  email:
16
64
  - mail@paulbarry.com
@@ -26,6 +74,9 @@ files:
26
74
  - fixie.gemspec
27
75
  - lib/fixie.rb
28
76
  - lib/fixie/version.rb
77
+ - test/fixie_test.rb
78
+ - test/fixtures/cities.yml
79
+ - test/fixtures/countries.yml
29
80
  homepage: http://github.com/pjb3/fixie
30
81
  licenses: []
31
82
  post_install_message:
@@ -50,5 +101,8 @@ rubygems_version: 1.8.24
50
101
  signing_key:
51
102
  specification_version: 3
52
103
  summary: A standalone library for managing test fixture data
53
- test_files: []
104
+ test_files:
105
+ - test/fixie_test.rb
106
+ - test/fixtures/cities.yml
107
+ - test/fixtures/countries.yml
54
108
  has_rdoc: