michael-ken 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +70 -22
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/examples/artist.rb +2 -2
- data/examples/artist_links.rb +28 -0
- data/ken.gemspec +82 -0
- data/lib/ken/attribute.rb +16 -7
- data/lib/ken/collection.rb +0 -8
- data/lib/ken/property.rb +5 -0
- data/lib/ken/resource.rb +17 -14
- data/lib/ken/session.rb +34 -11
- data/lib/ken/type.rb +17 -2
- data/lib/ken/view.rb +13 -0
- data/lib/ken.rb +4 -10
- data/rails/init.rb +2 -0
- data/tasks/ken.rb +4 -0
- data/tasks/spec.rb +1 -1
- data/{spec → test}/fixtures/music_artist.json +1 -0
- data/test/fixtures/the_police.json +940 -0
- data/{spec/integration/ken_spec.rb → test/integration/ken_test.rb} +33 -28
- data/test/test_helper.rb +51 -0
- data/test/unit/attribute_test.rb +48 -0
- data/test/unit/property_test.rb +27 -0
- data/test/unit/resource_test.rb +48 -0
- data/test/unit/session_test.rb +27 -0
- data/test/unit/type_test.rb +35 -0
- data/test/unit/view_test.rb +48 -0
- metadata +38 -26
- data/lib/ken/version.rb +0 -3
- data/spec/fixtures/the_police.json +0 -891
- data/spec/spec.opts +0 -3
- data/spec/spec_helper.rb +0 -40
- data/spec/unit/attribute_spec.rb +0 -50
- data/spec/unit/property_spec.rb +0 -30
- data/spec/unit/resource_spec.rb +0 -63
- data/spec/unit/session_spec.rb +0 -18
- data/spec/unit/type_spec.rb +0 -21
- data/spec/unit/view_spec.rb +0 -28
@@ -1,39 +1,44 @@
|
|
1
|
-
require '
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
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
|
-
|
19
|
-
|
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
|
-
|
25
|
+
|
26
|
+
should "work with a limit specified" do
|
26
27
|
resources = Ken.all(:name => "Apple", :limit => 3)
|
27
|
-
resources.should
|
28
|
+
resources.length.should == 3
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
|
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
|
-
|
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
|
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
|
-
|
55
|
-
|
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
|
-
|
60
|
-
@the_police.attributes.should
|
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
|
-
|
65
|
-
@the_police.views.should
|
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
|
data/test/test_helper.rb
ADDED
@@ -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
|
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-
|
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
|
-
-
|
48
|
-
-
|
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
|
-
|
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:
|
94
|
+
specification_version: 3
|
84
95
|
summary: Ruby API for Accessing the Freebase
|
85
96
|
test_files:
|
86
|
-
-
|
87
|
-
-
|
88
|
-
-
|
89
|
-
-
|
90
|
-
-
|
91
|
-
-
|
92
|
-
-
|
93
|
-
-
|
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