Skip to content

Commit

Permalink
Added test for unique model names and renamed SuggestionModel to Gene…
Browse files Browse the repository at this point in the history
…ralSuggestionModel (oppia#5016)

* added test for model names

* fixed test and renamed SuggestionModel

* fixed linting

* review changes (1)

* added explanatory comment
  • Loading branch information
nithusha21 authored Jun 4, 2018
1 parent 9f27d9b commit 9ee7361
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 45 deletions.
50 changes: 50 additions & 0 deletions core/storage/storage_models_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2018 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for Oppia storage models."""
import inspect

from core.platform import models
from core.tests import test_utils


class StorageModelsTest(test_utils.GenericTestBase):
"""Tests for Oppia storage models."""

def test_all_model_names_unique(self):
all_model_names = []

# As models.NAMES is an enum, it cannot be iterated. So we use the
# __dict__ property which can be iterated.
for name in models.NAMES.__dict__:
if '__' not in name:
all_model_names.append(name)

names_of_ndb_model_subclasses = []
for name in all_model_names:
(module, ) = models.Registry.import_models([name])
for member_name, member_obj in inspect.getmembers(module):
if inspect.isclass(member_obj):
clazz = getattr(module, member_name)
ancestor_names = [
base_class.__name__ for base_class in clazz.__bases__]
if (
'ndb.Model' in ancestor_names or
'BaseModel' in ancestor_names or
'VersionedModel' in ancestor_names):
names_of_ndb_model_subclasses.append(clazz.__name__)

self.assertEqual(
len(set(names_of_ndb_model_subclasses)),
len(names_of_ndb_model_subclasses))
2 changes: 1 addition & 1 deletion core/storage/suggestion/gae_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
SCORE_CATEGORY_DELIMITER = '.'


class SuggestionModel(base_models.BaseModel):
class GeneralSuggestionModel(base_models.BaseModel):
"""Model to store suggestions made by Oppia users.
The ID of the suggestions are created is the same as the ID of the thread
Expand Down
112 changes: 68 additions & 44 deletions core/storage/suggestion/gae_models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,35 @@ class SuggestionModelUnitTests(test_utils.GenericTestBase):

def setUp(self):
super(SuggestionModelUnitTests, self).setUp()
suggestion_models.SuggestionModel.create(
suggestion_models.GeneralSuggestionModel.create(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT,
suggestion_models.TARGET_TYPE_EXPLORATION,
self.target_id, self.target_version_at_submission,
suggestion_models.STATUS_IN_REVIEW, 'author_1',
'reviewer_1', 'reviewer_1', self.change_cmd, self.score_category,
'exploration.exp1.thread_1')
suggestion_models.SuggestionModel.create(
suggestion_models.GeneralSuggestionModel.create(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT,
suggestion_models.TARGET_TYPE_EXPLORATION,
self.target_id, self.target_version_at_submission,
suggestion_models.STATUS_ACCEPTED, 'author_2',
'reviewer_2', 'reviewer_2', self.change_cmd, self.score_category,
'exploration.exp1.thread_2')
suggestion_models.SuggestionModel.create(
suggestion_models.GeneralSuggestionModel.create(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT,
suggestion_models.TARGET_TYPE_EXPLORATION,
self.target_id, self.target_version_at_submission,
suggestion_models.STATUS_ACCEPTED, 'author_2',
'reviewer_3', 'reviewer_2', self.change_cmd, self.score_category,
'exploration.exp1.thread_3')
suggestion_models.SuggestionModel.create(
suggestion_models.GeneralSuggestionModel.create(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT,
suggestion_models.TARGET_TYPE_EXPLORATION,
self.target_id, self.target_version_at_submission,
suggestion_models.STATUS_REJECTED, 'author_2',
'reviewer_2', 'reviewer_3', self.change_cmd, self.score_category,
'exploration.exp1.thread_4')
suggestion_models.SuggestionModel.create(
suggestion_models.GeneralSuggestionModel.create(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT,
suggestion_models.TARGET_TYPE_EXPLORATION,
self.target_id, self.target_version_at_submission,
Expand All @@ -77,7 +77,7 @@ def test_score_type_contains_delimiter(self):
suggestion_models.SCORE_CATEGORY_DELIMITER not in score_type)

def test_create_new_object_succesfully(self):
suggestion_models.SuggestionModel.create(
suggestion_models.GeneralSuggestionModel.create(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT,
suggestion_models.TARGET_TYPE_EXPLORATION,
self.target_id, self.target_version_at_submission,
Expand All @@ -87,8 +87,8 @@ def test_create_new_object_succesfully(self):

suggestion_id = 'exploration.exp1.thread_6'

observed_suggestion_model = suggestion_models.SuggestionModel.get_by_id(
suggestion_id)
observed_suggestion_model = (
suggestion_models.GeneralSuggestionModel.get_by_id(suggestion_id))


self.assertEqual(
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_create_suggestion_fails_if_id_collides_with_existing_one(self):
with self.assertRaisesRegexp(
Exception, 'There is already a suggestion with the given id: '
'exploration.exp1.thread_1'):
suggestion_models.SuggestionModel.create(
suggestion_models.GeneralSuggestionModel.create(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT,
suggestion_models.TARGET_TYPE_EXPLORATION,
self.target_id, self.target_version_at_submission,
Expand All @@ -128,76 +128,100 @@ def test_create_suggestion_fails_if_id_collides_with_existing_one(self):

def test_get_suggestions_by_type(self):
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_type(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT)), 5)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_type(
suggestion_models.SUGGESTION_EDIT_STATE_CONTENT)), 5)
with self.assertRaisesRegexp(
Exception, 'Value \'invalid_suggestion_type\' for property'
' suggestion_type is not an allowed choice'):
suggestion_models.SuggestionModel.get_suggestions_by_type(
suggestion_models.GeneralSuggestionModel.get_suggestions_by_type(
'invalid_suggestion_type')

def test_get_suggestion_by_author(self):
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_author(
'author_1')), 1)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_author('author_1')), 1)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_author(
'author_2')), 3)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_author('author_2')), 3)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_author(
'author_3')), 1)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_author('author_3')), 1)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_author(
'author_invalid')), 0)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_author('author_invalid')), 0)

def test_get_suggestion_assigned_to_reviewer(self):
self.assertEqual(
len(
suggestion_models.SuggestionModel
suggestion_models.GeneralSuggestionModel
.get_suggestions_assigned_to_reviewer('reviewer_1')), 1)
self.assertEqual(
len(
suggestion_models.SuggestionModel
suggestion_models.GeneralSuggestionModel
.get_suggestions_assigned_to_reviewer('reviewer_2')), 2)
self.assertEqual(
len(suggestion_models.SuggestionModel
len(suggestion_models.GeneralSuggestionModel
.get_suggestions_assigned_to_reviewer('reviewer_3')), 2)
self.assertEqual(
len(suggestion_models.SuggestionModel
len(suggestion_models.GeneralSuggestionModel
.get_suggestions_assigned_to_reviewer('reviewer_invalid')), 0)

def test_get_suggestion_by_reviewer(self):
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_reviewed_by(
'reviewer_1')), 1)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_reviewed_by('reviewer_1')), 1)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_reviewed_by(
'reviewer_2')), 3)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_reviewed_by('reviewer_2')), 3)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_reviewed_by(
'reviewer_3')), 1)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_reviewed_by('reviewer_3')), 1)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_reviewed_by(
'reviewer_invalid')), 0)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_reviewed_by('reviewer_invalid')), 0)

def test_get_suggestions_by_status(self):
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_status(
suggestion_models.STATUS_IN_REVIEW)), 1)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_status(
suggestion_models.STATUS_IN_REVIEW)), 1)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_status(
suggestion_models.STATUS_REJECTED)), 2)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_status(
suggestion_models.STATUS_REJECTED)), 2)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_status(
suggestion_models.STATUS_ACCEPTED)), 2)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_status(
suggestion_models.STATUS_ACCEPTED)), 2)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_status(
suggestion_models.STATUS_INVALID)), 0)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_status(
suggestion_models.STATUS_INVALID)), 0)

def test_get_suggestions_by_target_id(self):
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_target_id(
suggestion_models.TARGET_TYPE_EXPLORATION, self.target_id)), 5)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_target_id(
suggestion_models.TARGET_TYPE_EXPLORATION, self.target_id)),
5)
self.assertEqual(
len(suggestion_models.SuggestionModel.get_suggestions_by_target_id(
suggestion_models.TARGET_TYPE_EXPLORATION, 'exp_invalid')), 0)
len(
suggestion_models.GeneralSuggestionModel
.get_suggestions_by_target_id(
suggestion_models.TARGET_TYPE_EXPLORATION, 'exp_invalid')),
0)

0 comments on commit 9ee7361

Please sign in to comment.