Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
soitool
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bachelor-paa-bittet
soitool
Commits
65255953
Commit
65255953
authored
4 years ago
by
Anders H. Rebner
Browse files
Options
Downloads
Patches
Plain Diff
#40 Forbedret funksjonen insert_soi
parent
a45783be
No related branches found
No related tags found
1 merge request
!59
#40 Lagre SOI i database
Pipeline
#79478
failed
4 years ago
Stage: lint
Stage: test
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
soitool/database.py
+17
-7
17 additions, 7 deletions
soitool/database.py
test/test_database.py
+3
-0
3 additions, 0 deletions
test/test_database.py
with
20 additions
and
7 deletions
soitool/database.py
+
17
−
7
View file @
65255953
...
...
@@ -344,7 +344,7 @@ class Database:
def
insert_soi
(
self
,
soi
):
"""
Serialize, compress and insert SOI into database-table SOI.
If one or more SOI
'
s with the same title exist
s
in db, (1), (2), ...
If one or more SOI
'
s with the same title exist in db, (1), (2), ...
is added to the Title-column of the SOI to insert.
Parameters
...
...
@@ -352,19 +352,29 @@ class Database:
soi : soitool.soi.SOI
The SOI-instance to insert into database.
"""
title
=
soi
.
title
date
=
datetime
.
now
().
strftime
(
"
%Y-%m-%d
"
)
# Serialize and compress SOI
serialized_soi
=
serialize_soi
(
soi
)
compressed_soi
=
compress
(
serialized_soi
)
# Count SOI's with equal title
# Count SOI's in database with equal title,
# including titles ending with '(x)'
title
=
soi
.
title
length
=
len
(
title
)
stmt
=
"
SELECT COUNT(*) FROM SOI WHERE substr(Title,0,?)=?
"
count
=
self
.
conn
.
execute
(
stmt
,
(
length
+
1
,
title
)).
fetchone
()[
0
]
# Add '(x)' to title if SOI with equal title exists
stmt
=
(
"
SELECT COUNT(*) FROM SOI
"
"
WHERE substr(Title,0,?)=?
"
"
AND (length(Title)=? OR length(Title)=?)
"
)
count
=
self
.
conn
.
execute
(
stmt
,
(
length
+
1
,
title
,
length
,
length
+
3
)
).
fetchone
()[
0
]
# Add '(x)' to title if one or more SOI's with equal title exist
if
count
>
0
:
title
+=
"
(
"
+
str
(
count
+
1
)
+
"
)
"
# Insert SOI to database
stmt
=
"
INSERT INTO SOI (Title, SOI, Date) VALUES(?,?,?)
"
date
=
datetime
.
now
().
strftime
(
"
%Y-%m-%d
"
)
self
.
conn
.
execute
(
stmt
,
(
title
,
compressed_soi
,
date
))
self
.
conn
.
commit
()
This diff is collapsed.
Click to expand it.
test/test_database.py
+
3
−
0
View file @
65255953
...
...
@@ -8,6 +8,8 @@ from datetime import datetime
from
soitool.database
import
Database
from
soitool.soi
import
SOI
from
soitool.coder
import
get_code_length_needed
from
soitool.serialize_export_import_soi
import
serialize_soi
from
soitool.compressor
import
compress
TESTDBNAME
=
"
testDatabase
"
SOITOOL_DIR
=
Path
(
__file__
).
parent
.
parent
/
"
soitool
"
...
...
@@ -310,6 +312,7 @@ class DatabaseTest(unittest.TestCase):
# Assert SOI was written correctly
self
.
assertEqual
(
queried
[
0
][
"
Title
"
],
test_title
)
self
.
assertEqual
(
queried
[
0
][
"
Date
"
],
test_date
)
self
.
assertEqual
(
queried
[
0
][
"
SOI
"
],
compress
(
serialize_soi
(
soi
)))
# Insert same SOI again and assert '(2)' is added to title
self
.
database
.
insert_soi
(
soi
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment