fakery 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8e4c5473cf03a8d6eb710c2d1ef96b6a8e7d9bd
4
- data.tar.gz: 0051eeef94e885f4749b8e35870deb19594216ae
3
+ metadata.gz: f630e9880038c8ff0252674f3e1e5d267ad6a451
4
+ data.tar.gz: 3eab8b25954c9f99ea05fadada60ecfb05f90739
5
5
  SHA512:
6
- metadata.gz: e9be7034c965974049b538554c2d9fed42b1844f24f77ee9c5a9fd7f3cf3c8efac1ce604e0460bf8d37b6bb8a1ea071569a96f0a92a36f191b1a9fb0a63ebbd3
7
- data.tar.gz: 9f92a50048ca5ae55cb15697e3f2b52fe553163291301135ff2be1108f3c7dc9f860e7c2f01d6c57351c477db29fb337433ef749d117113ba49b432e5fd1d87e
6
+ metadata.gz: 457ae54f67b5f7f3733806826e85a100a08316c7e5625161be618b9ca0d2fc7d28c10a7e3c677c18d4a592019713577e6af8822cc0de81f5dba2f14f52d18703
7
+ data.tar.gz: 62f2cc100df0abda68408c5e667059f45ce2aad92a4325781b91bbc266c27ffc109b6b46c52b15e28730efb3bf8ae765e2cf8f0b13b43e78f5c9df090c30659a
data/README.md CHANGED
@@ -1,12 +1,46 @@
1
- fakery
2
- ======
1
+ # fakery
2
+
3
+ ## Descripton
3
4
 
4
- Descripton
5
- ----------
6
5
  Faking Ruby objects from JSON API responses
7
6
 
8
- Badges
9
- ------
7
+ ## Usage
8
+
9
+ ### Registering fakes
10
+
11
+ Automatically register all fakes stored in JSON files under their respective
12
+ filenames:
13
+
14
+ require 'fakery'
15
+ Fakery.register_files Dir[Rails.root + 'spec/support/fakes/*.json']
16
+
17
+ ### Building fakes
18
+
19
+ `Fakery.build(:foo)` now builds a fake object for the file named `foo.json` in
20
+ the the `spec/support/fakes` directory. `Fakery.build(:foo, with: { bar:
21
+ "something" })` builds the fake with its `bar` attribute set to the string
22
+ `"something"`.
23
+
24
+ For integration testing of services you can stub an API response with
25
+ `Fakery.build(:the_response).to_json` for your specs.
26
+
27
+ ### Instantiate objects with fake data
28
+
29
+ `Fakery.instance(:foo, as: TheModel)` instantiates the model TheModel with the
30
+ fake's data by passing it as a hash to the model's constructor.
31
+ `Fakery.instance(:foo, as: TheModel, with: { bar: "something" })` does the same
32
+ with the instances `bar` attribute set to the string `"something"`.
33
+
34
+ ### Seeding fake data from API endpoints
35
+
36
+ `Fakery.seed('http://api.example.com/foo/bar.json')` returns a fake initialized
37
+ from the URL's JSON content.
38
+ `Fakery.seed('http://api.example.com/foo/bar.json', register: :foo_bar)` also
39
+ registers the fake with the name `foo_bar`.
40
+ `Fakery.reseed(:foo_bar)` then reseeds the fake named `foo_bar` from the same
41
+ URL.
42
+
43
+ ## Badges
10
44
 
11
45
  [![Build Status](https://travis-ci.org/betterplace/fakery.svg?branch=master)](https://travis-ci.org/betterplace/fakery)
12
46
  [![Code Climate](https://codeclimate.com/github/betterplace/fakery.png)](https://codeclimate.com/github/betterplace/fakery)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,18 +1,18 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: fakery 0.3.1 ruby lib
2
+ # stub: fakery 0.4.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "fakery"
6
- s.version = "0.3.1"
6
+ s.version = "0.4.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["betterplace Developers"]
11
- s.date = "2016-06-01"
11
+ s.date = "2016-06-06"
12
12
  s.description = "This library fakes ruby objects from JSON API responses for testing purposes."
13
13
  s.email = "developers@betterplace.org"
14
- s.extra_rdoc_files = ["README.md", "lib/fakery.rb", "lib/fakery/api.rb", "lib/fakery/api_error.rb", "lib/fakery/change.rb", "lib/fakery/fake.rb", "lib/fakery/fakery_error.rb", "lib/fakery/registry.rb", "lib/fakery/seeding.rb", "lib/fakery/version.rb", "lib/fakery/wrapping.rb"]
15
- s.files = [".gitignore", ".rspec", ".travis.yml", "Gemfile", "LICENSE", "README.md", "Rakefile", "TODO.md", "VERSION", "fakery.gemspec", "lib/fakery.rb", "lib/fakery/api.rb", "lib/fakery/api_error.rb", "lib/fakery/change.rb", "lib/fakery/fake.rb", "lib/fakery/fakery_error.rb", "lib/fakery/registry.rb", "lib/fakery/seeding.rb", "lib/fakery/version.rb", "lib/fakery/wrapping.rb", "spec/api_spec.rb", "spec/change_spec.rb", "spec/fake_spec.rb", "spec/registry_spec.rb", "spec/seeding_spec.rb", "spec/spec_helper.rb", "spec/wrapping_spec.rb"]
14
+ s.extra_rdoc_files = ["README.md", "lib/fakery.rb", "lib/fakery/api.rb", "lib/fakery/api_error.rb", "lib/fakery/change.rb", "lib/fakery/fake.rb", "lib/fakery/fakery_error.rb", "lib/fakery/registry.rb", "lib/fakery/seeding.rb", "lib/fakery/state_error.rb", "lib/fakery/version.rb", "lib/fakery/wrapping.rb"]
15
+ s.files = [".gitignore", ".rspec", ".travis.yml", "Gemfile", "LICENSE", "README.md", "Rakefile", "TODO.md", "VERSION", "fakery.gemspec", "lib/fakery.rb", "lib/fakery/api.rb", "lib/fakery/api_error.rb", "lib/fakery/change.rb", "lib/fakery/fake.rb", "lib/fakery/fakery_error.rb", "lib/fakery/registry.rb", "lib/fakery/seeding.rb", "lib/fakery/state_error.rb", "lib/fakery/version.rb", "lib/fakery/wrapping.rb", "spec/api_spec.rb", "spec/change_spec.rb", "spec/fake_spec.rb", "spec/registry_spec.rb", "spec/seeding_spec.rb", "spec/spec_helper.rb", "spec/wrapping_spec.rb"]
16
16
  s.homepage = "http://github.com/betterplace/fakery"
17
17
  s.licenses = ["Apache-2.0"]
18
18
  s.rdoc_options = ["--title", "Fakery -- ", "--main", "README.md"]
@@ -9,6 +9,7 @@ module Fakery
9
9
  require 'fakery/version'
10
10
  require 'fakery/fakery_error'
11
11
  require 'fakery/api_error'
12
+ require 'fakery/state_error'
12
13
  require 'fakery/api'
13
14
  require 'fakery/change'
14
15
  require 'fakery/fake'
@@ -19,8 +20,7 @@ module Fakery
19
20
  class << self
20
21
  extend Forwardable
21
22
 
22
- def_delegators :'Fakery::Registry', :register, :registered?, :build,
23
- :source, :register_files
23
+ def_delegators :'Fakery::Registry', :register, :registered?, :build, :register_files
24
24
 
25
25
  def_delegators :'Fakery::Seeding', :seed, :reseed
26
26
 
@@ -3,7 +3,7 @@ module Fakery::Api
3
3
 
4
4
  # Get a http response from +url+ with parameters +params+.
5
5
  def get_response(url, params: {})
6
- Typhoeus.get(url, params: params)
6
+ Typhoeus.get(url, params: params, followlocation: true)
7
7
  end
8
8
 
9
9
  # Get the +url+ and parse the response body into a Fakery::Fake object. If
@@ -58,6 +58,8 @@ class Fakery::Fake < JSON::GenericObject
58
58
  end
59
59
 
60
60
  def reseed
61
+ __api_seed_url__.nil? and raise Fakery::StateError,
62
+ 'reseeding not possible, this fake was not originally seeded'
61
63
  myself = self
62
64
  self.class.from_hash(Fakery::Api.get(__api_seed_url__)).instance_eval do
63
65
  self.__api_seed_url__ = myself.__api_seed_url__
@@ -68,15 +70,6 @@ class Fakery::Fake < JSON::GenericObject
68
70
  myself
69
71
  end
70
72
 
71
- def register_as_ruby(register_name)
72
- register_name = register_name.to_sym
73
- <<EOT
74
- Fakery.register(#{register_name.inspect}, %{
75
- #{JSON.pretty_generate(self).gsub(/^/, ' ')}
76
- })
77
- EOT
78
- end
79
-
80
73
  def initialize_copy(other)
81
74
  other.instance_variable_set :@changes, @changes.dup
82
75
  super
@@ -6,6 +6,10 @@ module Fakery::Registry
6
6
  end
7
7
  private :registered_fakes
8
8
 
9
+ def list
10
+ registered_fakes.keys
11
+ end
12
+
9
13
  # Clear all registered fakes.
10
14
  def clear
11
15
  registered_fakes.clear
@@ -28,6 +32,7 @@ module Fakery::Registry
28
32
  registered_fakes[register_name] = Fakery::Fake.cast(fake)
29
33
  end
30
34
 
35
+ # Register all +files+ as fakes with their filename as their name.
31
36
  def register_files(*files)
32
37
  files = files.flatten
33
38
  puts "Registering fakes…"
@@ -52,10 +57,4 @@ module Fakery::Registry
52
57
  rescue KeyError
53
58
  raise ArgumentError, "no fake registered under the name #{register_name.inspect}"
54
59
  end
55
-
56
- # Returns the ruby source to register +fake+ under the +name+.
57
- def source(name, fake)
58
- fake = Fakery::Fake.cast(fake)
59
- fake.__send__(:register_as_ruby, name)
60
- end
61
60
  end
@@ -1,14 +1,30 @@
1
1
  module Fakery::Seeding
2
2
  module_function
3
3
 
4
- # TODO
5
- def seed(api_seed_url)
6
- Fakery::Fake.seed_from_url(api_seed_url)
4
+ # Seed a fake from the JSON returned by +api_seed_url+. If a name was passed
5
+ # via +register+ option the fake is registered under that name.
6
+ def seed(api_seed_url, register: nil)
7
+ fake = Fakery::Fake.seed_from_url(api_seed_url)
8
+ register and Fakery::Registry.register register, fake
9
+ fake
7
10
  end
8
11
 
9
- # TODO
10
- def reseed(fake, url = nil)
12
+ # Reseed the fake (also the name of a registered fake) given as +fake+ from
13
+ # its original +api_seed_url+ or from the URL passed as +url+ option. If the
14
+ # +register+ option is true and the fake was registered the newly seeded fake
15
+ # will be registered under its original name again. If +register+ was a name
16
+ # this name will be used instead.
17
+ def reseed(fake, url: nil, register: true)
18
+ if register == true
19
+ if Fakery::Registry.registered?(fake)
20
+ register = fake
21
+ else
22
+ register = nil
23
+ end
24
+ end
25
+ fake = Fakery::Fake.cast(fake)
11
26
  url and fake.__api_seed_url__ = url
27
+ register and Fakery::Registry.register register, fake
12
28
  fake.__send__(:reseed)
13
29
  end
14
30
  end
@@ -0,0 +1,2 @@
1
+ class Fakery::StateError < Fakery::FakeryError
2
+ end
@@ -1,6 +1,6 @@
1
1
  module Fakery
2
2
  # Fakery version
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -49,15 +49,6 @@ describe Fakery::Fake do
49
49
  expect(fake.name).to eq 'foo'
50
50
  end
51
51
 
52
- it 'can output ruby code for registration in the Fake::Registry' do
53
- string = Fakery.source(:my_name, fake)
54
- expect(string).to be_a String
55
- expect(Fakery).to_not be_registered :my_name
56
- eval(string)
57
- expect(Fakery).to be_registered :my_name
58
- expect(Fakery.build(:my_name).name).to eq 'foo'
59
- end
60
-
61
52
  context 'casting' do
62
53
  it 'casts to itself' do
63
54
  casted = Fakery::Fake.cast(fake)
@@ -66,7 +57,6 @@ describe Fakery::Fake do
66
57
  end
67
58
 
68
59
  it 'casts from a name to a registered fake' do
69
- Fakery.register(:foo, fake)
70
60
  casted = Fakery::Fake.cast(:foo)
71
61
  expect(casted).to be_a Fakery::Fake
72
62
  expect(casted.name).to eq 'foo'
@@ -14,7 +14,7 @@ describe Fakery::Registry do
14
14
  end
15
15
 
16
16
  it 'warns iff a fake was registered a second time' do
17
- expect(Fakery::Registry).to receive(:warn).with(any_args).and_call_original
17
+ expect(Fakery::Registry).to receive(:warn).with(any_args)
18
18
  Fakery.register 'foo', fake
19
19
  Fakery.register 'foo', fake
20
20
  end
@@ -46,5 +46,9 @@ describe Fakery::Registry do
46
46
  Fakery.register(:bar, foo)
47
47
  expect(Fakery.build(:bar).bar).to eq true
48
48
  end
49
+
50
+ it 'can be listed' do
51
+ expect(Fakery::Registry.list).to eq %i[ foo ]
52
+ end
49
53
  end
50
54
  end
@@ -5,24 +5,59 @@ describe Fakery::Seeding do
5
5
  Fakery::Fake.from_hash(name: 'foo')
6
6
  end
7
7
 
8
+ let :another_fake do
9
+ f = fake.dup
10
+ f.name = 'bar'
11
+ f
12
+ end
13
+
14
+ before do
15
+ expect(Fakery::Api).to receive(:get).and_return(another_fake)
16
+ end
17
+
8
18
  it 'can be seeded from an URL' do
9
19
  url = 'http://api.some.where/foo.json'
10
- another_fake = fake.dup
11
- another_fake.name = 'bar'
12
- expect(Fakery::Api).to receive(:get).and_return(another_fake)
13
20
  fake = Fakery.seed url
14
21
  expect(fake.name).to eq another_fake.name
15
22
  expect(fake.__api_seed_url__).to eq url
16
23
  end
17
24
 
18
- it 'can be reseeded from an URL' do
25
+ it 'can be seeded from an URL and registered' do
26
+ url = 'http://api.some.where/foo.json'
27
+ fake = Fakery.seed url, register: :foo
28
+ expect(Fakery.build(:foo).name).to eq another_fake.name
29
+ expect(Fakery.build(:foo).__api_seed_url__).to eq url
30
+ end
31
+
32
+ it 'can be reseeded from its original URL' do
19
33
  url = 'http://api.some.where/foo.json'
20
- another_fake = fake.dup
21
- another_fake.name = 'bar'
22
34
  fake.__api_seed_url__ = url
23
- expect(Fakery::Api).to receive(:get).and_return(another_fake)
24
35
  Fakery.reseed(fake)
25
36
  expect(fake.name).to eq another_fake.name
26
37
  expect(fake.__api_seed_url__).to eq url
27
38
  end
39
+
40
+ it 'can be reseeded from an URL' do
41
+ url = 'http://api.some.where/foo.json'
42
+ Fakery.reseed(fake, url: url)
43
+ expect(fake.name).to eq another_fake.name
44
+ expect(fake.__api_seed_url__).to eq url
45
+ end
46
+
47
+ it 'can be reseeded from an URL and registered' do
48
+ url = 'http://api.some.where/foo.json'
49
+ fake.__api_seed_url__ = url
50
+ Fakery.reseed(fake, register: :foo)
51
+ expect(Fakery.build(:foo).name).to eq another_fake.name
52
+ expect(Fakery.build(:foo).__api_seed_url__).to eq url
53
+ end
54
+
55
+ it 'can be reseeded from an URL and registered under its previous name' do
56
+ url = 'http://api.some.where/foo.json'
57
+ Fakery.register :foo, fake
58
+ expect(Fakery.build(:foo).name).to eq fake.name
59
+ expect(Fakery::Registry).to receive(:warn).with(any_args)
60
+ Fakery.reseed(:foo, url: url, register: true)
61
+ expect(Fakery.build(:foo).name).to eq another_fake.name
62
+ end
28
63
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - betterplace Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-01 00:00:00.000000000 Z
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar
@@ -150,6 +150,7 @@ extra_rdoc_files:
150
150
  - lib/fakery/fakery_error.rb
151
151
  - lib/fakery/registry.rb
152
152
  - lib/fakery/seeding.rb
153
+ - lib/fakery/state_error.rb
153
154
  - lib/fakery/version.rb
154
155
  - lib/fakery/wrapping.rb
155
156
  files:
@@ -171,6 +172,7 @@ files:
171
172
  - lib/fakery/fakery_error.rb
172
173
  - lib/fakery/registry.rb
173
174
  - lib/fakery/seeding.rb
175
+ - lib/fakery/state_error.rb
174
176
  - lib/fakery/version.rb
175
177
  - lib/fakery/wrapping.rb
176
178
  - spec/api_spec.rb