Skip to content
Snippets Groups Projects
calibrators.service.spec.ts 2.5 KiB
Newer Older
import {CalibratorsService} from './calibrators.service';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {TestBed} from "@angular/core/testing/";
import {ConfigurationService} from "../env/configuration.service";
import {Configuration} from "../env/model/configuration";

export class FakeConfigurationService {
  get config() {
    return {
      dryrun: "false",
      rootDataDirectory: "/lustre/aoc/cluster/pipeline/vlass_test/spool",
      url: "http://localhost:4200/VlassMngr",
      weblogbaseurl: "http://webtest.aoc.nrao.edu/",
      projectcode: "VLASS1.1",
      wsurl: "ws://localhost:8081/VlassMngr/changes",
      currentepoch: "1"
    } as Configuration;
  };
}

describe('CalibratorsService', () => {

  let httpMock: HttpTestingController;
  let calibratorsService: CalibratorsService;
  let fakeConfigService: FakeConfigurationService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [CalibratorsService, {provide: ConfigurationService, useClass: FakeConfigurationService}]
    });
    calibratorsService = TestBed.get(CalibratorsService);
    httpMock = TestBed.get(HttpTestingController);
    fakeConfigService = TestBed.get(ConfigurationService);
  });

  it('should retrieve calibrators from endpoint', () => {
    const calibrations = [{
      "id": 734,
      "name": "A0004-1148",
      "sctCatalogId": null,
      "sctId": 1,
      "type": null,
      "fluxes": [],
      "polarization": []
    }];

    calibratorsService.getCalibrators().subscribe(response => {
      expect(response).toEqual(calibrations, 'calibrations');
    }, fail);

    const req = httpMock.expectOne(fakeConfigService.config.url + calibratorsService.endPoint);
    expect(req.request.method).toBe("GET");
    req.flush(calibrations);

    httpMock.verify();
  });

  it('should create a calibration', () => {
    const flux = {
      frequency: 12.3,
      flux: 45,
      spectralIndex: 67,
      observingDate: null,
      uvMin: 8,
      uvMax: 9,
      id: 123
    };

    calibratorsService.saveCalibratorFlux(734, 123, 12.3, 45, 67, 8, 9).subscribe(response => {
      console.log('response type:', typeof response);
      expect(Object.assign({}, response)).toEqual(Object.assign({}, flux), 'create flux');
    });

    const req = httpMock.expectOne(fakeConfigService.config.url + calibratorsService.endPoint + '734/flux/123');
    expect(req.request.method).toBe("POST");
    req.flush(flux);

    httpMock.verify();
  });

});