Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created May 5, 2015 19:53
Show Gist options
  • Save hayduke19us/5e22fbcaacb18456c689 to your computer and use it in GitHub Desktop.
Save hayduke19us/5e22fbcaacb18456c689 to your computer and use it in GitHub Desktop.
require 'test_helper'
class Api::V1::Scout::SyncControllerTest < ActionController::TestCase
test "sync without an email returns error" do
get :get_sync, format: :json
assert_equal "Missing email header", response_body["message"]
end
test "sync with wrong email returns error and no timestamp or token" do
@request.headers["Email"] = 'afadf'
get :get_sync, format: :json
assert_equal 2, response_body.count
end
test "sync with correct email empty timestamp and wrong token" do
@request.headers["Timestamp"] = ""
get :get_sync, format: :json
assert_equal false, response_body["success"]
end
test "sync with correct params empty timestamp returns full sync" do
authenticate_existing_user
@request.headers["Timestamp"] = ""
get :get_sync, format: :json
assert_equal true, response_body["success"]
end
test "sync with timestamp and new grower, field, farm time now" do
authenticate_existing_user
time_now_header
create_scout_resources
get :get_sync, format: :json
data = response_body["data"]
assert_equal 1, data["reports"]["1"].count
assert_equal 1, data["growers"]["1"].count
assert_equal 1, data["farms"]['1'].count
assert_equal 1, data["fields"]['1'].count
end
test "sync with timestamp and new grower, there schould be no data
in growers etc" do
authenticate_existing_user
create_scout_resources # This is where the field and farm are created
time_now_header
new_grower = {"id" => 323, "name" => "Another GROWER"}
post :post_sync, "data" => {"growers" => { "1" => [new_grower]} }, format: :json
data = response_body["data"]
assert_equal 1, data["growers"]['4'].count
end
test "sync with timestamp and existing resources new report and sample for report
no reports or others should be returned because the timestamp is new" do
scout = authenticate_existing_user
grower = scout.growers.last
farm = grower.farms.first
report = {"field_id" => farm.fields.first.id,
"grower_id" => grower.id,
"temp" => 60,
"id" => 29092,
}
other_sample = { report_id: 29092,
comment: "TEST SAMPLE",
id: 3425,
long: "-121.0",
lat: "33.0" }
time_now_header
post :post_sync, "data" => {"reports" => {"1" => [report]},
"others" => {"1" => [other_sample]}}, format: :json
data = response_body["data"]
assert_equal 1, data["others"]['4'].count
assert_equal 1, data["reports"]['4'].count
report = data["reports"]['4'].first['new_id']
end
test "sync with new image and new other sample with location we set the timestamp
right before creating grower,farm,relationship,field,report to simulate
creating things on the server and receiving a post of a new report etc." do
authenticate_existing_user
time_now_header
create_scout_resources # This is where the field and farm are created
other_sample = { report_id: Report.first.id,
comment: "TEST SAMPLE",
id: 3425,
long: "-121.0",
lat: "33.0" }
image = {
"imageable_id" => 3425,
"imageable_type" => "OtherSample",
"id" => 390,
"file_name" => "Something.png"
}
post :post_sync, "data" => {"others" => {"1" => [other_sample]},
"images" => {"1" => [image]}}, format: :json
data = response_body["data"]
assert_equal 1, data["others"]["4"].count
assert OtherSample.find(data["others"]["4"].first["new_id"]).location.lonlat
assert_equal 1, data["images"]["4"].count
i = Image.find(data["images"]["4"].first["new_id"])
assert i.asset
i.destroy
end
test "create a disorder sample" do
authenticate_existing_user
time_now_header
post :post_sync, "data" => { "disorders" => {"1" => [disorder_sample]}},
format: :json
data = response_body["data"]
assert_equal disorder_sample['id'], data["disorders"]["4"].first["id"]
assert DisorderSample.find(data["disorders"]["4"].first["new_id"])
end
test "create a disease sample" do
authenticate_existing_user
time_now_header
post :post_sync, "data" => { "diseases" => {"1" => [disorder_sample]}},
format: :json
data = response_body["data"]
assert_equal disorder_sample['id'], data["diseases"]["4"].first["id"]
assert DiseaseSample.find(data["diseases"]["4"].first["new_id"])
end
test "create a weed sample" do
authenticate_existing_user
time_now_header
post :post_sync, "data" => { "weeds" => {"1" => [disorder_sample]}},
format: :json
data = response_body["data"]
assert_equal disorder_sample['id'], data["weeds"]["4"].first["id"]
assert WeedSample.find(data["weeds"]["4"].first["new_id"])
end
# This is a big test
focus
test "create new report with a new corn stand sample and image
new disorder, with image and weeds to existing report" do
authenticate_existing_user
time = Time.current
@request.headers["Timestamp"] = time.to_json
post :post_sync, "data" => {
"reports" => {"1" => [minimum_report]},
"weeds" => {"1" => [weed_sample]},
"disorders" => {"1" => [disorder_sample]},
"images" => {"1" => [attached_image(3425, "DisorderSample"),
attached_image(3425, "CornStandCountSample")]},
"corn_stand" => {"1" => [corn_stand_sample]}
},
format: :json
data = response_body["data"]
report = Report.find(data["reports"]["4"].first["new_id"])
weed = WeedSample.find(data["weeds"]["4"].first["new_id"])
disorder = DisorderSample.find(data["disorders"]["4"].first["new_id"])
corn_stand = CornStandCountSample.find(data["corn_stand"]["4"].first["new_id"])
assert_equal 1, disorder.images.count
assert_equal 1, corn_stand.images.count
assert_equal report.id, weed.report_id
assert_equal report.id, disorder.report_id
end
# TODO
test "when a report is synced and created intel is notified if it meets the
stipulations SEE google doc Anomaly report" do
authenticate_existing_user
time = Time.current
@request.headers["Timestamp"] = time.to_json
assert_equal 2, Admin.all.count
assert_difference '::Delayed::Job.all.size', +2 do
post :post_sync, "data" => {
"reports" => {"1" => [immediate_attention_report]},
"diseases" => {"1" => [disease_sample('heavy')]}}, format: :json
end
data = response_body["data"]
report = Report.find(data["reports"]["4"].first["new_id"])
disease = DiseaseSample.find(data["diseases"]["4"].first["new_id"])
assert_equal report.id, disease.report_id
end
def assert_nothing_is_sent_back_in_created response
assert_equal 0, response["reports"]["1"].count
# Weed is returning and it is to an existing report
assert_equal 0, response["weeds"]["1"].count
assert_equal 0, response["disorders"]["1"].count
assert_equal 0, response["corn_stand"]["1"].count
end
test "another sync with new image and new corn sample with preixisting report
with location" do
authenticate_existing_user
post :post_sync, "data" => image_and_corn_stand, format: :json
data = response_body["data"]
assert_equal 1, data["corn_stand"]["4"].count
assert CornStandCountSample.find(data["corn_stand"]["4"].first["new_id"]).location.lonlat
assert_equal 1, data["images"]["4"].count
end
# This is where the error was
test "new report without temp with corn sample should return null" do
authenticate_existing_user
corn = corn_stand_count_samples(:one).attributes.except("id")
corn = corn.merge({long: 0, lat: 0, id: 3425})
post :post_sync, "data" => {"reports" => {"1" => [minimum_report]},
"corn_stand" => {"1" => [corn_stand_sample]}}, format: :json
data = response_body["data"]
assert CornStandCountSample.find(data["corn_stand"]["4"].first["new_id"]).location.lonlat
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment