Skip to content

Commit e42bfc8

Browse files
author
remi Taylor
committed
Add local spec for App Engine Cloud SQL sample
- Cloud SQL end-to-end test is skipped unless E2E is set to true to disable this test unless explicitly run - Cloud SQL sample spec can be run locally - Sample spec is run on Travis (still not currently running end-to-end)
1 parent aca0cc5 commit e42bfc8

6 files changed

Lines changed: 87 additions & 20 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ env:
3939
- TEST_DIR=translate
4040
- TEST_DIR=language
4141
- TEST_DIR=appengine/endpoints
42+
- TEST_DIR=appengine/cloudsql
4243
- TEST_DIR=vision
4344

4445
before_install:

appengine/cloudsql/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ group :test do
2525
gem "rspec"
2626
gem "capybara"
2727
gem "poltergeist"
28+
gem "rack-test"
29+
gem "sqlite3"
2830
end

appengine/cloudsql/Gemfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ GEM
4747
rack (~> 1.5)
4848
rack-protection (~> 1.4)
4949
tilt (>= 1.3, < 3)
50+
sqlite3 (1.3.12)
5051
tilt (2.0.5)
5152
websocket-driver (0.6.4)
5253
websocket-extensions (>= 0.1.0)
@@ -61,9 +62,11 @@ DEPENDENCIES
6162
capybara
6263
mysql2
6364
poltergeist
65+
rack-test
6466
rspec
6567
sequel
6668
sinatra
69+
sqlite3
6770

6871
BUNDLED WITH
69-
1.12.5
72+
1.13.3

appengine/cloudsql/spec/cloudsql_spec.rb

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,43 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
require File.expand_path("../../../../spec/e2e", __FILE__)
15+
require_relative "../app.rb"
1616
require "rspec"
17-
require "capybara/rspec"
18-
require "capybara/poltergeist"
17+
require "rack/test"
1918

20-
Capybara.current_driver = :poltergeist
19+
describe "Cloud SQL sample", type: :feature do
20+
include Rack::Test::Methods
2121

22-
RSpec.describe "Cloud SQL on Google App Engine", type: :feature do
23-
before :all do
24-
app_yaml = File.expand_path("../../app.yaml", __FILE__)
22+
def app
23+
Sinatra::Application
24+
end
25+
26+
before do
27+
@database = Sequel.sqlite database: ":memory:"
28+
29+
expect(Sequel).to receive(:mysql2).and_return @database
30+
end
2531

26-
configuration = File.read(app_yaml)
27-
configuration.sub! "[YOUR_USER]", ENV["MYSQL_USER"]
28-
configuration.sub! "[YOUR_PASSWORD]", ENV["MYSQL_PASSWORD"]
29-
configuration.sub! "[YOUR_DATABASE]", ENV["MYSQL_DATABASE"]
30-
configuration.sub! "[YOUR_SOCKET_PATH]", ENV["MYSQL_SOCKET_PATH"]
32+
it "can create database schema by running create_tables.rb" do
33+
expect(@database.tables).not_to include :visits
3134

32-
File.write(app_yaml, configuration)
35+
load File.expand_path("../create_tables.rb", __dir__)
3336

34-
@url = E2E.url
37+
expect(@database.tables).to include :visits
3538
end
3639

37-
it "displays recent visits" do
38-
2.times { visit @url }
40+
it "displays hashes of the IP addresses of the top 10 most recent visits" do
41+
load File.expand_path("../create_tables.rb", __dir__)
42+
expect(@database[:visits].count).to eq 0
43+
44+
localhost_user_ip = Digest::SHA256.hexdigest "127.0.0.1"
45+
46+
15.times { get "/" }
3947

40-
expect(page).to have_content "Last 10 visits:"
41-
expect(page).to have_content "Time:"
42-
expect(page).to have_content "Addr:"
48+
expect(@database[:visits].count).to eq 15
49+
expect(@database[:visits].first[:user_ip]).to eq localhost_user_ip
50+
expect(last_response.body).to include "Last 10 visits"
51+
expect(last_response.body).to include "Addr: #{localhost_user_ip}"
52+
expect(last_response.body.scan("Addr:").count).to eq 10
4353
end
4454
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require File.expand_path("../../../../spec/e2e", __FILE__)
16+
require "rspec"
17+
require "capybara/rspec"
18+
require "capybara/poltergeist"
19+
20+
Capybara.current_driver = :poltergeist
21+
22+
RSpec.describe "Cloud SQL on Google App Engine", type: :feature do
23+
before :all do
24+
skip "End-to-end tests skipped" unless E2E.run?
25+
26+
app_yaml = File.expand_path("../../app.yaml", __FILE__)
27+
28+
configuration = File.read(app_yaml)
29+
configuration.sub! "[YOUR_USER]", ENV["MYSQL_USER"]
30+
configuration.sub! "[YOUR_PASSWORD]", ENV["MYSQL_PASSWORD"]
31+
configuration.sub! "[YOUR_DATABASE]", ENV["MYSQL_DATABASE"]
32+
configuration.sub! "[YOUR_SOCKET_PATH]", ENV["MYSQL_SOCKET_PATH"]
33+
34+
File.write(app_yaml, configuration)
35+
36+
@url = E2E.url
37+
end
38+
39+
it "displays recent visits" do
40+
2.times { visit @url }
41+
42+
expect(page).to have_content "Last 10 visits:"
43+
expect(page).to have_content "Time:"
44+
expect(page).to have_content "Addr:"
45+
end
46+
end

spec/e2e.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
class E2E
1717
class << self
18+
def run?
19+
# Only run end-to-end tests when E2E environment variable is set to TRUE
20+
ENV["E2E"] == "true"
21+
end
22+
1823
def check()
1924
if @url.nil?
2025
test_dir = ENV["TEST_DIR"]

0 commit comments

Comments
 (0)