|
| 1 | +import datajoint as dj |
| 2 | +from packaging import version |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +from . import PREFIX, schema, schema_simple, schema_advanced |
| 6 | + |
| 7 | +namespace = locals() |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(scope="session") |
| 11 | +def connection_root(): |
| 12 | + """Root user database connection.""" |
| 13 | + dj.config["safemode"] = False |
| 14 | + connection = dj.Connection( |
| 15 | + host=os.getenv("DJ_HOST"), |
| 16 | + user=os.getenv("DJ_USER"), |
| 17 | + password=os.getenv("DJ_PASS"), |
| 18 | + ) |
| 19 | + yield connection |
| 20 | + dj.config["safemode"] = True |
| 21 | + connection.close() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="session") |
| 25 | +def connection_test(connection_root): |
| 26 | + """Test user database connection.""" |
| 27 | + database = f"{PREFIX}%%" |
| 28 | + credentials = dict( |
| 29 | + host=os.getenv("DJ_HOST"), user="datajoint", password="datajoint" |
| 30 | + ) |
| 31 | + permission = "ALL PRIVILEGES" |
| 32 | + |
| 33 | + # Create MySQL users |
| 34 | + if version.parse( |
| 35 | + connection_root.query("select @@version;").fetchone()[0] |
| 36 | + ) >= version.parse("8.0.0"): |
| 37 | + # create user if necessary on mysql8 |
| 38 | + connection_root.query( |
| 39 | + f""" |
| 40 | + CREATE USER IF NOT EXISTS '{credentials["user"]}'@'%%' |
| 41 | + IDENTIFIED BY '{credentials["password"]}'; |
| 42 | + """ |
| 43 | + ) |
| 44 | + connection_root.query( |
| 45 | + f""" |
| 46 | + GRANT {permission} ON `{database}`.* |
| 47 | + TO '{credentials["user"]}'@'%%'; |
| 48 | + """ |
| 49 | + ) |
| 50 | + else: |
| 51 | + # grant permissions. For MySQL 5.7 this also automatically creates user |
| 52 | + # if not exists |
| 53 | + connection_root.query( |
| 54 | + f""" |
| 55 | + GRANT {permission} ON `{database}`.* |
| 56 | + TO '{credentials["user"]}'@'%%' |
| 57 | + IDENTIFIED BY '{credentials["password"]}'; |
| 58 | + """ |
| 59 | + ) |
| 60 | + |
| 61 | + connection = dj.Connection(**credentials) |
| 62 | + yield connection |
| 63 | + connection_root.query(f"""DROP USER `{credentials["user"]}`""") |
| 64 | + connection.close() |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture(scope="module") |
| 68 | +def schema_any(connection_test): |
| 69 | + schema_any = dj.Schema( |
| 70 | + PREFIX + "_test1", schema.__dict__, connection=connection_test |
| 71 | + ) |
| 72 | + schema_any(schema.TTest) |
| 73 | + schema_any(schema.TTest2) |
| 74 | + schema_any(schema.TTest3) |
| 75 | + schema_any(schema.NullableNumbers) |
| 76 | + schema_any(schema.TTestExtra) |
| 77 | + schema_any(schema.TTestNoExtra) |
| 78 | + schema_any(schema.Auto) |
| 79 | + schema_any(schema.User) |
| 80 | + schema_any(schema.Subject) |
| 81 | + schema_any(schema.Language) |
| 82 | + schema_any(schema.Experiment) |
| 83 | + schema_any(schema.Trial) |
| 84 | + schema_any(schema.Ephys) |
| 85 | + schema_any(schema.Image) |
| 86 | + schema_any(schema.UberTrash) |
| 87 | + schema_any(schema.UnterTrash) |
| 88 | + schema_any(schema.SimpleSource) |
| 89 | + schema_any(schema.SigIntTable) |
| 90 | + schema_any(schema.SigTermTable) |
| 91 | + schema_any(schema.DjExceptionName) |
| 92 | + schema_any(schema.ErrorClass) |
| 93 | + schema_any(schema.DecimalPrimaryKey) |
| 94 | + schema_any(schema.IndexRich) |
| 95 | + schema_any(schema.ThingA) |
| 96 | + schema_any(schema.ThingB) |
| 97 | + schema_any(schema.ThingC) |
| 98 | + schema_any(schema.Parent) |
| 99 | + schema_any(schema.Child) |
| 100 | + schema_any(schema.ComplexParent) |
| 101 | + schema_any(schema.ComplexChild) |
| 102 | + schema_any(schema.SubjectA) |
| 103 | + schema_any(schema.SessionA) |
| 104 | + schema_any(schema.SessionStatusA) |
| 105 | + schema_any(schema.SessionDateA) |
| 106 | + schema_any(schema.Stimulus) |
| 107 | + schema_any(schema.Longblob) |
| 108 | + yield schema_any |
| 109 | + schema_any.drop() |
| 110 | + |
| 111 | + |
| 112 | +@pytest.fixture(scope="module") |
| 113 | +def schema_simp(connection_test): |
| 114 | + schema = dj.Schema( |
| 115 | + PREFIX + "_relational", schema_simple.__dict__, connection=connection_test |
| 116 | + ) |
| 117 | + schema(schema_simple.IJ) |
| 118 | + schema(schema_simple.JI) |
| 119 | + schema(schema_simple.A) |
| 120 | + schema(schema_simple.B) |
| 121 | + schema(schema_simple.L) |
| 122 | + schema(schema_simple.D) |
| 123 | + schema(schema_simple.E) |
| 124 | + schema(schema_simple.F) |
| 125 | + schema(schema_simple.F) |
| 126 | + schema(schema_simple.DataA) |
| 127 | + schema(schema_simple.DataB) |
| 128 | + schema(schema_simple.Website) |
| 129 | + schema(schema_simple.Profile) |
| 130 | + schema(schema_simple.Website) |
| 131 | + schema(schema_simple.TTestUpdate) |
| 132 | + schema(schema_simple.ArgmaxTest) |
| 133 | + schema(schema_simple.ReservedWord) |
| 134 | + schema(schema_simple.OutfitLaunch) |
| 135 | + yield schema |
| 136 | + schema.drop() |
| 137 | + |
| 138 | + |
| 139 | +@pytest.fixture(scope="module") |
| 140 | +def schema_adv(connection_test): |
| 141 | + schema = dj.Schema( |
| 142 | + PREFIX + "_advanced", schema_advanced.__dict__, connection=connection_test |
| 143 | + ) |
| 144 | + schema(schema_advanced.Person) |
| 145 | + schema(schema_advanced.Parent) |
| 146 | + schema(schema_advanced.Subject) |
| 147 | + schema(schema_advanced.Prep) |
| 148 | + schema(schema_advanced.Slice) |
| 149 | + schema(schema_advanced.Cell) |
| 150 | + schema(schema_advanced.InputCell) |
| 151 | + schema(schema_advanced.LocalSynapse) |
| 152 | + schema(schema_advanced.GlobalSynapse) |
| 153 | + yield schema |
| 154 | + schema.drop() |
0 commit comments