Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from moigagoo/feature/125_readonly_models
Forbid mutating procs for types with `ro` pragma.
- Loading branch information
Showing
14 changed files
with
217 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ func `$`*[_](s: StringOfCap[_]): string = | |
|
||
func `$`*[_](s: PaddedStringOfCap[_]): string = | ||
string(s) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import std/[unittest, strutils, sugar, options] | ||
|
||
import norm/[model, postgres] | ||
|
||
import ../models | ||
|
||
|
||
const | ||
dbHost = "postgres" | ||
dbUser = "postgres" | ||
dbPassword = "postgres" | ||
dbDatabase = "postgres" | ||
|
||
|
||
suite "Read-only models, non-mutating procs": | ||
proc resetDb = | ||
let dbConn = open(dbHost, dbUser, dbPassword, "template1") | ||
dbConn.exec(sql "DROP DATABASE IF EXISTS $#" % dbDatabase) | ||
dbConn.exec(sql "CREATE DATABASE $#" % dbDatabase) | ||
close dbConn | ||
|
||
setup: | ||
resetDb() | ||
let dbConn = open(dbHost, dbUser, dbPassword, dbDatabase) | ||
|
||
var | ||
alice = newPerson("Alice", none Pet) | ||
bob = newPerson("Bob", none Pet) | ||
|
||
dbConn.createTables(newPerson()) | ||
dbConn.insert(alice) | ||
dbConn.insert(bob) | ||
|
||
teardown: | ||
close dbConn | ||
resetDb() | ||
|
||
test "Select rows": | ||
var personNames = @[newPersonName()] | ||
|
||
dbConn.selectAll(personNames) | ||
|
||
assert len(personNames) == 2 | ||
assert personNames[0].name == "Alice" | ||
assert personNames[1].name == "Bob" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
discard """ | ||
action: "reject" | ||
errormsg: "can't use mutating procs with read-only models" | ||
file: "model.nim" | ||
""" | ||
|
||
import std/[unittest, strutils, sugar] | ||
|
||
import norm/[model, postgres] | ||
|
||
import ../models | ||
|
||
|
||
const | ||
dbHost = "postgres" | ||
dbUser = "postgres" | ||
dbPassword = "postgres" | ||
dbDatabase = "postgres" | ||
|
||
|
||
suite "Read-only models, mutating procs": | ||
proc resetDb = | ||
let dbConn = open(dbHost, dbUser, dbPassword, "template1") | ||
dbConn.exec(sql "DROP DATABASE IF EXISTS $#" % dbDatabase) | ||
dbConn.exec(sql "CREATE DATABASE $#" % dbDatabase) | ||
close dbConn | ||
|
||
setup: | ||
resetDb() | ||
let dbConn = open(dbHost, dbUser, dbPassword, dbDatabase) | ||
|
||
dbConn.createTables(newPerson()) | ||
|
||
teardown: | ||
close dbConn | ||
resetDb() | ||
|
||
test "Insert row": | ||
var personName = PersonName(name: "Name") | ||
|
||
dbConn.insert(personName) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ suite "Database manipulation": | |
dropDb() | ||
|
||
check not fileExists(dbFile) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import std/[unittest, os, strutils, options] | ||
|
||
import norm/[model, sqlite] | ||
|
||
import ../models | ||
|
||
|
||
const dbFile = "test.db" | ||
|
||
|
||
suite "Read-only models, non-mutating procs": | ||
setup: | ||
removeFile dbFile | ||
|
||
putEnv(dbHostEnv, dbFile) | ||
|
||
var | ||
alice = newPerson("Alice", none Pet) | ||
bob = newPerson("Bob", none Pet) | ||
|
||
withDb: | ||
db.createTables(newPerson()) | ||
db.insert(alice) | ||
db.insert(bob) | ||
|
||
teardown: | ||
delEnv(dbHostEnv) | ||
removeFile dbFile | ||
|
||
test "Select rows": | ||
var personNames = @[newPersonName()] | ||
|
||
withDb: | ||
db.selectAll(personNames) | ||
|
||
assert len(personNames) == 2 | ||
assert personNames[0].name == "Alice" | ||
assert personNames[1].name == "Bob" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
discard """ | ||
action: "reject" | ||
errormsg: "can't use mutating procs with read-only models" | ||
file: "model.nim" | ||
""" | ||
|
||
import std/[unittest, os, strutils] | ||
|
||
import norm/[model, sqlite] | ||
|
||
import ../models | ||
|
||
|
||
const dbFile = "test.db" | ||
|
||
|
||
suite "Read-only models, mutating procs": | ||
setup: | ||
removeFile dbFile | ||
|
||
putEnv(dbHostEnv, dbFile) | ||
|
||
withDb: | ||
db.createTables(newPerson()) | ||
|
||
teardown: | ||
delEnv(dbHostEnv) | ||
removeFile dbFile | ||
|
||
test "Insert row": | ||
var personName = PersonName(name: "Name") | ||
|
||
withDb: | ||
db.insert(personName) | ||
|