redgraph 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +8 -0
- data/CHANGELOG.md +26 -1
- data/Gemfile +2 -0
- data/Gemfile.lock +22 -1
- data/README.md +70 -8
- data/lib/redgraph.rb +11 -0
- data/lib/redgraph/edge.rb +17 -4
- data/lib/redgraph/graph.rb +44 -79
- data/lib/redgraph/graph/edge_methods.rb +105 -0
- data/lib/redgraph/graph/node_methods.rb +104 -0
- data/lib/redgraph/node.rb +11 -3
- data/lib/redgraph/node_model.rb +123 -0
- data/lib/redgraph/node_model/class_methods.rb +76 -0
- data/lib/redgraph/query_response.rb +82 -5
- data/lib/redgraph/util.rb +21 -0
- data/lib/redgraph/version.rb +1 -1
- data/redgraph.gemspec +2 -1
- data/test/graph_edge_methods_test.rb +100 -0
- data/test/graph_manipulation_test.rb +59 -0
- data/test/graph_node_methods_test.rb +112 -0
- data/test/graph_queries_test.rb +22 -35
- data/test/graph_test.rb +26 -0
- data/test/node_model_class_methods_test.rb +66 -0
- data/test/node_model_labels_test.rb +57 -0
- data/test/node_model_test.rb +126 -0
- data/test/test_helper.rb +21 -0
- metadata +27 -3
| @@ -0,0 +1,66 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "test_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class NodeModelClassMethodsTest < Minitest::Test
         | 
| 6 | 
            +
              include TestHelpers
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              GRAPH = Redgraph::Graph.new("movies", url: $REDIS_URL)
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def setup
         | 
| 11 | 
            +
                @graph = GRAPH
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def teardown
         | 
| 15 | 
            +
                @graph.delete
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              # test classes
         | 
| 19 | 
            +
              #
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              class Actor
         | 
| 22 | 
            +
                include Redgraph::NodeModel
         | 
| 23 | 
            +
                self.graph = GRAPH
         | 
| 24 | 
            +
                attribute :name
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              # tests
         | 
| 28 | 
            +
              #
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              def test_count
         | 
| 31 | 
            +
                quick_add_node(label: 'actor', properties: {_type: Actor.name, name: "Al Pacino"})
         | 
| 32 | 
            +
                quick_add_node(label: 'actor', properties: {_type: Actor.name, name: "John Travolta"})
         | 
| 33 | 
            +
                assert_equal(2, Actor.count)
         | 
| 34 | 
            +
                assert_equal(1, Actor.count(properties: {name: "Al Pacino"}))
         | 
| 35 | 
            +
              end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
              def test_all
         | 
| 38 | 
            +
                al = Actor.new(name: "Al Pacino").add_to_graph
         | 
| 39 | 
            +
                john = Actor.new(name: "John Travolta").add_to_graph
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                items = Actor.all
         | 
| 42 | 
            +
                assert_equal(2, items.size)
         | 
| 43 | 
            +
                assert_includes(items, al)
         | 
| 44 | 
            +
                assert_includes(items, john)
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                items = Actor.all(properties: {name: "Al Pacino"})
         | 
| 47 | 
            +
                assert_equal(1, items.size)
         | 
| 48 | 
            +
                assert_includes(items, al)
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              def test_find
         | 
| 52 | 
            +
                al = quick_add_node(label: 'actor', properties: {name: "Al Pacino"})
         | 
| 53 | 
            +
                item = Actor.find(al.id)
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                assert_equal(Actor, item.class)
         | 
| 56 | 
            +
                assert_predicate(item, :persisted?)
         | 
| 57 | 
            +
                assert_equal(al.id, item.id)
         | 
| 58 | 
            +
                assert_equal("Al Pacino", item.name)
         | 
| 59 | 
            +
              end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
              def test_find_bad_id
         | 
| 62 | 
            +
                quick_add_node(label: 'actor', properties: {name: "Al Pacino"})
         | 
| 63 | 
            +
                item = Actor.find("-1")
         | 
| 64 | 
            +
                assert_nil(item)
         | 
| 65 | 
            +
              end
         | 
| 66 | 
            +
            end
         | 
| @@ -0,0 +1,57 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "test_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class NodeModelLabelsTest < Minitest::Test
         | 
| 6 | 
            +
              include TestHelpers
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              GRAPH = Redgraph::Graph.new("movies", url: $REDIS_URL)
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def setup
         | 
| 11 | 
            +
                @graph = GRAPH
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def teardown
         | 
| 15 | 
            +
                @graph.delete
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              # test classes
         | 
| 19 | 
            +
              #
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              class Actor
         | 
| 22 | 
            +
                include Redgraph::NodeModel
         | 
| 23 | 
            +
                self.graph = GRAPH
         | 
| 24 | 
            +
                attribute :name
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              class Artist
         | 
| 28 | 
            +
                include Redgraph::NodeModel
         | 
| 29 | 
            +
                self.label = "_artist"
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              class Painter < Artist
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              class Pianist < Artist
         | 
| 36 | 
            +
                self.label = "pianist"
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              # tests
         | 
| 40 | 
            +
              #
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def test_label
         | 
| 43 | 
            +
                assert_equal("actor", Actor.label)
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def test_custom_label
         | 
| 47 | 
            +
                assert_equal("_artist", Artist.label)
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def test_default_label_when_inherited
         | 
| 51 | 
            +
                assert_equal("painter", Painter.label)
         | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              def test_custom_label_when_inherited
         | 
| 55 | 
            +
                assert_equal("pianist", Pianist.label)
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
            end
         | 
| @@ -0,0 +1,126 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require "test_helper"
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class NodeModelTest < Minitest::Test
         | 
| 6 | 
            +
              include TestHelpers
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              GRAPH = Redgraph::Graph.new("movies", url: $REDIS_URL)
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def setup
         | 
| 11 | 
            +
                @graph = GRAPH
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              def teardown
         | 
| 15 | 
            +
                @graph.delete
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              # test classes
         | 
| 19 | 
            +
              #
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              class Animal
         | 
| 22 | 
            +
                include Redgraph::NodeModel
         | 
| 23 | 
            +
                attribute :name
         | 
| 24 | 
            +
                self.graph = "pippo"
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              class Dog < Animal
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              class Actor
         | 
| 31 | 
            +
                include Redgraph::NodeModel
         | 
| 32 | 
            +
                self.graph = GRAPH
         | 
| 33 | 
            +
                attribute :name
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              class Film
         | 
| 37 | 
            +
                include Redgraph::NodeModel
         | 
| 38 | 
            +
                self.graph = GRAPH
         | 
| 39 | 
            +
                attribute :name
         | 
| 40 | 
            +
                attribute :year
         | 
| 41 | 
            +
              end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              # tests
         | 
| 44 | 
            +
              #
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def test_graph_accessor
         | 
| 47 | 
            +
                assert_equal("pippo", Animal.graph)
         | 
| 48 | 
            +
                assert_equal("pippo", Animal.new.graph)
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              def test_class_inheritance
         | 
| 52 | 
            +
                assert_equal("pippo", Dog.graph)
         | 
| 53 | 
            +
                assert_equal("dog", Dog.label)
         | 
| 54 | 
            +
                assert_equal([:id, :name], Dog.attribute_names)
         | 
| 55 | 
            +
              end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
              def test_attribute_names
         | 
| 58 | 
            +
                assert_equal([:id, :name, :year], Film.attribute_names)
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                film = Film.new(name: "Star Wars", year: 1977)
         | 
| 61 | 
            +
                assert_equal("Star Wars", film.name)
         | 
| 62 | 
            +
                assert_equal(1977, film.year)
         | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
              def test_attributes
         | 
| 66 | 
            +
                film = Film.new(name: "Star Wars", year: 1977)
         | 
| 67 | 
            +
                assert_equal({id: nil, name: "Star Wars", year: 1977}, film.attributes)
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
              def test_add_to_graph
         | 
| 71 | 
            +
                assert_equal(0, Film.count)
         | 
| 72 | 
            +
                film = Film.new(name: "Star Wars", year: 1977)
         | 
| 73 | 
            +
                item = film.add_to_graph
         | 
| 74 | 
            +
                assert_predicate(item, :persisted?)
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                assert_equal(1, Film.count)
         | 
| 77 | 
            +
              end
         | 
| 78 | 
            +
             | 
| 79 | 
            +
              def test_merge_into_graph
         | 
| 80 | 
            +
                film = Film.new(name: "Star Wars", year: 1977)
         | 
| 81 | 
            +
                item = film.add_to_graph(allow_duplicates: false)
         | 
| 82 | 
            +
                assert_predicate(item, :persisted?)
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                assert_equal(1, Film.count)
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                film = Film.new(name: "Star Wars", year: 1977)
         | 
| 87 | 
            +
                item = film.add_to_graph(allow_duplicates: false)
         | 
| 88 | 
            +
                assert_predicate(item, :persisted?)
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                assert_equal(1, Film.count)
         | 
| 91 | 
            +
              end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
              def test_add_relation
         | 
| 94 | 
            +
                film = Film.new(name: "Star Wars", year: 1977).add_to_graph
         | 
| 95 | 
            +
                actor = Actor.new(name: "Harrison Ford").add_to_graph
         | 
| 96 | 
            +
                edge = actor.add_relation(type: "ACTED_IN", node: film, properties: {role: "Han Solo"})
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                assert_predicate(edge, :persisted?)
         | 
| 99 | 
            +
                assert_equal("ACTED_IN", edge.type)
         | 
| 100 | 
            +
              end
         | 
| 101 | 
            +
             | 
| 102 | 
            +
              def test_add_relation_with_duplicate_control
         | 
| 103 | 
            +
                film = Film.new(name: "Star Wars", year: 1977).add_to_graph
         | 
| 104 | 
            +
                actor = Actor.new(name: "Harrison Ford").add_to_graph
         | 
| 105 | 
            +
             | 
| 106 | 
            +
                actor.add_relation(type: "ACTED_IN", node: film, properties: {role: "Han Solo"}, allow_duplicates: true)
         | 
| 107 | 
            +
                assert_equal(1, @graph.count_edges)
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                actor.add_relation(type: "ACTED_IN", node: film, properties: {role: "Han Solo"}, allow_duplicates: true)
         | 
| 110 | 
            +
                assert_equal(2, @graph.count_edges)
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                actor.add_relation(type: "ACTED_IN", node: film, properties: {role: "Han Solo"}, allow_duplicates: false)
         | 
| 113 | 
            +
                assert_equal(2, @graph.count_edges)
         | 
| 114 | 
            +
              end
         | 
| 115 | 
            +
             | 
| 116 | 
            +
              def test_casting_query
         | 
| 117 | 
            +
                Film.new(name: "Star Wars", year: 1977).add_to_graph
         | 
| 118 | 
            +
                Actor.new(name: "Harrison Ford").add_to_graph
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                items = Film.query("MATCH (node) RETURN node ORDER BY node.name")
         | 
| 121 | 
            +
                assert_equal(2, items.size)
         | 
| 122 | 
            +
                assert_kind_of(Actor, items[0][0])
         | 
| 123 | 
            +
                assert_kind_of(Film, items[1][0])
         | 
| 124 | 
            +
              end
         | 
| 125 | 
            +
             | 
| 126 | 
            +
            end
         | 
    
        data/test/test_helper.rb
    CHANGED
    
    | @@ -1,5 +1,14 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 | 
            +
            if ENV['COVERAGE']
         | 
| 4 | 
            +
              require 'simplecov'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              SimpleCov.start do
         | 
| 7 | 
            +
                enable_coverage :branch
         | 
| 8 | 
            +
                add_filter %r{^/test/}
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
            end
         | 
| 11 | 
            +
             | 
| 3 12 | 
             
            $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
         | 
| 4 13 | 
             
            require "redgraph"
         | 
| 5 14 |  | 
| @@ -10,3 +19,15 @@ unless $REDIS_URL = ENV['TEST_REDIS_URL'] | |
| 10 19 | 
             
              puts "To run the tests you need to define the TEST_REDIS_URL environment variable"
         | 
| 11 20 | 
             
              exit(1)
         | 
| 12 21 | 
             
            end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            module TestHelpers
         | 
| 24 | 
            +
              private
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              def quick_add_node(label:, properties:)
         | 
| 27 | 
            +
                @graph.add_node(Redgraph::Node.new(label: label, properties: properties))
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              def quick_add_edge(type:, src:, dest:, properties:)
         | 
| 31 | 
            +
                @graph.add_edge(Redgraph::Edge.new(type: type, src: src, dest: dest, properties: properties))
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: redgraph
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.2.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Paolo Zaccagnini
         | 
| 8 8 | 
             
            autorequire:
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2021-04- | 
| 11 | 
            +
            date: 2021-04-19 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: redis
         | 
| @@ -24,6 +24,20 @@ dependencies: | |
| 24 24 | 
             
                - - "~>"
         | 
| 25 25 | 
             
                  - !ruby/object:Gem::Version
         | 
| 26 26 | 
             
                    version: '4'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: activesupport
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: 3.0.0
         | 
| 34 | 
            +
              type: :runtime
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: 3.0.0
         | 
| 27 41 | 
             
            description:
         | 
| 28 42 | 
             
            email:
         | 
| 29 43 | 
             
            - hi@pzac.net
         | 
| @@ -44,14 +58,24 @@ files: | |
| 44 58 | 
             
            - lib/redgraph.rb
         | 
| 45 59 | 
             
            - lib/redgraph/edge.rb
         | 
| 46 60 | 
             
            - lib/redgraph/graph.rb
         | 
| 61 | 
            +
            - lib/redgraph/graph/edge_methods.rb
         | 
| 62 | 
            +
            - lib/redgraph/graph/node_methods.rb
         | 
| 47 63 | 
             
            - lib/redgraph/node.rb
         | 
| 64 | 
            +
            - lib/redgraph/node_model.rb
         | 
| 65 | 
            +
            - lib/redgraph/node_model/class_methods.rb
         | 
| 48 66 | 
             
            - lib/redgraph/query_response.rb
         | 
| 67 | 
            +
            - lib/redgraph/util.rb
         | 
| 49 68 | 
             
            - lib/redgraph/version.rb
         | 
| 50 69 | 
             
            - redgraph.gemspec
         | 
| 51 70 | 
             
            - test/graph_connection_test.rb
         | 
| 71 | 
            +
            - test/graph_edge_methods_test.rb
         | 
| 52 72 | 
             
            - test/graph_manipulation_test.rb
         | 
| 73 | 
            +
            - test/graph_node_methods_test.rb
         | 
| 53 74 | 
             
            - test/graph_queries_test.rb
         | 
| 54 75 | 
             
            - test/graph_test.rb
         | 
| 76 | 
            +
            - test/node_model_class_methods_test.rb
         | 
| 77 | 
            +
            - test/node_model_labels_test.rb
         | 
| 78 | 
            +
            - test/node_model_test.rb
         | 
| 55 79 | 
             
            - test/redgraph_test.rb
         | 
| 56 80 | 
             
            - test/test_helper.rb
         | 
| 57 81 | 
             
            homepage: https://github.com/pzac/redgraph
         | 
| @@ -60,7 +84,7 @@ licenses: | |
| 60 84 | 
             
            metadata:
         | 
| 61 85 | 
             
              homepage_uri: https://github.com/pzac/redgraph
         | 
| 62 86 | 
             
              source_code_uri: https://github.com/pzac/redgraph
         | 
| 63 | 
            -
              changelog_uri: https://github.com/pzac/redgraph/CHANGELOG.md
         | 
| 87 | 
            +
              changelog_uri: https://github.com/pzac/redgraph/blob/master/CHANGELOG.md
         | 
| 64 88 | 
             
            post_install_message:
         | 
| 65 89 | 
             
            rdoc_options: []
         | 
| 66 90 | 
             
            require_paths:
         |