<commit_before>import { it, inject, beforeEachProviders, expect, describe } from '@angular/core/testing';
import { Injectable, Injector } from '@angular/core';
import { MockStore } from './mock.store';
import { BaseModel, identifier } from '../models/model';
import { BaseStore } from './store';
import { MinLength, Validate, ValidatorConstraint } from '../validation';
import { ValidationException } from '../../server/exeptions/exceptions';
import { ValidatorConstraintInterface } from '../validation';
import Spy = jasmine.Spy;
import { Primary } from '../models/types/primary.decorator';

@Injectable()
class StubService {

  public isTruthy(value: any): boolean {
    return !!value;
  }

  public isTruthyPromise(value: any): Promise<boolean> {
    return Promise.resolve(!!value);
  }

}

@Injectable()
@ValidatorConstraint()
class CustomValidator implements ValidatorConstraintInterface {

  constructor(protected service: StubService) {
  }

  public validate(value: any): boolean {

    return this.service.isTruthy(value);
  }

}

@Injectable()
@ValidatorConstraint()
class CustomValidatorAsync implements ValidatorConstraintInterface {

  constructor(protected service: StubService) {
  }

  public validate(value: any): Promise<boolean> {

    return this.service.isTruthyPromise(value);
  }

}

class Ship extends BaseModel {
  @Primary()
  public shipId: number;

  @MinLength(2)
  public name: string;

  public idRandom: string;
  public idSeeded: string;

  @Validate(CustomValidator)
  public truthyValue: any;

  @Validate(CustomValidatorAsync)
  public truthyValueAsync: any;

}

abstract class ShipStore extends BaseStore<Ship> {

}

@Injectable()
class ShipMockStore extends MockStore<Ship> {

  constructor(injector: Injector) {
    super(Ship, injector);
  }

  protected getMock(id?: identifier): Ship {
    return new this.modelStatic({
      shipId: id || this.chance()
        .integer(),
      name: 'Razorback',
      idRandom: this.chance()
        .guid(),
      idSeeded: this.chance(1)
        .guid(),
      truthyValue: true,
      truthyValueAsync: true,
    });
  }

}

@Injectable()
class TestClass {

  constructor(public shipStore: ShipStore) {

  }

}

//we need to have a singleton instance here to spy on, as the validator creates new singletons
// of the custom validator
const stubServiceSingleton = new StubService();

const providers:any[] = [
  TestClass,
  CustomValidator,
  CustomValidatorAsync,
  {provide: StubService, deps: [], useFactory: () => stubServiceSingleton},
  {provide: ShipStore, useClass: ShipMockStore},
];

describe('Mock Store', () => {

  beforeEachProviders(() => providers);

  it('is injected with the store instance token', inject([TestClass], (c: TestClass) => {

    expect(c instanceof TestClass)
      .toBe(true);
    expect(c.shipStore instanceof BaseStore)
      .toBe(true);
    expect(c.shipStore instanceof ShipMockStore)
      .toBe(true);

  }));

  it('retrieves a promise of a mock entity', inject([TestClass], (c: TestClass) => {

    return c.shipStore.findOne(1234)
      .then((entity) => {

        expect(entity instanceof Ship)<commit_msg>chore(test): Add missing test for abstract store<commit_after>import { it, inject, beforeEachProviders, expect, describe } from '@angular/core/testing';
import { Injectable, Injector } from '@angular/core';
import { MockStore } from './mock.store';
import { BaseModel, identifier } from '../models/model';
import { BaseStore } from './store';
import { MinLength, Validate, ValidatorConstraint } from '../validation';
import { ValidationException } from '../../server/exeptions/exceptions';
import { ValidatorConstraintInterface } from '../validation';
import Spy = jasmine.Spy;
import { Primary } from '../models/types/primary.decorator';

@Injectable()
class StubService {

  public isTruthy(value: any): boolean {
    return !!value;
  }

  public isTruthyPromise(value: any): Promise<boolean> {
    return Promise.resolve(!!value);
  }

}

@Injectable()
@ValidatorConstraint()
class CustomValidator implements ValidatorConstraintInterface {

  constructor(protected service: StubService) {
  }

  public validate(value: any): boolean {

    return this.service.isTruthy(value);
  }

}

@Injectable()
@ValidatorConstraint()
class CustomValidatorAsync implements ValidatorConstraintInterface {

  constructor(protected service: StubService) {
  }

  public validate(value: any): Promise<boolean> {

    return this.service.isTruthyPromise(value);
  }

}

class Ship extends BaseModel {
  @Primary()
  public shipId: number;

  @MinLength(2)
  public name: string;

  public idRandom: string;
  public idSeeded: string;

  @Validate(CustomValidator)
  public truthyValue: any;

  @Validate(CustomValidatorAsync)
  public truthyValueAsync: any;

}

abstract class ShipStore extends BaseStore<Ship> {

}

@Injectable()
class ShipMockStore extends MockStore<Ship> {

  constructor(injector: Injector) {
    super(Ship, injector);
  }

  protected getMock(id?: identifier): Ship {
    return new this.modelStatic({
      shipId: id || this.chance()
        .integer(),
      name: 'Razorback',
      idRandom: this.chance()
        .guid(),
      idSeeded: this.chance(1)
        .guid(),
      truthyValue: true,
      truthyValueAsync: true,
    });
  }

}

@Injectable()
class TestClass {

  constructor(public shipStore: ShipStore) {

  }

}

//we need to have a singleton instance here to spy on, as the validator creates new singletons
// of the custom validator
const stubServiceSingleton = new StubService();

const providers:any[] = [
  TestClass,
  CustomValidator,
  CustomValidatorAsync,
  {provide: StubService, deps: [], useFactory: () => stubServiceSingleton},
  {provide: ShipStore, useClass: ShipMockStore},
];

describe('Mock Store', () => {

  beforeEachProviders(() => providers);

  it('is injected with the store instance token', inject([TestClass], (c: TestClass) => {

    expect(c instanceof TestClass)
      .toBe(true);
    expect(c.shipStore instanceof BaseStore)
      .toBe(true);
    expect(c.shipStore instanceof ShipMockStore)
      .toBe(true);

  }));

  it('is provides a dummy initialized method', inject([TestClass], (c: TestClass) => {

    c.shipStore.initialized().then((instance) => {
      expect(instance)
        .toEqual(c.shipStore);
    });

  }));

  it('retrieves a promise of a mock entity', inject([TestClass], (c: TestClass) => {

    return c.shipStore.findOne(1234)
      .then((entity) => {

        expect(entity instanceof Ship)
{'fraction_non_alphanumeric': 0.12732014112593956, 'fraction_numerical': 0.0018407731247123793, 'mean_word_length': 4.0192455735180905, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '4530932', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1937, 'n_words': 501}