Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator cleanup with tests #1717

Merged
merged 47 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
064aee4
moved generator logic out of railtie
mrhardikjoshi Jan 5, 2024
95600ca
Fix activegraph module for zeitwerk loading
mrhardikjoshi Jan 8, 2024
81942b3
e2e tests github action
mrhardikjoshi Jan 11, 2024
34ed67c
updated script access
mrhardikjoshi Jan 11, 2024
7497fc1
add exe access on shell file
mrhardikjoshi Jan 11, 2024
4883f86
updated action
mrhardikjoshi Jan 11, 2024
4442aab
corrected port
mrhardikjoshi Jan 11, 2024
cb09978
corrected activegraph ref in script
mrhardikjoshi Jan 11, 2024
3fa5e2b
corrected shell script
mrhardikjoshi Jan 11, 2024
c4cac61
use dynamic SHA
mrhardikjoshi Jan 11, 2024
b1bf5e3
corrected quotes around SHA
mrhardikjoshi Jan 11, 2024
08874af
debug action
mrhardikjoshi Jan 11, 2024
875fb3b
debug action
mrhardikjoshi Jan 11, 2024
99e2d0e
Update setup.sh
mrhardikjoshi Jan 11, 2024
2a7aeca
Update e2e_test.yml
mrhardikjoshi Jan 11, 2024
07ea9a7
updated setup
mrhardikjoshi Jan 11, 2024
2f50fe3
use full sha
mrhardikjoshi Jan 12, 2024
9047350
uncomment till model generation
mrhardikjoshi Jan 12, 2024
2b15d7e
corrected sed in shell script
mrhardikjoshi Jan 12, 2024
82a666c
using 3.2.2
mrhardikjoshi Jan 12, 2024
a5a000e
disable other tests
mrhardikjoshi Jan 12, 2024
8e54025
corrected script to run in dev environment
mrhardikjoshi Jan 12, 2024
1087a1f
Update e2e_test.yml
mrhardikjoshi Jan 12, 2024
8af9dbb
Update e2e_test.yml
mrhardikjoshi Jan 12, 2024
2ce7fbc
Update e2e_test.yml
mrhardikjoshi Jan 12, 2024
db92ead
port fix
mrhardikjoshi Jan 12, 2024
cc846ac
added rails console and server test
mrhardikjoshi Jan 12, 2024
d568973
Update setup.sh
mrhardikjoshi Jan 12, 2024
d8a41b6
Update setup.sh
mrhardikjoshi Jan 12, 2024
9ff0001
generator tests
mrhardikjoshi Jan 12, 2024
33f26a5
Update setup.sh
mrhardikjoshi Jan 12, 2024
f6a9fbd
bundle install in github actions
mrhardikjoshi Jan 12, 2024
d020dee
Update model_generator_spec.rb
mrhardikjoshi Jan 12, 2024
68e644e
add jruby tests
mrhardikjoshi Jan 12, 2024
33ca198
downgrading jruby version
mrhardikjoshi Jan 12, 2024
73f8169
jruby excluded against rails 7
mrhardikjoshi Jan 12, 2024
106e797
Update e2e_test.yml
mrhardikjoshi Jan 12, 2024
edbb339
re enabling all normal tests
mrhardikjoshi Jan 12, 2024
64f535a
enhance e2esetup steps
mrhardikjoshi Jan 16, 2024
28418f3
added comment explaining jruby change
mrhardikjoshi Jan 16, 2024
7db5338
added documentation for running e2e tests
mrhardikjoshi Jan 16, 2024
8b7a384
corrected formatting
mrhardikjoshi Jan 17, 2024
b620602
Update activegraph.rb
mrhardikjoshi Jan 22, 2024
e32918a
Update e2e_test.yml
mrhardikjoshi Jan 22, 2024
b74984c
revert unintentional change
mrhardikjoshi Jan 22, 2024
b66032d
Update e2e_test.yml
mrhardikjoshi Jan 25, 2024
2d2f968
cleanup
mrhardikjoshi Jan 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/e2e_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: E2E Test

on:
push:
branches: [ '11' ]
pull_request:
branches: [ '11' ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.ruby == 'ruby' }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
ruby: [ jruby-9.4.5.0, ruby-3.2.2 ]
neo4j: [ 5.15.0 ]
active_model: [ 7.1.2 ]
# exclude:
# - ruby: jruby-9.4.5.0
# active_model: 7.1.2
env:
ACTIVE_MODEL_VERSION: ${{ matrix.active_model }}
JRUBY_OPTS: --debug -J-Xmx1280m -Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -Xcompile.mode=OFF
steps:
- name: Start neo4j
run: docker run --name neo4j --env NEO4J_AUTH=neo4j/password --env NEO4J_ACCEPT_LICENSE_AGREEMENT=yes --env NEO4J_dbms_directories_import= -p7687:7687 -p7474:7474 -v `pwd`/tmp:/var/lib/neo4j/import --rm neo4j:${{ matrix.neo4j }}-enterprise &

- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}

- name: Wait for neo4j
run: while [ $((curl localhost:7474/ > /dev/null 2>&1); echo $?) -ne 0 ]; do sleep 1; done

- name: Setup test rails app
run: ./e2e_tests/setup.sh

- name: Install dependencies
run: bundle update

- name: Run tests
run: bundle exec rspec -Oe2e_tests/.e2e_rspec e2e_tests/
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
run: bundle exec rspec

# - name: Run tests
# run: bundle exec rubocop
# run: bundle exec rubocop
2 changes: 1 addition & 1 deletion docs/activegraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

environment generator
environment nil, env: 'development' do <<END
config.neo4j.driver.url = 'neo4j://localhost:7472'
config.neo4j.driver.url = 'bolt://localhost:7687'
mrhardikjoshi marked this conversation as resolved.
Show resolved Hide resolved
config.neo4j.driver.auth_token = Neo4j::Driver::AuthTokens.basic('neo4j', 'password')
config.neo4j.driver.encryption = false
END
Expand Down
3 changes: 3 additions & 0 deletions e2e_tests/.e2e_rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--tty
--require ./e2e_tests/spec_helper.rb
19 changes: 19 additions & 0 deletions e2e_tests/migration_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load_migration('blah_migration.rb')

describe 'Migration Generator' do
describe 'generated migration file' do
it 'inherits from ActiveGraph::Migrations::Base' do
expect(BlahMigration).to be < ActiveGraph::Migrations::Base
end

it 'defines up method' do
migration = CreateUser.new(nil)
expect(migration.method(:up)).to be_present
end

it 'defines down method' do
migration = CreateUser.new(nil)
expect(migration.method(:up)).to be_present
end
end
end
29 changes: 29 additions & 0 deletions e2e_tests/model_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load 'myapp/app/models/user.rb'
load_migration('create_user.rb')

describe 'Model Generator' do
describe 'generated model class' do
it 'includes ActiveGraph::Node' do
expect(User < ActiveGraph::Node).to be true
end

it 'declares correct property' do
expect(User.attributes['name'].type).to be String
end
end

describe 'generated migration file' do
it 'inherits from ActiveGraph::Migrations::Base' do
expect(CreateUser).to be < ActiveGraph::Migrations::Base
end

it 'can be run/rollback without issue' do
migration = CreateUser.new(nil)
migration.down rescue nil # we make sure migration is not run before
expect do
migration.up
migration.down
end.not_to raise_error
end
end
end
50 changes: 50 additions & 0 deletions e2e_tests/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh

if [[ -n "$ACTIVE_MODEL_VERSION" ]]
then
gem install rails -v $ACTIVE_MODEL_VERSION --no-document
else
gem install rails -v 7.1.2 --no-document
fi

if [[ -n "$SHA" ]]
mrhardikjoshi marked this conversation as resolved.
Show resolved Hide resolved
then
sed 's/.*gem '"'"'activegraph'"'"'.*/gem '"'"'activegraph'"'"', github: "neo4jrb\/activegraph", ref: "'"$SHA"'"/' docs/activegraph.rb > template.tmp
else
echo "SHA=$(git rev-parse "$GITHUB_SHA")" >> $GITHUB_ENV
sed 's/.*gem '"'"'activegraph'"'"'.*/gem '"'"'activegraph'"'"', github: "neo4jrb\/activegraph", ref: "'"$(git rev-parse "$GITHUB_SHA")"'"/' docs/activegraph.rb > template.tmp
fi
rails _7.1.2_ new myapp -O -m ./template.tmp
mrhardikjoshi marked this conversation as resolved.
Show resolved Hide resolved
rm -f ./template.tmp
cd myapp

if [[ -n "$E2E_PORT" ]]
then
sed 's/7687/'$E2E_PORT'/' config/environments/development.rb > dev_env.tmp
mv dev_env.tmp config/environments/development.rb
fi

if [[ -n "$E2E_NO_CRED" ]]
then
sed "s/'neo4j'/''/" config/environments/development.rb > dev_env.tmp
mv dev_env.tmp config/environments/development.rb
sed "s/'password'/''/" config/environments/development.rb > dev_env.tmp
mv dev_env.tmp config/environments/development.rb
fi

bundle exec rails generate model User name:string
bundle exec rails generate migration BlahMigration
bundle exec rake neo4j:migrate
if echo 'puts "hi"' | bundle exec rails c
then
echo "rails console works correctly"
else
exit 1
fi
bundle exec rails s -d
# while [ $((curl localhost:3000/ > /dev/null 2>&1); echo $?) -ne 0 ]; do sleep 1; done
until $(curl --output /dev/null --silent --head --fail localhost:3000); do
printf '.'
sleep 2
done
kill `cat tmp/pids/server.pid`
11 changes: 11 additions & 0 deletions e2e_tests/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'active_graph'
require 'find'

server_url = ENV['NEO4J_URL'] || 'bolt://localhost:7687'
ActiveGraph::Base.driver = Neo4j::Driver::GraphDatabase.driver(server_url, Neo4j::Driver::AuthTokens.basic('neo4j', 'password'))

def load_migration(suffix)
Find.find('myapp/db/neo4j/migrate') do |path|
load path if path =~ /.*#{suffix}$/
end
end
2 changes: 2 additions & 0 deletions lib/active_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
loader.ignore(File.expand_path('rails', __dir__))
loader.ignore(File.expand_path('active_graph/railtie.rb', __dir__))
loader.inflector.inflect("ansi" => "ANSI")
module ActiveGraph
end
loader.setup
# loader.eager_load

Expand Down
12 changes: 7 additions & 5 deletions lib/active_graph/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Need the action_dispatch railtie to have action_dispatch.rescue_responses initialized correctly
require 'action_dispatch/railtie'
require 'rails/generators'
require 'rails/generators/active_model'
require 'rails/generators/named_base'
require 'rails/railtie'
require File.expand_path('../rails/generators/migration_helper.rb', __dir__)
Rails::Generators::GeneratedAttribute.include ActiveGraph::Generators::GeneratedAttribute
require 'active_graph'

module ActiveGraph
Expand Down Expand Up @@ -44,6 +39,13 @@ def empty_config
ActiveGraph::Config[:verbose_query_logs] = false
end

# By default, Rails loads generators from load path.
# However, if we want to place generators at a different location we have to use "generators" hook
# https://api.rubyonrails.org/classes/Rails/Railtie.html
generators do
require File.expand_path('../rails/generators/migration_helper.rb', __dir__)
end

# Starting Neo after :load_config_initializers allows apps to
# register migrations in config/initializers
initializer 'neo4j.start', after: :load_config_initializers do |app|
Expand Down
3 changes: 3 additions & 0 deletions lib/rails/generators/migration_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ def type_class
end
end
end

require 'rails/generators/named_base'
Rails::Generators::GeneratedAttribute.include ActiveGraph::Generators::GeneratedAttribute
Loading