khalby786 / jsoning

✨ A simple key-value JSON-based lightweight database. ✨
http://jsoning.js.org/
MIT License
91 stars 10 forks source link

`has` test doesn't exercise existing key case #25

Closed wh0 closed 3 years ago

wh0 commented 3 years ago

Describe the bug

the test Jsoning#has - existing element checks for key bar, but an earlier test of clear has deleted it. and the test checks for false instead of true

Reproducible code sample (if applicable)

n/a

Expected behavior

the test exercises a case where the key exists

Further details

Additional context

n/a

khalby786 commented 3 years ago

I'm planning to switch to mocha from ava for testing because ava runs tests concurrently whereas mocha executes tests serially which will provide more accurate results when it comes to read/write of JSON files! The tests seem to fail randomly, especially where the presence of a pre-existing JSON file positively affects the number of test passed. Maybe I'm not writing tests properly?

khalby786 commented 3 years ago

I've managed to fix this and successfully pass all the tests! Since ava runs tests concurrently, I initialised a new JSON file for each test case, but hey, it works! Although I'm pretty sure there's a better way to do this...

const test = require("ava");

const jsoning = require("../src/jsoning.js");

test("Jsoning#set", async (t) => {
  const db = new jsoning("./tests/test1.test.json");
  t.is(await db.set("foo", "bar"), true);
});

test("Jsoning#clear", async (t) => {
  const db = new jsoning("./tests/test2.test.json");
  await db.set("foo", "bar");
  t.truthy(await db.clear());
});

test("Jsoning#push - new element", async (t) => {
  const db = new jsoning("./tests/test3.test.json");
  t.is(await db.push("bar", "bar"), true);
});

test("Jsoning#all", async (t) => {
  const db = new jsoning("./tests/test4.test.json");
  await db.push("bar", "bar");
  t.truthy(await db.all());
});

test("Jsoning#push - already existing element", async (t) => {
  const db = new jsoning("./tests/test5.test.json");
  await db.push("bar", "bar");
  t.is(await db.push("bar", "foo"), true);
});

test("Jsoning#get", async (t) => {
  const db = new jsoning("./tests/test6.test.json");
  await db.push("bar", "bar");
  t.deepEqual(await db.get("bar"), ["bar"]);
});

test("Jsoning#math", async (t) => {
  const db = new jsoning("./tests/test7.test.json");
  await db.set("number", 300);
  t.is(await db.math("number", "add", 300), true);
});

test("Jsoning#has - deliberate false", async (t) => {
  const db = new jsoning("./tests/test8.test.json");
  t.is(await db.has("khaleel"), false);
});

test("Jsoning#has - existing element", async (t) => {
  const db = new jsoning("./tests/test9.test.json");
  // await db.clear();
  await db.push("bar", "pog");
  t.is(await db.has("bar"), true);
});
javaarchive commented 3 years ago

According to docs you need to use the serial function to write tests to not make them run concurrently but parallel tests in this case are faster (assuming you're not running this on something like the microsd card of a raspberry pi). https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md#running-tests-serially

khalby786 commented 3 years ago

This is great, and yes, I'm running this on my Samsung fridge.

wh0 commented 3 years ago

👍