ken 0.1.2

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.
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
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
8
+
9
+ should 'return a Ken::Resource' do
10
+ the_police = Ken.get("/en/the_police")
11
+ the_police.should be_kind_of(Ken::Resource)
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
17
+ end
18
+
19
+ context "Ken.all" do
20
+ should "return a Ken::Collection of Ken::Resources" do
21
+ resources = Ken.all(:name => "Apple")
22
+ resources.should be_kind_of(Ken::Collection)
23
+ resources.first.should be_kind_of(Ken::Resource)
24
+ end
25
+
26
+ should "work with a limit specified" do
27
+ resources = Ken.all(:name => "Apple", :limit => 3)
28
+ resources.length.should == 3
29
+ end
30
+
31
+ should "be able to return more than 100 resources (using cursored queries) " do
32
+ Ken.all({:type => '/chemistry/chemical_element'}).length.should > 100
33
+ end
34
+
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
42
+ query = {
43
+ :directed_by => "George Lucas",
44
+ :starring => [
45
+ {
46
+ :actor => "Harrison Ford"
47
+ }
48
+ ],
49
+ :type => "/film/film"
50
+ }
51
+
52
+ resources = Ken.all(query)
53
+ resources.length.should == 3
54
+ resources.first.name.should == "Star Wars Episode IV: A New Hope"
55
+ resources.last.name.should == "The Star Wars Holiday Special"
56
+ end
57
+ end
58
+
59
+ context "A Ken::Resource Instance" do
60
+ setup do
61
+ @the_police = Ken.get("/en/the_police")
62
+ end
63
+
64
+ should "provide attributes" do
65
+ @the_police.attributes.length.should >= 1
66
+ @the_police.attributes.first.should be_kind_of(Ken::Attribute)
67
+ end
68
+
69
+ should "have views" do
70
+ @the_police.views.length.should >= 1
71
+ @the_police.views.first.should be_kind_of(Ken::View)
72
+ @the_police.views.first.type.should be_kind_of(Ken::Type)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,61 @@
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
52
+
53
+ class Object
54
+ # nice for debugging
55
+ # usage: print_call_stack(:method_name, 2, 10)
56
+ def print_call_stack(from = 2, to = nil, html = false)
57
+ (from..(to ? to : caller.length)).each do |idx|
58
+ p "[#{idx}]: #{caller[idx]}#{html ? '<br />' : ''}"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,49 @@
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.views[0].active_start
10
+ @unique_object_attribute = @the_police.views[0].origin
11
+ @unique_object_attribute.unique?
12
+ @non_unique_value_attribute = @the_police.views[1].alias
13
+ @non_unique_object_attribute = @the_police.views[0].album
14
+ end
15
+
16
+ should "should have values" do
17
+ @attribute.should have(1).values
18
+ @non_unique_object_attribute.should have(14).values
19
+ end
20
+
21
+ context "with unique value type" do
22
+ should "be unique and no object_type" do
23
+ @unique_value_attribute.unique?.should == true
24
+ @unique_value_attribute.object_type?.should == false
25
+ end
26
+ end
27
+
28
+ context "with unique object type" do
29
+ should "be unique and an object type" do
30
+ @unique_object_attribute.unique?.should == true
31
+ @unique_object_attribute.object_type?.should == true
32
+ end
33
+ end
34
+
35
+ context "with non-unique value type" do
36
+ should "not be unique and not an object type" do
37
+ @non_unique_value_attribute.unique?.should == false
38
+ @non_unique_value_attribute.object_type?.should == false
39
+ end
40
+ end
41
+
42
+ context "with non-unique object type" do
43
+ should "be unique and an object type" do
44
+ @non_unique_object_attribute.unique?.should == false
45
+ @non_unique_object_attribute.object_type?.should == true
46
+ end
47
+ end
48
+ end # context
49
+ 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,58 @@
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 "return individual view based requested type id" do
22
+ @the_police.view('/music/artist').should be_kind_of(Ken::View)
23
+ @the_police.view('/location/location').should be_nil # not existent view
24
+ end
25
+
26
+ should "return individual type based requested type id" do
27
+ @the_police.type('/music/artist').should be_kind_of(Ken::Type)
28
+ @the_police.type('/location/location').should be_nil # not existent type
29
+ end
30
+
31
+ should 'have a full set of attributes' do
32
+ @the_police.attributes.should_not be_nil
33
+ end
34
+
35
+ should "have id and name properties" do
36
+ @the_police.id.should be_kind_of(String)
37
+ @the_police.name.should be_kind_of(String)
38
+ end
39
+
40
+ should 'load attributes only on demand' do
41
+ @the_police.attributes_loaded?.should == false
42
+ @the_police.attributes
43
+ @the_police.attributes_loaded?.should == true
44
+ end
45
+
46
+ should 'load schema only on demand when calling types' do
47
+ @the_police.schema_loaded?.should == false
48
+ @the_police.types
49
+ @the_police.schema_loaded?.should == true
50
+ end
51
+
52
+ should 'load schema only on demand when calling views' do
53
+ @the_police.schema_loaded?.should == false
54
+ @the_police.views
55
+ @the_police.schema_loaded?.should == true
56
+ end
57
+ end # context
58
+ 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 >= 1
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.attribute('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
+ @view.not_existing_attribute.should be_nil
45
+ end
46
+ end # context
47
+ end # context
48
+ end # ViewTest
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ken
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - michael
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-16 00:00:00 +02:00
13
+ default_executable:
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:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: addressable
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description:
46
+ email: ma[at]zive[dot]at
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.textile
54
+ - README.txt
55
+ files:
56
+ - .gitignore
57
+ - History.txt
58
+ - LICENSE
59
+ - README.textile
60
+ - README.txt
61
+ - Rakefile
62
+ - TODO
63
+ - VERSION
64
+ - examples/artist.rb
65
+ - examples/artist_links.rb
66
+ - ken.gemspec
67
+ - lib/ken.rb
68
+ - lib/ken/attribute.rb
69
+ - lib/ken/collection.rb
70
+ - lib/ken/logger.rb
71
+ - lib/ken/property.rb
72
+ - lib/ken/resource.rb
73
+ - lib/ken/session.rb
74
+ - lib/ken/type.rb
75
+ - lib/ken/util.rb
76
+ - lib/ken/view.rb
77
+ - rails/init.rb
78
+ - tasks/ken.rb
79
+ - tasks/spec.rb
80
+ - test/fixtures/music_artist.json
81
+ - test/fixtures/the_police.json
82
+ - test/integration/ken_test.rb
83
+ - test/test_helper.rb
84
+ - test/unit/attribute_test.rb
85
+ - test/unit/property_test.rb
86
+ - test/unit/resource_test.rb
87
+ - test/unit/session_test.rb
88
+ - test/unit/type_test.rb
89
+ - test/unit/view_test.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/michael/ken
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options:
96
+ - --charset=UTF-8
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ version:
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ version:
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.5
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Ruby API for Accessing the Freebase
118
+ test_files:
119
+ - test/integration/ken_test.rb
120
+ - test/test_helper.rb
121
+ - test/unit/attribute_test.rb
122
+ - test/unit/property_test.rb
123
+ - test/unit/resource_test.rb
124
+ - test/unit/session_test.rb
125
+ - test/unit/type_test.rb
126
+ - test/unit/view_test.rb
127
+ - examples/artist.rb
128
+ - examples/artist_links.rb