summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorPhilip Chimento <philip@endlessm.com>2013-10-23 21:58:49 -0700
committerPhilip Chimento <philip@endlessm.com>2013-10-23 22:00:01 -0700
commit758af67e4d3c48c530f11995351d5640231ed914 (patch)
tree6d231bddeba2928d9e6c16daa8f60871554ab991 /test
parent6dfd4f24b97b1d3fa730b5f5c2c87f8e2d8c4fa4 (diff)
wiki: Add tests for refactored models
For good code hygiene, add tests for the models. After the refactoring, they are less linked together, so easier to test. [endlessm/eos-sdk#367]
Diffstat (limited to 'test')
-rw-r--r--test/Makefile.am4
-rw-r--r--test/wikipedia/models/testArticleModel.js71
-rw-r--r--test/wikipedia/models/testCategoryModel.js76
-rw-r--r--test/wikipedia/models/testDomainWikiModel.js141
4 files changed, 292 insertions, 0 deletions
diff --git a/test/Makefile.am b/test/Makefile.am
index 60a4057..53ff6ff 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -35,6 +35,9 @@ javascript_tests = \
test/tools/eos-run-test/sanitycheck.js \
test/webhelper/testTranslate.js \
test/webhelper/testWebActions.js \
+ test/wikipedia/models/testCategoryModel.js \
+ test/wikipedia/models/testArticleModel.js \
+ test/wikipedia/models/testDomainWikiModel.js \
$(NULL)
EXTRA_DIST += $(javascript_tests)
@@ -46,6 +49,7 @@ TESTS = \
TEST_EXTENSIONS = .js
JS_LOG_COMPILER = tools/eos-run-test
AM_JS_LOG_FLAGS = \
+ --include-path=$(top_srcdir)/wikipedia \
--include-path=$(top_srcdir)/webhelper \
$(NULL)
LOG_COMPILER = gtester
diff --git a/test/wikipedia/models/testArticleModel.js b/test/wikipedia/models/testArticleModel.js
new file mode 100644
index 0000000..bea76dc
--- /dev/null
+++ b/test/wikipedia/models/testArticleModel.js
@@ -0,0 +1,71 @@
+const ArticleModel = imports.models.article_model;
+
+let mockJsonData = {
+ title: 'Article Title',
+ url: 'file:///',
+ categories: [
+ 'Category One',
+ 'Category Two'
+ ]
+};
+
+function _assertCategoryListHasIds(categoryList, idList) {
+ assertEquals(idList.length, categoryList.length);
+ idList.forEach(function (id) {
+ assertTrue(categoryList.some(function (actualId) {
+ return actualId == id;
+ }));
+ });
+}
+
+function testNewModelFromJson() {
+ let model = ArticleModel.newFromJson(mockJsonData);
+ assertTrue(model instanceof ArticleModel.ArticleModel);
+ assertEquals('Article Title', model.title);
+ assertEquals('file:///', model.uri);
+ _assertCategoryListHasIds(model.getCategories(),
+ ['Category One', 'Category Two']);
+}
+
+function testNewWithProperties() {
+ let model = new ArticleModel.ArticleModel({
+ title: 'Article Title',
+ uri: 'file:///'
+ });
+ assertEquals('Article Title', model.title);
+ assertEquals('file:///', model.uri);
+ assertEquals(0, model.getCategories().length);
+}
+
+function testSetAndGetCategories() {
+ let model = new ArticleModel.ArticleModel();
+ let expectedCategories = ['One', 'Two', 'Three'];
+ model.setCategories(expectedCategories);
+ _assertCategoryListHasIds(model.getCategories(), expectedCategories);
+}
+
+function testSetCategoriesWipesPreviousCategories() {
+ let model = new ArticleModel.ArticleModel();
+ let firstCategories = ['One', 'Two', 'Three'];
+ model.setCategories(firstCategories);
+ let expectedCategories = ['A', 'B', 'C', 'D'];
+ model.setCategories(expectedCategories);
+ _assertCategoryListHasIds(model.getCategories(), expectedCategories);
+}
+
+function testAddAndGetCategories() {
+ let model = new ArticleModel.ArticleModel();
+ model.addCategory('One');
+ model.addCategory('Two');
+ model.addCategory('Three');
+ _assertCategoryListHasIds(model.getCategories(), ['One', 'Two', 'Three']);
+}
+
+function testHasCategories() {
+ let model = new ArticleModel.ArticleModel();
+ let expectedCategories = ['One', 'Two', 'Three'];
+ model.setCategories(expectedCategories);
+ expectedCategories.forEach(function (id) {
+ assertTrue(model.hasCategory(id));
+ });
+}
diff --git a/test/wikipedia/models/testCategoryModel.js b/test/wikipedia/models/testCategoryModel.js
new file mode 100644
index 0000000..9314e84
--- /dev/null
+++ b/test/wikipedia/models/testCategoryModel.js
@@ -0,0 +1,76 @@
+const CategoryModel = imports.models.category_model;
+
+let mockJsonData = {
+ category_name: 'Category Name',
+ content_text: 'Lorem Ipsum',
+ image_file: 'file:///image.jpg',
+ image_thumb_uri: 'file:///thumb.jpg',
+ is_main_category: false,
+ subcategories: [ 'Category Two' ]
+};
+
+function testNewModelFromJson() {
+ let model = CategoryModel.newFromJson(mockJsonData);
+ assertTrue(model instanceof CategoryModel.CategoryModel);
+ assertEquals('Category Name', model.id);
+ assertEquals(0, model.getSubcategories().length);
+}
+
+function testNewWithProperties() {
+ let model = new CategoryModel.CategoryModel({
+ id: 'id',
+ title: 'title',
+ description: 'description',
+ image_uri: 'image-uri',
+ image_thumbnail_uri: 'image-thumbnail-uri',
+ is_main_category: true,
+ has_articles: true
+ });
+ assertEquals('id', model.id);
+ assertEquals('title', model.title);
+ assertEquals('description', model.description);
+ assertEquals('image-uri', model.image_uri);
+ assertEquals('image-thumbnail-uri', model.image_thumbnail_uri);
+ assertEquals(true, model.is_main_category);
+ assertEquals(true, model.has_articles);
+
+ model.has_articles = false;
+ assertEquals(false, model.has_articles);
+}
+
+function testGetSubcategoriesEmpty() {
+ let model = new CategoryModel.CategoryModel();
+ assertEquals(0, model.getSubcategories().length);
+}
+
+function _assertCategoryListContainsCategoryIds(categoryList, idList) {
+ assertEquals(idList.length, categoryList.length);
+ idList.forEach(function (id) {
+ assertTrue(categoryList.some(function (categoryModel) {
+ return categoryModel.id == id;
+ }));
+ });
+}
+
+function testAddAndGetSubcategories() {
+ let model1 = new CategoryModel.CategoryModel({ id: 'Category One' });
+ let model2 = new CategoryModel.CategoryModel({ id: 'Category Two' });
+ let model3 = new CategoryModel.CategoryModel({ id: 'Category Three' });
+ model1.addSubcategory(model2);
+ model1.addSubcategory(model3);
+
+ let categories = model1.getSubcategories();
+ _assertCategoryListContainsCategoryIds(categories,
+ ['Category Two', 'Category Three']);
+}
+
+function testAddSubcategoryDoesNothingForDuplicate() {
+ let model1 = new CategoryModel.CategoryModel({ id: 'Category One' });
+ let model2 = new CategoryModel.CategoryModel({ id: 'Category Two' });
+ let model3 = new CategoryModel.CategoryModel({ id: 'Category Two' });
+ model1.addSubcategory(model2);
+ model1.addSubcategory(model3);
+
+ let categories = model1.getSubcategories();
+ _assertCategoryListContainsCategoryIds(categories, ['Category Two']);
+}
diff --git a/test/wikipedia/models/testDomainWikiModel.js b/test/wikipedia/models/testDomainWikiModel.js
new file mode 100644
index 0000000..f476b16
--- /dev/null
+++ b/test/wikipedia/models/testDomainWikiModel.js
@@ -0,0 +1,141 @@
+const DomainWikiModel = imports.models.domain_wiki_model;
+
+let model;
+
+let mockJsonData = {
+ categories: [
+ {
+ category_name: 'Main Category',
+ content_text: 'Lorem Ipsum',
+ image_file: 'file:///image.jpg',
+ image_thumb_uri: 'file:///image_thumb.jpg',
+ is_main_category: true,
+ subcategories: [
+ 'Category One',
+ 'Category Two'
+ ]
+ },
+ {
+ category_name: 'Category One',
+ content_text: 'Lorem Ipsum',
+ image_file: 'file:///image.jpg',
+ image_thumb_uri: 'file:///image_thumb.jpg',
+ is_main_category: false,
+ subcategories: []
+ },
+ {
+ category_name: 'Category Two',
+ content_text: 'Lorem Ipsum',
+ image_file: 'file:///image.jpg',
+ image_thumb_uri: 'file:///image_thumb.jpg',
+ is_main_category: false,
+ subcategories: [
+ 'Category Three'
+ ]
+ },
+ {
+ category_name: 'Category Three',
+ content_text: 'Lorem Ipsum',
+ image_file: 'file:///image.jpg',
+ image_thumb_uri: 'file:///image_thumb.jpg',
+ is_main_category: false,
+ subcategories: []
+ },
+ ],
+ articles: [
+ {
+ title: 'Article One',
+ url: 'file:///article1.html',
+ categories: [
+ 'Category One'
+ ]
+ },
+ {
+ title: 'Article Two',
+ url: 'file:///article2.html',
+ categories: [
+ 'Category One',
+ 'Category Two'
+ ]
+ },
+ {
+ title: 'Article Three',
+ url: 'file:///article3.html',
+ categories: [
+ 'Category Two'
+ ]
+ }
+ ]
+};
+
+function setUp() {
+ model = new DomainWikiModel.DomainWikiModel();
+}
+
+function _assertArticleListContainsArticleTitles(articleList, titleList) {
+ assertEquals(titleList.length, articleList.length);
+ titleList.forEach(function (title) {
+ assertTrue(articleList.some(function (articleModel) {
+ return articleModel.title == title;
+ }));
+ });
+}
+
+function testGetArticlesReturnsAllArticles() {
+ model.loadFromJson(mockJsonData);
+ let articles = model.getArticles();
+ _assertArticleListContainsArticleTitles(articles,
+ [ 'Article One', 'Article Two', 'Article Three' ]);
+}
+
+function testGetArticlesForCategoryWithArticles() {
+ model.loadFromJson(mockJsonData);
+ let articles = model.getArticlesForCategory('Category One');
+ _assertArticleListContainsArticleTitles(articles,
+ [ 'Article One', 'Article Two' ]);
+}
+
+function testGetArticlesForCategoryWithoutArticles() {
+ model.loadFromJson(mockJsonData);
+ assertEquals(0, model.getArticlesForCategory('Main Category').length);
+}
+
+function testGetArticlesForCategoryWithNonexistentId() {
+ assertEquals(0, model.getArticlesForCategory('Nonexistent').length);
+}
+
+function testCategoryHasArticlesReturnsTrue() {
+ model.loadFromJson(mockJsonData);
+ assertTrue(model._getCategoryHasArticles('Category Two'));
+}
+
+function testCategoryHasArticlesReturnsFalse() {
+ model.loadFromJson(mockJsonData);
+ assertFalse(model._getCategoryHasArticles('Category Three'));
+}
+
+function testCategoryHasArticlesWithNonexistentId() {
+ assertFalse(model._getCategoryHasArticles('Nonexistent'));
+}
+
+function testGetCategory() {
+ model.loadFromJson(mockJsonData);
+ let category = model.getCategory('Category One');
+ assertEquals('CategoryModel', category.__name__);
+ assertEquals('Category One', category.title);
+}
+
+function testGetNonexistentCategory() {
+ assertUndefined(model.getCategory('Nonexistent'));
+}
+
+function testGetMainCategory() {
+ model.loadFromJson(mockJsonData);
+ let category = model.getMainCategory();
+ assertTrue(category.__name__ == 'CategoryModel');
+ assertEquals('Main Category', category.title);
+}
+
+function testGetUnsetMainCategory() {
+ assertNull(model.getMainCategory());
+}