/
conftest.py
973 lines (698 loc) · 25.1 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
import json
import os
import random
import time
from datetime import datetime
import django_webtest
import pytest
import webtest
from dbtemplates.models import Template
from rest_framework.test import APIClient
from splinter.driver import DriverAPI
from pbn_api.models import Language
from bpp.models.szablondlaopisubibliograficznego import SzablonDlaOpisuBibliograficznego
from bpp.util import get_fixture
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from model_mommy import mommy
from bpp import const
from bpp.const import GR_WPROWADZANIE_DANYCH, TO_AUTOR
from bpp.fixtures import get_openaccess_data
from bpp.models import (
Autor_Dyscyplina,
Dyscyplina_Naukowa,
Wydawca,
Zewnetrzna_Baza_Danych,
)
from bpp.models.autor import Autor, Funkcja_Autora, Tytul
from bpp.models.patent import Patent
from bpp.models.praca_doktorska import Praca_Doktorska
from bpp.models.praca_habilitacyjna import Praca_Habilitacyjna
from bpp.models.struktura import Jednostka, Uczelnia, Wydzial
from bpp.models.system import (
Charakter_Formalny,
Jezyk,
Status_Korekty,
Typ_KBN,
Typ_Odpowiedzialnosci,
)
from bpp.models.wydawnictwo_ciagle import Wydawnictwo_Ciagle
from bpp.models.wydawnictwo_zwarte import Wydawnictwo_Zwarte
from bpp.models.zrodlo import Zrodlo
from django_bpp.selenium_util import wait_for_page_load, wait_for_websocket_connection
NORMAL_DJANGO_USER_LOGIN = "test_login_bpp"
NORMAL_DJANGO_USER_PASSWORD = "test_password"
from asgi_live_server import asgi_live_server # noqa
from bpp.tests.util import setup_mommy
setup_mommy()
def current_rok():
return datetime.now().date().year
@pytest.fixture
def rok():
return current_rok()
@pytest.fixture
def dyscyplina1(db):
return Dyscyplina_Naukowa.objects.get_or_create(
nazwa="memetyka stosowana", kod="1.1"
)[0]
@pytest.fixture
def dyscyplina2(db):
return Dyscyplina_Naukowa.objects.get_or_create(
nazwa="druga dyscyplina", kod="2.2"
)[0]
@pytest.fixture
def dyscyplina3(db):
return Dyscyplina_Naukowa.objects.get_or_create(
nazwa="trzecia dyscyplina", kod="3.3"
)[0]
@pytest.fixture
def normal_django_user(request, db, django_user_model): # , django_username_field):
"""
A normal Django user
"""
try:
obj = django_user_model.objects.get(username=NORMAL_DJANGO_USER_LOGIN)
except django_user_model.DoesNotExist:
obj = django_user_model.objects.create_user(
username=NORMAL_DJANGO_USER_LOGIN, password=NORMAL_DJANGO_USER_PASSWORD
)
def fin():
obj.delete()
return obj
def _preauth_session_id_helper(
username,
password,
client,
browser,
asgi_live_server, # noqa
django_user_model,
django_username_field,
):
res = client.login(username=username, password=password)
assert res is True
with wait_for_page_load(browser):
browser.visit(asgi_live_server.url + "/non-existant-url")
browser.cookies.add({"sessionid": client.cookies["sessionid"].value})
browser.authorized_user = django_user_model.objects.get(
**{django_username_field: username}
)
# with wait_for_page_load(browser):
# browser.reload()
return browser
@pytest.fixture
def preauth_browser(
normal_django_user,
client,
browser,
asgi_live_server, # noqa
django_user_model,
django_username_field,
):
browser = _preauth_session_id_helper(
NORMAL_DJANGO_USER_LOGIN,
NORMAL_DJANGO_USER_PASSWORD,
client,
browser,
asgi_live_server,
django_user_model,
django_username_field,
)
yield browser
browser.quit()
@pytest.fixture
def preauth_asgi_browser(preauth_browser, transactional_db, asgi_live_server): # noqa
with wait_for_page_load(preauth_browser):
preauth_browser.visit(asgi_live_server.url)
wait_for_websocket_connection(preauth_browser)
return preauth_browser
@pytest.fixture
def admin_browser(
admin_user,
client,
browser,
asgi_live_server, # noqa
django_user_model,
django_username_field,
transactional_db,
) -> DriverAPI:
browser = _preauth_session_id_helper(
"admin",
"password",
client,
browser,
asgi_live_server,
django_user_model,
django_username_field,
)
browser.driver.set_window_size(1920, 1600)
yield browser
browser.execute_script("window.onbeforeunload = function(e) {};")
browser.quit()
@pytest.fixture(scope="function")
def uczelnia(db):
return Uczelnia.objects.get_or_create(
skrot="TE",
nazwa="Testowa uczelnia",
)[0]
@pytest.mark.django_db
def _wydzial_maker(nazwa, skrot, uczelnia, **kwargs):
return Wydzial.objects.get_or_create(
uczelnia=uczelnia, skrot=skrot, nazwa=nazwa, **kwargs
)[0]
@pytest.mark.django_db
@pytest.fixture
def wydzial_maker(db):
return _wydzial_maker
@pytest.mark.django_db
@pytest.fixture(scope="function")
def wydzial(uczelnia, db):
return _wydzial_maker(uczelnia=uczelnia, skrot="W1", nazwa="Wydział Testowy I")
def _autor_maker(imiona, nazwisko, tytul="dr", **kwargs):
tytul = Tytul.objects.get(skrot=tytul)
return Autor.objects.get_or_create(
tytul=tytul, imiona=imiona, nazwisko=nazwisko, **kwargs
)[0]
@pytest.fixture
def autor_maker(db):
return _autor_maker
@pytest.fixture(scope="function")
def autor_jan_nowak(db, tytuly):
return _autor_maker(imiona="Jan", nazwisko="Nowak")
@pytest.fixture(scope="function")
def autor(db, tytuly):
return mommy.make(Autor)
@pytest.fixture(scope="function")
def typ_odpowiedzialnosci_autor(db):
return Typ_Odpowiedzialnosci.objects.get_or_create(
skrot="aut.", nazwa="autor", typ_ogolny=TO_AUTOR
)
@pytest.fixture(scope="function")
def autor_jan_kowalski(db, tytuly):
return _autor_maker(imiona="Jan", nazwisko="Kowalski", tytul="prof. dr hab. med.")
def _jednostka_maker(nazwa, skrot, wydzial, **kwargs):
ret = Jednostka.objects.get_or_create(
nazwa=nazwa, skrot=skrot, wydzial=wydzial, uczelnia=wydzial.uczelnia, **kwargs
)[0]
ret.refresh_from_db()
return ret
JEDNOSTKA_UCZELNI = "Jednostka Uczelni"
@pytest.mark.django_db
@pytest.fixture(scope="function")
def jednostka(wydzial, db):
return _jednostka_maker(JEDNOSTKA_UCZELNI, skrot="Jedn. Ucz.", wydzial=wydzial)
JEDNOSTKA_PODRZEDNA = "Jednostka P-rzedna"
@pytest.mark.django_db
@pytest.fixture(scope="function")
def jednostka_podrzedna(jednostka):
return _jednostka_maker(
JEDNOSTKA_PODRZEDNA, skrot="JP", wydzial=jednostka.wydzial, parent=jednostka
)
@pytest.mark.django_db
@pytest.fixture(scope="function")
def druga_jednostka(wydzial, db):
return _jednostka_maker(
"Druga Jednostka Uczelni", skrot="Dr. Jedn. Ucz.", wydzial=wydzial
)
@pytest.mark.django_db
@pytest.fixture(scope="function")
def obca_jednostka(wydzial):
return _jednostka_maker(
"Obca Jednostka",
skrot="OJ",
wydzial=wydzial,
skupia_pracownikow=False,
zarzadzaj_automatycznie=False,
widoczna=False,
wchodzi_do_raportow=False,
)
@pytest.fixture
def jednostka_maker():
return _jednostka_maker
def _zrodlo_maker(nazwa, skrot, **kwargs):
return mommy.make(Zrodlo, nazwa=nazwa, skrot=skrot, **kwargs)
@pytest.fixture
def zrodlo_maker():
return _zrodlo_maker
@pytest.fixture(scope="function")
def zrodlo(db):
return _zrodlo_maker(nazwa="Testowe Źródło", skrot="Test. Źr.")
def set_default(varname, value, dct):
if varname not in dct:
dct[varname] = value
def _wydawnictwo_maker(klass, **kwargs):
if "rok" not in kwargs:
kwargs["rok"] = current_rok()
c = time.time()
kl = str(klass).split(".")[-1].replace("'>", "")
kw_wyd = dict(
tytul=f"Tytul {kl} {c}",
tytul_oryginalny=f"Tytul oryginalny {kl} {c}",
uwagi=f"Uwagi {kl} {c}",
szczegoly=f"Szczegóły {kl} {c}",
)
if klass == Patent:
del kw_wyd["tytul"]
for key, value in kw_wyd.items():
set_default(key, value, kwargs)
return mommy.make(klass, **kwargs)
def _wydawnictwo_ciagle_maker(**kwargs):
if "zrodlo" not in kwargs:
set_default(
"zrodlo",
_zrodlo_maker(
nazwa="Źrodło Ciągłego Wydawnictwa", skrot="Źród. Ciąg. Wyd."
),
kwargs,
)
set_default("informacje", "zrodlo-informacje", kwargs)
set_default("issn", "123-IS-SN-34", kwargs)
if "jezyk" not in kwargs:
jezyk = Jezyk.objects.get(nazwa__icontains="polski")
jezyk.pbn_uid = Language.objects.get_or_create(
pk="pol",
language={
"de": "Polnisch",
"en": "Polish",
"pl": "polski",
"639-1": "pl",
"639-2": "pol",
"wikiUrl": "https://en.wikipedia.org/wiki/Polish_language",
},
)[0]
kwargs["jezyk"] = jezyk
return _wydawnictwo_maker(Wydawnictwo_Ciagle, **kwargs)
def wydawnictwo_ciagle_maker(db):
return _wydawnictwo_ciagle_maker
@pytest.fixture(scope="function")
@pytest.mark.django_db
def wydawnictwo_ciagle(
jezyki, charaktery_formalne, typy_kbn, statusy_korekt, typy_odpowiedzialnosci
):
ret = _wydawnictwo_ciagle_maker()
return ret
def _zwarte_base_maker(klass, **kwargs):
if klass not in [Praca_Doktorska, Praca_Habilitacyjna, Patent]:
set_default("liczba_znakow_wydawniczych", 31337, kwargs)
set_default("informacje", "zrodlo-informacje dla zwarte", kwargs)
if klass not in [Patent]:
set_default("miejsce_i_rok", "Lublin %s" % current_rok(), kwargs)
set_default("wydawnictwo", "Wydawnictwo FOLIUM", kwargs)
set_default("isbn", "123-IS-BN-34", kwargs)
set_default("redakcja", "Redakcja", kwargs)
return _wydawnictwo_maker(klass, **kwargs)
def _zwarte_maker(**kwargs):
return _zwarte_base_maker(Wydawnictwo_Zwarte, **kwargs)
@pytest.fixture(scope="function")
def wydawnictwo_zwarte(
jezyki, charaktery_formalne, typy_kbn, statusy_korekt, typy_odpowiedzialnosci
):
"""
:rtype: bpp.models.Wydawnictwo_Zwarte
"""
return _zwarte_maker(tytul_oryginalny="Wydawnictwo Zwarte ĄćłłóńŹ")
@pytest.fixture
def zwarte_maker(db):
return _zwarte_maker
@pytest.fixture
def wydawca(db):
return Wydawca.objects.get_or_create(nazwa="Wydawca Testowy")[0]
@pytest.fixture
def alias_wydawcy(wydawca):
return Wydawca.objects.create(nazwa="Drugi taki tam", alias_dla=wydawca)
def _habilitacja_maker(**kwargs):
Charakter_Formalny.objects.get_or_create(nazwa="Praca habilitacyjna", skrot="H")
return _zwarte_base_maker(Praca_Habilitacyjna, **kwargs)
@pytest.fixture(scope="function")
def habilitacja(jednostka, db, charaktery_formalne, jezyki, typy_odpowiedzialnosci):
return _habilitacja_maker(
tytul_oryginalny="Praca habilitacyjna", jednostka=jednostka
)
@pytest.fixture
def praca_habilitacyjna(
habilitacja,
) -> Praca_Habilitacyjna:
return habilitacja
@pytest.fixture
def habilitacja_maker(db):
return _habilitacja_maker
def _doktorat_maker(**kwargs):
return _zwarte_base_maker(Praca_Doktorska, **kwargs)
@pytest.fixture(scope="function")
def doktorat(jednostka, charaktery_formalne, jezyki, typy_odpowiedzialnosci):
return _doktorat_maker(tytul_oryginalny="Praca doktorska", jednostka=jednostka)
@pytest.fixture(scope="function")
def praca_doktorska(doktorat):
return doktorat
@pytest.fixture
def doktorat_maker(db):
return _doktorat_maker
def _patent_maker(**kwargs):
return _zwarte_base_maker(Patent, **kwargs)
@pytest.fixture
def patent(db, typy_odpowiedzialnosci, jezyki, charaktery_formalne, typy_kbn):
return _patent_maker(tytul_oryginalny="PATENT!")
@pytest.fixture
def patent_maker(db):
return _patent_maker
# def _lookup_fun(klass):
# def fun(skrot):
# return klass.objects.filter(skrot=skrot)
# return fun
#
#
# typ_kbn = _lookup_fun(Typ_KBN)
# jezyk = _lookup_fun(Jezyk)
# charakter = _lookup_fun(Charakter_Formalny)
@pytest.fixture(scope="function")
def webtest_app(request):
wtm = django_webtest.WebTestMixin()
wtm._patch_settings()
request.addfinalizer(wtm._unpatch_settings)
return django_webtest.DjangoTestApp()
def _webtest_login(webtest_app, username, password, login_form="login_form"):
form = webtest_app.get(reverse(login_form)).form
form["username"] = username # normal_django_user.username
form["password"] = password # NORMAL_DJANGO_USER_PASSWORD
res = form.submit().maybe_follow()
assert res.context["user"].username == username # normal_django_user.username
return webtest_app
@pytest.fixture(scope="function")
def wprowadzanie_danych_user(normal_django_user):
from django.contrib.auth.models import Group
grp = Group.objects.get_or_create(name=GR_WPROWADZANIE_DANYCH)[0]
normal_django_user.groups.add(grp)
return normal_django_user
@pytest.fixture(scope="function")
def app(webtest_app, normal_django_user) -> webtest.app.TestApp:
return _webtest_login(
webtest_app, NORMAL_DJANGO_USER_LOGIN, NORMAL_DJANGO_USER_PASSWORD
)
@pytest.fixture(scope="function")
def wd_app(webtest_app, wprowadzanie_danych_user):
return _webtest_login(
webtest_app, NORMAL_DJANGO_USER_LOGIN, NORMAL_DJANGO_USER_PASSWORD
)
@pytest.fixture(scope="function")
def admin_app(webtest_app, admin_user):
"""
:rtype: django_webtest.DjangoTestApp
"""
return _webtest_login(webtest_app, "admin", "password")
def fixture(name):
return json.load(
open(
os.path.abspath(
os.path.join(os.path.dirname(__file__), "../bpp", "fixtures", name)
),
"rb",
)
)
@pytest.fixture(scope="function")
def typy_odpowiedzialnosci(db):
for elem in fixture("typ_odpowiedzialnosci_v2.json"):
Typ_Odpowiedzialnosci.objects.get_or_create(pk=elem["pk"], **elem["fields"])
return {x.skrot: x for x in Typ_Odpowiedzialnosci.objects.all()}
@pytest.fixture(scope="function")
def tytuly():
for elem in fixture("tytul.json"):
Tytul.objects.get_or_create(pk=elem["pk"], **elem["fields"])
@pytest.fixture(scope="function")
def jezyki(db):
pl, created = Jezyk.objects.get_or_create(pk=1, skrot="pol.", nazwa="polski")
pl.skrot_dla_pbn = "PL"
pl.save()
assert pl.pk == 1
ang, created = Jezyk.objects.get_or_create(pk=2, skrot="ang.", nazwa="angielski")
ang.skrot_dla_pbn = "EN"
ang.save()
assert ang.pk == 2
for elem in fixture("jezyk.json"):
Jezyk.objects.get_or_create(**elem["fields"])
return {jezyk.skrot: jezyk for jezyk in Jezyk.objects.all()}
@pytest.fixture(scope="function")
def charaktery_formalne():
Charakter_Formalny.objects.all().delete()
for elem in fixture("charakter_formalny.json"):
Charakter_Formalny.objects.get_or_create(pk=elem["pk"], **elem["fields"])
chf_ksp = Charakter_Formalny.objects.get(skrot="KSP")
chf_ksp.rodzaj_pbn = const.RODZAJ_PBN_KSIAZKA
chf_ksp.charakter_ogolny = const.CHARAKTER_OGOLNY_KSIAZKA
chf_ksp.charakter_sloty = const.CHARAKTER_SLOTY_KSIAZKA
chf_ksp.nazwa_w_primo = "Książka"
chf_ksp.save()
chf_roz = Charakter_Formalny.objects.get(skrot="ROZ")
chf_roz.rodzaj_pbn = const.RODZAJ_PBN_ROZDZIAL
chf_ksp.charakter_ogolny = const.CHARAKTER_OGOLNY_ROZDZIAL
chf_roz.charakter_sloty = const.CHARAKTER_SLOTY_ROZDZIAL
chf_roz.save()
return {x.skrot: x for x in Charakter_Formalny.objects.all()}
@pytest.fixture(scope="function")
def ksiazka_polska(charaktery_formalne):
return charaktery_formalne["KSP"]
@pytest.fixture(scope="function")
def artykul_w_czasopismie(charaktery_formalne):
return charaktery_formalne["AC"]
@pytest.fixture(scope="function")
def typy_kbn():
for elem in fixture("typ_kbn.json"):
Typ_KBN.objects.get_or_create(pk=elem["pk"], **elem["fields"])
@pytest.fixture(scope="function")
def statusy_korekt(db):
for elem in fixture("status_korekty.json"):
Status_Korekty.objects.get_or_create(pk=elem["pk"], **elem["fields"])
return {status.nazwa: status for status in Status_Korekty.objects.all()}
@pytest.fixture(scope="function")
def przed_korekta(statusy_korekt):
return statusy_korekt["przed korektą"]
@pytest.fixture(scope="function")
def po_korekcie(statusy_korekt):
return statusy_korekt["po korekcie"]
@pytest.fixture(scope="function")
def w_trakcie_korekty(statusy_korekt):
return statusy_korekt["w trakcie korekty"]
@pytest.fixture(scope="function")
def funkcje_autorow():
for elem in get_fixture("funkcja_autora").values():
Funkcja_Autora.objects.get_or_create(**elem)
@pytest.fixture(scope="function")
def standard_data(
typy_odpowiedzialnosci,
tytuly,
jezyki,
charaktery_formalne,
typy_kbn,
statusy_korekt,
funkcje_autorow,
):
class StandardData:
@classmethod
def clean(self):
for model in (
Typ_Odpowiedzialnosci,
Tytul,
Jezyk,
Charakter_Formalny,
Typ_KBN,
Status_Korekty,
Funkcja_Autora,
):
model.objects.all().delete()
return StandardData
@pytest.mark.django_db
@pytest.fixture(scope="function")
def openaccess_data():
from django.contrib.contenttypes.models import ContentType
for model_name, skrot, nazwa in get_openaccess_data():
klass = ContentType.objects.get_by_natural_key("bpp", model_name).model_class()
klass.objects.get_or_create(nazwa=nazwa, skrot=skrot)
@pytest.fixture(scope="function")
def wydawnictwo_ciagle_z_autorem(
wydawnictwo_ciagle, autor_jan_kowalski, jednostka, typy_odpowiedzialnosci
) -> Wydawnictwo_Ciagle:
wydawnictwo_ciagle.dodaj_autora(autor_jan_kowalski, jednostka)
return wydawnictwo_ciagle
@pytest.fixture(scope="function")
def wydawnictwo_zwarte_z_autorem(wydawnictwo_zwarte, autor_jan_kowalski, jednostka):
wydawnictwo_zwarte.dodaj_autora(autor_jan_kowalski, jednostka)
return wydawnictwo_zwarte
@pytest.fixture(scope="function")
def wydawnictwo_ciagle_z_dwoma_autorami(
wydawnictwo_ciagle_z_autorem, autor_jan_nowak, jednostka, typy_odpowiedzialnosci
):
wydawnictwo_ciagle_z_autorem.dodaj_autora(autor_jan_nowak, jednostka)
return wydawnictwo_ciagle_z_autorem
def pytest_configure():
from django.conf import settings
if hasattr(settings, "RAVEN_CONFIG"):
del settings.RAVEN_CONFIG # setattr(settings, "RAVEN_CONFIG", None)
settings.TESTING = True
settings.CELERY_ALWAYS_EAGER = True
settings.CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
from bpp.models.cache import Autorzy, Rekord
Rekord._meta.managed = True
Autorzy._meta.managed = True
# @pytest.fixture(scope="session")
# def splinter_driver_kwargs(splinter_webdriver):
# if splinter_webdriver == "remote":
# from selenium import webdriver
# chrome_op = webdriver.ChromeOptions()
# chrome_op.add_argument("--disable-extensions")
# chrome_op.add_argument("--disable-extensions-file-access-check")
# chrome_op.add_argument("--disable-extensions-http-throttling")
# chrome_op.add_argument("--disable-infobars")
# chrome_op.add_argument("--enable-automation")
# chrome_op.add_argument("--start-maximized")
# chrome_op.add_experimental_option('prefs', {
# 'credentials_enable_service': False,
# 'profile': {
# 'password_manager_enabled': False
# }
# })
# return {'browser': "chrome",
# "desired_capabilities": chrome_op.to_capabilities()}
collect_ignore = [os.path.join(os.path.dirname(__file__), "media")]
import os
import pytest
@pytest.fixture
def denorms():
from denorm import denorms
yield denorms
@pytest.fixture
def praca_z_dyscyplina(wydawnictwo_ciagle_z_autorem, dyscyplina1, rok, db, denorms):
wydawnictwo_ciagle_z_autorem.punkty_kbn = 5
wydawnictwo_ciagle_z_autorem.save()
wca = wydawnictwo_ciagle_z_autorem.autorzy_set.first()
Autor_Dyscyplina.objects.create(
autor=wca.autor, rok=wca.rekord.rok, dyscyplina_naukowa=dyscyplina1
)
wca.dyscyplina_naukowa = dyscyplina1
wca.save()
denorms.flush()
return wydawnictwo_ciagle_z_autorem
@pytest.fixture
def api_client(client):
return APIClient()
@pytest.fixture
def baza_wos():
return Zewnetrzna_Baza_Danych.objects.get_or_create(
nazwa="Web of Science", skrot="WOS"
)[0]
from asgi_live_server import asgi_live_server # noqa
@pytest.fixture
def wydawnictwo_zwarte_przed_korekta(statusy_korekt):
return mommy.make(
Wydawnictwo_Zwarte, status_korekty=statusy_korekt["przed korektą"]
)
@pytest.fixture
def wydawnictwo_zwarte_w_trakcie_korekty(statusy_korekt):
return mommy.make(
Wydawnictwo_Zwarte, status_korekty=statusy_korekt["w trakcie korekty"]
)
@pytest.fixture
def wydawnictwo_zwarte_po_korekcie(statusy_korekt):
return mommy.make(Wydawnictwo_Zwarte, status_korekty=statusy_korekt["po korekcie"])
@pytest.fixture
def ksiazka(wydawnictwo_zwarte, ksiazka_polska) -> "Wydawnictwo_Zwarte":
wydawnictwo_zwarte.charakter_formalny = ksiazka_polska
wydawnictwo_zwarte.save()
return wydawnictwo_zwarte
@pytest.fixture
def artykul(wydawnictwo_ciagle, artykul_w_czasopismie):
wydawnictwo_ciagle.charakter_formalny = artykul_w_czasopismie
wydawnictwo_ciagle.save()
return wydawnictwo_ciagle
def gen_kod_dyscypliny_func():
top = random.randint(1, 8)
bottom = random.randint(1, 500)
return f"{top}.{bottom}"
mommy.generators.add(
"bpp.models.dyscyplina_naukowa.KodDyscyplinyField", gen_kod_dyscypliny_func
)
@pytest.fixture
def autor_z_dyscyplina(autor_jan_nowak, dyscyplina1, rok) -> Autor_Dyscyplina:
return Autor_Dyscyplina.objects.get_or_create(
autor=autor_jan_nowak, dyscyplina_naukowa=dyscyplina1, rok=rok
)[0]
#
# Monkeypatch fixture-teardown to allow TRUNCATE
#
from django.core.management import call_command
from django.db import connections
from django.test import TransactionTestCase
def _fixture_teardown(self):
# Allow TRUNCATE ... CASCADE and don't emit the post_migrate signal
# when flushing only a subset of the apps
for db_name in self._databases_names(include_mirrors=False):
# Flush the database
inhibit_post_migrate = (
self.available_apps is not None
or ( # Inhibit the post_migrate signal when using serialized
# rollback to avoid trying to recreate the serialized data.
self.serialized_rollback
and hasattr(connections[db_name], "_test_serialized_contents")
)
)
call_command(
"flush",
verbosity=0,
interactive=False,
database=db_name,
reset_sequences=False,
# In the real TransactionTestCase this is conditionally set to False.
allow_cascade=True,
inhibit_post_migrate=inhibit_post_migrate,
)
TransactionTestCase._fixture_teardown = _fixture_teardown
def pytest_collection_modifyitems(items):
# Dodaj marker "selenium" dla wszystkich testów uzywających fikstur 'browser'
# lub 'admin_browser', aby można było szybko uruchamiać wyłacznie te testy
# lub nie uruchamiać ich:
flaky_test = pytest.mark.flaky(reruns=5)
for item in items:
fixtures = getattr(item, "fixturenames", ())
if "browser" in fixtures or "admin_browser" in fixtures:
item.add_marker("selenium")
item.add_marker(flaky_test)
@pytest.fixture
def szablony():
dirname = os.path.dirname(__file__)
def template_n(elem):
return f"{dirname}/../bpp/templates/{elem}"
def create_template(Template, name):
Template.objects.create(
name=name,
content=open(template_n(name)).read(),
)
def instaluj_szablony():
create_template(Template, "opis_bibliograficzny.html")
create_template(Template, "browse/praca_tabela.html")
SzablonDlaOpisuBibliograficznego.objects.create(
model=None,
template=Template.objects.get(name="opis_bibliograficzny.html"),
)
instaluj_szablony()
return Template.objects
import pytest
@pytest.fixture(scope="session")
def django_db_setup(django_db_setup, django_db_blocker):
from denorm import denorms
with django_db_blocker.unblock():
denorms.install_triggers()
@pytest.fixture(scope="function")
def django_db_setup(django_db_setup, django_db_blocker): # noqa
from denorm import denorms
with django_db_blocker.unblock():
denorms.install_triggers()
@pytest.fixture(scope="class")
def django_db_setup(django_db_setup, django_db_blocker): # noqa
from denorm import denorms
with django_db_blocker.unblock():
denorms.install_triggers()
# https://github.com/pytest-dev/pytest-splinter/issues/158
# AttributeError: module 'splinter.driver.webdriver.firefox' has no attribute 'WebDriverElement'
from pytest_splinter.webdriver_patches import patch_webdriver
@pytest.fixture(scope="session")
def browser_patches():
patch_webdriver()