Skip to content

Commit

Permalink
solved test 1
Browse files Browse the repository at this point in the history
  • Loading branch information
PayalKumari10 committed Apr 25, 2024
1 parent 16930bd commit 17ae547
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 294 deletions.
3 changes: 1 addition & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node

const readline = require("readline");
const { executeSELECTQuery, executeINSERTQuery,executeDELETEQuery,
} = require("./index");
const { executeSELECTQuery, executeINSERTQuery,executeDELETEQuery,} = require("./index");

const rl = readline.createInterface({
input: process.stdin,
Expand Down
2 changes: 1 addition & 1 deletion src/csvReader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const fs = require("fs");
const csv = require("csv-parser");
const { parse } = require("json2csv");
Expand All @@ -19,6 +18,7 @@ async function readCSV(filePath) {
});
});
}

async function writeCSV(filename, data) {
try {
const csvData = parse(data);
Expand Down
2 changes: 0 additions & 2 deletions src/queryExecutor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const { parseSelectQuery,parseInsertQuery, parseDeleteQuery } = require('./query
const { readCSV, writeCSV }= require('./csvReader');




// Helper functions for different JOIN types
function performInnerJoin(data, joinData, joinCondition, fields, table) {
return data.flatMap(mainRow => {
Expand Down
2 changes: 0 additions & 2 deletions src/queryParser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function parseSelectQuery(query) {
try {

// Trim the query to remove any leading/trailing whitespaces
query = query.trim();

Expand Down Expand Up @@ -96,7 +95,6 @@ function checkAggregateWithoutGroupBy(query, groupByFields) {
return aggregateFunctionRegex.test(query) && !groupByFields;
}


function parseJoinClause(query) {
const joinRegex = /\s(INNER|LEFT|RIGHT) JOIN\s(.+?)\sON\s([\w.]+)\s*=\s*([\w.]+)/i;
const joinMatch = query.match(joinRegex);
Expand Down
2 changes: 1 addition & 1 deletion tests/step-01/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test('Basic Jest Test', () => {
test("Basic Jest Test", () => {
expect(1).toBe(1);
});
5 changes: 3 additions & 2 deletions tests/step-02/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { readCSV } = require('../../src/csvReader');
const { readCSV } = require("../../src/csvReader");

test("Read CSV File", async () => {
test(
"Read CSV File", async () => {
const data = await readCSV("./student.csv");
expect(data.length).toBeGreaterThan(0);
expect(data.length).toBe(4);
Expand Down
8 changes: 5 additions & 3 deletions tests/step-03/index.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const { readCSV } = require("../../src/csvReader");
const { parseJoinClause, parseSelectQuery } = require('../../src/queryParser');
const { parseJoinClause, parseSelectQuery } = require("../../src/queryParser");

test("Read CSV File", async () => {
test(
"Read CSV File", async () => {
const data = await readCSV("./student.csv");
expect(data.length).toBeGreaterThan(0);
expect(data.length).toBe(4);
expect(data[0].name).toBe("John");
expect(data[0].age).toBe("30"); //ignore the string type here, we will fix this later
});

test("Parse SQL Query", () => {
test(
"Parse SQL Query", () => {
const query = "SELECT id, name FROM student";
const parsed = parseSelectQuery(query);
expect(parsed).toEqual({
Expand Down
9 changes: 6 additions & 3 deletions tests/step-04/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ const { parseSelectQuery, parseJoinClause } = require("../../src/queryParser");
const { executeSELECTQuery } = require("../../src/queryExecutor");


test("Read CSV File", async () => {
test(
"Read CSV File", async () => {
const data = await readCSV("./student.csv");
expect(data.length).toBeGreaterThan(0);
expect(data.length).toBe(4);
expect(data[0].name).toBe("John");
expect(data[0].age).toBe("30"); //ignore the string type here, we will fix this later
});

test("Parse SQL Query", () => {
test(
"Parse SQL Query", () => {
const query = "SELECT id, name FROM student";
const parsed = parseSelectQuery(query);
expect(parsed).toEqual({
Expand All @@ -29,7 +31,8 @@ test("Read CSV File", async () => {
});
});

test("Execute SELECT Query", async () => {
test(
"Execute SELECT Query", async () => {
const query = "SELECT id, name FROM student";
const result = await executeSELECTQuery(query);
expect(result.length).toBeGreaterThan(0);
Expand Down
57 changes: 31 additions & 26 deletions tests/step-05/index.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const {readCSV} = require('../../src/csvReader');
const {executeSELECTQuery } = require('../../src/queryExecutor');
const { parseJoinClause, parseSelectQuery } = require('../../src/queryParser');
const {readCSV} = require("../../src/csvReader");
const {executeSELECTQuery } = require("../../src/queryExecutor");
const { parseJoinClause, parseSelectQuery } = require("../../src/queryParser");

test('Read CSV File', async () => {
const data = await readCSV('./student.csv');
test(
"Read CSV File", async () => {
const data = await readCSV("./student.csv");
expect(data.length).toBeGreaterThan(0);
expect(data.length).toBe(4);
expect(data[0].name).toBe('John');
expect(data[0].age).toBe('30'); //ignore the string type here, we will fix this later
expect(data[0].name).toBe("John");
expect(data[0].age).toBe("30"); //ignore the string type here, we will fix this later
});

test('Parse SQL Query', () => {
const query = 'SELECT id, name FROM student';
test(
"Parse SQL Query", () => {
const query = "SELECT id, name FROM student";
const parsed = parseSelectQuery(query);
expect(parsed).toEqual({
fields: ['id', 'name'],
table: 'student',
fields: ["id", "name"],
table: "student",
whereClauses: [],
joinCondition: null,
joinTable: null,
Expand All @@ -28,21 +30,23 @@ test('Parse SQL Query', () => {
});
});

test('Execute SQL Query', async () => {
const query = 'SELECT id, name FROM student';
test(
"Execute SQL Query", async () => {
const query = "SELECT id, name FROM student";
const result = await executeSELECTQuery(query);
expect(result.length).toBeGreaterThan(0);
expect(result[0]).toHaveProperty('id');
expect(result[0]).toHaveProperty('name');
expect(result[0]).not.toHaveProperty('age');
expect(result[0]).toEqual({ id: '1', name: 'John' });
expect(result[0]).toHaveProperty("id");
expect(result[0]).toHaveProperty("name");
expect(result[0]).not.toHaveProperty("age");
expect(result[0]).toEqual({ id: "1", name: "John" });
});
test('Parse SQL Query with WHERE Clause', () => {
const query = 'SELECT id, name FROM student WHERE age = 25';
test(
"Parse SQL Query with WHERE Clause", () => {
const query = "SELECT id, name FROM student WHERE age = 25";
const parsed = parseSelectQuery(query);
expect(parsed).toEqual({
fields: ['id', 'name'],
table: 'student',
fields: ["id", "name"],
table: "student",
whereClauses: [{
"field": "age",
"operator": "=",
Expand All @@ -58,11 +62,12 @@ test('Parse SQL Query with WHERE Clause', () => {
isDistinct: false
});
});
test('Execute SQL Query with WHERE Clause', async () => {
const query = 'SELECT id, name FROM student WHERE age = 25';
test(
"Execute SQL Query with WHERE Clause", async () => {
const query = "SELECT id, name FROM student WHERE age = 25";
const result = await executeSELECTQuery(query);
expect(result.length).toBe(1);
expect(result[0]).toHaveProperty('id');
expect(result[0]).toHaveProperty('name');
expect(result[0].id).toBe('2');
expect(result[0]).toHaveProperty("id");
expect(result[0]).toHaveProperty("name");
expect(result[0].id).toBe("2");
});
24 changes: 16 additions & 8 deletions tests/step-06/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ const { readCSV } = require("../../src/csvReader");
const { parseSelectQuery, parseJoinClause } = require("../../src/queryParser");
const { executeSELECTQuery } = require("../../src/queryExecutor");

test("Read CSV File", async () => {
test(
"Read CSV File", async () => {
const data = await readCSV("./student.csv");
expect(data.length).toBeGreaterThan(0);
expect(data.length).toBe(4);
expect(data[0].name).toBe("John");
expect(data[0].age).toBe("30"); //ignore the string type here, we will fix this later
});

test("Parse SQL Query", () => {
test(
"Parse SQL Query", () => {
const query = "SELECT id, name FROM student";
const parsed = parseSelectQuery(query);
expect(parsed).toEqual({
Expand All @@ -28,7 +30,8 @@ test("Parse SQL Query", () => {
});
});

test("Execute SQL Query", async () => {
test(
"Execute SQL Query", async () => {
const query = "SELECT id, name FROM student";
const result = await executeSELECTQuery(query);
expect(result.length).toBeGreaterThan(0);
Expand All @@ -38,7 +41,8 @@ test("Execute SQL Query", async () => {
expect(result[0]).toEqual({ id: "1", name: "John" });
});

test("Parse SQL Query with WHERE Clause", () => {
test(
"Parse SQL Query with WHERE Clause", () => {
const query = "SELECT id, name FROM student WHERE age = 25";
const parsed = parseSelectQuery(query);
expect(parsed).toEqual({
Expand All @@ -62,7 +66,8 @@ test("Parse SQL Query with WHERE Clause", () => {
});
});

test("Execute SQL Query with WHERE Clause", async () => {
test(
"Execute SQL Query with WHERE Clause", async () => {
const query = "SELECT id, name FROM student WHERE age = 25";
const result = await executeSELECTQuery(query);
expect(result.length).toBe(1);
Expand All @@ -71,7 +76,8 @@ test("Execute SQL Query with WHERE Clause", async () => {
expect(result[0].id).toBe("2");
});

test("Parse SQL Query with Multiple WHERE Clauses", () => {
test(
"Parse SQL Query with Multiple WHERE Clauses", () => {
const query = "SELECT id, name FROM student WHERE age = 30 AND name = John";
const parsed = parseSelectQuery(query);
expect(parsed).toEqual({
Expand Down Expand Up @@ -100,14 +106,16 @@ test("Parse SQL Query with Multiple WHERE Clauses", () => {
});
});

test("Execute SQL Query with Multiple WHERE Clause", async () => {
test(
"Execute SQL Query with Multiple WHERE Clause", async () => {
const query = "SELECT id, name FROM student WHERE age = 30 AND name = John";
const result = await executeSELECTQuery(query);
expect(result.length).toBe(1);
expect(result[0]).toEqual({ id: "1", name: "John" });
});

test("Invalid Where Clause", async () => {
test(
"Invalid Where Clause", async () => {
const query = "SELECT id, name FROM student WHERE age 30 AND name = John";
expect(() => {
parseSelectQuery(query);
Expand Down
Loading

0 comments on commit 17ae547

Please sign in to comment.