# Path: yithlibraryserver/datetimeservice/testing.py
# class FakeDatetimeService(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def utcnow(self):
#         fake = os.environ['YITH_FAKE_DATETIME']
#         parts = [int(p) for p in fake.split('-')]
#         return datetime.datetime(*parts)
# 
# Path: yithlibraryserver/db.py
# class MongoDB(object):
#     """Simple wrapper to get pymongo real objects from the settings uri"""
# 
#     def __init__(self, db_uri=DEFAULT_MONGODB_URI,
#                  connection_factory=pymongo.Connection):
#         self.db_uri = urlparse.urlparse(db_uri)
#         self.connection = connection_factory(
#             host=self.db_uri.hostname or DEFAULT_MONGODB_HOST,
#             port=self.db_uri.port or DEFAULT_MONGODB_PORT,
#             tz_aware=True)
# 
#         if self.db_uri.path:
#             self.database_name = self.db_uri.path[1:]
#         else:
#             self.database_name = DEFAULT_MONGODB_NAME
# 
#     def get_connection(self):
#         return self.connection
# 
#     def get_database(self):
#         database = self.connection[self.database_name]
#         if self.db_uri.username and self.db_uri.password:
#             database.authenticate(self.db_uri.username, self.db_uri.password)
# 
#         return database
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# 
# Path: yithlibraryserver/contributions/models.py
# def include_sticker(amount):
#     return amount > 1
# 
# Path: yithlibraryserver/contributions/models.py
# def create_donation(request, data):
#     amount = int(data['amount'])
#     donation = {
#         'amount': amount,
#         'firstname': data['firstname'],
#         'lastname': data['lastname'],
#         'city': data['city'],
#         'country': data['country'],
#         'state': data['state'],
#         'street': data['street'],
#         'zip': data['zip'],
#         'email': data['email'],
#         'creation': request.datetime_service.utcnow(),
#     }
#     if include_sticker(amount):
#         donation['send_sticker'] = not ('no-sticker' in data)
#     else:
#         donation['send_sticker'] = False
# 
#     if request.user is not None:
#         donation['user'] = request.user['_id']
#     else:
#         donation['user'] = None
# 
#     _id = request.db.donations.insert(donation, safe=True)
#     donation['_id'] = _id
#     return donation


import datetime
import os
import unittest
from pyramid import testing
from yithlibraryserver.datetimeservice.testing import FakeDatetimeService
from yithlibraryserver.db import MongoDB
from yithlibraryserver.testing import MONGO_URI
from yithlibraryserver.contributions.models import include_sticker
from yithlibraryserver.contributions.models import create_donation
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.






class ModelTests(unittest.TestCase):

    def setUp(self):
        self.config = testing.setUp()
        mdb = MongoDB(MONGO_URI)
        self.db = mdb.get_database()
        os.environ['YITH_FAKE_DATETIME'] = '2013-1-2-10-11-02'

        self.request = testing.DummyRequest()
        self.request.datetime_service = FakeDatetimeService(self.request)
        self.request.db = self.db

    def tearDown(self):
        testing.tearDown()
        self.db.drop_collection('donations')
        del os.environ['YITH_FAKE_DATETIME']

    def test_include_sticker(self):
        self.assertFalse(include_sticker(1))
        self.assertTrue(include_sticker(5))

# Path: yithlibraryserver/datetimeservice/testing.py
# class FakeDatetimeService(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def utcnow(self):
#         fake = os.environ['YITH_FAKE_DATETIME']
#         parts = [int(p) for p in fake.split('-')]
#         return datetime.datetime(*parts)
# 
# Path: yithlibraryserver/db.py
# class MongoDB(object):
#     """Simple wrapper to get pymongo real objects from the settings uri"""
# 
#     def __init__(self, db_uri=DEFAULT_MONGODB_URI,
#                  connection_factory=pymongo.Connection):
#         self.db_uri = urlparse.urlparse(db_uri)
#         self.connection = connection_factory(
#             host=self.db_uri.hostname or DEFAULT_MONGODB_HOST,
#             port=self.db_uri.port or DEFAULT_MONGODB_PORT,
#             tz_aware=True)
# 
#         if self.db_uri.path:
#             self.database_name = self.db_uri.path[1:]
#         else:
#             self.database_name = DEFAULT_MONGODB_NAME
# 
#     def get_connection(self):
#         return self.connection
# 
#     def get_database(self):
#         database = self.connection[self.database_name]
#         if self.db_uri.username and self.db_uri.password:
#             database.authenticate(self.db_uri.username, self.db_uri.password)
# 
#         return database
# 
# Path: yithlibraryserver/testing.py
# MONGO_URI = 'mongodb://localhost:27017/test-yith-library'
# 
# Path: yithlibraryserver/contributions/models.py
# def include_sticker(amount):
#     return amount > 1
# 
# Path: yithlibraryserver/contributions/models.py
# def create_donation(request, data):
#     amount = int(data['amount'])
#     donation = {
#         'amount': amount,
#         'firstname': data['firstname'],
#         'lastname': data['lastname'],
#         'city': data['city'],
#         'country': data['country'],
#         'state': data['state'],
#         'street': data['street'],
#         'zip': data['zip'],
#         'email': data['email'],
#         'creation': request.datetime_service.utcnow(),
#     }
#     if include_sticker(amount):
#         donation['send_sticker'] = not ('no-sticker' in data)
#     else:
#         donation['send_sticker'] = False
# 
#     if request.user is not None:
#         donation['user'] = request.user['_id']
#     else:
#         donation['user'] = None
# 
#     _id = request.db.donations.insert(donation, safe=True)
#     donation['_id'] = _id
#     return donation


# Path: yithlibraryserver/contributions/tests/test_models.py
import datetime
import os
import unittest
from pyramid import testing
from yithlibraryserver.datetimeservice.testing import FakeDatetimeService
from yithlibraryserver.db import MongoDB
from yithlibraryserver.testing import MONGO_URI
from yithlibraryserver.contributions.models import include_sticker
from yithlibraryserver.contributions.models import create_donation

# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Yith Library Server.  If not, see <http://www.gnu.org/licenses/>.






class ModelTests(unittest.TestCase):

    def setUp(self):
        self.config = testing.setUp()
        mdb = MongoDB(MONGO_URI)
        self.db = mdb.get_database()
        os.environ['YITH_FAKE_DATETIME'] = '2013-1-2-10-11-02'

        self.request = testing.DummyRequest()
        self.request.datetime_service = FakeDatetimeService(self.request)
        self.request.db = self.db

    def tearDown(self):
        testing.tearDown()
        self.db.drop_collection('donations')
        del os.environ['YITH_FAKE_DATETIME']

    def test_include_sticker(self):
        self.assertFalse(include_sticker(1))
        self.assertTrue(include_sticker(5))

        self.assertTrue(include_sticker(10))