tests/app/soc/logic/models/test_base.py
changeset 1614 797f5ae462e7
child 1673 9f67ec81f1ef
equal deleted inserted replaced
1613:59e5cc89e509 1614:797f5ae462e7
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 
       
    18 __authors__ = [
       
    19   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    20   ]
       
    21 
       
    22 
       
    23 import unittest
       
    24 
       
    25 from google.appengine.api import users
       
    26 from google.appengine.ext import db
       
    27 
       
    28 from soc.logic.models import base
       
    29 
       
    30 
       
    31 class TestModel(db.Model):
       
    32   """Simpel test model.
       
    33   """
       
    34 
       
    35   value = db.IntegerProperty()
       
    36 
       
    37 
       
    38 class TestModelLogic(base.Logic):
       
    39   """Simple test logic.
       
    40   """
       
    41 
       
    42   def __init__(self):
       
    43     super(TestModelLogic, self).__init__(TestModel)
       
    44 
       
    45 
       
    46 class UserTest(unittest.TestCase):
       
    47   """Tests related to user logic.
       
    48   """
       
    49 
       
    50   def setUp(self):
       
    51     """Set up required for the slot allocation tests.
       
    52     """
       
    53 
       
    54     entities = []
       
    55 
       
    56     for i in range(5):
       
    57       entity = TestModel(key_name='test_%d' % i, value=i)
       
    58       entity.put()
       
    59       entities.append(entity)
       
    60 
       
    61     self.logic = TestModelLogic()
       
    62     self.entities = entities
       
    63 
       
    64   def testGetForFields(self):
       
    65     """Test that all entries were retrieved.
       
    66     """
       
    67 
       
    68     expected = set(range(5))
       
    69     actual = set([i.value for i in self.logic.getForFields()])
       
    70     self.assertEqual(expected, actual)
       
    71 
       
    72   def testGetForFieldsFiltered(self):
       
    73     """Test that only the entry that matches the filter is retrieved.
       
    74     """
       
    75 
       
    76     fields = {'value': 1}
       
    77 
       
    78     expected = [1]
       
    79     actual = [i.value for i in self.logic.getForFields(fields)]
       
    80 
       
    81     self.assertEqual(expected, actual)
       
    82 
       
    83   def testGetForFieldsWithOperator(self):
       
    84     """Test that all entries matching the filter are retrieved.
       
    85     """
       
    86 
       
    87     fields = {'value <': 3}
       
    88 
       
    89     expected = set(range(3))
       
    90     actual = set([i.value for i in self.logic.getForFields(fields)])
       
    91 
       
    92     self.assertEqual(expected, actual)
       
    93 
       
    94   def testGetForFieldsNonMatching(self):
       
    95     """Test that unique returns None instead of a list.
       
    96     """
       
    97 
       
    98     fields = {'value': 1337}
       
    99 
       
   100     expected = []
       
   101     actual = self.logic.getForFields(fields)
       
   102     self.assertEqual(expected, actual)
       
   103 
       
   104   def testGetForFieldsUnique(self):
       
   105     """Test that unique returns an entry instead of a list.
       
   106     """
       
   107 
       
   108     fields = {'value': 1}
       
   109 
       
   110     actual = self.logic.getForFields(fields, unique=True)
       
   111     self.assertTrue(isinstance(actual, TestModel))
       
   112 
       
   113   def testGetForFieldsUniqueEmpty(self):
       
   114     """Test that unique returns None instead of a list.
       
   115     """
       
   116 
       
   117     fields = {'value': 1337}
       
   118 
       
   119     expected = None
       
   120     actual = self.logic.getForFields(fields, unique=True)
       
   121     self.assertEqual(expected, actual)
       
   122 
       
   123   def testGetForFieldsMultiFilter(self):
       
   124     """Test that all entries matching an 'IN' filter are returned.
       
   125     """
       
   126 
       
   127     fields = {'value': [1, 2]}
       
   128 
       
   129     expected = 2
       
   130     actual = len(self.logic.getForFields(fields))
       
   131     self.assertEqual(expected, actual)
       
   132 
       
   133   def testGetFieldsOrdened(self):
       
   134     """Test that fields can be ordened.
       
   135     """
       
   136 
       
   137     order = ['value']
       
   138 
       
   139     expected = range(5)
       
   140     actual = [i.value for i in self.logic.getForFields(order=order)]
       
   141     self.assertEqual(expected, actual)
       
   142 
       
   143   def testGetFieldsReverseOrdened(self):
       
   144     """Test that fields can be ordened in reverse.
       
   145     """
       
   146 
       
   147     order = ['-value']
       
   148 
       
   149     expected = range(5)
       
   150     expected.reverse()
       
   151     actual = [i.value for i in self.logic.getForFields(order=order)]
       
   152     self.assertEqual(expected, actual)