|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -require File.expand_path("../../../../spec/e2e", __FILE__) |
| 15 | +require_relative "../app.rb" |
16 | 16 | require "rspec" |
17 | | -require "capybara/rspec" |
18 | | -require "capybara/poltergeist" |
| 17 | +require "rack/test" |
19 | 18 |
|
20 | | -Capybara.current_driver = :poltergeist |
| 19 | +describe "Cloud SQL sample", type: :feature do |
| 20 | + include Rack::Test::Methods |
21 | 21 |
|
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 |
25 | 31 |
|
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 |
31 | 34 |
|
32 | | - File.write(app_yaml, configuration) |
| 35 | + load File.expand_path("../create_tables.rb", __dir__) |
33 | 36 |
|
34 | | - @url = E2E.url |
| 37 | + expect(@database.tables).to include :visits |
35 | 38 | end |
36 | 39 |
|
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 "/" } |
39 | 47 |
|
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 |
43 | 53 | end |
44 | 54 | end |
0 commit comments