<commit_before>import {QueryBuilder} from '../../src/QueryBuilder';
import {Query} from '../../src/Query';
import {Wetland} from '../../src/Wetland';
import {queries} from '../resource/queries';
import {Todo} from '../resource/entity/todo/Todo';
import {List} from '../resource/entity/todo/List';
import {assert} from 'chai';

let wetland = new Wetland({
  stores  : {
    defaultStore: {
      client    : 'mysql',
      connection: {
        user    : 'root',
        host    : '127.0.0.1',
        database: 'wetland_test'
      }
    }
  },
  entities: [Todo, List]
});

function getQueryBuilder() {
  return wetland.getManager().getRepository(Todo).getQueryBuilder('t');
}

describe('QueryBuilder', () => {
  describe('.createAlias()', () => {
    it('should create an alias', () => {
      let alias = getQueryBuilder().createAlias('t');

      assert.strictEqual(alias, 't0');
    });
  });

  describe('.makeJoin()', () => {
    it('should create a select query with a join clause by specifying the type of join manually', () => {
      let queryBuilder = getQueryBuilder();
      let query        = queryBuilder
        .select('task')
        .makeJoin('innerJoin', 'list', 'l')
        .getQuery()
        .getSQL();

      assert.strictEqual(query, queries.queryBuilder.join);
    });
  });

  describe('.leftJoin()', () => {
    it('should create a query with a left join clause', () => {
      let queryBuilder = getQueryBuilder();
      let query        = queryBuilder
        .select('t')
        .leftJoin('list', 'l')
        .getQuery()<commit_msg>test(QueryBuilder): add test for relation validation<commit_after>import {QueryBuilder} from '../../src/QueryBuilder';
import {Query} from '../../src/Query';
import {Wetland} from '../../src/Wetland';
import {queries} from '../resource/queries';
import {Todo} from '../resource/entity/todo/Todo';
import {List} from '../resource/entity/todo/List';
import {assert} from 'chai';

let wetland = new Wetland({
  stores  : {
    defaultStore: {
      client    : 'mysql',
      connection: {
        user    : 'root',
        host    : '127.0.0.1',
        database: 'wetland_test'
      }
    }
  },
  entities: [Todo, List]
});

function getQueryBuilder() {
  return wetland.getManager().getRepository(Todo).getQueryBuilder('t');
}

describe('QueryBuilder', () => {
  describe('.createAlias()', () => {
    it('should create an alias', () => {
      let alias = getQueryBuilder().createAlias('t');

      assert.strictEqual(alias, 't0');
    });
  });

  describe('.makeJoin()', () => {
    it('should create a select query with a join clause by specifying the type of join manually', () => {
      let queryBuilder = getQueryBuilder();
      let query        = queryBuilder
        .select('task')
        .makeJoin('innerJoin', 'list', 'l')
        .getQuery()
        .getSQL();

      assert.strictEqual(query, queries.queryBuilder.join);
    });

    it('should throw an error on invalid join property', () => {
      let queryBuilder  = getQueryBuilder();
      let expectedError = 'Invalid relation supplied for join. Property not found on entity, or relation not defined. '
        + 'Are you registering the joins in the wrong order?';

      assert.throws(() => {
        queryBuilder
          .select('task')
          .makeJoin('innerJoin', 'death', 'toJoins');
      }, expectedError);
    });
  });

  describe('.leftJoin()', () => {
    it('should create a query with a left join clause', () => {
      let queryBuilder = getQueryBuilder();
      let query        = queryBuilder
        .select('t')
        .leftJoin('list', 'l')
        .getQuery()
{'fraction_non_alphanumeric': 0.17300275482093663, 'fraction_numerical': 0.003856749311294766, 'mean_word_length': 2.645582329317269, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5192522', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1067, 'n_words': 271}