<commit_before>// Copyright (c) Rotorz Limited. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root.


// Packages
import Rx from "rxjs";
import given from "mocha-testdata";
import should from "should";

// Project
import Environment from "../../src/Environment";
import PluginContext from "../../src/PluginContext";
import ResourceType from "../../src/ResourceType";
import generateResource from "../../src/behaviors/generateResource";

// Test
import FakeErrorGenerator from "../fakes/FakeErrorGenerator";
import FakeGenerator from "../fakes/FakeGenerator";


describe("behaviors/generateResource", function () {

  beforeEach(function () {
    this.env = new Environment();
    this.env.generators.set("standard", new FakeGenerator());
    this.env.generators.set("always-fails", new FakeErrorGenerator());
  });


  it("is a function", function () {
    generateResource
      .should.be.a.Function();
  });

  it("is named 'generateResource'", function () {
    generateResource.name
      .should.be.eql("generateResource");
  });


  it("throws error when argument 'env' is not a webreed environment", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType("foo");

    (() => generateResource(null, resource, resourceType))
      .should.throw("argument 'env' must be a webreed environment");
  });

  given( undefined, null, 42, "", { } ).
  it("throws error when argument 'resource' is not a `Resource`", function (resource) {
    let resourceType = new ResourceType("foo");
    (() => generateResource(this.env, resource, resourceType))
      .should.throw("argument 'resource' must be a `Resource`");
  });

  given( undefined, null, 42, "", { } ).
  it("throws error when argument 'resourceType' is not a `ResourceType`", function (resourceType) {
    let resource = this.env.createResource();
    (() => generateResource(this.env, resource, resourceType))
      .should.throw("argument 'resourceType' must be a `ResourceType`");
  });


  it("throws error when resource references an unknown generator", function () {
    let resource = this.env.createResource({ _generator: "generator-that-does-not-exist" });
    let resourceType = new ResourceType("foo");

    (() => generateResource(this.env, resource, resourceType))
      .should.throw("Generator 'generator-that-does-not-exist' is not defined.");
  });

  it("throws error when resource type references an unknown generator", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType("foo");
    resourceType.generator = new PluginContext("generator-that-does-not-exist");

    (() => generateResource(this.env, resource, resourceType))
      .should.throw("Generator 'generator-that-does-not-exist' is not defined.");
  });


  it("returns an observable stream of output resources", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType("foo");

    generateResource(this.env, resource, resourceType)
      .should.be.instanceOf(Rx.Observable);
  });

  it("produces an output `Resource`", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType("foo");

    return generateResource(this.env, resource, resourceType)
      .reduce((a, outputResource) => a || outputResource.wentThroughFakeGenerator, false)
      .toPromise()
      .should.eventually.be.true();
  });

  it("rejects with error from generator", function () {
    let resource = this.env.createResource({ _generator: "always-fails" });
    let resourceType = new ResourceType("foo");

    return generateResource(this.env, resource, resourceType)
      .toPromise()
      .should.be.rejectedWith("generate failed!");
  });


  it("provides correct arguments to the generator's 'generate' function", function () {
    let fakeGenerator = this.env.generators.get("standard");

    let resource = this.env.createResource({ inputProperty: 42 });
    let resourceType = new ResourceType("foo");

    return generateResource(this.env, resource, resourceType)
      .toPromise()
      .then(() => {
        fakeGenerator.lastGenerateArguments[0]
          .should.be.exactly(this.env);

        fakeGenerator.lastGenerateArguments[1].inputProperty
          .should.be.eql(42);

        fakeGenerator.lastGenerateArguments[2].generator
          .should.have.properties({
            name: "standard",
            options: { }
          });
        fakeGenerator.lastGenerateArguments[2].resourceType
          .should.be.exactly(resourceType);
      });
  });

  it("includes generator instance options in argument 'context' that is provided to 'generate' function", function () {
    let fakeGenerator = this.env.generators.get("standard");
    let instanceOptions = { key: "value" };

    let resource = this.env.createResource({ inputProperty: 42 });
    let resourceType = new ResourceType("foo");
    resourceType.generator = new PluginContext("standard", instanceOptions);

    return generateResource(this.env, resource, resourceType)
      .toPromise()<commit_msg>Remove invalid from construction of fake `ResourceType` instances.<commit_after>// Copyright (c) Rotorz Limited. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root.


// Packages
import Rx from "rxjs";
import given from "mocha-testdata";
import should from "should";

// Project
import Environment from "../../src/Environment";
import PluginContext from "../../src/PluginContext";
import ResourceType from "../../src/ResourceType";
import generateResource from "../../src/behaviors/generateResource";

// Test
import FakeErrorGenerator from "../fakes/FakeErrorGenerator";
import FakeGenerator from "../fakes/FakeGenerator";


describe("behaviors/generateResource", function () {

  beforeEach(function () {
    this.env = new Environment();
    this.env.generators.set("standard", new FakeGenerator());
    this.env.generators.set("always-fails", new FakeErrorGenerator());
  });


  it("is a function", function () {
    generateResource
      .should.be.a.Function();
  });

  it("is named 'generateResource'", function () {
    generateResource.name
      .should.be.eql("generateResource");
  });


  it("throws error when argument 'env' is not a webreed environment", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType();

    (() => generateResource(null, resource, resourceType))
      .should.throw("argument 'env' must be a webreed environment");
  });

  given( undefined, null, 42, "", { } ).
  it("throws error when argument 'resource' is not a `Resource`", function (resource) {
    let resourceType = new ResourceType();
    (() => generateResource(this.env, resource, resourceType))
      .should.throw("argument 'resource' must be a `Resource`");
  });

  given( undefined, null, 42, "", { } ).
  it("throws error when argument 'resourceType' is not a `ResourceType`", function (resourceType) {
    let resource = this.env.createResource();
    (() => generateResource(this.env, resource, resourceType))
      .should.throw("argument 'resourceType' must be a `ResourceType`");
  });


  it("throws error when resource references an unknown generator", function () {
    let resource = this.env.createResource({ _generator: "generator-that-does-not-exist" });
    let resourceType = new ResourceType();

    (() => generateResource(this.env, resource, resourceType))
      .should.throw("Generator 'generator-that-does-not-exist' is not defined.");
  });

  it("throws error when resource type references an unknown generator", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType();
    resourceType.generator = new PluginContext("generator-that-does-not-exist");

    (() => generateResource(this.env, resource, resourceType))
      .should.throw("Generator 'generator-that-does-not-exist' is not defined.");
  });


  it("returns an observable stream of output resources", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType();

    generateResource(this.env, resource, resourceType)
      .should.be.instanceOf(Rx.Observable);
  });

  it("produces an output `Resource`", function () {
    let resource = this.env.createResource();
    let resourceType = new ResourceType();

    return generateResource(this.env, resource, resourceType)
      .reduce((a, outputResource) => a || outputResource.wentThroughFakeGenerator, false)
      .toPromise()
      .should.eventually.be.true();
  });

  it("rejects with error from generator", function () {
    let resource = this.env.createResource({ _generator: "always-fails" });
    let resourceType = new ResourceType();

    return generateResource(this.env, resource, resourceType)
      .toPromise()
      .should.be.rejectedWith("generate failed!");
  });


  it("provides correct arguments to the generator's 'generate' function", function () {
    let fakeGenerator = this.env.generators.get("standard");

    let resource = this.env.createResource({ inputProperty: 42 });
    let resourceType = new ResourceType();

    return generateResource(this.env, resource, resourceType)
      .toPromise()
      .then(() => {
        fakeGenerator.lastGenerateArguments[0]
          .should.be.exactly(this.env);

        fakeGenerator.lastGenerateArguments[1].inputProperty
          .should.be.eql(42);

        fakeGenerator.lastGenerateArguments[2].generator
          .should.have.properties({
            name: "standard",
            options: { }
          });
        fakeGenerator.lastGenerateArguments[2].resourceType
          .should.be.exactly(resourceType);
      });
  });

  it("includes generator instance options in argument 'context' that is provided to 'generate' function", function () {
    let fakeGenerator = this.env.generators.get("standard");
    let instanceOptions = { key: "value" };

    let resource = this.env.createResource({ inputProperty: 42 });
    let resourceType = new ResourceType();
    resourceType.generator = new PluginContext("standard", instanceOptions);

    return generateResource(this.env, resource, resourceType)
      .toPromise()
{'fraction_non_alphanumeric': 0.13899726882559502, 'fraction_numerical': 0.0027311744049941474, 'mean_word_length': 3.7227084292952557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4209599', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2678, 'n_words': 728}