Skip to content

Commit

Permalink
update test files, home page and config files
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyoseni committed Apr 3, 2017
1 parent bf430ca commit 8142580
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 1,309 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ engines:
ratings:
paths:
- "src/js/app.js"
- "src/inverted-index.js"
- "src/InvertedIndex.js"
- "jasmine/specs/*"
languages:
JavaScript: true
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gulp.task('browserSync', () => {
});

gulp.task('scripts', () => {
gulp.src('jasmine/spec/invertedIndex-spec.js')
gulp.src('jasmine/spec/InvertedIndexSpec.js')
.pipe(browserify())
.pipe(rename('bundle.js'))
.pipe(gulp.dest('jasmine/testfiles'));
Expand Down
2 changes: 1 addition & 1 deletion jasmine/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<script src="lib/jasmine-2.5.2/jasmine-html.js"></script>
<script src="lib/jasmine-2.5.2/boot.js"></script>
<!-- include source files here... -->
<script src="../src/inverted-index.js"></script>
<script src="../src/InvertedIndex.js"></script>
<!-- include spec files here... -->
<script src="testfiles/bundle.js"></script>
</head>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const values = require('object.values'); // shim Object.values
// book with valid contents
const book = require('./allBooks/books.json');
const secondBook = require('./allBooks/newBook.json');
const myBook = require('./allBooks/myBook.json');
// book with invalid content
const invalidFile = require('./allBooks/invalid.json');
// book with valid content
const secondBook = require('./allBooks/newBook.json');
// empty book
const empty = require('./allBooks/empty.json');
// an array
Expand All @@ -14,27 +13,31 @@ const emptyFile = require('./allBooks/emptyArray.json');
// an invalid JSON file
const notValid = require('./allBooks/notArrayOfArray.json');

describe('invertedIndex Index', () => {
beforeAll(() => {
this.invertedIndex = new InvertedIndex();
const meek = new InvertedIndex();

describe('Meek Inverted Index', () => {
beforeAll(() => {
meek.createIndex('book.json', book);
meek.createIndex('secondBook.json', secondBook);
});

describe('Constructor', () => {

describe('The InvertedIndex class', () => {
it('should be defined', () => {
expect(InvertedIndex).toBeDefined();
});

it('should have defined instance', () => {
expect(meek).toBeDefined();
});

it('can create instances of inverted index class', () => {
expect(typeof this.invertedIndex)
.toEqual('object');
expect(this.invertedIndex instanceof InvertedIndex)
.toBe(true);
expect(meek instanceof InvertedIndex).toBeTruthy();
});
});

const invertedIndex = new InvertedIndex();
invertedIndex.createIndex('book.json', book);
invertedIndex.createIndex('secondBook.json', secondBook);

describe('InvertedIndex class, check all methods', () => {
describe('The InvertedIndex class', () => {
it('should check that the class has a createIndex method', () => {
expect(typeof invertedIndex.createIndex).toBe('function');
expect(typeof meek.createIndex).toBe('function');
});

it('should check that the class has a validateFile method', () => {
Expand All @@ -46,28 +49,19 @@ describe('invertedIndex Index', () => {
});

it('should check that the class has a getIndex method', () => {
expect(typeof invertedIndex.getIndex).toBe('function');
expect(typeof meek.getIndex).toBe('function');
});

it('should check that the class has a searchIndex method', () => {
expect(typeof invertedIndex.searchIndex).toBe('function');
expect(typeof meek.searchIndex).toBe('function');
});

it('should check that the class has a setIndex method', () => {
expect(typeof invertedIndex.setIndex).toBe('function');
expect(typeof meek.setIndex).toBe('function');
});
});

describe('validateFile should check files', () => {
it('should check that the contents of the file to be uploaded is valid',
() => {
expect(InvertedIndex.validateFile(book)).toBeTruthy();
});

it('should return false for empty json files', () => {
expect(InvertedIndex.validateFile(empty)).toBeFalsy();
});

describe('The validateFile method', () => {
it('should return true if file has property "title" and "text" ', () => {
expect(InvertedIndex.validateFile(book)).toBeTruthy();
});
Expand All @@ -76,6 +70,14 @@ describe('invertedIndex Index', () => {
() => {
expect(InvertedIndex.validateFile(invalidFile)).toBeFalsy();
});
it('should check that the contents of the file to be uploaded is valid',
() => {
expect(InvertedIndex.validateFile(book)).toBeTruthy();
});

it('should return false for empty json files', () => {
expect(InvertedIndex.validateFile(empty)).toBeFalsy();
});

it('should return false if file is not an array of JSON object',
() => {
Expand All @@ -94,30 +96,41 @@ describe('invertedIndex Index', () => {
});
});

describe('Create Index', () => {
describe('The createIndex method', () => {
it('should return a response if index is created', () => {
const response = 'Index created';
expect(meek.createIndex('myBook.json', myBook)).toEqual(response);
});

it('should return a msg if index is not created', () => {
const msg = 'Index not created';
expect(invertedIndex.createIndex(notValid)).toEqual(msg);
expect(meek.createIndex('notValid.json', notValid)).toEqual(msg);
});
});

describe('Tokenize words', () => {
describe('The tokenize method', () => {
it('should check that tokens are split and in sorted order', () => {
let words = 'Hello Dear how are YOU';
const expectedTokens = ['are', 'dear', 'hello', 'how', 'you'];
words = InvertedIndex.tokenize(words);
expect(expectedTokens).toEqual(words);
});

it('should strip out special characters', () => {
let words = "I love Programming! @ gmail ) Mercy's * &^%$#";
const expectedTokens = ['gmail', 'i', 'love', 'mercy\'s', 'programming'];
words = InvertedIndex.tokenize(words);
expect(expectedTokens).toEqual(words);
});
});

describe('Generate Index', () => {
it('should verify that index has been created', () => {
expect(Object.keys(invertedIndex.getIndex('book.json')).length)
.toBeGreaterThan(0);
describe('The getIndex method', () => {
it('should return an object', () => {
expect(typeof meek.getIndex('book.json')).toEqual('object');
});

it('should check that index maps the string to the correct objects in json'
+ ' array', () => {
it(`should check that index maps the string to the correct objects in json
array`, () => {
const expectedIndex = {
and: [0, 1],
barbie: [1],
Expand All @@ -132,74 +145,58 @@ describe('invertedIndex Index', () => {
love: [0, 1],
you: [0]
};
let result = {};
result = invertedIndex.getIndex('secondBook.json');
expect(Object.keys(result)).toEqual(Object.keys(expectedIndex));
expect(values(result)).toEqual(values(expectedIndex));
const result = meek.getIndex('secondBook.json');
expect(result).toEqual(expectedIndex);
});
});

describe('Search index', () => {
it('should return true if search term is a string', () => {
const words = 'I love Barbie and Alice';
expect(Object.keys(invertedIndex.searchIndex(words, 'book.json')))
.toBeTruthy();
});

it('should return true if search term is a string', () => {
describe('The searchIndex method', () => {
it(`should search through indexed files and return expected
result for a search`, () => {
const texts = 'I love Barbie and Alice';
expect(Object.keys(invertedIndex.searchIndex(texts, 'secondBook.json')))
.toBeTruthy();
});

it('should return true if search term is a number', () => {
let number = 1234;
number = number.toString();
expect(Object.keys(invertedIndex.searchIndex(number, 'secondBook.json')))
.toBeTruthy();
});

it('should search through single files that are indexed', () => {
const expectedResult = {
'secondBook.json':
{
barbie: [1],
and: [0, 1],
cindarella: [1],
dearie: [0]
barbie: [1],
i: [0, 1],
love: [0, 1]
}
};
let search = {};
search = invertedIndex.searchIndex('barbie, mercy and cindarella dearie',
'secondBook.json');
expect(Object.keys(search)).toEqual(Object.keys(expectedResult));
expect(values(expectedResult)).toEqual(values(expectedResult));
expect((meek.searchIndex(texts, 'secondBook.json')))
.toEqual(expectedResult);
});

it('should search through all files', () => {
const allFiles =
const result = {
'book.json':
{
'book.json':
{
alice: [0],
an: [1],
barbie: [2],
cartoons: [2],
of: [0, 1],
unusual: [1],
wizard: [1]
},
'secondBook.json':
{
barbie: [1]
}
};
let search = {};
search = invertedIndex.searchIndex('Barbie loves cartoons but she\'s' +
'scared of an unusual wizard, alice fall\'s',
alice: [0],
an: [1],
and: [0, 1],
barbie: [2],
cartoons: [2],
'fall\'s': [0],
of: [0, 1],
unusual: [1],
wizard: [1]
},
'secondBook.json':
{
and: [0, 1],
barbie: [1]
},
'myBook.json':
{
and: [0],
'mercy\'s': [0],
name: [0]
}
};
const search = meek.searchIndex(`Barbie loves cartoons but she's
scared of an unusual wizard, alice fall's and mercy's name`,
'All files');
expect(Object.keys(search)).toEqual(Object.keys(allFiles));
expect(values(allFiles)).toEqual(values(allFiles));
expect(search).toEqual(result);
});
});
});
6 changes: 6 additions & 0 deletions jasmine/spec/allBooks/myBook.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"title": "Mercy's Book",
"text": "My name is Mercy and Oseni"
}
]
Loading

0 comments on commit 8142580

Please sign in to comment.