Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
K
kafka-Studies
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
최석원
kafka-Studies
Commits
ab8724c7
Commit
ab8724c7
authored
4 years ago
by
Seok Won
Browse files
Options
Downloads
Patches
Plain Diff
Create pytest tests
parent
7e8b02de
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
python/tests/test_open.py
+29
-0
29 additions, 0 deletions
python/tests/test_open.py
python/tests/test_parser.py
+64
-0
64 additions, 0 deletions
python/tests/test_parser.py
with
93 additions
and
0 deletions
python/tests/test_open.py
0 → 100644
+
29
−
0
View file @
ab8724c7
import
json
import
os
from
contextlib
import
contextmanager
from
pathlib
import
Path
BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
JSON_PATH
=
os
.
path
.
join
(
BASE_DIR
,
"
test.json
"
)
@contextmanager
def
jsonify
(
mode
):
f
=
open
(
JSON_PATH
,
mode
=
mode
)
read
=
json
.
load
(
f
)
yield
read
f
.
close
()
def
test_open
():
if
not
Path
(
JSON_PATH
).
is_file
():
base_data
=
{
"
POSTS
"
:
{}}
with
open
(
JSON_PATH
,
"
a+
"
)
as
f_read
:
f_read
.
write
(
json
.
dumps
(
base_data
))
with
jsonify
(
"
r+
"
)
as
f
:
data
=
f
assert
data
is
not
None
,
"
data is None.
"
This diff is collapsed.
Click to expand it.
python/tests/test_parser.py
0 → 100644
+
64
−
0
View file @
ab8724c7
import
json
import
requests
from
bs4
import
BeautifulSoup
ADDRESS
=
"
https://www.ajou.ac.kr/kr/ajou/notice.do
"
# Make data into dictionary format
def
makeJson
(
postId
,
postTitle
,
postDate
,
postLink
,
postWriter
):
return
{
postId
:
{
"
TITLE
"
:
postTitle
,
"
DATE
"
:
postDate
,
"
LINK
"
:
ADDRESS
+
postLink
,
"
WRITER
"
:
postWriter
,
}
}
def
parser
():
LENGTH
=
10
req
=
requests
.
get
(
f
"
{
ADDRESS
}
?mode=list&&articleLimit=
{
LENGTH
}
&article.offset=0
"
)
req
.
encoding
=
"
utf-8
"
html
=
req
.
text
soup
=
BeautifulSoup
(
html
,
"
html.parser
"
)
ids
=
soup
.
select
(
"
table > tbody > tr > td.b-num-box
"
)
posts
=
soup
.
select
(
"
table > tbody > tr > td.b-td-left > div > a
"
)
dates
=
soup
.
select
(
"
table > tbody > tr > td.b-td-left > div > div > span.b-date
"
)
writers
=
soup
.
select
(
"
table > tbody > tr > td.b-td-left > div > div.b-m-con > span.b-writer
"
)
return
ids
,
posts
,
dates
,
writers
# Test #1
def
test_parse
():
ids
,
posts
,
dates
,
writers
=
parser
()
assert
len
(
ids
)
==
10
,
f
"
Check your parser:
{
ids
}
"
assert
len
(
posts
)
==
10
,
f
"
Check your parser:
{
posts
}
"
assert
len
(
dates
)
==
10
,
f
"
Check your parser:
{
dates
}
"
assert
len
(
writers
)
==
10
,
f
"
Check your parser:
{
writers
}
"
for
i
in
range
(
LENGTH
):
postId
=
ids
[
i
].
text
.
strip
()
postLink
=
posts
[
i
].
get
(
"
href
"
)
postTitle
=
posts
[
i
].
text
.
strip
()
postDate
=
dates
[
i
].
text
.
strip
()
postWriter
=
writers
[
i
].
text
assert
int
(
postId
)
>
10000
,
f
"
postId is None.
"
assert
postLink
is
not
None
,
f
"
postLink is None.
"
assert
postTitle
is
not
None
,
f
"
postTitle is None.
"
assert
postDate
is
not
None
,
f
"
postDate is None.
"
assert
postWriter
is
not
None
,
f
"
postWriter is None.
"
data
=
makeJson
(
postId
,
postTitle
,
postDate
,
postLink
,
postWriter
)
print
(
"
data
"
,
json
.
dumps
(
data
[
postId
]))
if
__name__
==
"
__main__
"
:
test_parse
()
# print(next(iter(read["POSTS"].keys()))) # Last Key
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