#!/usr/pkg/bin/ruby26
# $Id: misc_tests.rb 6700 2018-01-04 15:27:34Z flaterco $
# Miscellaneous checks to do after the last import.

require "pg"

db = PG::Connection.open(:dbname => "harmbase2")

# 1. There should be no duplicated names.

print "Checking for duplicate names: "
query = db.exec("select name from data_sets group by name having count(*) > 1")
if query.ntuples > 0
  print "FAIL\n"
  puts query.values
else
  print "pass\n"
end

# 2. There should be only these 5 stations without states.
#  station_id |             name              
# ------------+-------------------------------
#  1619000    | Johnston Atoll, Pacific Ocean
#  1619910    | Sand Island, Midway Islands
#  8447436    | Texas Tower, Georges Shoal
#  TPT2739    | Palmyra Island
#  TPT2737    | Howland Island
# (5 rows)
# N.B., Wake Island was assigned to MH in 2017.

Stateless = ["1619000", "1619910", "8447436", "TPT2739", "TPT2737"]
print "Checking for stations missing states: "
query = db.exec("select station_id, name from data_sets where state is null")
if query.ntuples != 5 or !(query.column_values(0) - Stateless).empty?
  print "FAIL\n"
  puts query.values
else
  print "pass\n"
end

# 3. No station should have switched hemispheres from one year to the next.

print "Checking for stations that switched hemispheres: "
query = db.exec("select A.station_id, A.name from data_sets A, data_sets_old B where A.station_id = B.station_id and sign(A.lng)*sign(B.lng) != 1")
if query.ntuples > 0
  print "FAIL\n"
  puts query.values
else
  print "pass\n"
end

# 4. GU, MH, MP, FM, and PW are all in the eastern hemisphere; AS is in the
# western hemisphere.

print "Checking for Pacific islands in the wrong hemisphere: "
query = db.exec("select station_id, name from data_sets where state in ('GU', 'MH', 'MP', 'FM', 'PW') and lng < 0 or state = 'AS' and lng > 0")
if query.ntuples > 0
  print "FAIL\n"
  puts query.values
else
  print "pass\n"
end

# 5. St. Lawrence Island is in time zone :America/Adak.

print "Checking time zones on St. Lawrence Island: "
query = db.exec("select station_id, name from data_sets where name ~* 'St. Lawrence Island' and timezone != ':America/Adak'")
if query.ntuples > 0
  print "FAIL\n"
  puts query.values
else
  print "pass\n"
end

# 6. Port Saint Joe and White City, FL are in time zone :America/New_York.

print "Checking time zones for Port Saint Joe and White City, FL: "
query = db.exec("select station_id, name, timezone from data_sets where station_id in ('8728912', '8728853') and timezone != ':America/New_York'")
if query.ntuples > 0
  print "FAIL\n"
  puts query.values
else
  print "pass\n"
end

# 7. Checking for any sub stations depending on those.

print "Checking for any subs referring to Port Saint Joe and White City, FL: "
query = db.exec("select station_id, name, timezone from data_sets where ref_index in (select index from data_sets where station_id in ('8728912', '8728853') and datum is not null)")
if query.ntuples > 0
  print "FAIL\n"
  puts query.values
else
  print "pass\n"
end
