A webapp/form for people to join pEp coop. Fork of Cultural Commons Collecting Society (C3S) SCE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_models.py 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  1. # -*- coding: utf-8 -*-
  2. # import os
  3. from datetime import(
  4. date,
  5. datetime,
  6. timedelta,
  7. )
  8. from decimal import Decimal as D
  9. from decimal import InvalidOperation
  10. import unittest
  11. from pyramid import testing
  12. from sqlalchemy import create_engine
  13. from sqlalchemy.exc import IntegrityError
  14. import transaction
  15. from c3smembership.data.model.base import (
  16. Base,
  17. DBSession,
  18. )
  19. from c3smembership.models import (
  20. C3sMember,
  21. C3sStaff,
  22. Dues15Invoice,
  23. Dues16Invoice,
  24. Dues17Invoice,
  25. Group,
  26. )
  27. # Disable Pylint error message when using DBSession methods
  28. # pylint: disable=no-member
  29. DEBUG = False
  30. class C3sMembershipModelTestBase(unittest.TestCase):
  31. def setUp(self):
  32. self.config = testing.setUp()
  33. self.config.include('pyramid_mailer.testing')
  34. try:
  35. DBSession.remove()
  36. except:
  37. pass
  38. # engine = create_engine('sqlite:///test_models.db')
  39. engine = create_engine('sqlite:///:memory:')
  40. self.session = DBSession
  41. DBSession.configure(bind=engine)
  42. Base.metadata.create_all(engine)
  43. def tearDown(self):
  44. self.session.close()
  45. self.session.remove()
  46. # os.remove('test_models.db')
  47. @classmethod
  48. def _get_target_class(cls):
  49. return C3sMember
  50. def _make_one(self,
  51. firstname=u'SomeFirstnäme',
  52. lastname=u'SomeLastnäme',
  53. email=u'some@shri.de',
  54. address1=u"addr one",
  55. address2=u"addr two",
  56. postcode=u"12345",
  57. city=u"Footown Mäh",
  58. country=u"Foocountry",
  59. locale=u"DE",
  60. date_of_birth=date.today(),
  61. email_is_confirmed=False,
  62. email_confirm_code=u'ABCDEFGHIK',
  63. password=u'arandompassword',
  64. date_of_submission=date.today(),
  65. membership_type=u'normal',
  66. member_of_colsoc=True,
  67. name_of_colsoc=u"GEMA",
  68. num_shares=u'23'):
  69. return self._get_target_class()( # order of params DOES matter
  70. firstname, lastname, email,
  71. password,
  72. address1, address2, postcode,
  73. city, country, locale,
  74. date_of_birth, email_is_confirmed, email_confirm_code,
  75. num_shares,
  76. date_of_submission,
  77. membership_type,
  78. member_of_colsoc, name_of_colsoc,
  79. )
  80. def _make_another_one(self,
  81. firstname=u'SomeFirstname',
  82. lastname=u'SomeLastname',
  83. email=u'some@shri.de',
  84. address1=u"addr one",
  85. address2=u"addr two",
  86. postcode=u"12345",
  87. city=u"Footown Muh",
  88. country=u"Foocountry",
  89. locale=u"DE",
  90. date_of_birth=date.today(),
  91. email_is_confirmed=False,
  92. email_confirm_code=u'0987654321',
  93. password=u'arandompassword',
  94. date_of_submission=date.today(),
  95. membership_type=u'investing',
  96. member_of_colsoc=False,
  97. name_of_colsoc=u"deletethis",
  98. num_shares=u'23'):
  99. return self._get_target_class()( # order of params DOES matter
  100. firstname, lastname, email,
  101. password,
  102. address1, address2, postcode,
  103. city, country, locale,
  104. date_of_birth, email_is_confirmed, email_confirm_code,
  105. num_shares,
  106. date_of_submission,
  107. membership_type, member_of_colsoc, name_of_colsoc,
  108. )
  109. class C3sMembershipModelTests(C3sMembershipModelTestBase):
  110. def setUp(self):
  111. """
  112. prepare for tests: have one member in the database
  113. """
  114. super(C3sMembershipModelTests, self).setUp()
  115. with transaction.manager:
  116. member1 = C3sMember( # german
  117. firstname=u'SomeFirstnäme',
  118. lastname=u'SomeLastnäme',
  119. email=u'some@shri.de',
  120. address1=u"addr one",
  121. address2=u"addr two",
  122. postcode=u"12345",
  123. city=u"Footown Mäh",
  124. country=u"Foocountry",
  125. locale=u"DE",
  126. date_of_birth=date.today(),
  127. email_is_confirmed=False,
  128. email_confirm_code=u'ABCDEFGFOO',
  129. password=u'arandompassword',
  130. date_of_submission=date.today(),
  131. membership_type=u'normal',
  132. member_of_colsoc=True,
  133. name_of_colsoc=u"GEMA",
  134. num_shares=u'23',
  135. )
  136. DBSession.add(member1)
  137. DBSession.flush()
  138. def test_constructor(self):
  139. instance = self._make_one()
  140. self.assertEqual(instance.firstname, u'SomeFirstnäme', "No match!")
  141. self.assertEqual(instance.lastname, u'SomeLastnäme', "No match!")
  142. self.assertEqual(instance.email, u'some@shri.de', "No match!")
  143. self.assertEqual(instance.address1, u'addr one', "No match!")
  144. self.assertEqual(instance.address2, u'addr two', "No match!")
  145. self.assertEqual(instance.email, u'some@shri.de', "No match!")
  146. self.assertEqual(
  147. instance.email_confirm_code, u'ABCDEFGHIK', "No match!")
  148. self.assertEqual(instance.email_is_confirmed, False, "expected False")
  149. self.assertEqual(instance.membership_type, u'normal', "No match!")
  150. def test_get_password(self):
  151. """
  152. Test the _get_password function.
  153. """
  154. instance = self._make_one()
  155. self.assertEqual(instance.password, instance._password)
  156. def test_get_number(self):
  157. """
  158. test: get the number of entries in the database
  159. """
  160. instance = self._make_one()
  161. self.session.add(instance)
  162. self.assertEqual(self._get_target_class().get_number(), 2)
  163. # GET BY .. tests # # # # # # # # # # # # # # # # # # # # # # # # # # #
  164. def test_get_by_code(self):
  165. """
  166. test: get one entry by code
  167. """
  168. instance = self._make_one()
  169. self.session.add(instance)
  170. instance_from_db = self._get_target_class().get_by_code(u'ABCDEFGHIK')
  171. self.assertEqual(instance.firstname, u'SomeFirstnäme')
  172. self.assertEqual(instance_from_db.email, u'some@shri.de')
  173. def test_get_by_bcgvtoken(self):
  174. """
  175. test: get one entry by bcgv18 token
  176. """
  177. instance = self._make_one()
  178. self.session.add(instance)
  179. instance.email_invite_token_bcgv18 = u'SHINY_TOKEN'
  180. instance_from_db = self._get_target_class().get_by_bcgvtoken(
  181. u'SHINY_TOKEN')
  182. self.assertEqual(instance_from_db.firstname, u'SomeFirstnäme')
  183. self.assertEqual(instance_from_db.email, u'some@shri.de')
  184. self.assertEqual(
  185. instance_from_db.email_invite_token_bcgv18, u'SHINY_TOKEN')
  186. def test_get_by_dues15_token(self):
  187. """
  188. test: get one entry by token
  189. """
  190. instance = self._make_one()
  191. self.session.add(instance)
  192. instance.dues15_token = u'THIS_ONE'
  193. instance_from_db = self._get_target_class().get_by_dues15_token(
  194. u'THIS_ONE')
  195. self.assertEqual(instance_from_db.firstname, u'SomeFirstnäme')
  196. self.assertEqual(instance_from_db.email, u'some@shri.de')
  197. def test_get_by_dues16_token(self):
  198. """
  199. test: get one entry by token
  200. """
  201. instance = self._make_one()
  202. self.session.add(instance)
  203. instance.dues16_token = u'F73sjf29g4eEf9giJ'
  204. instance_from_db = self._get_target_class().get_by_dues16_token(
  205. u'F73sjf29g4eEf9giJ')
  206. self.assertEqual(instance_from_db.firstname, u'SomeFirstnäme')
  207. self.assertEqual(instance_from_db.email, u'some@shri.de')
  208. def test_get_by_dues17_token(self):
  209. """
  210. test: get one entry by token
  211. """
  212. instance = self._make_one()
  213. self.session.add(instance)
  214. instance.dues17_token = u'aa84f59a8fjf79oa83kd'
  215. instance_from_db = self._get_target_class().get_by_dues17_token(
  216. u'aa84f59a8fjf79oa83kd')
  217. self.assertEqual(instance_from_db.firstname, u'SomeFirstnäme')
  218. self.assertEqual(instance_from_db.email, u'some@shri.de')
  219. def test_get_by_email(self):
  220. """
  221. test: get one entry by email
  222. """
  223. instance = self._make_one()
  224. self.session.add(instance)
  225. list_from_db = self._get_target_class().get_by_email(
  226. u'some@shri.de')
  227. self.assertEqual(list_from_db[0].firstname, u'SomeFirstnäme')
  228. self.assertEqual(list_from_db[0].email, u'some@shri.de')
  229. def test_get_by_id(self):
  230. """
  231. test: get one entry by id
  232. """
  233. instance = self._make_one()
  234. self.session.add(instance)
  235. self.session.flush()
  236. _id = instance.id
  237. _date_of_birth = instance.date_of_birth
  238. _date_of_submission = instance.date_of_submission
  239. instance_from_db = self._get_target_class().get_by_id(_id)
  240. self.assertEqual(instance_from_db.firstname, u'SomeFirstnäme')
  241. self.assertEqual(instance_from_db.lastname, u'SomeLastnäme')
  242. self.assertEqual(instance_from_db.email, u'some@shri.de')
  243. self.assertEqual(instance_from_db.address1, u'addr one')
  244. self.assertEqual(instance_from_db.address2, u'addr two')
  245. self.assertEqual(instance_from_db.postcode, u'12345')
  246. self.assertEqual(instance_from_db.city, u'Footown Mäh')
  247. self.assertEqual(instance_from_db.country, u'Foocountry')
  248. self.assertEqual(instance_from_db.locale, u'DE')
  249. self.assertEqual(instance_from_db.date_of_birth, _date_of_birth)
  250. self.assertEqual(instance_from_db.email_is_confirmed, False)
  251. self.assertEqual(instance_from_db.email_confirm_code, u'ABCDEFGHIK')
  252. self.assertEqual(instance_from_db.date_of_submission,
  253. _date_of_submission)
  254. self.assertEqual(instance_from_db.membership_type, u'normal')
  255. self.assertEqual(instance_from_db.member_of_colsoc, True)
  256. self.assertEqual(instance_from_db.name_of_colsoc, u'GEMA')
  257. self.assertEqual(instance_from_db.num_shares, u'23')
  258. def test_get_all(self):
  259. """
  260. test: get all entries
  261. """
  262. instance = self._make_one()
  263. instance2 = self._make_another_one()
  264. self.session.add(instance, instance2)
  265. self.session.flush()
  266. my_membership_signee_class = self._get_target_class()
  267. self.assertEquals(len(my_membership_signee_class.get_all()), 2)
  268. def test_get_dues15_invoicees(self):
  269. """
  270. test: get all members that haven't had their invoices sent
  271. """
  272. instance = self._make_one()
  273. instance2 = self._make_another_one()
  274. self.session.add(instance, instance2)
  275. self.session.flush()
  276. my_membership_signee_class = self._get_target_class()
  277. invoicees = my_membership_signee_class.get_dues15_invoicees(27)
  278. self.assertEquals(len(invoicees), 0)
  279. # change details so they be found
  280. instance.membership_accepted = True
  281. instance2.membership_accepted = True
  282. invoicees = my_membership_signee_class.get_dues15_invoicees(27)
  283. self.assertEquals(len(invoicees), 1)
  284. def test_get_dues16_invoicees(self):
  285. """
  286. test: get all members that haven't had their invoices sent
  287. """
  288. instance = self._make_one()
  289. instance2 = self._make_another_one()
  290. self.session.add(instance, instance2)
  291. self.session.flush()
  292. my_membership_signee_class = self._get_target_class()
  293. invoicees = my_membership_signee_class.get_dues16_invoicees(27)
  294. self.assertEquals(len(invoicees), 0)
  295. # change details so they be found
  296. instance.membership_accepted = True
  297. instance2.membership_accepted = True
  298. invoicees = my_membership_signee_class.get_dues16_invoicees(27)
  299. self.assertEquals(len(invoicees), 1)
  300. def test_get_dues17_invoicees(self):
  301. """
  302. test: get all members that haven't had their invoices sent
  303. """
  304. instance = self._make_one()
  305. instance2 = self._make_another_one()
  306. self.session.add(instance)
  307. self.session.add(instance2)
  308. self.session.flush()
  309. my_membership_signee_class = self._get_target_class()
  310. instance.membership_accepted = False
  311. instance.membership_date = None
  312. instance2.membership_accepted = False
  313. instance2.membership_date = None
  314. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  315. self.assertEquals(len(invoicees), 0)
  316. # change details so they be found
  317. instance.membership_accepted = True
  318. instance.membership_date = date(2016, 12, 1)
  319. instance2.membership_accepted = False
  320. instance2.membership_date = None
  321. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  322. self.assertEquals(len(invoicees), 1)
  323. instance.membership_accepted = True
  324. instance.membership_date = date(2016, 12, 1)
  325. instance2.membership_accepted = True
  326. instance2.membership_date = date(2016, 12, 2)
  327. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  328. self.assertEquals(len(invoicees), 2)
  329. # test boundary cases for membership date with one instance
  330. self.session.delete(instance2)
  331. self.session.flush()
  332. instance.membership_date = date(2017, 1, 1)
  333. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  334. self.assertEquals(len(invoicees), 1)
  335. instance.membership_date = date(2017, 12, 31)
  336. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  337. self.assertEquals(len(invoicees), 1)
  338. instance.membership_date = date(2016, 12, 31)
  339. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  340. self.assertEquals(len(invoicees), 1)
  341. instance.membership_date = date(2018, 1, 1)
  342. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  343. self.assertEquals(len(invoicees), 0)
  344. # test membership loss
  345. instance.membership_date = date(2016, 2, 3)
  346. instance.membership_loss_date = None
  347. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  348. self.assertEquals(len(invoicees), 1)
  349. instance.membership_loss_date = date(2018, 1, 1)
  350. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  351. self.assertEquals(len(invoicees), 1)
  352. instance.membership_loss_date = date(2017, 12, 31)
  353. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  354. self.assertEquals(len(invoicees), 1)
  355. instance.membership_loss_date = date(2017, 1, 1)
  356. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  357. self.assertEquals(len(invoicees), 1)
  358. instance.membership_loss_date = date(2016, 12, 31)
  359. invoicees = my_membership_signee_class.get_dues17_invoicees(27)
  360. self.assertEquals(len(invoicees), 0)
  361. def test_delete_by_id(self):
  362. """
  363. test: delete one entry by id
  364. """
  365. instance = self._make_one()
  366. self.session.add(instance)
  367. my_membership_signee_class = self._get_target_class()
  368. instance_from_db = my_membership_signee_class.get_by_id('1')
  369. my_membership_signee_class.delete_by_id('1')
  370. instance_from_db = my_membership_signee_class.get_by_id('1')
  371. self.assertEqual(None, instance_from_db)
  372. def test_check_user_or_none(self):
  373. """
  374. XXX TODO
  375. """
  376. instance = self._make_one()
  377. self.session.add(instance)
  378. my_membership_signee_class = self._get_target_class()
  379. # get first dataset (id = 1)
  380. members = my_membership_signee_class.check_user_or_none('1')
  381. self.assertEqual(1, members.id)
  382. # get invalid dataset
  383. result2 = my_membership_signee_class.check_user_or_none('1234567')
  384. self.assertEqual(None, result2)
  385. def test_existing_confirm_code(self):
  386. """
  387. XXX TODO
  388. """
  389. instance = self._make_one()
  390. self.session.add(instance)
  391. my_membership_signee_class = self._get_target_class()
  392. members = my_membership_signee_class.check_for_existing_confirm_code(
  393. u'ABCDEFGHIK')
  394. self.assertEqual(members, True)
  395. result2 = my_membership_signee_class.check_for_existing_confirm_code(
  396. u'ABCDEFGHIK0000000000')
  397. self.assertEqual(result2, False)
  398. def test_member_listing(self):
  399. """
  400. Test the member_listing classmethod in models.py
  401. """
  402. instance = self._make_one()
  403. self.session.add(instance)
  404. instance2 = self._make_another_one()
  405. self.session.add(instance2)
  406. my_membership_signee_class = self._get_target_class()
  407. members = my_membership_signee_class.member_listing("id")
  408. self.failUnless(members[0].firstname == u"SomeFirstnäme")
  409. self.failUnless(members[1].firstname == u"SomeFirstnäme")
  410. self.failUnless(members[2].firstname == u"SomeFirstname")
  411. self.assertEqual(len(members.all()), 3)
  412. def test_member_listing_exception(self):
  413. """
  414. XXX TODO
  415. """
  416. instance = self._make_one()
  417. self.session.add(instance)
  418. instance2 = self._make_another_one()
  419. self.session.add(instance2)
  420. my_membership_signee_class = self._get_target_class()
  421. with self.assertRaises(Exception):
  422. members = my_membership_signee_class.member_listing("foo")
  423. if DEBUG:
  424. print members
  425. def test_nonmember_listing(self):
  426. """
  427. Test the nonmember_listing classmethod in models.py
  428. """
  429. instance = self._make_one()
  430. self.session.add(instance)
  431. instance2 = self._make_another_one()
  432. self.session.add(instance2)
  433. my_membership_signee_class = self._get_target_class()
  434. # try order_by with faulty expression -- must raise
  435. with self.assertRaises(Exception):
  436. members = my_membership_signee_class.nonmember_listing(
  437. 0, 100, 'schmoo')
  438. # try order with faulty expression -- must raise
  439. with self.assertRaises(Exception):
  440. members = my_membership_signee_class.nonmember_listing(
  441. 0, 100, 'id', 'schmoo')
  442. members = my_membership_signee_class.nonmember_listing(
  443. 0, 100, 'id')
  444. self.failUnless(members[0].firstname == u'SomeFirstnäme')
  445. self.failUnless(members[1].firstname == u'SomeFirstnäme')
  446. self.failUnless(members[2].firstname == u'SomeFirstname')
  447. for member in members:
  448. self.assertTrue(not member.membership_accepted)
  449. members = my_membership_signee_class.nonmember_listing(
  450. 0, 100, 'id', 'desc')
  451. self.failUnless(members[0].firstname == u'SomeFirstname')
  452. self.failUnless(members[1].firstname == u'SomeFirstnäme')
  453. self.failUnless(members[2].firstname == u'SomeFirstnäme')
  454. for member in members:
  455. self.assertTrue(not member.membership_accepted)
  456. def test_nonmember_listing_count(self):
  457. """
  458. Test the nonmember_listing_count classmethod in models.py
  459. """
  460. instance = self._make_one()
  461. self.session.add(instance)
  462. instance2 = self._make_another_one()
  463. self.session.add(instance2)
  464. my_membership_signee_class = self._get_target_class()
  465. # try order with faulty expression -- must raise
  466. with self.assertRaises(Exception):
  467. members = my_membership_signee_class.nonmember_listing(
  468. 0, 100, 'id', 'schmoo')
  469. members = my_membership_signee_class.nonmember_listing(
  470. 0, 100, 'id')
  471. self.failUnless(members[0].firstname == u'SomeFirstnäme')
  472. self.failUnless(members[1].firstname == u'SomeFirstnäme')
  473. self.failUnless(members[2].firstname == u'SomeFirstname')
  474. result2 = my_membership_signee_class.nonmember_listing(
  475. 0, 100, 'id', 'desc')
  476. self.failUnless(result2[0].firstname == u'SomeFirstname')
  477. self.failUnless(result2[1].firstname == u'SomeFirstnäme')
  478. self.failUnless(result2[2].firstname == u'SomeFirstnäme')
  479. def test_get_num_members_accepted(self):
  480. """
  481. test: get the number of accepted member entries in the database
  482. """
  483. instance = self._make_one()
  484. self.session.add(instance)
  485. my_membership_signee_class = self._get_target_class()
  486. self.assertEqual(
  487. my_membership_signee_class.get_num_members_accepted(),
  488. 0)
  489. # go again
  490. instance.membership_accepted = True
  491. self.assertEqual(
  492. my_membership_signee_class.get_num_members_accepted(),
  493. 1)
  494. def test_get_num_non_accepted(self):
  495. """
  496. test: get the number of non-accepted member entries in the database
  497. """
  498. instance = self._make_one()
  499. self.session.add(instance)
  500. my_membership_signee_class = self._get_target_class()
  501. self.assertEqual(my_membership_signee_class.get_num_non_accepted(), 2)
  502. # go again
  503. instance.membership_accepted = True
  504. self.assertEqual(my_membership_signee_class.get_num_non_accepted(), 1)
  505. def test_get_num_mem_nat_acc(self):
  506. """
  507. test: get the number of accepted member entries being natural persons
  508. """
  509. instance = self._make_one()
  510. self.session.add(instance)
  511. my_membership_signee_class = self._get_target_class()
  512. self.assertEqual(my_membership_signee_class.get_num_mem_nat_acc(), 0)
  513. # go again
  514. instance.membership_accepted = True
  515. self.assertEqual(my_membership_signee_class.get_num_mem_nat_acc(), 1)
  516. def test_get_num_mem_jur_acc(self):
  517. """
  518. test: get the number of accepted member entries being legal entities
  519. """
  520. instance = self._make_one()
  521. self.session.add(instance)
  522. my_membership_signee_class = self._get_target_class()
  523. self.assertEqual(my_membership_signee_class.get_num_mem_jur_acc(), 0)
  524. # go again
  525. instance.membership_accepted = True
  526. instance.is_legalentity = True
  527. self.assertEqual(my_membership_signee_class.get_num_mem_jur_acc(), 1)
  528. def test_get_num_mem_norm(self):
  529. """
  530. test: get the number of accepted member entries being normal members.
  531. """
  532. instance = self._make_one()
  533. self.session.add(instance)
  534. my_membership_signee_class = self._get_target_class()
  535. self.assertEqual(my_membership_signee_class.get_num_mem_norm(), 0)
  536. # go again
  537. instance.membership_accepted = True
  538. self.assertEqual(instance.membership_type, u'normal')
  539. self.assertEqual(my_membership_signee_class.get_num_mem_norm(), 1)
  540. def test_get_num_mem_invest(self):
  541. """
  542. test: get the number of accepted member entries being investing members
  543. """
  544. instance = self._make_one()
  545. self.session.add(instance)
  546. my_membership_signee_class = self._get_target_class()
  547. number_from_db = my_membership_signee_class.get_num_mem_invest()
  548. self.assertEqual(number_from_db, 0)
  549. # go again
  550. instance.membership_accepted = True
  551. instance.membership_type = u'investing'
  552. self.assertEqual(instance.membership_type, u'investing')
  553. number_from_db = my_membership_signee_class.get_num_mem_invest()
  554. self.assertEqual(number_from_db, 1)
  555. def test_get_num_mem_other_features(self):
  556. """
  557. test: get number of accepted member entries with silly membership type
  558. """
  559. instance = self._make_one()
  560. self.session.add(instance)
  561. my_membership_signee_class = self._get_target_class()
  562. number_from_db = my_membership_signee_class.get_num_mem_other_features()
  563. self.assertEqual(number_from_db, 0)
  564. # go again
  565. instance.membership_accepted = True
  566. instance.membership_type = u'pondering'
  567. self.assertEqual(instance.membership_type, u'pondering')
  568. number_from_db = my_membership_signee_class.get_num_mem_other_features()
  569. self.assertEqual(number_from_db, 1)
  570. def test_is_member(self):
  571. member = C3sMember( # german
  572. firstname=u'SomeFirstnäme',
  573. lastname=u'SomeLastnäme',
  574. email=u'some@shri.de',
  575. address1=u"addr one",
  576. address2=u"addr two",
  577. postcode=u"12345",
  578. city=u"Footown Mäh",
  579. country=u"Foocountry",
  580. locale=u"DE",
  581. date_of_birth=date.today(),
  582. email_is_confirmed=False,
  583. email_confirm_code=u'ABCDEFGFOO',
  584. password=u'arandompassword',
  585. date_of_submission=date.today(),
  586. membership_type=u'normal',
  587. member_of_colsoc=True,
  588. name_of_colsoc=u"GEMA",
  589. num_shares=u'23',
  590. )
  591. # not member
  592. member.membership_accepted = False
  593. member.membership_loss_date = None
  594. self.assertEqual(member.is_member(), False)
  595. # Accepted after check date
  596. member.membership_accepted = True
  597. member.membership_date = date(2016, 1, 1)
  598. member.membership_loss_date = None
  599. self.assertEqual(member.is_member(date(2015, 12, 31)), False)
  600. # Accepted in the past
  601. member.membership_accepted = True
  602. member.membership_date = date(2016, 1, 1)
  603. member.membership_loss_date = None
  604. self.assertEqual(member.is_member(), True)
  605. # If loss date is today then the member still has membership until the
  606. # end of the day
  607. member.membership_accepted = True
  608. member.membership_date = date(2016, 1, 1)
  609. member.membership_loss_date = date.today()
  610. self.assertEqual(member.is_member(), True)
  611. # If the loss date is in the future then the member still has membership
  612. member.membership_accepted = True
  613. member.membership_date = date(2016, 1, 1)
  614. member.membership_loss_date = date.today() + timedelta(days=1)
  615. self.assertEqual(member.is_member(), True)
  616. # If the loss date is in the past then the member no longer has
  617. # membership
  618. member.membership_accepted = True
  619. member.membership_date = date(2016, 1, 1)
  620. member.membership_loss_date = date.today() - timedelta(days=1)
  621. self.assertEqual(member.is_member(), False)
  622. class TestMemberListing(C3sMembershipModelTestBase):
  623. """
  624. XXX TODO
  625. """
  626. def setUp(self):
  627. super(TestMemberListing, self).setUp()
  628. instance = self._make_one(
  629. lastname=u"ABC",
  630. firstname=u'xyz',
  631. email_confirm_code=u'0987654321')
  632. self.session.add(instance)
  633. instance = self._make_another_one(
  634. lastname=u"DEF",
  635. firstname=u'abc',
  636. email_confirm_code=u'19876543210')
  637. self.session.add(instance)
  638. instance = self._make_another_one(
  639. lastname=u"GHI",
  640. firstname=u'def',
  641. email_confirm_code=u'098765432101')
  642. self.session.add(instance)
  643. self.session.flush()
  644. self.class_under_test = self._get_target_class()
  645. def test_order_last_sort_last(self):
  646. result = self.class_under_test.member_listing(order_by='lastname')
  647. self.assertIsNotNone(result)
  648. self.assertIsNotNone(result[0])
  649. self.assertEqual("ABC", result[0].lastname)
  650. self.assertEqual("GHI", result[-1].lastname)
  651. def test_order_last_asc_sort_last(self):
  652. result = self.class_under_test.member_listing(
  653. order_by='lastname', order="asc")
  654. self.assertIsNotNone(result)
  655. self.assertIsNotNone(result[0])
  656. self.assertEqual("ABC", result[0].lastname)
  657. self.assertEqual("GHI", result[-1].lastname)
  658. def test_order_last_desc_sort_last(self):
  659. result = self.class_under_test.member_listing(
  660. order_by='lastname', order="desc")
  661. self.assertIsNotNone(result)
  662. self.assertIsNotNone(result[0])
  663. self.assertEqual("GHI", result[0].lastname)
  664. self.assertEqual("ABC", result[-1].lastname)
  665. def test_order_invalid(self):
  666. self.assertRaises(self.class_under_test.member_listing,
  667. order_by='unknown', order="desc")
  668. self.assertRaises(self.class_under_test.member_listing,
  669. order_by=None, order="desc")
  670. self.assertRaises(self.class_under_test.member_listing,
  671. order_by="", order="desc")
  672. self.assertRaises(self.class_under_test.member_listing,
  673. order_by='lastname', order="unknown")
  674. self.assertRaises(self.class_under_test.member_listing,
  675. order_by='lastname', order="")
  676. self.assertRaises(self.class_under_test.member_listing,
  677. order_by='lastname', order=None)
  678. class GroupTests(unittest.TestCase):
  679. """
  680. test the groups
  681. """
  682. def setUp(self):
  683. self.config = testing.setUp()
  684. self.config.include('pyramid_mailer.testing')
  685. try:
  686. DBSession.remove()
  687. except:
  688. pass
  689. # engine = create_engine('sqlite:///test_model_groups.db')
  690. engine = create_engine('sqlite://')
  691. self.session = DBSession
  692. self.session.configure(bind=engine)
  693. Base.metadata.create_all(engine)
  694. with transaction.manager:
  695. group1 = Group(name=u'staff')
  696. DBSession.add(group1)
  697. DBSession.flush()
  698. self.assertEquals(group1.__str__(), 'group:staff')
  699. def tearDown(self):
  700. self.session.close()
  701. self.session.remove()
  702. # os.remove('test_model_groups.db')
  703. def test_group(self):
  704. result = Group.get_staffers_group()
  705. self.assertEquals(result.__str__(), 'group:staff')
  706. def test__str__(self):
  707. staffers_group = Group.get_staffers_group()
  708. res = staffers_group.__str__()
  709. self.assertEquals(res, 'group:staff')
  710. class C3sStaffTests(unittest.TestCase):
  711. """
  712. test the staff and cashiers accounts
  713. """
  714. def setUp(self):
  715. self.config = testing.setUp()
  716. self.config.include('pyramid_mailer.testing')
  717. try:
  718. DBSession.remove()
  719. except:
  720. pass
  721. # engine = create_engine('sqlite:///test_model_staff.db')
  722. engine = create_engine('sqlite://')
  723. self.session = DBSession
  724. self.session.configure(bind=engine)
  725. Base.metadata.create_all(engine)
  726. with transaction.manager:
  727. group1 = Group(name=u'staff')
  728. group2 = Group(name=u'staff2')
  729. DBSession.add(group1, group2)
  730. DBSession.flush()
  731. def tearDown(self):
  732. self.session.close()
  733. self.session.remove()
  734. # os.remove('test_model_staff.db')
  735. def test_staff(self):
  736. staffer1 = C3sStaff(
  737. login=u'staffer1',
  738. password=u'stafferspassword'
  739. )
  740. staffer1.group = ['staff']
  741. staffer2 = C3sStaff(
  742. login=u'staffer2',
  743. password=u'staffer2spassword',
  744. )
  745. staffer2.group = ['staff2']
  746. self.session.add(staffer1)
  747. self.session.add(staffer2)
  748. self.session.flush()
  749. _staffer2_id = staffer2.id
  750. _staffer1_id = staffer1.id
  751. self.assertTrue(staffer2.password is not '')
  752. self.assertEqual(
  753. C3sStaff.get_by_id(_staffer1_id),
  754. C3sStaff.get_by_login(u'staffer1')
  755. )
  756. self.assertEqual(
  757. C3sStaff.get_by_id(_staffer2_id),
  758. C3sStaff.get_by_login(u'staffer2')
  759. )
  760. # test get_all
  761. res = C3sStaff.get_all()
  762. self.assertEqual(len(res), 2)
  763. # test delete_by_id
  764. C3sStaff.delete_by_id(1)
  765. res = C3sStaff.get_all()
  766. self.assertEqual(len(res), 1)
  767. # test check_user_or_none
  768. res1 = C3sStaff.check_user_or_none(u'staffer2')
  769. res2 = C3sStaff.check_user_or_none(u'staffer1')
  770. self.assertTrue(res1 is not None)
  771. self.assertTrue(res2 is None)
  772. # test check_password
  773. C3sStaff.check_password(u'staffer2', u'staffer2spassword')
  774. class Dues15InvoiceModelTests(unittest.TestCase):
  775. """
  776. test the dues15 invoice model
  777. """
  778. def setUp(self):
  779. self.config = testing.setUp()
  780. self.config.include('pyramid_mailer.testing')
  781. try:
  782. DBSession.remove()
  783. except:
  784. pass
  785. # engine = create_engine('sqlite:///test_model_staff.db')
  786. engine = create_engine('sqlite://')
  787. self.session = DBSession
  788. self.session.configure(bind=engine)
  789. Base.metadata.create_all(engine)
  790. with transaction.manager:
  791. member1 = C3sMember(
  792. firstname=u'SomeFirstnäme',
  793. lastname=u'SomeLastnäme',
  794. email=u'some@shri.de',
  795. address1=u"addr one",
  796. address2=u"addr two",
  797. postcode=u"12345",
  798. city=u"Footown Mäh",
  799. country=u"Foocountry",
  800. locale=u"DE",
  801. date_of_birth=date.today(),
  802. email_is_confirmed=False,
  803. email_confirm_code=u'ABCDEFGFOO',
  804. password=u'arandompassword',
  805. date_of_submission=date.today(),
  806. membership_type=u'normal',
  807. member_of_colsoc=True,
  808. name_of_colsoc=u"GEMA",
  809. num_shares=u'23',
  810. )
  811. DBSession.add(member1)
  812. member2 = C3sMember(
  813. firstname=u'Franziska',
  814. lastname=u'Musterfrau',
  815. email=u'some@shri.de',
  816. address1=u"addr one",
  817. address2=u"addr two",
  818. postcode=u"12345",
  819. city=u"Footown Mäh",
  820. country=u"Foocountry",
  821. locale=u"DE",
  822. date_of_birth=date.today(),
  823. email_is_confirmed=False,
  824. email_confirm_code=u'ABCDEFGFO1',
  825. password=u'arandompassword',
  826. date_of_submission=date.today(),
  827. membership_type=u'normal',
  828. member_of_colsoc=False,
  829. name_of_colsoc=u'',
  830. num_shares=u'23',
  831. )
  832. DBSession.add(member2)
  833. member3 = C3sMember(
  834. firstname=u'Jane',
  835. lastname=u'Somebody',
  836. email=u'some@shri.de',
  837. address1=u"addr one",
  838. address2=u"addr two",
  839. postcode=u"12345",
  840. city=u"Footown Mäh",
  841. country=u"Foocountry",
  842. locale=u"DE",
  843. date_of_birth=date.today(),
  844. email_is_confirmed=False,
  845. email_confirm_code=u'ABCDEFGFO2',
  846. password=u'arandompassword',
  847. date_of_submission=date.today(),
  848. membership_type=u'normal',
  849. member_of_colsoc=False,
  850. name_of_colsoc=u'',
  851. num_shares=u'23',
  852. )
  853. DBSession.add(member3)
  854. dues1 = Dues15Invoice(
  855. invoice_no=1,
  856. invoice_no_string=u'C3S-dues15-0001',
  857. invoice_date=date(2015, 10, 01),
  858. invoice_amount=D('-37.50'),
  859. member_id=1,
  860. membership_no=1,
  861. email=u'uat.yes@c3s.cc',
  862. token=u'ABCDEFGH',
  863. )
  864. DBSession.add(dues1)
  865. dues2 = Dues15Invoice(
  866. invoice_no=2,
  867. invoice_no_string=u'C3S-dues15-0002-S',
  868. invoice_date=date(2015, 10, 02),
  869. invoice_amount=D('17.25'),
  870. member_id=1,
  871. membership_no=1,
  872. email=u'uat.yes@c3s.cc',
  873. token=u'fa4wfjlasjfd',
  874. )
  875. dues2.is_reversal = True
  876. DBSession.add(dues2)
  877. dues3 = Dues15Invoice(
  878. invoice_no=3,
  879. invoice_no_string=u'C3S-dues15-0003',
  880. invoice_date=date(2015, 11, 25),
  881. invoice_amount=D('74.58'),
  882. member_id=1,
  883. membership_no=2,
  884. email=u'uat.yes@c3s.cc',
  885. token=u'Jleifjsw9e',
  886. )
  887. DBSession.add(dues3)
  888. dues4 = Dues15Invoice(
  889. invoice_no=4,
  890. invoice_no_string=u'C3S-dues15-0004-S',
  891. invoice_date=date(2015, 11, 27),
  892. invoice_amount=D('23.85'),
  893. member_id=1,
  894. membership_no=2,
  895. email=u'uat.yes@c3s.cc',
  896. token=u'f348h98sdf',
  897. )
  898. dues4.is_reversal = True
  899. DBSession.add(dues4)
  900. dues5 = Dues15Invoice(
  901. invoice_no=5,
  902. invoice_no_string=u'C3S-dues15-0005',
  903. invoice_date=date(2015, 11, 29),
  904. invoice_amount=D('12.89'),
  905. member_id=1,
  906. membership_no=3,
  907. email=u'uat.yes@c3s.cc',
  908. token=u'sgdfoiddfg',
  909. )
  910. DBSession.add(dues5)
  911. dues6 = Dues15Invoice(
  912. invoice_no=6,
  913. invoice_no_string=u'C3S-dues15-0006-S',
  914. invoice_date=date(2015, 11, 30),
  915. invoice_amount=D('77.79'),
  916. member_id=1,
  917. membership_no=3,
  918. email=u'uat.yes@c3s.cc',
  919. token=u'3o948n',
  920. )
  921. dues6.is_reversal = True
  922. DBSession.add(dues6)
  923. DBSession.flush()
  924. member1.set_dues15_payment(D('12.34'), date(2015, 10, 31))
  925. member2.set_dues15_payment(D('95.65'), date(2015, 11, 5))
  926. member3.set_dues15_payment(D('-85.12'), date(2015, 11, 30))
  927. def tearDown(self):
  928. self.session.close()
  929. self.session.remove()
  930. # os.remove('test_model_staff.db')
  931. def test_get_all(self):
  932. '''
  933. test get_all
  934. '''
  935. res = Dues15Invoice.get_all()
  936. self.assertEqual(len(res), 6)
  937. def test_get_by_invoice_no(self):
  938. '''
  939. test get_by_invoice_no
  940. '''
  941. res = Dues15Invoice.get_by_invoice_no(1)
  942. self.assertEqual(res.id, 1)
  943. def test_get_by_membership_no(self):
  944. '''
  945. test get_by_invoice_no
  946. '''
  947. res = Dues15Invoice.get_by_membership_no(1)
  948. self.assertEqual(res[0].id, 1) # is a list
  949. def test_get_max_invoice_no(self):
  950. '''
  951. test get_max_invoice_no
  952. '''
  953. res = Dues15Invoice.get_max_invoice_no()
  954. self.assertEqual(res, 6)
  955. def test_existing_dues15_token(self):
  956. """
  957. test check_for_existing_dues15_token
  958. """
  959. res = Dues15Invoice.check_for_existing_dues15_token(
  960. u'ABCDEFGH')
  961. self.assertEqual(res, True)
  962. res2 = Dues15Invoice.check_for_existing_dues15_token(
  963. u'ABCDEFGHIK0000000000')
  964. self.assertEqual(res2, False)
  965. def test_get_monthly_stats(self):
  966. """
  967. Test get_monthly_stats.
  968. """
  969. stats = Dues15Invoice.get_monthly_stats()
  970. self.assertEqual(len(stats), 2)
  971. self.assertEqual(stats[0]['month'], datetime(2015, 10, 1))
  972. self.assertAlmostEqual(stats[0]['amount_invoiced_normal'], D('-37.50'))
  973. self.assertAlmostEqual(
  974. stats[0]['amount_invoiced_reversal'], D('17.25'))
  975. self.assertAlmostEqual(stats[0]['amount_paid'], D('12.34'))
  976. self.assertEqual(stats[1]['month'], datetime(2015, 11, 1))
  977. self.assertAlmostEqual(stats[1]['amount_invoiced_normal'], D('87.47'))
  978. self.assertAlmostEqual(
  979. stats[1]['amount_invoiced_reversal'], D('101.64'))
  980. self.assertAlmostEqual(stats[1]['amount_paid'], D('10.53'))
  981. def test_decimality(self):
  982. """
  983. test the features of the 'amounts', esp. the format and persistence
  984. """
  985. # try to make another invoice with the same number
  986. def trigger_integrity_error_1():
  987. dues2 = Dues15Invoice(
  988. invoice_no=1,
  989. invoice_no_string=u'C3S-dues15-0001',
  990. invoice_date=date.today(),
  991. invoice_amount=unicode(D('-37.50').to_eng_string()),
  992. member_id=1,
  993. membership_no=1,
  994. email=u'uat.yes@c3s.cc',
  995. token=u'ABCDEFGH',
  996. )
  997. DBSession.add(dues2)
  998. DBSession.flush()
  999. self.assertRaises(IntegrityError, trigger_integrity_error_1)
  1000. self.session.rollback()
  1001. res = Dues15Invoice.get_all()
  1002. self.assertEqual(len(res), 6)
  1003. # try to make another invoice with the same string
  1004. def trigger_integrity_error_2():
  1005. dues2 = Dues15Invoice(
  1006. invoice_no=2,
  1007. invoice_no_string=u'C3S-dues15-0001',
  1008. invoice_date=date.today(),
  1009. invoice_amount=unicode(D('-37.50').to_eng_string()),
  1010. member_id=1,
  1011. membership_no=1,
  1012. email=u'uat.yes@c3s.cc',
  1013. token=u'ABCDEFGH',
  1014. )
  1015. DBSession.add(dues2)
  1016. DBSession.flush()
  1017. self.assertRaises(IntegrityError, trigger_integrity_error_2)
  1018. self.session.rollback()
  1019. res = Dues15Invoice.get_all()
  1020. self.assertEqual(len(res), 6)
  1021. # try to make another invoice with a non-decimal amount
  1022. # InvalidOperation: Invalid literal for Decimal: '-37.50.20'
  1023. def trigger_invalid_operation():
  1024. dues2 = Dues15Invoice(
  1025. invoice_no=5,
  1026. invoice_no_string=u'C3S-dues15-0002',
  1027. invoice_date=date.today(),
  1028. invoice_amount=unicode(D('-37.50.20').to_eng_string()),
  1029. member_id=1,
  1030. membership_no=1,
  1031. email=u'uat.yes@c3s.cc',
  1032. token=u'ABCDEFGH',
  1033. )
  1034. DBSession.add(dues2)
  1035. DBSession.flush()
  1036. self.assertRaises(InvalidOperation, trigger_invalid_operation)
  1037. # trigger_invalid_operation()
  1038. self.session.rollback()
  1039. res = Dues15Invoice.get_all()
  1040. self.assertEqual(len(res), 6)
  1041. # now really store a new Dues15Invoice
  1042. dues3 = Dues15Invoice(
  1043. invoice_no=7,
  1044. invoice_no_string=u'C3S-dues15-0002',
  1045. invoice_date=date.today(),
  1046. # invoice_amount=unicode(D('-37.50').to_eng_string()),
  1047. invoice_amount=D('-37.50').to_eng_string(),
  1048. member_id=1,
  1049. membership_no=1,
  1050. email=u'uat.yes@c3s.cc',
  1051. token=u'ABCDEFGH',
  1052. )
  1053. DBSession.add(dues3)
  1054. DBSession.flush()
  1055. res = Dues15Invoice.get_all()
  1056. self.assertEqual(len(res), 7)
  1057. self.assertEqual(dues3.id, 7)
  1058. class Dues16InvoiceModelTests(unittest.TestCase):
  1059. """
  1060. test the dues16 invoice model
  1061. """
  1062. def setUp(self):
  1063. self.config = testing.setUp()
  1064. self.config.include('pyramid_mailer.testing')
  1065. try:
  1066. DBSession.remove()
  1067. except:
  1068. pass
  1069. # engine = create_engine('sqlite:///test_model_staff.db')
  1070. engine = create_engine('sqlite://')
  1071. self.session = DBSession
  1072. self.session.configure(bind=engine)
  1073. Base.metadata.create_all(engine)
  1074. with transaction.manager:
  1075. member1 = C3sMember(
  1076. firstname=u'SomeFirstnäme',
  1077. lastname=u'SomeLastnäme',
  1078. email=u'some@shri.de',
  1079. address1=u"addr one",
  1080. address2=u"addr two",
  1081. postcode=u"12345",
  1082. city=u"Footown Mäh",
  1083. country=u"Foocountry",
  1084. locale=u"DE",
  1085. date_of_birth=date.today(),
  1086. email_is_confirmed=False,
  1087. email_confirm_code=u'ABCDEFGFOO',
  1088. password=u'arandompassword',
  1089. date_of_submission=date.today(),
  1090. membership_type=u'normal',
  1091. member_of_colsoc=True,
  1092. name_of_colsoc=u"GEMA",
  1093. num_shares=u'23',
  1094. )
  1095. DBSession.add(member1)
  1096. member2 = C3sMember(
  1097. firstname=u'Franziska',
  1098. lastname=u'Musterfrau',
  1099. email=u'some@shri.de',
  1100. address1=u"addr one",
  1101. address2=u"addr two",
  1102. postcode=u"12345",
  1103. city=u"Footown Mäh",
  1104. country=u"Foocountry",
  1105. locale=u"DE",
  1106. date_of_birth=date.today(),
  1107. email_is_confirmed=False,
  1108. email_confirm_code=u'ABCDEFGFO1',
  1109. password=u'arandompassword',
  1110. date_of_submission=date.today(),
  1111. membership_type=u'normal',
  1112. member_of_colsoc=False,
  1113. name_of_colsoc=u'',
  1114. num_shares=u'23',
  1115. )
  1116. DBSession.add(member2)
  1117. member3 = C3sMember(
  1118. firstname=u'Jane',
  1119. lastname=u'Somebody',
  1120. email=u'some@shri.de',
  1121. address1=u"addr one",
  1122. address2=u"addr two",
  1123. postcode=u"12345",
  1124. city=u"Footown Mäh",
  1125. country=u"Foocountry",
  1126. locale=u"DE",
  1127. date_of_birth=date.today(),
  1128. email_is_confirmed=False,
  1129. email_confirm_code=u'ABCDEFGFO2',
  1130. password=u'arandompassword',
  1131. date_of_submission=date.today(),
  1132. membership_type=u'normal',
  1133. member_of_colsoc=False,
  1134. name_of_colsoc=u'',
  1135. num_shares=u'23',
  1136. )
  1137. DBSession.add(member3)
  1138. dues1 = Dues16Invoice(
  1139. invoice_no=1,
  1140. invoice_no_string=u'C3S-dues16-0001',
  1141. invoice_date=date(2015, 10, 01),
  1142. invoice_amount=D('-37.50'),
  1143. member_id=1,
  1144. membership_no=1,
  1145. email=u'uat.yes@c3s.cc',
  1146. token=u'ABCDEFGH',
  1147. )
  1148. DBSession.add(dues1)
  1149. dues2 = Dues16Invoice(
  1150. invoice_no=2,
  1151. invoice_no_string=u'C3S-dues16-0002-S',
  1152. invoice_date=date(2015, 10, 02),
  1153. invoice_amount=D('17.25'),
  1154. member_id=1,
  1155. membership_no=1,
  1156. email=u'uat.yes@c3s.cc',
  1157. token=u'fa4wfjlasjfd',
  1158. )
  1159. dues2.is_reversal = True
  1160. DBSession.add(dues2)
  1161. dues3 = Dues16Invoice(
  1162. invoice_no=3,
  1163. invoice_no_string=u'C3S-dues16-0003',
  1164. invoice_date=date(2015, 11, 25),
  1165. invoice_amount=D('74.58'),
  1166. member_id=1,
  1167. membership_no=2,
  1168. email=u'uat.yes@c3s.cc',
  1169. token=u'Jleifjsw9e',
  1170. )
  1171. DBSession.add(dues3)
  1172. dues4 = Dues16Invoice(
  1173. invoice_no=4,
  1174. invoice_no_string=u'C3S-dues16-0004-S',
  1175. invoice_date=date(2015, 11, 27),
  1176. invoice_amount=D('23.85'),
  1177. member_id=1,
  1178. membership_no=2,
  1179. email=u'uat.yes@c3s.cc',
  1180. token=u'f348h98sdf',
  1181. )
  1182. dues4.is_reversal = True
  1183. DBSession.add(dues4)
  1184. dues5 = Dues16Invoice(
  1185. invoice_no=5,
  1186. invoice_no_string=u'C3S-dues16-0005',
  1187. invoice_date=date(2015, 11, 29),
  1188. invoice_amount=D('12.89'),
  1189. member_id=1,
  1190. membership_no=3,
  1191. email=u'uat.yes@c3s.cc',
  1192. token=u'sgdfoiddfg',
  1193. )
  1194. DBSession.add(dues5)
  1195. dues6 = Dues16Invoice(
  1196. invoice_no=6,
  1197. invoice_no_string=u'C3S-dues16-0006-S',
  1198. invoice_date=date(2015, 11, 30),
  1199. invoice_amount=D('77.79'),
  1200. member_id=1,
  1201. membership_no=3,
  1202. email=u'uat.yes@c3s.cc',
  1203. token=u'3o948n',
  1204. )
  1205. dues6.is_reversal = True
  1206. DBSession.add(dues6)
  1207. DBSession.flush()
  1208. member1.set_dues16_payment(D('12.34'), date(2015, 10, 31))
  1209. member2.set_dues16_payment(D('95.65'), date(2015, 11, 5))
  1210. member3.set_dues16_payment(D('-85.12'), date(2015, 11, 30))
  1211. def tearDown(self):
  1212. self.session.close()
  1213. self.session.remove()
  1214. # os.remove('test_model_staff.db')
  1215. def test_get_all(self):
  1216. '''
  1217. test get_all
  1218. '''
  1219. res = Dues16Invoice.get_all()
  1220. self.assertEqual(len(res), 6)
  1221. def test_get_by_invoice_no(self):
  1222. '''
  1223. test get_by_invoice_no
  1224. '''
  1225. res = Dues16Invoice.get_by_invoice_no(1)
  1226. self.assertEqual(res.id, 1)
  1227. def test_get_by_membership_no(self):
  1228. '''
  1229. test get_by_invoice_no
  1230. '''
  1231. res = Dues16Invoice.get_by_membership_no(1)
  1232. self.assertEqual(res[0].id, 1) # is a list
  1233. def test_get_max_invoice_no(self):
  1234. '''
  1235. test get_max_invoice_no
  1236. '''
  1237. res = Dues16Invoice.get_max_invoice_no()
  1238. self.assertEqual(res, 6)
  1239. def test_existing_dues16_token(self):
  1240. """
  1241. test check_for_existing_dues16_token
  1242. """
  1243. res = Dues16Invoice.check_for_existing_dues16_token(
  1244. u'ABCDEFGH')
  1245. self.assertEqual(res, True)
  1246. res2 = Dues16Invoice.check_for_existing_dues16_token(
  1247. u'ABCDEFGHIK0000000000')
  1248. self.assertEqual(res2, False)
  1249. def test_get_monthly_stats(self):
  1250. """
  1251. Test get_monthly_stats.
  1252. """
  1253. stats = Dues16Invoice.get_monthly_stats()
  1254. self.assertEqual(len(stats), 2)
  1255. self.assertEqual(stats[0]['month'], datetime(2015, 10, 1))
  1256. self.assertAlmostEqual(stats[0]['amount_invoiced_normal'], D('-37.50'))
  1257. self.assertAlmostEqual(
  1258. stats[0]['amount_invoiced_reversal'], D('17.25'))
  1259. self.assertAlmostEqual(stats[0]['amount_paid'], D('12.34'))
  1260. self.assertEqual(stats[1]['month'], datetime(2015, 11, 1))
  1261. self.assertAlmostEqual(stats[1]['amount_invoiced_normal'], D('87.47'))
  1262. self.assertAlmostEqual(
  1263. stats[1]['amount_invoiced_reversal'], D('101.64'))
  1264. self.assertAlmostEqual(stats[1]['amount_paid'], D('10.53'))
  1265. def test_decimality(self):
  1266. """
  1267. test the features of the 'amounts', esp. the format and persistence
  1268. """
  1269. # try to make another invoice with the same number
  1270. def trigger_integrity_error_1():
  1271. dues2 = Dues16Invoice(
  1272. invoice_no=1,
  1273. invoice_no_string=u'C3S-dues16-0001',
  1274. invoice_date=date.today(),
  1275. invoice_amount=unicode(D('-37.50').to_eng_string()),
  1276. member_id=1,
  1277. membership_no=1,
  1278. email=u'uat.yes@c3s.cc',
  1279. token=u'ABCDEFGH',
  1280. )
  1281. DBSession.add(dues2)
  1282. DBSession.flush()
  1283. self.assertRaises(IntegrityError, trigger_integrity_error_1)
  1284. self.session.rollback()
  1285. res = Dues16Invoice.get_all()
  1286. self.assertEqual(len(res), 6)
  1287. # try to make another invoice with the same string
  1288. def trigger_integrity_error_2():
  1289. dues2 = Dues16Invoice(
  1290. invoice_no=2,
  1291. invoice_no_string=u'C3S-dues16-0001',
  1292. invoice_date=date.today(),
  1293. invoice_amount=unicode(D('-37.50').to_eng_string()),
  1294. member_id=1,
  1295. membership_no=1,
  1296. email=u'uat.yes@c3s.cc',
  1297. token=u'ABCDEFGH',
  1298. )
  1299. DBSession.add(dues2)
  1300. DBSession.flush()
  1301. self.assertRaises(IntegrityError, trigger_integrity_error_2)
  1302. self.session.rollback()
  1303. res = Dues16Invoice.get_all()
  1304. self.assertEqual(len(res), 6)
  1305. # try to make another invoice with a non-decimal amount
  1306. # InvalidOperation: Invalid literal for Decimal: '-37.50.20'
  1307. def trigger_invalid_operation():
  1308. dues2 = Dues16Invoice(
  1309. invoice_no=5,
  1310. invoice_no_string=u'C3S-dues16-0002',
  1311. invoice_date=date.today(),
  1312. invoice_amount=unicode(D('-37.50.20').to_eng_string()),
  1313. member_id=1,
  1314. membership_no=1,
  1315. email=u'uat.yes@c3s.cc',
  1316. token=u'ABCDEFGH',
  1317. )
  1318. DBSession.add(dues2)
  1319. DBSession.flush()
  1320. self.assertRaises(InvalidOperation, trigger_invalid_operation)
  1321. # trigger_invalid_operation()
  1322. self.session.rollback()
  1323. res = Dues16Invoice.get_all()
  1324. self.assertEqual(len(res), 6)
  1325. # now really store a new Dues16Invoice
  1326. dues3 = Dues16Invoice(
  1327. invoice_no=7,
  1328. invoice_no_string=u'C3S-dues16-0002',
  1329. invoice_date=date.today(),
  1330. # invoice_amount=unicode(D('-37.50').to_eng_string()),
  1331. invoice_amount=D('-37.50').to_eng_string(),
  1332. member_id=1,
  1333. membership_no=1,
  1334. email=u'uat.yes@c3s.cc',
  1335. token=u'ABCDEFGH',
  1336. )
  1337. DBSession.add(dues3)
  1338. DBSession.flush()
  1339. res = Dues16Invoice.get_all()
  1340. self.assertEqual(len(res), 7)
  1341. self.assertEqual(dues3.id, 7)
  1342. class Dues17InvoiceModelTests(unittest.TestCase):
  1343. """
  1344. test the dues17 invoice model
  1345. """
  1346. def setUp(self):
  1347. self.config = testing.setUp()
  1348. self.config.include('pyramid_mailer.testing')
  1349. try:
  1350. DBSession.remove()
  1351. except:
  1352. pass
  1353. # engine = create_engine('sqlite:///test_model_staff.db')
  1354. engine = create_engine('sqlite://')
  1355. self.session = DBSession
  1356. self.session.configure(bind=engine)
  1357. Base.metadata.create_all(engine)
  1358. with transaction.manager:
  1359. member1 = C3sMember(
  1360. firstname=u'SomeFirstnäme',
  1361. lastname=u'SomeLastnäme',
  1362. email=u'some@shri.de',
  1363. address1=u"addr one",
  1364. address2=u"addr two",
  1365. postcode=u"12345",
  1366. city=u"Footown Mäh",
  1367. country=u"Foocountry",
  1368. locale=u"DE",
  1369. date_of_birth=date.today(),
  1370. email_is_confirmed=False,
  1371. email_confirm_code=u'ABCDEFGFOO',
  1372. password=u'arandompassword',
  1373. date_of_submission=date.today(),
  1374. membership_type=u'normal',
  1375. member_of_colsoc=True,
  1376. name_of_colsoc=u"GEMA",
  1377. num_shares=u'23',
  1378. )
  1379. DBSession.add(member1)
  1380. member2 = C3sMember(
  1381. firstname=u'Franziska',
  1382. lastname=u'Musterfrau',
  1383. email=u'some@shri.de',
  1384. address1=u"addr one",
  1385. address2=u"addr two",
  1386. postcode=u"12345",
  1387. city=u"Footown Mäh",
  1388. country=u"Foocountry",
  1389. locale=u"DE",
  1390. date_of_birth=date.today(),
  1391. email_is_confirmed=False,
  1392. email_confirm_code=u'ABCDEFGFO1',
  1393. password=u'arandompassword',
  1394. date_of_submission=date.today(),
  1395. membership_type=u'normal',
  1396. member_of_colsoc=False,
  1397. name_of_colsoc=u'',
  1398. num_shares=u'23',
  1399. )
  1400. DBSession.add(member2)
  1401. member3 = C3sMember(
  1402. firstname=u'Jane',
  1403. lastname=u'Somebody',
  1404. email=u'some@shri.de',
  1405. address1=u"addr one",
  1406. address2=u"addr two",
  1407. postcode=u"12345",
  1408. city=u"Footown Mäh",
  1409. country=u"Foocountry",
  1410. locale=u"DE",
  1411. date_of_birth=date.today(),
  1412. email_is_confirmed=False,
  1413. email_confirm_code=u'ABCDEFGFO2',
  1414. password=u'arandompassword',
  1415. date_of_submission=date.today(),
  1416. membership_type=u'normal',
  1417. member_of_colsoc=False,
  1418. name_of_colsoc=u'',
  1419. num_shares=u'23',
  1420. )
  1421. DBSession.add(member3)
  1422. dues1 = Dues17Invoice(
  1423. invoice_no=1,
  1424. invoice_no_string=u'C3S-dues17-0001',
  1425. invoice_date=date(2015, 10, 01),
  1426. invoice_amount=D('-37.50'),
  1427. member_id=1,
  1428. membership_no=1,
  1429. email=u'uat.yes@c3s.cc',
  1430. token=u'ABCDEFGH',
  1431. )
  1432. DBSession.add(dues1)
  1433. dues2 = Dues17Invoice(
  1434. invoice_no=2,
  1435. invoice_no_string=u'C3S-dues17-0002-S',
  1436. invoice_date=date(2015, 10, 02),
  1437. invoice_amount=D('16.25'),
  1438. member_id=1,
  1439. membership_no=1,
  1440. email=u'uat.yes@c3s.cc',
  1441. token=u'fa4wfjlasjfd',
  1442. )
  1443. dues2.is_reversal = True
  1444. DBSession.add(dues2)
  1445. dues3 = Dues17Invoice(
  1446. invoice_no=3,
  1447. invoice_no_string=u'C3S-dues17-0003',
  1448. invoice_date=date(2015, 11, 25),
  1449. invoice_amount=D('74.58'),
  1450. member_id=1,
  1451. membership_no=2,
  1452. email=u'uat.yes@c3s.cc',
  1453. token=u'Jleifjsw9e',
  1454. )
  1455. DBSession.add(dues3)
  1456. dues4 = Dues17Invoice(
  1457. invoice_no=4,
  1458. invoice_no_string=u'C3S-dues17-0004-S',
  1459. invoice_date=date(2015, 11, 27),
  1460. invoice_amount=D('23.85'),
  1461. member_id=1,
  1462. membership_no=2,
  1463. email=u'uat.yes@c3s.cc',
  1464. token=u'f348h98sdf',
  1465. )
  1466. dues4.is_reversal = True
  1467. DBSession.add(dues4)
  1468. dues5 = Dues17Invoice(
  1469. invoice_no=5,
  1470. invoice_no_string=u'C3S-dues17-0005',
  1471. invoice_date=date(2015, 11, 29),
  1472. invoice_amount=D('12.89'),
  1473. member_id=1,
  1474. membership_no=3,
  1475. email=u'uat.yes@c3s.cc',
  1476. token=u'sgdfoiddfg',
  1477. )
  1478. DBSession.add(dues5)
  1479. dues6 = Dues17Invoice(
  1480. invoice_no=6,
  1481. invoice_no_string=u'C3S-dues17-0006-S',
  1482. invoice_date=date(2015, 11, 30),
  1483. invoice_amount=D('77.79'),
  1484. member_id=1,
  1485. membership_no=3,
  1486. email=u'uat.yes@c3s.cc',
  1487. token=u'3o948n',
  1488. )
  1489. dues6.is_reversal = True
  1490. DBSession.add(dues6)
  1491. DBSession.flush()
  1492. member1.set_dues17_payment(D('12.34'), date(2015, 10, 31))
  1493. member2.set_dues17_payment(D('95.65'), date(2015, 11, 5))
  1494. member3.set_dues17_payment(D('-85.12'), date(2015, 11, 30))
  1495. def tearDown(self):
  1496. self.session.close()
  1497. self.session.remove()
  1498. # os.remove('test_model_staff.db')
  1499. def test_get_all(self):
  1500. '''
  1501. test get_all
  1502. '''
  1503. res = Dues17Invoice.get_all()
  1504. self.assertEqual(len(res), 6)
  1505. def test_get_by_invoice_no(self):
  1506. '''
  1507. test get_by_invoice_no
  1508. '''
  1509. res = Dues17Invoice.get_by_invoice_no(1)
  1510. self.assertEqual(res.id, 1)
  1511. def test_get_by_membership_no(self):
  1512. '''
  1513. test get_by_invoice_no
  1514. '''
  1515. res = Dues17Invoice.get_by_membership_no(1)
  1516. self.assertEqual(res[0].id, 1) # is a list
  1517. def test_get_max_invoice_no(self):
  1518. '''
  1519. test get_max_invoice_no
  1520. '''
  1521. res = Dues17Invoice.get_max_invoice_no()
  1522. self.assertEqual(res, 6)
  1523. def test_existing_dues17_token(self):
  1524. """
  1525. test check_for_existing_dues17_token
  1526. """
  1527. res = Dues17Invoice.check_for_existing_dues17_token(
  1528. u'ABCDEFGH')
  1529. self.assertEqual(res, True)
  1530. res2 = Dues17Invoice.check_for_existing_dues17_token(
  1531. u'ABCDEFGHIK0000000000')
  1532. self.assertEqual(res2, False)
  1533. def test_get_monthly_stats(self):
  1534. """
  1535. Test get_monthly_stats.
  1536. """
  1537. stats = Dues17Invoice.get_monthly_stats()
  1538. self.assertEqual(len(stats), 2)
  1539. self.assertEqual(stats[0]['month'], datetime(2015, 10, 1))
  1540. self.assertAlmostEqual(stats[0]['amount_invoiced_normal'], D('-37.50'))
  1541. self.assertAlmostEqual(
  1542. stats[0]['amount_invoiced_reversal'], D('16.25'))
  1543. self.assertAlmostEqual(stats[0]['amount_paid'], D('12.34'))
  1544. self.assertEqual(stats[1]['month'], datetime(2015, 11, 1))
  1545. self.assertAlmostEqual(stats[1]['amount_invoiced_normal'], D('87.47'))
  1546. self.assertAlmostEqual(
  1547. stats[1]['amount_invoiced_reversal'], D('101.64'))
  1548. self.assertAlmostEqual(stats[1]['amount_paid'], D('10.53'))
  1549. def test_decimality(self):
  1550. """
  1551. test the features of the 'amounts', esp. the format and persistence
  1552. """
  1553. # try to make another invoice with the same number
  1554. def trigger_integrity_error_1():
  1555. dues2 = Dues17Invoice(
  1556. invoice_no=1,
  1557. invoice_no_string=u'C3S-dues17-0001',
  1558. invoice_date=date.today(),
  1559. invoice_amount=unicode(D('-37.50').to_eng_string()),
  1560. member_id=1,
  1561. membership_no=1,
  1562. email=u'uat.yes@c3s.cc',
  1563. token=u'ABCDEFGH',
  1564. )
  1565. DBSession.add(dues2)
  1566. DBSession.flush()
  1567. self.assertRaises(IntegrityError, trigger_integrity_error_1)
  1568. self.session.rollback()
  1569. res = Dues17Invoice.get_all()
  1570. self.assertEqual(len(res), 6)
  1571. # try to make another invoice with the same string
  1572. def trigger_integrity_error_2():
  1573. dues2 = Dues17Invoice(
  1574. invoice_no=2,
  1575. invoice_no_string=u'C3S-dues17-0001',
  1576. invoice_date=date.today(),
  1577. invoice_amount=unicode(D('-37.50').to_eng_string()),
  1578. member_id=1,
  1579. membership_no=1,
  1580. email=u'uat.yes@c3s.cc',
  1581. token=u'ABCDEFGH',
  1582. )
  1583. DBSession.add(dues2)
  1584. DBSession.flush()
  1585. self.assertRaises(IntegrityError, trigger_integrity_error_2)
  1586. self.session.rollback()
  1587. res = Dues17Invoice.get_all()
  1588. self.assertEqual(len(res), 6)
  1589. # try to make another invoice with a non-decimal amount
  1590. # InvalidOperation: Invalid literal for Decimal: '-37.50.20'
  1591. def trigger_invalid_operation():
  1592. dues2 = Dues17Invoice(
  1593. invoice_no=5,
  1594. invoice_no_string=u'C3S-dues17-0002',
  1595. invoice_date=date.today(),
  1596. invoice_amount=unicode(D('-37.50.20').to_eng_string()),
  1597. member_id=1,
  1598. membership_no=1,
  1599. email=u'uat.yes@c3s.cc',
  1600. token=u'ABCDEFGH',
  1601. )
  1602. DBSession.add(dues2)
  1603. DBSession.flush()
  1604. self.assertRaises(InvalidOperation, trigger_invalid_operation)
  1605. # trigger_invalid_operation()
  1606. self.session.rollback()
  1607. res = Dues17Invoice.get_all()
  1608. self.assertEqual(len(res), 6)
  1609. # now really store a new Dues17Invoice
  1610. dues3 = Dues17Invoice(
  1611. invoice_no=7,
  1612. invoice_no_string=u'C3S-dues17-0002',
  1613. invoice_date=date.today(),
  1614. # invoice_amount=unicode(D('-37.50').to_eng_string()),
  1615. invoice_amount=D('-37.50').to_eng_string(),
  1616. member_id=1,
  1617. membership_no=1,
  1618. email=u'uat.yes@c3s.cc',
  1619. token=u'ABCDEFGH',
  1620. )
  1621. DBSession.add(dues3)
  1622. DBSession.flush()
  1623. res = Dues17Invoice.get_all()
  1624. self.assertEqual(len(res), 7)
  1625. self.assertEqual(dues3.id, 7)