<commit_before>import { TestBed, async, inject } from '@angular/core/testing';
import {
  HttpModule,
  Http,
  Response,
  ResponseOptions,
  XHRBackend
} from '@angular/http';
import { MockBackend } from '@angular/http/testing';

import { Project } from './project';
import { ProjectService } from './project.service';

describe('ProjectService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpModule],
      providers: [
        ProjectService,
        { provide: XHRBackend, useClass: MockBackend },
      ]
    });
  });

  it('should get all projects from backend',
    inject([ProjectService, XHRBackend], (service, mockBackend) => {
      const mockResponse = {
        data: [
          {
            name: 'test',
            title: 'test project',
            summary: 'test summary',
            info: 'some info',
            link: 'http://some.location/here',
            resourcesUrl: 'http://some.location/'
          },
          {
            name: 'test2',
            title: 'test project2',
            summary: 'test2 summary',
            info: 'some info2',
            link: 'http://some.location/here/2',
            resourcesUrl: 'http://some.location/2'
          }
        ]
      };

      mockBackend.connections.subscribe((connection) => {
        connection.mockRespond(new Response(new ResponseOptions({
          body: JSON.stringify(mockResponse)
        })));
      });

      service.getProjects(0).subscribe((projects: Project[]) => {
        expect(projects.length).toBe(2);

        expect(projects[0].name).toBe('test');
        expect(projects[0].title).toBe('test project');
        expect(projects[0].summary).toBe('test summary');
        expect(projects[0].info).toBe('some info');
        expect(projects[0].link).toBe('http://some.location/here');
        expect(projects[0].resourcesUrl).toBe('http://some.location/');

        expect(projects[1].name).toBe('test2');
        expect(projects[1].title).toBe('test project2');
        expect(projects[1].summary).toBe('test2 summary');
        expect(projects[1].info).toBe('some info2');
        expect(projects[1].link).toBe('http://some.location/here/2');
        expect(projects[1].resourcesUrl).toBe('http://some.location/2');
      });
    })
  );<commit_msg>Cover error case in project service<commit_after>import { TestBed, async, inject } from '@angular/core/testing';
import {
  HttpModule,
  Http,
  Response,
  ResponseOptions,
  XHRBackend
} from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { Observable } from 'rxjs/Observable';

import { Project } from './project';
import { ProjectService } from './project.service';

const mockFirstProject: Project = {
  name: 'test',
  title: 'test project',
  summary: 'test summary',
  info: 'some info',
  link: 'http://some.location/here',
  resourcesUrl: 'http://some.location/'
};

const mockSecondProject: Project = {
  name: 'test2',
  title: 'test project2',
  summary: 'test2 summary',
  info: 'some info2',
  link: 'http://some.location/here/2',
  resourcesUrl: 'http://some.location/2'
};

describe('ProjectService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpModule],
      providers: [
        ProjectService,
        { provide: XHRBackend, useClass: MockBackend }
      ]
    });
  });

  it(
    'should get all projects from backend',
    inject([ProjectService, XHRBackend], (service, mockBackend) => {
      const mockResponse = {
        data: [mockFirstProject, mockSecondProject]
      };

      mockBackend.connections.subscribe(connection => {
        connection.mockRespond(
          new Response(
            new ResponseOptions({
              body: JSON.stringify(mockResponse)
            })
          )
        );
      });

      service.getProjects(0).subscribe((projects: Project[]) => {
        expect(projects.length).toBe(2);
        expect(projects[0]).toEqual(mockFirstProject);
        expect(projects[1]).toEqual(mockSecondProject);
      });
    })
  );

  it(
    'should get specific number of projects from backend',
    inject([ProjectService, XHRBackend], (service, mockBackend) => {
      const numberToFetch = 2;

      const mockResponse = {
        data: [
          mockFirstProject,
          mockSecondProject,
          mockFirstProject,
          mockSecondProject
        ]
      };

      mockBackend.connections.subscribe(connection => {
        connection.mockRespond(
          new Response(
            new ResponseOptions({
              body: JSON.stringify(mockResponse)
            })
          )
        );
      });

      service.getProjects(0, numberToFetch).subscribe((projects: Project[]) => {
        expect(projects.length).toBe(numberToFetch);
        expect(projects[0]).toEqual(mockFirstProject);
        expect(projects[1]).toEqual(mockSecondProject);
      });
    })
  );

  it(
    'Should handle errors when getting single album info',
    inject([ProjectService], (service: ProjectService) => {
      const http = TestBed.get(Http);
      spyOn(http, 'get').and.returnValue(Observable.throw(new Error()));

      service
        .getProjects(0)
        .subscribe(
          (response: Project[]) => fail('should not get here'),
          (err: Error) => expect(err).toBeTruthy()
        );
    })
  );
{'fraction_non_alphanumeric': 0.1497196261682243, 'fraction_numerical': 0.007663551401869159, 'mean_word_length': 2.539021164021164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7133268', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1496, 'n_words': 285}