11"""Tests for Entity Framework Core format (parser + generator)."""
2+
23from __future__ import annotations
34
45import sys
3536
3637# ── Parser Tests ──
3738
39+
3840def test_ef_parse_table_name ():
3941 """Parser should extract table name from [Table] attribute."""
4042 parser = EntityFrameworkParser ()
@@ -124,24 +126,26 @@ def test_ef_parse_no_models():
124126
125127def test_ef_parse_multiple_tables ():
126128 """Parser should handle multiple entity classes."""
127- cs = '\n ' .join ([
128- '[Table("users")]' ,
129- 'public class User' ,
130- '{' ,
131- ' [Key]' ,
132- ' public int Id { get; set; }' ,
133- ' public string Name { get; set; }' ,
134- '}' ,
135- '' ,
136- '[Table("posts")]' ,
137- 'public class Post' ,
138- '{' ,
139- ' [Key]' ,
140- ' public int Id { get; set; }' ,
141- ' [Required]' ,
142- ' public string Title { get; set; }' ,
143- '}' ,
144- ])
129+ cs = "\n " .join (
130+ [
131+ '[Table("users")]' ,
132+ "public class User" ,
133+ "{" ,
134+ " [Key]" ,
135+ " public int Id { get; set; }" ,
136+ " public string Name { get; set; }" ,
137+ "}" ,
138+ "" ,
139+ '[Table("posts")]' ,
140+ "public class Post" ,
141+ "{" ,
142+ " [Key]" ,
143+ " public int Id { get; set; }" ,
144+ " [Required]" ,
145+ " public string Title { get; set; }" ,
146+ "}" ,
147+ ]
148+ )
145149 parser = EntityFrameworkParser ()
146150 schema = parser .parse (cs )
147151 assert len (schema .tables ) == 2
@@ -171,16 +175,27 @@ def test_ef_parse_nullable_type():
171175
172176# ── Generator Tests ──
173177
178+
174179def test_ef_generate_simple ():
175180 """Generator should produce valid C# class structure."""
176181 from schemaforge .ir import Column , ColumnType , Schema , Table
177- schema = Schema (tables = [
178- Table (name = "users" , columns = [
179- Column (name = "id" , type = ColumnType .INTEGER , primary_key = True ),
180- Column (name = "name" , type = ColumnType .STRING , nullable = False ,
181- type_args = {"length" : 100 }),
182- ])
183- ])
182+
183+ schema = Schema (
184+ tables = [
185+ Table (
186+ name = "users" ,
187+ columns = [
188+ Column (name = "id" , type = ColumnType .INTEGER , primary_key = True ),
189+ Column (
190+ name = "name" ,
191+ type = ColumnType .STRING ,
192+ nullable = False ,
193+ type_args = {"length" : 100 },
194+ ),
195+ ],
196+ )
197+ ]
198+ )
184199 gen = EntityFrameworkGenerator ()
185200 output = gen .generate (schema )
186201 assert "using System" in output
@@ -193,12 +208,18 @@ def test_ef_generate_simple():
193208def test_ef_generate_datetime ():
194209 """Generator should map DATETIME to DateTime type."""
195210 from schemaforge .ir import Column , ColumnType , Schema , Table
196- schema = Schema (tables = [
197- Table (name = "events" , columns = [
198- Column (name = "id" , type = ColumnType .INTEGER , primary_key = True ),
199- Column (name = "ts" , type = ColumnType .DATETIME ),
200- ])
201- ])
211+
212+ schema = Schema (
213+ tables = [
214+ Table (
215+ name = "events" ,
216+ columns = [
217+ Column (name = "id" , type = ColumnType .INTEGER , primary_key = True ),
218+ Column (name = "ts" , type = ColumnType .DATETIME ),
219+ ],
220+ )
221+ ]
222+ )
202223 gen = EntityFrameworkGenerator ()
203224 output = gen .generate (schema )
204225 assert "DateTime" in output
@@ -207,13 +228,22 @@ def test_ef_generate_datetime():
207228def test_ef_generate_decimal ():
208229 """Generator should add [Column] for decimal precision/scale."""
209230 from schemaforge .ir import Column , ColumnType , Schema , Table
210- schema = Schema (tables = [
211- Table (name = "products" , columns = [
212- Column (name = "id" , type = ColumnType .INTEGER , primary_key = True ),
213- Column (name = "price" , type = ColumnType .DECIMAL ,
214- type_args = {"precision" : 12 , "scale" : 4 }),
215- ])
216- ])
231+
232+ schema = Schema (
233+ tables = [
234+ Table (
235+ name = "products" ,
236+ columns = [
237+ Column (name = "id" , type = ColumnType .INTEGER , primary_key = True ),
238+ Column (
239+ name = "price" ,
240+ type = ColumnType .DECIMAL ,
241+ type_args = {"precision" : 12 , "scale" : 4 },
242+ ),
243+ ],
244+ )
245+ ]
246+ )
217247 gen = EntityFrameworkGenerator ()
218248 output = gen .generate (schema )
219249 assert "decimal" in output
@@ -223,6 +253,7 @@ def test_ef_generate_decimal():
223253def test_ef_generate_empty ():
224254 """Empty schema should produce just the boilerplate."""
225255 from schemaforge .ir import Schema
256+
226257 gen = EntityFrameworkGenerator ()
227258 output = gen .generate (Schema ())
228259 assert "using System" in output
@@ -231,6 +262,7 @@ def test_ef_generate_empty():
231262
232263# ── Roundtrip / Conversion Tests ──
233264
265+
234266def test_ef_to_sql_roundtrip ():
235267 """C# entities should convert to valid SQL."""
236268 sql = convert_schema (SAMPLE_CS , "ef" , "sql" )
0 commit comments