#!/bin/env perl

# This is a standalone testing executable for PS::MOPS::DC::History::Removal.

use strict;

use Benchmark ':hireswallclock';

# Define variables for benchmarking.
our $g_start;
our $g_end;
our $g_diff;

# Testing environment.
use Test::More tests=>3; # Perl testing framework.
use PS::MOPS::DC::History::Removal;
use PS::MOPS::Test::Instance;
use PS::MOPS::Constants qw($MOPS_EFF_CLEAN);
use PS::MOPS::DC::Field;
use PS::MOPS::DC::Tracklet;
use PS::MOPS::DC::Orbit;
use PS::MOPS::DC::DerivedObject;

# Make the PS-MOPS Instance available through $g_inst and
# the Test Instance available through $g_test_inst.
our $g_test_inst=PS::MOPS::Test::Instance->new();
our $g_inst=$g_test_inst->getPSMOPSInstance();

our $g_h_remov;

# create and insert related mops objects
# make orbits
our $g_o1=PS::MOPS::DC::Orbit->new($g_inst,
                                   e=>0,
                                   epoch=>0,
                                   timePeri=>0,
                                   argPeri=>0,
                                   node=>0,
                                   q=>0,
                                   i=>0,
                                   hV=>0,
                                   residual=>0);

# insert orbits
$g_o1->insert();

# make derived objects
our $g_do1=PS::MOPS::DC::DerivedObject->new($g_inst); # Passing g_inst here is critical.

# insert derived objects
$g_do1->insert();

# make field
our @g_de=(0,0,0,0,0,0,0,0,0,0);
our $g_fld=PS::MOPS::DC::Field->new($g_inst,
                                    epoch=>50000,
                                    filter=>'w', # referential integrity enforced.
                                    timeStop=>0,
                                    dec=>0,
                                    raSigma=>0,
                                    surveyMode=>0,
                                    de=>\@g_de,
                                    decSigma=>0,
                                    ra=>0,
                                    timeStart=>0,
                                    obscode=>0,
                                    limitingMag=>0);

# insert field
$g_fld->insert();

# detections
our $g_d1; # detection
our $g_d2; # detection

$g_d1=PS::MOPS::DC::Detection->new($g_inst,
                                   objectName=>"test object 1",
                                   mag=>0,
                                   dec=>0,
                                   ra=>0,
                                   epoch=>'56000',
                                   isSynthetic=>0,
                                   filter=>'w', # value enforced by ref integrity
                                   s2n=>0,
                                   fieldId=>1
                                  );

$g_d2=PS::MOPS::DC::Detection->new($g_inst,
                                   objectName=>"test object 2",
                                   mag=>0,
                                   dec=>0,
                                   ra=>0,
                                   epoch=>'56001',
                                   isSynthetic=>0,
                                   filter=>'w', # value enforced by ref integrity
                                   s2n=>0,
                                   fieldId=>1
                                  );

$g_d1->addToField($g_fld); # detection is added to the db.
$g_d2->addToField($g_fld); # detection is added to the db.

# tracklets
our @g_dets=($g_d1,$g_d2);
our $g_t1=PS::MOPS::DC::Tracklet->new($g_inst,
                                      detections=>\@g_dets,
                                      fieldId=>1,
                                      classification=>$MOPS_EFF_CLEAN);

$g_t1->insert();

our @g_tracklet_ids=(1);

# Test definition 1.
# create new object
# Expected result: history removal object
#
# @return {1|0} Success is 1, failure is 0.
sub test_new
{
    my $success=0;

    # trackletId is required by record() but not mandated by new()
    $g_h_remov=PS::MOPS::DC::History::Removal->new($g_inst,
                                                   derivedobjectId=>1,
                                                   fieldId=>1,
                                                   orbitId=>1,
                                                   orbitCode=>0,
                                                   classification=>0,
                                                   trackletId=>1
                                                   );
    if($g_h_remov=~/PS::MOPS::DC::History::Removal/){$success=1;}
    return $success;
}

# Test definition 2.
# insert db record
# Expected result: db record
#
# @return {1|0} Success is 1, failure is 0.
sub test_record
{
    my $success=0;
    $g_h_remov->record();
    my $result=modchr_retrieve($g_inst,
                               eventId=>1);

    # since modchr_retrieve() is not fully implemented this is not a complete test

    if($result=~/DBI::db/){$success=1;}
    return $success;
}

# Test definition 3.
# retrieve db record as object
# Expected result: history removal object
#
# @return {1|0} Success is 1, failure is 0.
sub test_modchr_retrieve
{
    my $success=0;

    # this function has no explicit return value
    # this test is not meaningful
    my $result=modchr_retrieve($g_inst);
    if($result=~/DBI::db/){$success=1;}
    return $success;
}

# Test 1: test_new.
$g_start=new Benchmark;
ok(test_new, "PS-MOPS-DC-History-Removal->test_new");
$g_end=new Benchmark;
print "time: PS-MOPS-DC-History-Removal->test_new: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 2: test_record.
$g_start=new Benchmark;
ok(test_record, "PS-MOPS-DC-History-Removal->test_record");
$g_end=new Benchmark;
print "time: PS-MOPS-DC-History-Removal->test_record: " . timestr(timediff($g_end,$g_start),'all') . "\n";

# Test 3: modchr_retrieve.
$g_start=new Benchmark;
ok(test_modchr_retrieve, "PS-MOPS-DC-History-Removal->test_modchr_retrieve");
$g_end=new Benchmark;
print "time: PS-MOPS-DC-History-Removal->test_modchr_retrieve: " . timestr(timediff($g_end,$g_start),'all') . "\n";
