michael-ken 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,39 +1,44 @@
1
- require 'pathname'
1
+ require 'test_helper'
2
2
 
3
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
4
-
5
- describe Ken do
6
- before :all do
7
- # Ken::Logger.new(STDOUT, :info)
8
- Ken::Session.new('http://www.freebase.com', 'ma', 'xxxxx')
9
- end
3
+ class KenTest < Test::Unit::TestCase
4
+ context "Ken.get" do
5
+ setup do
6
+ Ken::Session.new('http://www.freebase.com', 'ma', 'xxxxx')
7
+ end
10
8
 
11
- describe "Ken.get('/en/the_police')" do
12
- it "should return a Ken::Resource" do
9
+ should 'return a Ken::Resource' do
13
10
  the_police = Ken.get("/en/the_police")
14
11
  the_police.should be_kind_of(Ken::Resource)
15
12
  end
13
+
14
+ should 'raise a Ken::ResourceNotFound error if id does not exist' do
15
+ lambda { Ken.get("/en/non_existent_resource") }.should raise_error(Ken::ResourceNotFound)
16
+ end
16
17
  end
17
18
 
18
- describe "Ken.all" do
19
- it "should return a Ken::Collection of Ken::Resources" do
19
+ context "Ken.all" do
20
+ should "return a Ken::Collection of Ken::Resources" do
20
21
  resources = Ken.all(:name => "Apple")
21
22
  resources.should be_kind_of(Ken::Collection)
22
23
  resources.first.should be_kind_of(Ken::Resource)
23
24
  end
24
-
25
- it "should work with a limit specified" do
25
+
26
+ should "work with a limit specified" do
26
27
  resources = Ken.all(:name => "Apple", :limit => 3)
27
- resources.should have(3).items
28
+ resources.length.should == 3
28
29
  end
29
30
 
30
- it "should work with a type specified" do
31
- resources = Ken.all(:name => "Apple", :type => "/music/album")
32
- resources.should have_at_least(1).items
33
- resources.each {|r| r.types.select {|t| t.id == "/music/album"}.should have(1).items }
31
+ should "be able to return more than 100 resources (using cursored queries) " do
32
+ Ken.all({:type => '/chemistry/chemical_element'}).length.should > 100
34
33
  end
35
34
 
36
- it "should understand nested queries" do
35
+ should "work with a type specified" do
36
+ resources = Ken.all(:name => "Apple", :type => "/music/album")
37
+ resources.length.should >= 1
38
+ resources.each {|r| r.types.select {|t| t.id == "/music/album"}.length.should == 1 }
39
+ end
40
+
41
+ should "understand nested queries" do
37
42
  query = {
38
43
  :directed_by => "George Lucas",
39
44
  :starring => [
@@ -43,26 +48,26 @@ describe Ken do
43
48
  ],
44
49
  :type => "/film/film"
45
50
  }
46
-
51
+
47
52
  resources = Ken.all(query)
48
- resources.should have(3).items
53
+ resources.length.should == 3
49
54
  resources.first.name.should == "Star Wars Episode IV: A New Hope"
50
55
  resources.last.name.should == "The Star Wars Holiday Special"
51
56
  end
52
57
  end
53
58
 
54
- describe "Ken::Resource" do
55
- before :all do
59
+ context "A Ken::Resource Instance" do
60
+ setup do
56
61
  @the_police = Ken.get("/en/the_police")
57
62
  end
58
63
 
59
- it "should provide attributes" do
60
- @the_police.attributes.should have_at_least(1).items
64
+ should "provide attributes" do
65
+ @the_police.attributes.length.should >= 1
61
66
  @the_police.attributes.first.should be_kind_of(Ken::Attribute)
62
67
  end
63
68
 
64
- it "should have views" do
65
- @the_police.views.should have_at_least(1).items
69
+ should "have views" do
70
+ @the_police.views.length.should >= 1
66
71
  @the_police.views.first.should be_kind_of(Ken::View)
67
72
  @the_police.views.first.type.should be_kind_of(Ken::Type)
68
73
  end
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'pathname'
5
+ require 'json'
6
+
7
+ gem 'jnunemaker-matchy', '0.4.0'
8
+ require 'matchy'
9
+
10
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+
13
+ TEST_ROOT = Pathname(__FILE__).dirname.expand_path
14
+ require TEST_ROOT.parent + 'lib/ken'
15
+
16
+ def load_fixture(fixture_name)
17
+ fname = "#{File.dirname(__FILE__)}/fixtures/#{fixture_name}.json"
18
+ unless File.exists?(fname)
19
+ open(fname, "w") do |file|
20
+ puts "WARNING: Fixtures could not be loaded."
21
+ end
22
+ end
23
+ JSON.parse open(fname,"r").read
24
+ end
25
+
26
+ class Test::Unit::TestCase
27
+ custom_matcher :be_nil do |receiver, matcher, args|
28
+ matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
29
+ matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
30
+ receiver.nil?
31
+ end
32
+
33
+ custom_matcher :have do |receiver, matcher, args|
34
+ count = args[0]
35
+ something = matcher.chained_messages[0].name
36
+ actual = receiver.send(something).size
37
+ actual == count
38
+ end
39
+
40
+ custom_matcher :be_true do |receiver, matcher, args|
41
+ matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
42
+ matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
43
+ receiver.eql?(true)
44
+ end
45
+
46
+ custom_matcher :be_false do |receiver, matcher, args|
47
+ matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
48
+ matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
49
+ receiver.eql?(false)
50
+ end
51
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class AttributeTest < Test::Unit::TestCase
4
+ context "An Attribute instance" do
5
+ setup do
6
+ data = load_fixture('the_police')
7
+ @the_police = Ken::Resource.new(data)
8
+
9
+ @attribute = @unique_value_attribute = @the_police.attributes[2] # /music/artist/active_start
10
+ @unique_object_attribute = @the_police.attributes[12] # /music/artist/origin
11
+ @non_unique_value_attribute = @the_police.attributes[9] # /common/topic/alias
12
+ @non_unique_object_attribute = @the_police.attributes[5] # /music/artist/album
13
+ end
14
+
15
+ should "should have values" do
16
+ @attribute.should have(1).values
17
+ @non_unique_object_attribute.should have(14).values
18
+ end
19
+
20
+ context "with unique value type" do
21
+ should "be unique and no object_type" do
22
+ @unique_value_attribute.unique?.should == true
23
+ @unique_value_attribute.object_type?.should == false
24
+ end
25
+ end
26
+
27
+ context "with unique object type" do
28
+ should "be unique and an object type" do
29
+ @unique_object_attribute.unique?.should == true
30
+ @unique_object_attribute.object_type?.should == true
31
+ end
32
+ end
33
+
34
+ context "with non-unique value type" do
35
+ should "not be unique and not an object type" do
36
+ @non_unique_value_attribute.unique?.should == false
37
+ @non_unique_value_attribute.object_type?.should == false
38
+ end
39
+ end
40
+
41
+ context "with non-unique object type" do
42
+ should "be unique and an object type" do
43
+ @non_unique_object_attribute.unique?.should == false
44
+ @non_unique_object_attribute.object_type?.should == true
45
+ end
46
+ end
47
+ end # context
48
+ end # AttributeTest
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class PropertyTest < Test::Unit::TestCase
4
+ context "A Property instance" do
5
+ setup do
6
+ data = load_fixture('music_artist')
7
+ @type = Ken::Type.new(data)
8
+ @value_property = @type.properties[1]
9
+ @object_property = @type.properties[3]
10
+ end
11
+
12
+ should 'be a valid property instance' do
13
+ @value_property.should be_kind_of(Ken::Property)
14
+ @object_property.should be_kind_of(Ken::Property)
15
+ end
16
+
17
+ should 'have a type' do
18
+ @value_property.type.should be_kind_of(Ken::Type)
19
+ @object_property.type.should be_kind_of(Ken::Type)
20
+ end
21
+
22
+ should 'distinguish wheter it is an object or value type' do
23
+ @value_property.object_type?.should == false
24
+ @object_property.object_type?.should == true
25
+ end
26
+ end # context
27
+ end # PropertyTest
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class ResourceTest < Test::Unit::TestCase
4
+ context "A Resource instance" do
5
+ setup do
6
+ data = load_fixture('the_police')
7
+ @the_police = Ken::Resource.new(data)
8
+ end
9
+
10
+ should "have types" do
11
+ @the_police.should have(6).types
12
+ @the_police.types.first.should be_kind_of(Ken::Type)
13
+ end
14
+
15
+ should "have views" do
16
+ @the_police.should have(6).views
17
+ @the_police.views.first.should be_kind_of(Ken::View)
18
+ @the_police.views.first.type.should be_kind_of(Ken::Type)
19
+ end
20
+
21
+ should 'have a full set of attributes' do
22
+ @the_police.attributes.should_not be_nil
23
+ end
24
+
25
+ should "have id and name properties" do
26
+ @the_police.id.should be_kind_of(String)
27
+ @the_police.name.should be_kind_of(String)
28
+ end
29
+
30
+ should 'load attributes only on demand' do
31
+ @the_police.attributes_loaded?.should == false
32
+ @the_police.attributes
33
+ @the_police.attributes_loaded?.should == true
34
+ end
35
+
36
+ should 'load schema only on demand when calling types' do
37
+ @the_police.schema_loaded?.should == false
38
+ @the_police.types
39
+ @the_police.schema_loaded?.should == true
40
+ end
41
+
42
+ should 'load schema only on demand when calling views' do
43
+ @the_police.schema_loaded?.should == false
44
+ @the_police.views
45
+ @the_police.schema_loaded?.should == true
46
+ end
47
+ end # context
48
+ end # ResourceTest
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class SessionTest < Test::Unit::TestCase
4
+ context "A Session instance" do
5
+ setup do
6
+ Ken::Logger.new(STDOUT, :info)
7
+ Ken::Session.new('http://www.freebase.com', 'ma', 'xxxxx')
8
+ end
9
+
10
+ should 'return the correct set of types' do
11
+ result = Ken.session.mqlread({:id => "/en/the_police", :type => []})
12
+ result['type'].length.should == 6
13
+ end
14
+
15
+ should 'raise a Ken::MqlReadError if node does not exist' do
16
+ lambda { Ken.session.mqlread({:id => "/en/the_police", :evil_property => []}) }.should raise_error(Ken::ReadError)
17
+ end
18
+
19
+ should 'do uncursored queries' do
20
+ Ken.session.mqlread([{:type => '/chemistry/chemical_element'}]).length == 100
21
+ end
22
+
23
+ should 'do cursored queries' do
24
+ Ken.session.mqlread([{:type => '/chemistry/chemical_element'}], :cursor => true).length.should >= 117
25
+ end
26
+ end # context
27
+ end # SessionTest
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class TypeTest < Test::Unit::TestCase
4
+ context "A Session instance" do
5
+ setup do
6
+ data = load_fixture('the_police')
7
+ @type = Ken::Resource.new(data).types.first
8
+ end
9
+
10
+ should 'have an id and a name' do
11
+ @type.id.should be_kind_of(String)
12
+ @type.name.should be_kind_of(String)
13
+ end
14
+
15
+ should 'have properties' do
16
+ @type.should have(16).properties
17
+ end
18
+
19
+ context "when accessing a property directly" do
20
+ setup do
21
+ @genre = @type.genre
22
+ @album = @type.album
23
+ end
24
+
25
+ should "be kind of Ken::Property" do
26
+ @genre.should be_kind_of(Ken::Property)
27
+ @album.should be_kind_of(Ken::Property)
28
+ end
29
+
30
+ should "raise AttributeNotFound when invalid propertyname is supplied" do
31
+ lambda { @type.not_existing_property }.should raise_error(Ken::PropertyNotFound)
32
+ end
33
+ end # context
34
+ end # context
35
+ end # TypeTest
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class ViewTest < Test::Unit::TestCase
4
+ context "A View instance" do
5
+ setup do
6
+ Ken::Logger.new(STDOUT, :info)
7
+ data = load_fixture('the_police')
8
+ @the_police = Ken::Resource.new(data)
9
+ @view = @the_police.views.first
10
+ end
11
+
12
+ should "have a type" do
13
+ @view.type.should_not be_nil
14
+ @view.type.should be_kind_of(Ken::Type)
15
+ end
16
+
17
+ should "have properties" do
18
+ @view.properties.should_not be_nil
19
+ @view.properties.each { |p| p.should be_kind_of(Ken::Property)}
20
+ end
21
+
22
+ should "have attributes" do
23
+ @view.attributes.should_not be_nil
24
+ @view.attributes.each { |p| p.should be_kind_of(Ken::Attribute)}
25
+ end
26
+
27
+ context "when accessing a direct attribute" do
28
+ setup do
29
+ @genre = @view.genre
30
+ @album = @view.album
31
+ end
32
+
33
+ should "be kind of Ken::Attribute" do
34
+ @genre.should be_kind_of(Ken::Attribute)
35
+ @album.should be_kind_of(Ken::Attribute)
36
+ end
37
+
38
+ should "be able to get values" do
39
+ @genre.should have(6).values
40
+ @album.should have(14).values
41
+ end
42
+
43
+ should "raise AttributeNotFound when invalid propertyname is supplied" do
44
+ lambda { @view.not_existing_attribute }.should raise_error(Ken::AttributeNotFound)
45
+ end
46
+ end # context
47
+ end # context
48
+ end # ViewTest
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: michael-ken
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - michael
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-14 00:00:00 -07:00
12
+ date: 2009-07-08 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: extlib
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
16
25
  description:
17
26
  email: ma[at]zive[dot]at
18
27
  executables: []
@@ -33,6 +42,8 @@ files:
33
42
  - TODO
34
43
  - VERSION
35
44
  - examples/artist.rb
45
+ - examples/artist_links.rb
46
+ - ken.gemspec
36
47
  - lib/ken.rb
37
48
  - lib/ken/attribute.rb
38
49
  - lib/ken/collection.rb
@@ -42,21 +53,21 @@ files:
42
53
  - lib/ken/session.rb
43
54
  - lib/ken/type.rb
44
55
  - lib/ken/util.rb
45
- - lib/ken/version.rb
46
56
  - lib/ken/view.rb
47
- - spec/fixtures/music_artist.json
48
- - spec/fixtures/the_police.json
49
- - spec/integration/ken_spec.rb
50
- - spec/spec.opts
51
- - spec/spec_helper.rb
52
- - spec/unit/attribute_spec.rb
53
- - spec/unit/property_spec.rb
54
- - spec/unit/resource_spec.rb
55
- - spec/unit/session_spec.rb
56
- - spec/unit/type_spec.rb
57
- - spec/unit/view_spec.rb
57
+ - rails/init.rb
58
+ - tasks/ken.rb
58
59
  - tasks/spec.rb
59
- has_rdoc: true
60
+ - test/fixtures/music_artist.json
61
+ - test/fixtures/the_police.json
62
+ - test/integration/ken_test.rb
63
+ - test/test_helper.rb
64
+ - test/unit/attribute_test.rb
65
+ - test/unit/property_test.rb
66
+ - test/unit/resource_test.rb
67
+ - test/unit/session_test.rb
68
+ - test/unit/type_test.rb
69
+ - test/unit/view_test.rb
70
+ has_rdoc: false
60
71
  homepage: http://github.com/michael/ken
61
72
  post_install_message:
62
73
  rdoc_options:
@@ -80,15 +91,16 @@ requirements: []
80
91
  rubyforge_project:
81
92
  rubygems_version: 1.2.0
82
93
  signing_key:
83
- specification_version: 2
94
+ specification_version: 3
84
95
  summary: Ruby API for Accessing the Freebase
85
96
  test_files:
86
- - spec/integration/ken_spec.rb
87
- - spec/spec_helper.rb
88
- - spec/unit/attribute_spec.rb
89
- - spec/unit/property_spec.rb
90
- - spec/unit/resource_spec.rb
91
- - spec/unit/session_spec.rb
92
- - spec/unit/type_spec.rb
93
- - spec/unit/view_spec.rb
97
+ - test/integration/ken_test.rb
98
+ - test/test_helper.rb
99
+ - test/unit/attribute_test.rb
100
+ - test/unit/property_test.rb
101
+ - test/unit/resource_test.rb
102
+ - test/unit/session_test.rb
103
+ - test/unit/type_test.rb
104
+ - test/unit/view_test.rb
94
105
  - examples/artist.rb
106
+ - examples/artist_links.rb
data/lib/ken/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module Ken
2
- VERSION = '0.0.2'
3
- end