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
33a38b00
Commit
33a38b00
authored
5 years ago
by
Anders H. Rebner
Browse files
Options
Downloads
Patches
Plain Diff
#7 Småforbedringer, #9 tester for CategoryWords() og get_categories()
parent
16fb1513
No related branches found
No related tags found
1 merge request
!10
Database setup
Pipeline
#70504
failed
5 years ago
Stage: lint
Stage: test
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+3
-0
3 additions, 0 deletions
.gitignore
soitool/Database
+0
-0
0 additions, 0 deletions
soitool/Database
soitool/database.py
+11
-11
11 additions, 11 deletions
soitool/database.py
test/test_database.py
+46
-25
46 additions, 25 deletions
test/test_database.py
with
60 additions
and
36 deletions
.gitignore
+
3
−
0
View file @
33a38b00
...
...
@@ -29,4 +29,7 @@ tags
# Coc configuration directory
.vim
# Database
soitool/Database
__pycache__/
This diff is collapsed.
Click to expand it.
soitool/Database
deleted
100644 → 0
+
0
−
0
View file @
16fb1513
File deleted
This diff is collapsed.
Click to expand it.
soitool/database.py
+
11
−
11
View file @
33a38b00
...
...
@@ -77,7 +77,7 @@ class Database():
# Loop through words on file and insert them into ByHeart-table
stmt
=
'
INSERT INTO ByHeart(Word) VALUES(?)
'
for
word
in
f
:
self
.
conn
.
execute
(
stmt
,
(
word
[:
-
1
],))
# Remove \n
self
.
conn
.
execute
(
stmt
,
(
word
.
rstrip
(),))
f
.
close
()
def
fill_category_words
(
self
):
...
...
@@ -86,20 +86,19 @@ class Database():
f
=
open
(
file_path
,
"
r
"
,
encoding
=
'
utf-8
'
)
# Get number of categories on file
no_of_categories
=
int
(
f
.
readline
()
[:
-
1
])
# Remove \n
no_of_categories
=
int
(
f
.
readline
()
.
rstrip
())
# Loop through categories on file
for
_
in
range
(
no_of_categories
):
line
=
f
.
readline
().
split
(
"
,
"
)
# Get category and number of words in category
line
=
f
.
readline
().
split
(
"
,
"
)
category
=
line
[
0
]
no_of_words
=
int
(
line
[
1
]
[:
-
1
])
# Remove \n
no_of_words
=
int
(
line
[
1
]
.
rstrip
())
# Loop through words in category
# Loop through words in category and add rows to DB
stmt
=
'
INSERT INTO CategoryWords(Word, Category) VALUES(?, ?)
'
for
_
in
range
(
no_of_words
):
word
=
f
.
readline
()[:
-
1
]
# Remove \n
stmt
=
'
INSERT INTO CategoryWords(Word, Category) VALUES(?, ?)
'
word
=
f
.
readline
().
rstrip
()
self
.
conn
.
execute
(
stmt
,
(
word
,
category
,))
f
.
close
()
...
...
@@ -113,12 +112,13 @@ class Database():
Categories
"""
stmt
=
'
SELECT DISTINCT Category FROM CategoryWords
'
test
=
self
.
conn
.
execute
(
stmt
)
queried
=
self
.
conn
.
execute
(
stmt
)
categories
=
[]
for
row
in
test
:
for
row
in
queried
:
categories
.
append
(
row
[
'
Category
'
])
return
categories
# temp = Database()
if
__name__
==
'
__main__
'
:
temp
=
Database
()
This diff is collapsed.
Click to expand it.
test/test_database.py
+
46
−
25
View file @
33a38b00
...
...
@@ -5,7 +5,6 @@ import unittest
from
soitool.database
import
Database
TESTDATA_PATH
=
Path
(
__file__
).
parent
.
parent
/
"
soitool/testdata
"
# testdata_path = __file__[:-21] + "soitool\\testdata\\"
class
DatabaseTest
(
unittest
.
TestCase
):
...
...
@@ -18,7 +17,7 @@ class DatabaseTest(unittest.TestCase):
"""
def
setUp
(
self
):
"""
Connect to database.
"""
"""
Connect to
/create
database.
"""
self
.
database
=
Database
()
def
test_connection
(
self
):
...
...
@@ -26,14 +25,17 @@ class DatabaseTest(unittest.TestCase):
self
.
assertIsNotNone
(
self
.
database
)
def
test_by_heart
(
self
):
"""
Assert contents of table ByHeart in DB matches testdata-file.
"""
"""
Assert contents of table ByHeart in DB matches testdata.
"""
# Open and read file:
file_path
=
os
.
path
.
join
(
TESTDATA_PATH
,
"
ByHeart.txt
"
)
f
=
open
(
file_path
,
"
r
"
,
encoding
=
"
utf-8
"
)
file_content
=
f
.
read
()
# Retrieve number of expressions and expressions from file:
no_of_expr
=
len
(
file_content
.
split
(
"
\n
"
))
-
1
# ignore last \n
expressions
=
file_content
.
split
(
"
\n
"
)[:
-
1
]
# ignore last \n
# Retrieve expressions from DB:
stmt
=
'
SELECT * FROM ByHeart
'
queried
=
self
.
database
.
conn
.
execute
(
stmt
).
fetchall
()
...
...
@@ -48,39 +50,58 @@ class DatabaseTest(unittest.TestCase):
f
.
close
()
def
test_category_words
(
self
):
"""
Rai.
Assert contents of table CategoryWords in DB matches testdata-file.
"""
"""
Assert contents of table CategoryWords in DB matches testdata.
"""
file_path
=
os
.
path
.
join
(
TESTDATA_PATH
,
"
CategoryWords.txt
"
)
f
=
open
(
file_path
,
"
r
"
,
encoding
=
"
utf-8
"
)
file_content
=
f
.
read
()
# Assert equal amount of categories in table and file
no_of_categories
=
int
(
file_content
[
0
])
categories_db
=
self
.
database
.
get_categories
()
self
.
assertEqual
(
len
(
categories_db
),
no_of_categories
)
categories_file
=
[]
words_file
=
[]
# Get number of categories on file
no_of_categories
=
int
(
f
.
readline
().
rstrip
(
"
\\
n
"
))
#
Assert equal amount of words in table and
file
# Ignore last \
n an
d noOfC
ategories
no_
of
_
words
=
len
(
file_content
.
split
(
"
\n
"
)[:
-
1
])
-
no_of_categories
-
1
stmt
=
'
SELECT COUNT(*) FROM CategoryWords
'
no_of_words_db
=
self
.
database
.
conn
.
execute
(
stmt
).
fetchone
()
[
0
]
self
.
assertEqual
(
no_of_words_db
,
no_of_words
)
#
Loop through categories on
file
for
_
i
n
r
an
ge
(
no_of_c
ategories
):
# Get category and number
of
words
in category
line
=
f
.
readline
().
split
(
"
,
"
)
categories_file
.
append
(
line
[
0
]
)
no_of_words
=
int
(
line
[
1
][:
-
1
]
)
# Assert information is equal in table and file
# t do
# Loop through words in category
for
_
in
range
(
no_of_words
):
words_file
.
append
(
f
.
readline
().
rstrip
())
f
.
close
()
# Assert equal categories in table and file
categories_db
=
self
.
database
.
get_categories
()
self
.
assertEqual
(
categories_db
,
categories_file
)
# Assert equal words in table and file
stmt
=
'
SELECT * FROM CategoryWords
'
queried
=
self
.
database
.
conn
.
execute
(
stmt
).
fetchall
()
words_db
=
[
queried
[
x
][
0
]
for
x
in
range
(
len
(
queried
))]
# words_db = queried[:][0]
self
.
assertEqual
(
words_db
,
words_file
)
def
test_get_categories
(
self
):
"""
Assert function get_categories works as expected.
"""
categories
=
[
'
Hunderase
'
,
'
Bilmerke
'
,
'
Land
'
,
'
Tresort
'
,
'
President
'
,
'
Some
'
]
categories_db
=
self
.
database
.
get_categories
()
file_path
=
os
.
path
.
join
(
TESTDATA_PATH
,
"
CategoryWords.txt
"
)
f
=
open
(
file_path
,
"
r
"
,
encoding
=
"
utf-8
"
)
# Get number of categories on file
no_of_categories
=
int
(
f
.
readline
().
rstrip
())
categories_file
=
[]
for
_
in
range
(
no_of_categories
):
line
=
f
.
readline
().
split
(
"
,
"
)
categories_file
.
append
(
line
[
0
])
# Skip all words:
[
f
.
readline
()
for
_
in
range
(
int
(
line
[
1
].
rstrip
()))]
f
.
close
()
# Assert categories are equal
# self.assertEqual(len(categories_db), len(
categories
)
)
self
.
assertEqual
(
categories_
db
,
categories
)
categories_db
=
self
.
database
.
get_
categories
(
)
self
.
assertEqual
(
categories_
file
,
categories
_db
)
if
__name__
==
'
__main__
'
:
...
...
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