github.com/CiscoM31/godata@v1.0.10/providers/mysql_test.go (about) 1 package mysql 2 3 /* 4 5 import ( 6 . "godata" 7 ) 8 9 func TestCoffeeDatabase(t *testing.T) { 10 11 cxnParams := &MySQLConnectionParams{ 12 Hostname: "localhost", 13 Port: "3306", 14 Database: "Coffee", 15 Username: "dev", 16 Password: "dev", 17 } 18 provider := BuildMySQLProvider(cxnParams, "Coffee") 19 20 roaster := provider.ExposeEntity("roaster", "Roaster") 21 roaster.ExposeKey("id", "RoasterID", GoDataInt32) 22 roaster.ExposePrimitive("name", "Name", GoDataString) 23 roaster.ExposePrimitive("location", "Location", GoDataString) 24 roaster.ExposePrimitive("website", "Website", GoDataString) 25 roasterSet := provider.ExposeEntitySet(roaster, "Roasters") 26 27 variety := provider.ExposeEntity("variety", "Variety") 28 variety.ExposeKey("id", "VarietyID", GoDataInt32) 29 variety.ExposePrimitive("name", "Name", GoDataString) 30 varietySet := provider.ExposeEntitySet(variety, "Varieties") 31 32 roastLevel := provider.ExposeEntity("roast_level", "RoastLevel") 33 roastLevel.ExposeKey("id", "RoastLevelID", GoDataInt32) 34 roastLevel.ExposePrimitive("order", "Order", GoDataInt32) 35 roastLevel.ExposePrimitive("name", "Name", GoDataString) 36 roastLevel.ExposePrimitive("qualifier", "Qualifier", GoDataString) 37 roastLevelSet := provider.ExposeEntitySet(roastLevel, "RoastLevels") 38 39 process := provider.ExposeEntity("process", "Process") 40 process.ExposeKey("id", "ProcessID", GoDataInt32) 41 process.ExposePrimitive("name", "Name", GoDataString) 42 processSet := provider.ExposeEntitySet(process, "Processes") 43 44 bean := provider.ExposeEntity("bean", "Bean") 45 bean.ExposeKey("id", "BeanID", GoDataInt32) 46 bean.ExposePrimitive("country", "Country", GoDataString) 47 bean.ExposePrimitive("region", "Region", GoDataString) 48 bean.ExposePrimitive("min_elevation", "MinElevation", GoDataInt32) 49 bean.ExposePrimitive("max_elevation", "MaxElevation", GoDataInt32) 50 beanSet := provider.ExposeEntitySet(bean, "Beans") 51 52 // map many beans to one roaster 53 provider.ExposeManyToOne(bean, roaster, "roaster_id", "Roaster", "Beans") 54 provider.ExposeManyToOne(bean, roastLevel, "roaster_level_id", "RoastLevel", "Beans") 55 provider.ExposeManyToOne(bean, process, "process_id", "Process", "Beans") 56 provider.ExposeManyToMany(bean, variety, "bean_variety_map", "Varieties", "Beans") 57 provider.BindProperty(beanSet, roasterSet, "Roaster", "Roaster", "Beans", "Beans") 58 provider.BindProperty(beanSet, roastLevelSet, "RoastLevel", "RoastLevel", "Beans", "Beans") 59 provider.BindProperty(beanSet, processSet, "Process", "Process", "Beans", "Beans") 60 provider.BindProperty(beanSet, varietySet, "Varieties", "Varieties", "Beans", "Beans") 61 62 actual, err := provider.BuildMetadata().String() 63 64 expected := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 65 expected += "<edmx:Edmx xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\" Version=\"4.0\">\n" 66 expected += " <edmx:DataServices>\n" 67 expected += " <Schema Namespace=\"Coffee\">\n" 68 expected += " <EntityContainer Name=\"Coffee\">\n" 69 expected += " <EntitySet Name=\"Varieties\" EntityType=\"Coffee.Variety\">\n" 70 expected += " <NavigationPropertyBinding Path=\"Beans\" Target=\"Beans\"></NavigationPropertyBinding>\n" 71 expected += " </EntitySet>\n" 72 expected += " <EntitySet Name=\"RoastLevels\" EntityType=\"Coffee.RoastLevel\">\n" 73 expected += " <NavigationPropertyBinding Path=\"Beans\" Target=\"Beans\"></NavigationPropertyBinding>\n" 74 expected += " </EntitySet>\n" 75 expected += " <EntitySet Name=\"Processes\" EntityType=\"Coffee.Process\">\n" 76 expected += " <NavigationPropertyBinding Path=\"Beans\" Target=\"Beans\"></NavigationPropertyBinding>\n" 77 expected += " </EntitySet>\n" 78 expected += " <EntitySet Name=\"Beans\" EntityType=\"Coffee.Bean\">\n" 79 expected += " <NavigationPropertyBinding Path=\"Roaster\" Target=\"Roaster\"></NavigationPropertyBinding>\n" 80 expected += " <NavigationPropertyBinding Path=\"RoastLevel\" Target=\"RoastLevel\"></NavigationPropertyBinding>\n" 81 expected += " <NavigationPropertyBinding Path=\"Process\" Target=\"Process\"></NavigationPropertyBinding>\n" 82 expected += " <NavigationPropertyBinding Path=\"Varieties\" Target=\"Varieties\"></NavigationPropertyBinding>\n" 83 expected += " </EntitySet>\n" 84 expected += " <EntitySet Name=\"Roasters\" EntityType=\"Coffee.Roaster\">\n" 85 expected += " <NavigationPropertyBinding Path=\"Beans\" Target=\"Beans\"></NavigationPropertyBinding>\n" 86 expected += " </EntitySet>\n" 87 expected += " </EntityContainer>\n" 88 expected += " <EntityType Name=\"Roaster\">\n" 89 expected += " <Key>\n" 90 expected += " <PropertyRef Name=\"RoasterID\"></PropertyRef>\n" 91 expected += " </Key>\n" 92 expected += " <Property Name=\"RoasterID\" Type=\"Edm.Int32\"></Property>\n" 93 expected += " <Property Name=\"Name\" Type=\"Edm.String\"></Property>\n" 94 expected += " <Property Name=\"Location\" Type=\"Edm.String\"></Property>\n" 95 expected += " <Property Name=\"Website\" Type=\"Edm.String\"></Property>\n" 96 expected += " <NavigationProperty Name=\"Beans\" Type=\"Collection(Coffee.Bean)\" Partner=\"Roaster\">\n" 97 expected += " <ReferentialConstraint Property=\"RoasterID\" ReferencedProperty=\"RoasterID\"></ReferentialConstraint>\n" 98 expected += " </NavigationProperty>\n" 99 expected += " </EntityType>\n" 100 expected += " <EntityType Name=\"Variety\">\n" 101 expected += " <Key>\n" 102 expected += " <PropertyRef Name=\"VarietyID\"></PropertyRef>\n" 103 expected += " </Key>\n" 104 expected += " <Property Name=\"VarietyID\" Type=\"Edm.Int32\"></Property>\n" 105 expected += " <Property Name=\"Name\" Type=\"Edm.String\"></Property>\n" 106 expected += " <NavigationProperty Name=\"Beans\" Type=\"Collection(Coffee.Bean)\" Partner=\"Varieties\"></NavigationProperty>\n" 107 expected += " </EntityType>\n" 108 expected += " <EntityType Name=\"RoastLevel\">\n" 109 expected += " <Key>\n" 110 expected += " <PropertyRef Name=\"RoastLevelID\"></PropertyRef>\n" 111 expected += " </Key>\n" 112 expected += " <Property Name=\"RoastLevelID\" Type=\"Edm.Int32\"></Property>\n" 113 expected += " <Property Name=\"Order\" Type=\"Edm.Int32\"></Property>\n" 114 expected += " <Property Name=\"Name\" Type=\"Edm.String\"></Property>\n" 115 expected += " <Property Name=\"Qualifier\" Type=\"Edm.String\"></Property>\n" 116 expected += " <NavigationProperty Name=\"Beans\" Type=\"Collection(Coffee.Bean)\" Partner=\"RoastLevel\">\n" 117 expected += " <ReferentialConstraint Property=\"RoastLevelID\" ReferencedProperty=\"RoastLevelID\"></ReferentialConstraint>\n" 118 expected += " </NavigationProperty>\n" 119 expected += " </EntityType>\n" 120 expected += " <EntityType Name=\"Process\">\n" 121 expected += " <Key>\n" 122 expected += " <PropertyRef Name=\"ProcessID\"></PropertyRef>\n" 123 expected += " </Key>\n" 124 expected += " <Property Name=\"ProcessID\" Type=\"Edm.Int32\"></Property>\n" 125 expected += " <Property Name=\"Name\" Type=\"Edm.String\"></Property>\n" 126 expected += " <NavigationProperty Name=\"Beans\" Type=\"Collection(Coffee.Bean)\" Partner=\"Process\">\n" 127 expected += " <ReferentialConstraint Property=\"ProcessID\" ReferencedProperty=\"ProcessID\"></ReferentialConstraint>\n" 128 expected += " </NavigationProperty>\n" 129 expected += " </EntityType>\n" 130 expected += " <EntityType Name=\"Bean\">\n" 131 expected += " <Key>\n" 132 expected += " <PropertyRef Name=\"BeanID\"></PropertyRef>\n" 133 expected += " </Key>\n" 134 expected += " <Property Name=\"BeanID\" Type=\"Edm.Int32\"></Property>\n" 135 expected += " <Property Name=\"Country\" Type=\"Edm.String\"></Property>\n" 136 expected += " <Property Name=\"Region\" Type=\"Edm.String\"></Property>\n" 137 expected += " <Property Name=\"MinElevation\" Type=\"Edm.Int32\"></Property>\n" 138 expected += " <Property Name=\"MaxElevation\" Type=\"Edm.Int32\"></Property>\n" 139 expected += " <Property Name=\"RoasterID\" Type=\"Edm.Int32\"></Property>\n" 140 expected += " <Property Name=\"RoastLevelID\" Type=\"Edm.Int32\"></Property>\n" 141 expected += " <Property Name=\"ProcessID\" Type=\"Edm.Int32\"></Property>\n" 142 expected += " <NavigationProperty Name=\"Roaster\" Type=\"Coffee.Roaster\" Partner=\"Beans\"></NavigationProperty>\n" 143 expected += " <NavigationProperty Name=\"RoastLevel\" Type=\"Coffee.RoastLevel\" Partner=\"Beans\"></NavigationProperty>\n" 144 expected += " <NavigationProperty Name=\"Process\" Type=\"Coffee.Process\" Partner=\"Beans\"></NavigationProperty>\n" 145 expected += " <NavigationProperty Name=\"Varieties\" Type=\"Collection(Coffee.Variety)\" Partner=\"Beans\"></NavigationProperty>\n" 146 expected += " </EntityType>\n" 147 expected += " </Schema>\n" 148 expected += " </edmx:DataServices>\n" 149 expected += "</edmx:Edmx>" 150 151 if err != nil { 152 t.Error(err) 153 } 154 155 if actual != expected { 156 t.Error("Expected: \n"+expected, "\n\nGot: \n"+actual) 157 } 158 159 } 160 161 func BenchmarkCoffeeDatabase(b *testing.B) { 162 for i := 0; i < b.N; i++ { 163 164 cxnParams := &MySQLConnectionParams{ 165 Hostname: "localhost", 166 Port: "3306", 167 Database: "Coffee", 168 Username: "dev", 169 Password: "dev", 170 } 171 provider := BuildMySQLProvider(cxnParams, "Coffee") 172 173 roaster := provider.ExposeEntity("roaster", "Roaster") 174 roaster.ExposeKey("id", "RoasterID", GoDataInt32) 175 roaster.ExposePrimitive("name", "Name", GoDataString) 176 roaster.ExposePrimitive("location", "Location", GoDataString) 177 roaster.ExposePrimitive("website", "Website", GoDataString) 178 roasterSet := provider.ExposeEntitySet(roaster, "Roasters") 179 180 variety := provider.ExposeEntity("variety", "Variety") 181 variety.ExposeKey("id", "VarietyID", GoDataInt32) 182 variety.ExposePrimitive("name", "Name", GoDataString) 183 varietySet := provider.ExposeEntitySet(variety, "Varieties") 184 185 roastLevel := provider.ExposeEntity("roast_level", "RoastLevel") 186 roastLevel.ExposeKey("id", "RoastLevelID", GoDataInt32) 187 roastLevel.ExposePrimitive("order", "Order", GoDataInt32) 188 roastLevel.ExposePrimitive("name", "Name", GoDataString) 189 roastLevel.ExposePrimitive("qualifier", "Qualifier", GoDataString) 190 roastLevelSet := provider.ExposeEntitySet(roastLevel, "RoastLevels") 191 192 process := provider.ExposeEntity("process", "Process") 193 process.ExposeKey("id", "ProcessID", GoDataInt32) 194 process.ExposePrimitive("name", "Name", GoDataString) 195 processSet := provider.ExposeEntitySet(process, "Processes") 196 197 bean := provider.ExposeEntity("bean", "Bean") 198 bean.ExposeKey("id", "BeanID", GoDataInt32) 199 bean.ExposePrimitive("country", "Country", GoDataString) 200 bean.ExposePrimitive("region", "Region", GoDataString) 201 bean.ExposePrimitive("min_elevation", "MinElevation", GoDataInt32) 202 bean.ExposePrimitive("max_elevation", "MaxElevation", GoDataInt32) 203 beanSet := provider.ExposeEntitySet(bean, "Beans") 204 205 // map many beans to one roaster 206 provider.ExposeManyToOne(bean, roaster, "roaster_id", "Roaster", "Beans") 207 provider.ExposeManyToOne(bean, roastLevel, "roaster_level_id", "RoastLevel", "Beans") 208 provider.ExposeManyToOne(bean, process, "process_id", "Process", "Beans") 209 provider.ExposeManyToMany(bean, variety, "bean_variety_map", "Varieties", "Beans") 210 provider.BindProperty(beanSet, roasterSet, "Roaster", "Roaster", "Beans", "Beans") 211 provider.BindProperty(beanSet, roastLevelSet, "RoastLevel", "RoastLevel", "Beans", "Beans") 212 provider.BindProperty(beanSet, processSet, "Process", "Process", "Beans", "Beans") 213 provider.BindProperty(beanSet, varietySet, "Varieties", "Varieties", "Beans", "Beans") 214 215 provider.BuildMetadata() 216 } 217 } 218 */