<commit_before>const should = require("should");
const entityFactoryProvider = require("./entity");
const JSONSerializer = require("../middleware/JSONSerializer");
const { mockFileAdapter, exampleFileStructure } = require("../../test/mockFileAdapter");

describe("Entities", () => {
  const createEntityWithNewMock = () => {
    const resourceFactoryProviderStub = (serializer, entityFactoryProvider) => {
      return {
        create: (fsAdapter) => {
          return {
            directoryName: fsAdapter.directoryName,
            serializer: serializer,
            entityProvider: entityFactoryProvider
          };
        }
      };
    };
    const fileStructure = exampleFileStructure();
    const mockProvider = mockFileAdapter(fileStructure);
    const entityFactory = entityFactoryProvider(JSONSerializer, resourceFactoryProviderStub);
    const newEntity = entityFactory.create(mockProvider);
    newEntity.fileStructure = fileStructure;
    return newEntity;
  };

  function validatesResourceName(promiseGenerator) {
    it("should reject incorrect resource name", () => {
      const invalidValue = "something/someone";
      return promiseGenerator(invalidValue).should.be.rejectedWith(`Invalid identifier: ${invalidValue}`);
    });
  }

  describe("Loading content", () => {
    const entityProvider = createEntityWithNewMock();

    it("should load entity's content", () => {
      return entityProvider.load()
        .then((entity) => {
          should(entity).not.be.undefined();
          entity.content.should.equal("main entity");
          entity.id.should.equal(1);
        });
    });
  });

  describe("Saving content", () => {

    it("should save content correctly", () => {
      const entityProvider = createEntityWithNewMock();
      const value = "<PASSWORD>";
      const key = "something";
      return entityProvider.load()
        .then((entity) => {
          entity[key] = value;
          entityProvider.save(entity);
        })
        .then(() => {
          const encodedFile = entityProvider.fileStructure.files["entity.json"];
          const file = JSON.parse(encodedFile);
          file[key].should.equal(value);
        });
    });

    it("should not save empty content", () => {
      const entityProvider = createEntityWithNewMock();
      return entityProvider.load()
        .then(() => entityProvider.save())
        .should.be.rejectedWith("Cannot save empty content");
    });

  });

  describe("Listing resources", () => {
    const mock = createEntityWithNewMock();

    it("should return sub-resources", () => {
      return mock.listResources()
        .then((subResources) => {
            should(subResources).not.be.undefined();
            subResources.should.containEql("subresource1");
            subResources.should.containEql("subresource2");
          }
        );
    });
  });

  describe("Getting resources", () => {
    const entity = createEntityWithNewMock();

    it("should load resource correctly", () => {
      return entity.getResource("subresource1")
        .then((resource) => {
          should(resource).not.be.undefined();
          should(resource.name).equal("subresource1");
          resource.serializer.should.equal(JSONSerializer);
        });
    });

    it("should fail on non existing sub resource", () => {
      return entity.getResource("subresource2324524")
        .should.be.rejectedWith("Directory does not exist: subresource2324524");
    });
  });<commit_msg>Renamed stuff in the entity file<commit_after>const should = require("should");
const entityFactoryProvider = require("./entity");
const JSONSerializer = require("../middleware/JSONSerializer");
const { mockFileAdapter, exampleFileStructure } = require("../../test/mockFileAdapter");

describe("Entities", () => {
  const createEntityWithNewMock = () => {
    const resourceFactoryProviderStub = (serializer, entityFactoryProvider) => {
      return {
        create: (fsAdapter) => {
          return {
            directoryName: fsAdapter.directoryName,
            serializer: serializer,
            entityFactoryProvider: entityFactoryProvider
          };
        }
      };
    };
    const fileStructure = exampleFileStructure();
    const mockProvider = mockFileAdapter(fileStructure);
    const entityFactory = entityFactoryProvider(JSONSerializer, resourceFactoryProviderStub);
    const newEntity = entityFactory.create(mockProvider);
    newEntity.fileStructure = fileStructure;
    return newEntity;
  };

  function validatesResourceName(promiseGenerator) {
    it("should reject incorrect resource name", () => {
      const invalidValue = "something/someone";
      return promiseGenerator(invalidValue).should.be.rejectedWith(`Invalid identifier: ${invalidValue}`);
    });
  }

  describe("Loading content", () => {
    const entity = createEntityWithNewMock();

    it("should load entity's content", () => {
      return entity.load()
        .then((entity) => {
          should(entity).not.be.undefined();
          entity.content.should.equal("main entity");
          entity.id.should.equal(1);
        });
    });
  });

  describe("Saving content", () => {

    it("should save content correctly", () => {
      const entityProvider = createEntityWithNewMock();
      const value = "rtjkwgelfbmvdlksmfww";
      const key = "something";
      return entityProvider.load()
        .then((entity) => {
          entity[key] = value;
          entityProvider.save(entity);
        })
        .then(() => {
          const encodedFile = entityProvider.fileStructure.files["entity.json"];
          const file = JSON.parse(encodedFile);
          file[key].should.equal(value);
        });
    });

    it("should not save empty content", () => {
      const entityProvider = createEntityWithNewMock();
      return entityProvider.load()
        .then(() => entityProvider.save())
        .should.be.rejectedWith("Cannot save empty content");
    });

  });

  describe("Listing resources", () => {
    const mock = createEntityWithNewMock();

    it("should return sub-resources", () => {
      return mock.listResources()
        .then((subResources) => {
            should(subResources).not.be.undefined();
            subResources.should.containEql("subresource1");
            subResources.should.containEql("subresource2");
          }
        );
    });
  });

  describe("Getting resources", () => {
    const entity = createEntityWithNewMock();

    it("should load resource correctly", () => {
      return entity.getResource("subresource1")
        .then((resource) => {
          should(resource).not.be.undefined();
          should(resource.name).equal("subresource1");
          resource.serializer.should.equal(JSONSerializer);
          resource.entityFactoryProvider.should.equal(entityFactoryProvider);
        });
    });

    it("should fail on non existing sub resource", () => {
      return entity.getResource("subresource2324524")
        .should.be.rejectedWith("Directory does not exist: subresource2324524");
    });
  });
{'fraction_non_alphanumeric': 0.1398978433598184, 'fraction_numerical': 0.005391600454029512, 'mean_word_length': 2.9074279379157426, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 46, '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': '1650702', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1789, 'n_words': 336}