Skip to content

Commit 8948f99

Browse files
committed
Test coverage for new behavior.
1 parent 9381163 commit 8948f99

10 files changed

Lines changed: 148 additions & 34 deletions

File tree

PSql.Deploy.Engine.Tests/Seeds/SeedLoaderTests.cs

Lines changed: 111 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ public void Load_NullSeed()
1919
});
2020
}
2121

22-
[Test]
23-
public void Load_MissingModuleName()
24-
{
25-
var seed = WithSeed("MissingModuleName");
26-
27-
Should.Throw<FormatException>(() =>
28-
{
29-
SeedLoader.Load(seed);
30-
});
31-
}
32-
3322
[Test]
3423
public void Load_Empty()
3524
{
@@ -57,40 +46,141 @@ public void Load_Typical()
5746
loadedSeed.Seed .ShouldBeSameAs(seed);
5847
loadedSeed.Modules.AssignTo(out var modules);
5948

60-
modules.Length .ShouldBe(3);
49+
modules.Length .ShouldBe(4);
50+
6151
modules[0].Name .ShouldBe("(init)");
52+
modules[0].WorkerId .ShouldBe(0);
6253
modules[0].Provides .ShouldBeEmpty();
6354
modules[0].Requires .ShouldBeEmpty();
6455
modules[0].Batches .ShouldHaveSingleItem();
6556
modules[0].Batches[0].ShouldBe(""
6657
+ "PRINT 'This is in the initial module.';" + Eol
6758
);
6859

69-
modules[1].Name .ShouldBe("a");
70-
modules[1].Provides .ShouldBe(ImmutableArray.Create("x", "y"));
60+
modules[1].Name .ShouldBe("init-worker");
61+
modules[1].WorkerId .ShouldBe(-1);
62+
modules[1].Provides .ShouldBeEmpty();
7163
modules[1].Requires .ShouldBeEmpty();
7264
modules[1].Batches .ShouldHaveSingleItem();
7365
modules[1].Batches[0].ShouldBe(""
66+
+ "--# WORKER: all" + Eol
67+
+ "PRINT 'This is in an all-worker module.';" + Eol
68+
);
69+
70+
modules[2].Name .ShouldBe("a");
71+
modules[2].WorkerId .ShouldBe(0);
72+
modules[2].Provides .ShouldBe(ImmutableArray.Create("x", "y", "z"));
73+
modules[2].Requires .ShouldBeEmpty();
74+
modules[2].Batches .ShouldHaveSingleItem();
75+
modules[2].Batches[0].ShouldBe(""
7476
+ "--# PROVIDES: x y" + Eol
7577
+ "--# provides: y x" + Eol
7678
+ "--# Provides:" + Eol
7779
+ "PRINT 'This is in module a.';" + Eol
7880
+ "PRINT 'The value of ''foo'' is bar.';" + Eol
81+
+ "-- The value of foo is bar." + Eol
7982
);
8083
// TODO: I don't think the magic comment should be included in the batch text.
8184

82-
modules[2].Name .ShouldBe("b");
83-
modules[2].Provides .ShouldBeEmpty();
84-
modules[2].Requires .ShouldBe(ImmutableArray.Create("x", "y"));
85-
modules[2].Batches .ShouldHaveSingleItem();
86-
modules[2].Batches[0].ShouldBe(""
87-
+ "--# REQUIRES: x y" + Eol
88-
+ "--# requires: y x" + Eol
85+
modules[3].Name .ShouldBe("b");
86+
modules[3].WorkerId .ShouldBe(0);
87+
modules[3].Provides .ShouldBeEmpty();
88+
modules[3].Requires .ShouldBe(ImmutableArray.Create("x", "y", "z"));
89+
modules[3].Batches .ShouldHaveSingleItem();
90+
modules[3].Batches[0].ShouldBe(""
91+
+ "--# REQUIRES: x y z" + Eol
92+
+ "--# requires: z y x" + Eol
8993
+ "--# Requires: " + Eol
9094
+ "PRINT 'This is in module b.';" + Eol
9195
);
9296
}
9397

98+
[Test]
99+
public void Load_ModuleNameMissing()
100+
{
101+
var seed = WithSeed("MissingModuleName");
102+
103+
Should.Throw<FormatException>(() =>
104+
{
105+
SeedLoader.Load(seed);
106+
});
107+
}
108+
109+
[Test]
110+
public void Load_WorkerAll()
111+
{
112+
var seed = WithSeed("WorkerAll");
113+
var loaded = SeedLoader.Load(seed);
114+
115+
loaded.Seed .ShouldBeSameAs(seed);
116+
loaded.Modules.AssignTo(out var modules);
117+
118+
modules.Length .ShouldBe(2);
119+
120+
modules[0].Name .ShouldBe("(init)");
121+
modules[0].WorkerId.ShouldBe(0);
122+
modules[0].Provides.ShouldBeEmpty();
123+
modules[0].Requires.ShouldBeEmpty();
124+
modules[0].Batches .ShouldBeEmpty();
125+
126+
modules[1].Name .ShouldBe("init-worker");
127+
modules[1].WorkerId.ShouldBe(-1);
128+
modules[1].Provides.ShouldBeEmpty();
129+
modules[1].Requires.ShouldBeEmpty();
130+
modules[1].Batches .ShouldHaveSingleItem();
131+
modules[1].Batches[0].ShouldBe("--# WORKER: all" + Eol);
132+
}
133+
134+
[Test]
135+
public void Load_WorkerAny()
136+
{
137+
var seed = WithSeed("WorkerAny");
138+
var loaded = SeedLoader.Load(seed);
139+
140+
loaded.Seed .ShouldBeSameAs(seed);
141+
loaded.Modules.AssignTo(out var modules);
142+
143+
modules.Length.ShouldBe(1);
144+
145+
modules[0].Name .ShouldBe("(init)");
146+
modules[0].WorkerId .ShouldBe(0);
147+
modules[0].Provides .ShouldBeEmpty();
148+
modules[0].Requires .ShouldBeEmpty();
149+
modules[0].Batches .ShouldHaveSingleItem();
150+
modules[0].Batches[0].ShouldBe(
151+
"--# worker: ANY" + Eol
152+
);
153+
}
154+
155+
[Test]
156+
public void Load_WorkerMultipleArgs()
157+
{
158+
var path = WithSeed("WorkerMultipleArgs");
159+
160+
Should.Throw<FormatException>(() =>
161+
{
162+
SeedLoader.Load(path);
163+
})
164+
.Message.ShouldBe(
165+
"The WORKER magic comment expects exactly one argument."
166+
);
167+
}
168+
169+
[Test]
170+
public void Load_WorkerInvalid()
171+
{
172+
var seed = WithSeed("WorkerInvalid");
173+
174+
Should.Throw<FormatException>(() =>
175+
{
176+
SeedLoader.Load(seed);
177+
})
178+
.Message.ShouldBe(
179+
"The WORKER magic comment argument must be 'all' or 'any', " +
180+
"case-insensitive, without quotes."
181+
);
182+
}
183+
94184
private static Seed WithSeed(string name)
95185
{
96186
var path = Path.Combine(

PSql.Deploy.Engine.Tests/Seeds/SeedModuleTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ public void Construct_NullName()
1515
});
1616
}
1717

18+
[Test]
19+
public void Construct_OutOfRangeWorkerId()
20+
{
21+
Should.Throw<ArgumentOutOfRangeException>(() =>
22+
{
23+
_ = new SeedModule("a", -2, default, default, default);
24+
});
25+
}
26+
1827
[Test]
1928
public void Name_Get()
2029
{

PSql.Deploy.Engine.Tests/Seeds/SeedSessionTests.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ public void ExpectApplyTypicalSeed()
298298
ExpectUseConnection();
299299
ExpectReportApplying("(init)");
300300
ExpectInvokeBatch(TypicalSeed_InitialModule_Batch0);
301+
ExpectReportApplying("init-worker");
302+
ExpectInvokeBatch(TypicalSeed_InitWorkerModule_Batch0);
301303
ExpectReportApplying("a");
302304
ExpectInvokeBatch(TypicalSeed_ModuleA_Batch0);
303305
ExpectReportApplying("b");
@@ -415,16 +417,20 @@ internal void ExpectInvokeBatch(string sql)
415417

416418
private const string
417419
TypicalSeed_InitialModule_Batch0
418-
= "PRINT 'This is in the initial module.';" + Eol,
420+
= "PRINT 'This is in the initial module.';" + Eol,
421+
TypicalSeed_InitWorkerModule_Batch0
422+
= "--# WORKER: all" + Eol
423+
+ "PRINT 'This is in an all-worker module.';" + Eol,
419424
TypicalSeed_ModuleA_Batch0
420-
= "--# PROVIDES: x y" + Eol
421-
+ "--# provides: y x" + Eol
422-
+ "--# Provides:" + Eol
423-
+ "PRINT 'This is in module a.';" + Eol
424-
+ "PRINT 'The value of ''foo'' is bar.';" + Eol,
425+
= "--# PROVIDES: x y" + Eol
426+
+ "--# provides: y x" + Eol
427+
+ "--# Provides:" + Eol
428+
+ "PRINT 'This is in module a.';" + Eol
429+
+ "PRINT 'The value of ''foo'' is bar.';" + Eol
430+
+ "-- The value of foo is bar." + Eol,
425431
TypicalSeed_ModuleB_Batch0
426-
= "--# REQUIRES: x y" + Eol
427-
+ "--# requires: y x" + Eol
428-
+ "--# Requires: " + Eol
429-
+ "PRINT 'This is in module b.';" + Eol;
432+
= "--# REQUIRES: x y z" + Eol
433+
+ "--# requires: z y x" + Eol
434+
+ "--# Requires: " + Eol
435+
+ "PRINT 'This is in module b.';" + Eol;
430436
}

TestDbs/A/Seeds/Typical/0-Init.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
PRINT 'This is in the initial module.';
2+
--# MODULE: init-worker
3+
--# WORKER: all
4+
PRINT 'This is in an all-worker module.';
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
--# MODULE: a
1+
--# MODULE: a z
22
--# PROVIDES: x y
33
--# provides: y x
44
--# Provides:
55
PRINT 'This is in module a.';
66
PRINT 'The value of ''foo'' is $(foo).';
7+
-- The value of foo is $(foo).
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--#module:b
2-
--# REQUIRES: x y
3-
--# requires: y x
2+
--# REQUIRES: x y z
3+
--# requires: z y x
44
--# Requires:
55
PRINT 'This is in module b.';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--# MODULE: init-worker
2+
--# WORKER: all
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--# worker: ANY
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--# WORKER: invalid
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--# WORKER: any all

0 commit comments

Comments
 (0)